Guest User

Untitled

a guest
Apr 7th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import unittest
  2. import json
  3. from app.test.base import BaseTestCase
  4.  
  5.  
  6. def register_user(self):
  7. return self.client.post(
  8. '/user/',
  9. data=json.dumps(dict(
  10. email='example@gmail.com',
  11. username='username',
  12. password='123456'
  13. )),
  14. content_type='application/json'
  15. )
  16.  
  17.  
  18. def login_user(self):
  19. return self.client.post(
  20. '/auth/login',
  21. data=json.dumps(dict(
  22. email='example@gmail.com',
  23. password='123456'
  24. )),
  25. content_type='application/json'
  26. )
  27.  
  28.  
  29. class TestAuthBlueprint(BaseTestCase):
  30.  
  31. def test_registered_user_login(self):
  32. """ Test for login of registered-user login """
  33. with self.client:
  34. # user registration
  35. user_response = register_user(self)
  36. response_data = json.loads(user_response.data.decode())
  37. self.assertTrue(response_data['Authorization'])
  38. self.assertEqual(user_response.status_code, 201)
  39.  
  40. # registered user login
  41. login_response = login_user(self)
  42. data = json.loads(login_response.data.decode())
  43. self.assertTrue(data['Authorization'])
  44. self.assertEqual(login_response.status_code, 200)
  45.  
  46. def test_valid_logout(self):
  47. """ Test for logout before token expires """
  48. with self.client:
  49. # user registration
  50. user_response = register_user(self)
  51. response_data = json.loads(user_response.data.decode())
  52. self.assertTrue(response_data['Authorization'])
  53. self.assertEqual(user_response.status_code, 201)
  54.  
  55. # registered user login
  56. login_response = login_user(self)
  57. data = json.loads(login_response.data.decode())
  58. self.assertTrue(data['Authorization'])
  59. self.assertEqual(login_response.status_code, 200)
  60.  
  61. # valid token logout
  62. response = self.client.post(
  63. '/auth/logout',
  64. headers=dict(
  65. Authorization='Bearer ' + json.loads(
  66. login_response.data.decode()
  67. )['Authorization']
  68. )
  69. )
  70. data = json.loads(response.data.decode())
  71. self.assertTrue(data['status'] == 'success')
  72. self.assertEqual(response.status_code, 200)
  73.  
  74. if __name__ == '__main__':
  75. unittest.main()
Add Comment
Please, Sign In to add comment