Created
March 30, 2018 13:16
-
-
Save cosmic-byte/bc29258dc4a690520ec7a51249a7109b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import unittest | |
| import json | |
| from app.test.base import BaseTestCase | |
| def register_user(self): | |
| return self.client.post( | |
| '/user/', | |
| data=json.dumps(dict( | |
| email='[email protected]', | |
| username='username', | |
| password='123456' | |
| )), | |
| content_type='application/json' | |
| ) | |
| def login_user(self): | |
| return self.client.post( | |
| '/auth/login', | |
| data=json.dumps(dict( | |
| email='[email protected]', | |
| password='123456' | |
| )), | |
| content_type='application/json' | |
| ) | |
| class TestAuthBlueprint(BaseTestCase): | |
| def test_registered_user_login(self): | |
| """ Test for login of registered-user login """ | |
| with self.client: | |
| # user registration | |
| user_response = register_user(self) | |
| response_data = json.loads(user_response.data.decode()) | |
| self.assertTrue(response_data['Authorization']) | |
| self.assertEqual(user_response.status_code, 201) | |
| # registered user login | |
| login_response = login_user(self) | |
| data = json.loads(login_response.data.decode()) | |
| self.assertTrue(data['Authorization']) | |
| self.assertEqual(login_response.status_code, 200) | |
| def test_valid_logout(self): | |
| """ Test for logout before token expires """ | |
| with self.client: | |
| # user registration | |
| user_response = register_user(self) | |
| response_data = json.loads(user_response.data.decode()) | |
| self.assertTrue(response_data['Authorization']) | |
| self.assertEqual(user_response.status_code, 201) | |
| # registered user login | |
| login_response = login_user(self) | |
| data = json.loads(login_response.data.decode()) | |
| self.assertTrue(data['Authorization']) | |
| self.assertEqual(login_response.status_code, 200) | |
| # valid token logout | |
| response = self.client.post( | |
| '/auth/logout', | |
| headers=dict( | |
| Authorization='Bearer ' + json.loads( | |
| login_response.data.decode() | |
| )['Authorization'] | |
| ) | |
| ) | |
| data = json.loads(response.data.decode()) | |
| self.assertTrue(data['status'] == 'success') | |
| self.assertEqual(response.status_code, 200) | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment