Advertisement
Guest User

tests

a guest
Jan 29th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. # from django.core.serializers import json
  2. from django.test import TestCase
  3. import json
  4. # Create your tests here.
  5. from rest_framework.authtoken.models import Token
  6.  
  7. from api import messages
  8. from api.models import User, Role
  9.  
  10.  
  11. def response_to_json(response):
  12.  
  13. return json.loads(response.content)
  14.  
  15.  
  16. class DummyRoles(TestCase):
  17. # creating DummyRoles , So User can be created using role_id
  18. def setUp(self):
  19. self.role_1 = Role.objects.create(role_name="Admin")
  20. self.role_2 = Role.objects.create(role_name="Manager")
  21. self.role_3 = Role.objects.create(role_name="Customer")
  22.  
  23.  
  24. class DummyUsers(DummyRoles):
  25.  
  26. def setUp(self):
  27. super(DummyUsers, self).setUp()
  28.  
  29. self.user_1 = User.objects.create_user(email="user1@localhost", password="admin", role=self.role_2)
  30. self.user_2 = User.objects.create_user(email='user2@localhost', password='admin', role=self.role_2)
  31.  
  32. self.token_1 = Token.objects.get(user=self.user_1).key
  33. self.token_2 = Token.objects.get(user=self.user_2).key
  34.  
  35. self.header_1 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_1)}
  36. self.header_2 = {"HTTP_AUTHORIZATION": 'Token {}'.format(self.token_2)}
  37.  
  38.  
  39. class UserRegistrationTest(TestCase):
  40.  
  41. def setUp(self):
  42. self.role_1 = Role.objects.create(role_name="Admin")
  43. self.role_2 = Role.objects.create(role_name="Manager")
  44. self.role_3 = Role.objects.create(role_name="Customer")
  45.  
  46. def test_user_creation_with_email(self):
  47. data = {
  48. "email": "hello@localhost",
  49. "password": "admin",
  50. "role": self.role_1.id
  51. }
  52.  
  53. response = self.client.post('/api/users/registration', data=data)
  54. self.assertEqual(response.status_code, 201)
  55. self.assertEqual(User.objects.all().count(), 1)
  56.  
  57. def test_user_registration_with_email_fail(self):
  58. data = {
  59. "email": "invalid_email.com",
  60. "password": "admin",
  61. "role": self.role_1.id
  62. }
  63.  
  64. response = self.client.post('/api/users/registration', data=data)
  65. self.assertEqual(response.status_code, 400)
  66. self.assertIn("Enter a valid email address.", response.content)
  67.  
  68. def test_duplicate_email_not_allowed(self):
  69. data = {
  70. "email": "hello@localhost",
  71. "password": "admin",
  72. "role": self.role_1.id
  73. }
  74. self.client.post('/api/users/registration', data=data)
  75. response = self.client.post('/api/users/registration', data=data)
  76. self.assertEqual(response.status_code, 400)
  77. self.assertIn("This field must be unique.",
  78. response.content)
  79.  
  80. def test_without_email_not_allowed(self):
  81. data = {
  82. "password": "admin",
  83. "role": self.role_1.id
  84. }
  85. self.client.post('/api/users/registration', data=data)
  86. response = self.client.post('/api/users/registration', data=data)
  87. self.assertEqual(response.status_code, 400)
  88. self.assertIn(messages.REQUIRED_EMAIL["message"], response.content)
  89.  
  90. def test_without_password_not_allowed(self):
  91. data = {
  92. "email": "new@new.com",
  93. "role": self.role_1.id
  94. }
  95. self.client.post('/api/users/registration', data=data)
  96. response = self.client.post('/api/users/registration', data=data)
  97. self.assertEqual(response.status_code, 400)
  98.  
  99. def test_without_role_not_allowed(self):
  100. data = {
  101. "email": "new@new.com",
  102. "password": "123"
  103. }
  104. self.client.post('/api/users/registration', data=data)
  105. response = self.client.post('/api/users/registration', data=data)
  106. self.assertEqual(response.status_code, 400)
  107.  
  108.  
  109. class UserDetailTest(DummyUsers):
  110.  
  111. def setUp(self):
  112. super(UserDetailTest, self).setUp()
  113.  
  114. def test_get_successful_user_detail(self):
  115.  
  116. response = self.client.get('/api/users/%d/' % self.user_1.id,
  117. {}, **self.header_1)
  118. json_response = response_to_json(response)
  119.  
  120. self.assertEqual(json_response['email'], self.user_1.email)
  121.  
  122. def test_get_failing_user_detail(self):
  123.  
  124. response = self.client.get('/api/users/%d/' % self.user_1.id,
  125. {}, **self.header_2)
  126.  
  127. self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  128.  
  129. def test_put_successful_user_detail(self):
  130.  
  131. data_dict = {"first_name": "snow", "email": self.user_1.email}
  132. data = json.dumps(data_dict)
  133.  
  134. response = self.client.put('/api/users/%d/' % self.user_1.id,
  135. data=data,
  136. content_type='application/json',
  137. **self.header_1)
  138. self.assertEqual(response.status_code, 200)
  139. json_response = response_to_json(response)
  140.  
  141. self.assertEqual(data_dict['first_name'], json_response['first_name'])
  142.  
  143. def test_put_fail_user_detail(self):
  144.  
  145. data_dict = {"first_name": "snow", "email": self.user_1.email}
  146. data = json.dumps(data_dict)
  147.  
  148. response = self.client.put('/api/users/%d/' % self.user_1.id,
  149. data=data,
  150. content_type='application/json',
  151. **self.header_2)
  152.  
  153. self.assertEqual(response.status_code, 400)
  154. self.assertIn(messages.TOKEN_UNAUTHORIZED["message"], response.content)
  155.  
  156.  
  157. class UserLoginTest(DummyUsers):
  158.  
  159. def test_user_login_with_email_returns_token(self):
  160. data = {
  161. "email": "user1@localhost",
  162. "password": "admin"
  163. }
  164.  
  165. response = self.client.post('/api/users/login', data=data)
  166. json_response = response_to_json(response)
  167. self.assertNotEqual(json_response['token'], None)
  168.  
  169. def test_user_login_with_email_fails(self):
  170. data = {
  171. "email": "notExist@localhost",
  172. "password": "admin"
  173. }
  174.  
  175. response = self.client.post('/api/users/login', data=data)
  176. self.assertIn(messages.USER_WITH_EMAIL_DOES_NOT_EXISTS["message"], response.content)
  177. self.assertEqual(response.status_code, 404)
  178.  
  179. def test_user_login_with_password_fails(self):
  180. data = {"email": "user1@localhost", "password": "wrong_password"}
  181. response = self.client.post('/api/users/login', data=data)
  182. self.assertIn(messages.INVALID_EMAIL_OR_PASSWORD["message"], response.content)
  183. self.assertEqual(response.status_code, 401)
  184.  
  185. def test_user_login_without_password(self):
  186. data = {"email": "user1@localhost"}
  187. response = self.client.post('/api/users/login', data=data)
  188. self.assertIn(messages.REQUIRED_EMAIL_AND_PASSWORD["message"], response.content)
  189. self.assertEqual(response.status_code, 400)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement