def test_create_user_sql_error_500_status_code_error_message(self): """test creating a new user but throws SQL error when trying to add""" # mock return value of get_user which checks if user exists as None self.mocked_db.return_value.get_user.return_value = None # mock return value of add_user to throw SQLAlchemyError self.mocked_db.return_value.add_user.side_effect = SQLAlchemyError() # create example user request example_user = EXAMPLE_USER_REQUEST.copy() example_user['username'] = '******' # send request to test client response = self.test_app.post('/users', data=example_user) # assert 500 response code self.assertEqual(response.status_code, 500) # assert we get correct error message self.assertEqual(response.data, b'failed to create user')
def test_create_user_existing_409_status_code_error_message(self): """test creating a new user who already exists in the DB""" # mock return value of get_user which checks if user exists self.mocked_db.return_value.get_user.return_value = {} example_user_request = EXAMPLE_USER_REQUEST.copy() # create example user request example_user_request['username'] = '******' # send request to test client response = self.test_app.post('/users', data=example_user_request) # assert 409 response code self.assertEqual(response.status_code, 409) # assert we get correct error message self.assertEqual( response.data, 'user {} already exists'.format( example_user_request['username']).encode())
def test_login_non_existent_user_404_status_code_error_message(self): """test logging in with a user that does not exist""" # mock return value of get_user which checks if user exists as None self.mocked_db.return_value.get_user.return_value = None # example user request example_user_request = EXAMPLE_USER_REQUEST.copy() example_user_request['username'] = '******' # send request to test client response = self.test_app.get('/login', query_string=example_user_request) # assert 404 response self.assertEqual(response.status_code, 404) # assert we get correct error message self.assertEqual( response.data, 'user {} does not exist'.format( example_user_request['username']).encode())