def test_save_user_returns_error_for_existing_user(self, found_user_stub, db_mock): db_mock.return_value = MysqlMock() found_user_stub.return_value = Mocks().MYSQL_FIND_USER_RESULT_SINGLE self.user_controller = UserController() result = self.user_controller.save_user(Mocks().USER_MOCK) self.assertIn('error', result) self.assertEqual(result['error'], 'An user with this email already exists.')
class UserJsonResponseTest(TestCase): def setUp(self): self.user_mock = Mocks().MYSQL_FIND_USER_RESULT_SINGLE self.users_mock = Mocks().MYSQL_ALL_USERS_MOCK def test_raise_exception_if_build_parameter_is_none(self): self.assertRaises(ValueError, UserJsonResponse.build, {}) def test_raise_exception_if_build_parameter_length_is_less_than_7(self): self.user_mock.pop(0) self.assertRaises(ValueError, UserJsonResponse.build, self.user_mock) def test_build_returns_correct_json_response_object(self): result = UserJsonResponse.build(self.user_mock) self.assertEqual(result, { 'id': self.user_mock[0], 'first_name': self.user_mock[1], 'last_name': self.user_mock[2], 'username': self.user_mock[3], 'access_level': self.user_mock[4], 'email': self.user_mock[5] }) def test_build_from_array_returs_correct_json_response_object(self): result = UserJsonResponse.build_from_array(self.users_mock) self.assertEqual(result, {'data':[{ 'id':1232, 'first_name': 'Unit Test', 'last_name': 'User', 'username': '******', 'access_level': 9, 'email': '*****@*****.**' }, { 'id': 8768, 'username': '******', 'first_name': 'Luke', 'last_name': 'Skywalker', 'access_level': 99, 'email': '*****@*****.**' }, { 'id':24, 'username': '******', 'first_name': 'Mano', 'last_name': 'Brow', 'access_level': 0, 'email': '*****@*****.**' }]} )
def test_find_user_by_name_returns_array_with_user_info_for_existing_users(self, find_stub, db_mock): db_mock.return_value = MysqlMock() find_stub.return_value = tuple(filter((lambda user: user[1] == 'Luke'), Mocks().MYSQL_ALL_USERS_MOCK)) self.user_controller = UserController() result = self.user_controller.find_by_name('Luke') self.assertIn('data', result) self.assertEqual(len(result.get('data')),1)
def test_get_all_users_returns_array_of_users(self, get_all_stub, db_mock): db_mock.return_value = MysqlMock() get_all_stub.return_value = Mocks().MYSQL_ALL_USERS_MOCK self.user_controller = UserController() result = self.user_controller.get_all() self.assertIn('data', result) self.assertEqual(len(result.get('data')), 3)
def test_delete_user_returns_info_msg_for_successfully_deleted_user(self, delete_stub, db_mock): db_mock.return_value = MysqlMock() delete_stub.return_value = {'info': 'user successfully deleted.'} self.user_controller = UserController() result = self.user_controller.delete_by_id(Mocks().USER_MOCK['id']) self.assertEqual(result, {'info': 'user successfully deleted.'})
def test_save_user_returns_error_msg_for_failed_saving_operation(self, insert_stub, db_mock): db_mock.return_value = MysqlMock() insert_stub.return_value = {} self.user_controller = UserController() result = self.user_controller.save_user(Mocks().USER_MOCK) self.assertEqual(result, {'error': 'user could not be created.'})
def test_save_user_returns_info_msg_for_successfully_saved_user(self, insert_stub, db_mock): db_mock.return_value = MysqlMock() insert_stub.return_value = {'info':'user successfully created.'} self.user_controller = UserController() result = self.user_controller.save_user(Mocks().USER_MOCK) self.assertEqual(result, {'info':'user successfully created.'})
def setUp(self): self.user_mock = Mocks().USER_MOCK
def setUp(self): self.user_mock = Mocks().MYSQL_FIND_USER_RESULT_SINGLE self.users_mock = Mocks().MYSQL_ALL_USERS_MOCK
def setUp(self): self.new_user_request_body = Mocks().USER_MOCK
class NewUserRequestValidatorTest(TestCase): def setUp(self): self.new_user_request_body = Mocks().USER_MOCK def test_empty_request_data_raises_invalid_request_body_exception(self): self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, {}) def test_array_request_data_raises_invalid_request_body_exception(self): self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, []) def test_missing_first_name_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('first_name') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_missing_last_name_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('last_name') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_missing_username_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('username') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_missing_password_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('password') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_missing_access_level_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('access_level') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_missing_email_raises_invalid_request_body_exception(self): self.new_user_request_body.pop('email') self.assertRaises(InvalidRequestBody, NewUserRequestValidator.validate, self.new_user_request_body) def test_valid_request_body_returns_true(self): result = NewUserRequestValidator().validate(self.new_user_request_body) self.assertTrue(result)