from django.test import TestCase, Client from knox.models import AuthToken from authentication.models import ExtendedUser from core import settings class UserApiTest(TestCase): def setUp(self): self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test') self.user.save() self.token = AuthToken.objects.create(user=self.user) self.client = Client(headers={'Authorization': 'Token ' + self.token[1]}) def test_users(self): response = self.client.get('/api/2/users/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 2) self.assertEqual(response.json()[0]['username'], settings.LEGACY_USER_NAME) self.assertEqual(response.json()[0]['email'], 'mail@' + settings.MAIL_DOMAIN) self.assertEqual(response.json()[0]['first_name'], '') self.assertEqual(response.json()[0]['last_name'], '') self.assertEqual(response.json()[0]['id'], 1) self.assertEqual(response.json()[1]['username'], 'testuser') self.assertEqual(response.json()[1]['email'], 'test') self.assertEqual(response.json()[1]['first_name'], '') self.assertEqual(response.json()[1]['last_name'], '') def test_self_user(self): response = self.client.post('/api/2/self/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['username'], 'testuser') self.assertEqual(response.json()['email'], 'test') self.assertEqual(response.json()['first_name'], '') self.assertEqual(response.json()['last_name'], '') def test_register_user(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'username': 'testuser2', 'password': 'test', 'email': 'test2'}, content_type='application/json') self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['username'], 'testuser2') self.assertEqual(response.json()['email'], 'test2') self.assertEqual(len(ExtendedUser.objects.all()), 3) self.assertEqual(ExtendedUser.objects.get(username='testuser2').email, 'test2') self.assertTrue(ExtendedUser.objects.get(username='testuser2').check_password('test')) def test_register_user_duplicate(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'username': 'testuser', 'password': 'test', 'email': 'test2'}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json()['errors']['username'], 'Username already exists') self.assertEqual(len(ExtendedUser.objects.all()), 2) def test_register_user_no_username(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'password': 'test', 'email': 'test2'}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json()['errors']['username'], 'Username is required') self.assertEqual(len(ExtendedUser.objects.all()), 2) def test_register_user_no_password(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'username': 'testuser2', 'email': 'test2'}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json()['errors']['password'], 'Password is required') self.assertEqual(len(ExtendedUser.objects.all()), 2) def test_register_user_no_email(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'username': 'testuser2', 'password': 'test'}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json()['errors']['email'], 'Email is required') self.assertEqual(len(ExtendedUser.objects.all()), 2) def test_register_user_duplicate_email(self): anonymous = Client() response = anonymous.post('/api/2/register/', {'username': 'testuser2', 'password': 'test', 'email': 'test'}, content_type='application/json') self.assertEqual(response.status_code, 400) self.assertEqual(response.json()['errors']['email'], 'Email already exists') self.assertEqual(len(ExtendedUser.objects.all()), 2) def test_get_token(self): anonymous = Client() response = anonymous.post('/api/2/login/', {'username': 'testuser', 'password': 'test'}, content_type='application/json')