VRE Backend API and Scheduler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

from django.test import TestCase, override_settings
from rest_framework.test import RequestsClient
from requests_hawk import HawkAuth
from django.contrib.auth.models import User
from django.urls import reverse
import json
from lib.api.client import VRE_API_Client
# Create your tests here.
class UserLoginTest(TestCase):
def setUpTestData(cls):
cls.email = 'dummy@rug.nl'
cls.username = 'dummy@rug.nl'
cls.password = 'doemaarwat'
def setUp(self):
# We want to use the API REST Framework request client, as this enables us to use HAWK authentication during testing
self.client = RequestsClient()
def test_missing_credentials(self):
endpoint = 'http://testserver/api/auth/users/me/'
response = self.client.get(endpoint)
self.assertEqual(response.status_code, 401)
def test_valid_login(self):
login_data = {'username' : self.username, 'password' : self.password}
endpoint = 'http://testserver' + reverse('api:api-login')
response = self.client.post(endpoint, json=login_data)
self.assertEqual(response.status_code, 200)
self.assertIn('key', response.json())
self.assertIn('secret', response.json())
self.key = response.json()['key']
self.secret = response.json()['secret']
# Add HAWK Authentication to make sure key and secret are correct.
self.client.auth = HawkAuth(id=self.key , key=self.secret, always_hash_content=False)
endpoint = 'http://testserver/api/auth/users/me/'
response = self.client.get(endpoint)
self.assertEqual(response.status_code, 200)
self.assertIn('email', response.json())
self.assertIn('id', response.json())
self.assertIn('username', response.json())
self.assertEqual(response.json()['email'], self.email)
self.assertEqual(response.json()['username'], self.username)