api_v1.4/tests/test_user.py
2023-02-01 15:48:14 -05:00

125 lines
4.0 KiB
Python

import json
from .base_test import BaseTest, arrange
import unittest
from hub_api.helpers.auth import generate_auth_token
ordered, compare = arrange()
unittest.defaultTestLoader.sortTestMethodsUsing = compare
class UserTest(BaseTest):
"""
Tests for User API endpoints
"""
@classmethod
def setUpClass(cls):
# Call setUp from parent
super().setUpClass()
cls.user_dict = {"user": {
"name": "Test User",
"email": "testuser@gmail.com",
"password": "TestUser@12345",
"role": "Admin",
}}
cls.token = generate_auth_token(cls.user_dict)
@ordered
def test_create_user_by_non_admin(self):
# When
res = self.client.post('/v1.4/user', data=json.dumps(self.user_dict['user']))
# Then
self.assertEqual('Invalid payload', res.json['messages'])
self.assertEqual(400, res.status_code)
@ordered
def test_create_user_by_admin(self):
# When
res = self.client.post('/v1.4/user',
headers={
'Content-Type': 'application/json', 'Authorization': 'Bearer ' + str(self.token)
},
data=json.dumps(self.user_dict['user']))
# Then
user = res.json
self.assertEqual(201, res.status_code)
self.assertEqual(type(user['user']), dict)
self.assertEqual(user['user']['email'], self.user_dict['user']['email'])
self.assertEqual(user['user']['role'], self.user_dict['user']['role'])
@ordered
def test_create_user_with_existing_email(self):
# When
res = self.client.post('/v1.4/user',
headers={
'Content-Type': 'application/json', 'Authorization': 'Bearer ' + str(self.token)
},
data=json.dumps(self.user_dict['user']))
# Then
self.assertEqual('user with testuser@gmail.com email already exists', res.json['message'])
self.assertEqual(400, res.status_code)
@ordered
def test_create_user_with_weak_password(self):
# When
self.user_dict['user']['password'] = '1234'
self.user_dict['user']['email'] = 'new@gmail.com'
res = self.client.post('/v1.4/user',
headers={
'Content-Type': 'application/json', 'Authorization': 'Bearer ' + str(self.token)
},
data=json.dumps(self.user_dict['user']))
# Then
self.assertEqual('Sorry an error occurred while creating user', res.json['err_msg'])
self.assertEqual(400, res.status_code)
@ordered
def test_login_user_with_wrong_credential(self):
# When
res = self.client.post('/v1.4/user/login',
headers={
'Content-Type': 'application/json'
},
data=json.dumps({'email': 'wrong@gmail.com', 'password': 'wrong'}))
# Then
message = res.json
self.assertEqual('user not found', message['message'])
self.assertEqual(400, res.status_code)
@ordered
def test_login_user_with_correct_credential(self):
# When
self.user_dict['user']['password'] = 'TestUser@12345'
self.user_dict['user']['email'] = 'testuser@gmail.com'
login_data = {
"email": self.user_dict['user']['email'],
"password": self.user_dict['user']['password']
}
res = self.client.post('/v1.4/user/login', headers={'Content-Type': 'application/json'},
data=json.dumps(login_data))
# Then
user = res.json
self.assertEqual(user['user']['email'], self.user_dict['user']['email'])
self.assertEqual(user['user']['role'], self.user_dict['user']['role'])
self.assertIsNotNone(user['token'])
self.assertEqual(200, res.status_code)
@classmethod
def tearDownClass(cls) -> None:
# Call tearDown from parent
super().tearDownClass()
def suite():
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.TestLoader(
).loadTestsFromTestCase(UserTest))
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())