Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from django.core.serializers import json
- from django.test import TestCase
- import json
- # Create your tests here.
- from rest_framework.authtoken.models import Token
- from api import messages
- from api.models import User, Role
- def response_to_json(response):
- return json.loads(response.content)
- class DummyRoles(TestCase):
- # creating DummyRoles , So User can be created using role_id
- def setUp(self):
- self.role_1 = Role.objects.create(role_name="Admin")
- self.role_2 = Role.objects.create(role_name="Manager")
- self.role_3 = Role.objects.create(role_name="Customer")
- class DummyUsers(DummyRoles):
- def setUp(self):
- super(DummyUsers, self).setUp()
- self.user_1 = User.objects.create_user(email="user1@localhost", password="admin", role=self.role_2)
- self.user_2 = User.objects.create_user(email='user2@localhost', password='admin', role=self.role_2)
- self.token_1 = Token.objects.get(user=self.user_1).key
- self.token_2 = Token.objects.get(user=self.user_2).key
- self.header_1 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_1)}
- self.header_2 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_2)}
- class UserRegistrationTest(TestCase):
- def setUp(self):
- self.role_1 = Role.objects.create(role_name="Admin")
- self.role_2 = Role.objects.create(role_name="Manager")
- self.role_3 = Role.objects.create(role_name="Customer")
- def test_user_creation_with_email(self):
- data = {
- "email": "hello@localhost",
- "password": "admin",
- "role": self.role_1.id
- }
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 201)
- self.assertEqual(User.objects.all().count(), 1)
- def test_user_registration_with_email_fail(self):
- data = {
- "email": "invalid_email.com",
- "password": "admin",
- "role": self.role_1.id
- }
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn("Enter a valid email address.", response.content)
- def test_duplicate_email_not_allowed(self):
- data = {
- "email": "hello@localhost",
- "password": "admin",
- "role": self.role_1.id
- }
- self.client.post('/api/users/registration', data=data)
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn("This field must be unique.",
- response.content)
- def test_without_email_not_allowed(self):
- data = {
- "password": "admin",
- "role": self.role_1.id
- }
- self.client.post('/api/users/registration', data=data)
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.REQUIRED_EMAIL["message"], response.content)
- def test_without_password_not_allowed(self):
- data = {
- "email": "new@new.com",
- "role": self.role_1.id
- }
- self.client.post('/api/users/registration', data=data)
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 400)
- def test_without_role_not_allowed(self):
- data = {
- "email": "new@new.com",
- "password": "123"
- }
- self.client.post('/api/users/registration', data=data)
- response = self.client.post('/api/users/registration', data=data)
- self.assertEqual(response.status_code, 400)
- class UserDetailTest(DummyUsers):
- def setUp(self):
- super(UserDetailTest, self).setUp()
- def test_get_successful_user_detail(self):
- response = self.client.get('/api/users/%d/' % self.user_1.id,
- {}, **self.header_1)
- json_response = response_to_json(response)
- self.assertEqual(json_response['email'], self.user_1.email)
- def test_get_failing_user_detail(self):
- response = self.client.get('/api/users/%d/' % self.user_1.id,
- {}, **self.header_2)
- self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- def test_put_successful_user_detail(self):
- data_dict = {"first_name": "snow", "email": self.user_1.email}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_1)
- self.assertEqual(response.status_code, 200)
- json_response = response_to_json(response)
- self.assertEqual(data_dict['first_name'], json_response['first_name'])
- def test_put_fail_user_detail(self):
- data_dict = {"first_name": "snow", "email": self.user_1.email}
- data = json.dumps(data_dict)
- response = self.client.put('/api/users/%d/' % self.user_1.id,
- data=data,
- content_type='application/json',
- **self.header_2)
- self.assertEqual(response.status_code, 400)
- self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
- class UserLoginTest(DummyUsers):
- def test_user_login_with_email_returns_token(self):
- data = {
- "email": "user1@localhost",
- "password": "admin"
- }
- response = self.client.post('/api/users/login', data=data)
- json_response = response_to_json(response)
- self.assertNotEqual(json_response['token'], None)
- def test_user_login_with_email_fails(self):
- data = {
- "email": "notExist@localhost",
- "password": "admin"
- }
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.USER_WITH_EMAIL_DOES_NOT_EXISTS["message"], response.content)
- self.assertEqual(response.status_code, 404)
- def test_user_login_with_password_fails(self):
- data = {"email": "user1@localhost", "password": "wrong_password"}
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.INVALID_EMAIL_OR_PASSWORD["message"], response.content)
- self.assertEqual(response.status_code, 401)
- def test_user_login_without_password(self):
- data = {"email": "user1@localhost"}
- response = self.client.post('/api/users/login', data=data)
- self.assertIn(messages.REQUIRED_EMAIL_AND_PASSWORD["message"], response.content)
- self.assertEqual(response.status_code, 400)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement