def test_error_put_update_total_endpoint(self): """ Test that a PUT request with a new total updates the user """ random_user = generate_random_user() new_total = "I'm a wrong total" with patch.object(server, "update_total", return_value=False) as mock_update_total: url = URL_BASE + "users/{}/".format(random_user["username"]) response = self.app.put(url, data=ujson.dumps(dict(total=new_total)), content_type='application/json') self.assertEqual(mock_update_total.call_args[0][0], random_user["username"]) self.assertEqual(mock_update_total.call_args[0][1], new_total) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") response_message = "Error updating user '{}'".format( random_user["username"]) self.assertIn("message", response_dict) self.assertEqual(response_dict["message"], response_message)
def test_delete_user_error(self, mock_mongo): """ Test that an error when deleting a user is handled correctly """ random_user = generate_random_user() delete_error_raw_result = {"ok": 0} mock_mongo.db.users.delete_one.return_value.raw_result = delete_error_raw_result deleted = user_helpers.delete_user(random_user["username"]) self.assertFalse(deleted)
def test_get_single_user(self, mock_mongo): """ Test that a single user can be retrieved by its username """ random_user = generate_random_user() mock_mongo.db.users.find_one.return_value = random_user result = user_helpers.get_single_user(random_user["username"]) self.assertEqual(result, random_user) self.assertEqual(mock_mongo.db.users.find_one.call_args[0][0], {"username": random_user["username"]})
def test_delete_user_correct(self, mock_mongo): """ Test that a correct deletion works as expected """ random_user = generate_random_user() delete_ok_raw_result = {"ok": 1} mock_mongo.db.users.delete_one.return_value.raw_result = delete_ok_raw_result deleted = user_helpers.delete_user(random_user["username"]) self.assertTrue(deleted) self.assertEqual(mock_mongo.db.users.delete_one.call_args[0][0], {"username": random_user["username"]})
def test_create_new_user_successful(self, mock_mongo): """ Test that creating a user works as expected if the user is valid """ random_user = generate_random_user() str_random_user = str(random_user) mock_mongo.db.users.insert.return_value = random_user with patch.object(user_helpers, "validate_user", return_value=(True, "")): created, result = user_helpers.create_new_user(random_user) self.assertTrue(created) self.assertEqual(result, str_random_user)
def test_create_new_user_error(self, mock_mongo): """ Test that creating a user returns an error if the user is not valid """ random_user = generate_random_user() err_msg = "I'm an error message" mock_mongo.db.users.insert.return_value = random_user with patch.object(user_helpers, "validate_user", return_value=(False, err_msg)): created, result = user_helpers.create_new_user(random_user) self.assertFalse(created) self.assertEqual(result, err_msg)
def test_update_total_error(self, mock_mongo): """ Test that an error is properly handled when the update does not work as expected """ random_user = generate_random_user() random_total = random.randint(5, 100) update_error_raw_result = {"ok": 0} mock_mongo.db.users.update_one.return_value.raw_result = update_error_raw_result updated = user_helpers.update_total(random_user["username"], random_total) self.assertFalse(updated)
def test_update_total_successful(self, mock_mongo): """ Test that a user can be updated with a new total value """ random_user = generate_random_user() random_total = random.randint(5, 100) update_ok_result_dict = {"ok": 1} mock_mongo.db.users.update_one.return_value.raw_result = update_ok_result_dict update_param_dict = {"username": random_user["username"]}, {'$set': {'total': random_total}} updated = user_helpers.update_total(random_user["username"], random_total) self.assertTrue(updated) self.assertEqual(mock_mongo.db.users.update_one.call_args[0], update_param_dict)
def test_get_single_user_enpoint(self): """ Test that a correct GET with a username returns the user """ random_user = generate_random_user() with patch.object(server, "get_single_user", return_value=random_user): response = self.app.get(URL_BASE + "users/{}/".format(random_user["username"])) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "ok") self.assertIn("data", response_dict) self.assertEqual(response_dict["data"], random_user)
def test_get_single_user_enpoint_error(self): """ Test that a wrong GET with a username returns the user """ random_user = generate_random_user() with patch.object(server, "get_single_user", return_value=None) as mock_get_user: response = self.app.get(URL_BASE + "users/{}/".format(random_user["username"])) self.assertEqual(mock_get_user.call_args[0][0], random_user["username"]) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") self.assertIn("message", response_dict) self.assertEqual(response_dict["message"], "Unable to retrieve user '{}'".format(random_user["username"]))
def test_get_single_user_enpoint(self): """ Test that a correct GET with a username returns the user """ random_user = generate_random_user() with patch.object(server, "get_single_user", return_value=random_user): response = self.app.get( URL_BASE + "users/{}/".format(random_user["username"])) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "ok") self.assertIn("data", response_dict) self.assertEqual(response_dict["data"], random_user)
def test_post_create_user(self): """ Test that a successful POST creates a user """ random_user = generate_random_user() with patch.object(server, "create_new_user", return_value=(True, str(random_user))): response = self.app.post(URL_BASE + "users/", data=ujson.dumps(random_user), content_type='application/json') response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "ok") self.assertIn("data", response_dict) self.assertEqual(response_dict["data"], str(random_user))
def test_wrong_post_failure(self): """ Test that a wrong POST fails creating a user """ random_user = generate_random_user() err_msg = "I'm an error message" with patch.object(server, "create_new_user", return_value=(False, err_msg)): response = self.app.post(URL_BASE + "users/", data=ujson.dumps(random_user), content_type='application/json') response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") self.assertIn("error", response_dict) self.assertEqual(response_dict["error"], err_msg) self.assertIn("message", response_dict)
def test_error_delete_user_endpoint(self): """ Test that a wrong DELETE request returns an error """ random_user = generate_random_user() with patch.object(server, "delete_user", return_value=False) as mock_delete_user: url = URL_BASE + "users/{}/".format(random_user["username"]) response = self.app.delete(url, content_type='application/json') self.assertEqual(mock_delete_user.call_args[0][0], random_user["username"]) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") response_message = "Error deleting user '{}'".format(random_user["username"]) self.assertIn("message", response_dict) self.assertEqual(response_dict["message"], response_message)
def test_get_single_user_enpoint_error(self): """ Test that a wrong GET with a username returns the user """ random_user = generate_random_user() with patch.object(server, "get_single_user", return_value=None) as mock_get_user: response = self.app.get( URL_BASE + "users/{}/".format(random_user["username"])) self.assertEqual(mock_get_user.call_args[0][0], random_user["username"]) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") self.assertIn("message", response_dict) self.assertEqual( response_dict["message"], "Unable to retrieve user '{}'".format(random_user["username"]))
def test_error_put_update_total_endpoint(self): """ Test that a PUT request with a new total updates the user """ random_user = generate_random_user() new_total = "I'm a wrong total" with patch.object(server, "update_total", return_value=False) as mock_update_total: url = URL_BASE + "users/{}/".format(random_user["username"]) response = self.app.put(url, data=ujson.dumps(dict(total=new_total)), content_type='application/json') self.assertEqual(mock_update_total.call_args[0][0], random_user["username"]) self.assertEqual(mock_update_total.call_args[0][1], new_total) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") response_message = "Error updating user '{}'".format(random_user["username"]) self.assertIn("message", response_dict) self.assertEqual(response_dict["message"], response_message)
def test_update_total_successful(self, mock_mongo): """ Test that a user can be updated with a new total value """ random_user = generate_random_user() random_total = random.randint(5, 100) update_ok_result_dict = {"ok": 1} mock_mongo.db.users.update_one.return_value.raw_result = update_ok_result_dict update_param_dict = { "username": random_user["username"] }, { '$set': { 'total': random_total } } updated = user_helpers.update_total(random_user["username"], random_total) self.assertTrue(updated) self.assertEqual(mock_mongo.db.users.update_one.call_args[0], update_param_dict)
def test_error_delete_user_endpoint(self): """ Test that a wrong DELETE request returns an error """ random_user = generate_random_user() with patch.object(server, "delete_user", return_value=False) as mock_delete_user: url = URL_BASE + "users/{}/".format(random_user["username"]) response = self.app.delete(url, content_type='application/json') self.assertEqual(mock_delete_user.call_args[0][0], random_user["username"]) response_dict = ujson.loads(response.data.decode("utf-8")) self.assertIn("status", response_dict) self.assertEqual(response_dict["status"], "error") response_message = "Error deleting user '{}'".format( random_user["username"]) self.assertIn("message", response_dict) self.assertEqual(response_dict["message"], response_message)