diff --git a/tests/backend/integration/api/campaigns/test_resources.py b/tests/backend/integration/api/campaigns/test_resources.py index 8a9b89296c..0e2cfbf154 100644 --- a/tests/backend/integration/api/campaigns/test_resources.py +++ b/tests/backend/integration/api/campaigns/test_resources.py @@ -7,10 +7,13 @@ return_canned_campaign, ) from backend.models.postgis.statuses import UserRole +from backend.exceptions import get_message_from_sub_code CAMPAIGN_NAME = "Test Campaign" CAMPAIGN_ID = 1 NEW_CAMPAIGN_NAME = "New Campaign" +CAMPAIGN_NOT_FOUND_SUB_CODE = "CAMPAIGN_NOT_FOUND" +CAMPAIGN_NOT_FOUND_MESSAGE = get_message_from_sub_code(CAMPAIGN_NOT_FOUND_SUB_CODE) class TestCampaignsRestAPI(BaseTestCase): @@ -43,9 +46,11 @@ def test_get_non_existent_campaign_by_id_fails(self): """ response = self.client.get(f"{self.endpoint_url}99/") response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "No campaign found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], CAMPAIGN_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], CAMPAIGN_NOT_FOUND_SUB_CODE) + self.assertEqual(error_details["details"], {"campaign_id": 99}) # patch def test_update_existent_campaign_by_admin_passes(self): @@ -133,9 +138,11 @@ def test_update_non_existent_campaign_by_id_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Campaign not found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], CAMPAIGN_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], CAMPAIGN_NOT_FOUND_SUB_CODE) + self.assertEqual(error_details["details"], {"campaign_id": 99}) # delete def test_delete_campaign_by_admin_passes(self): @@ -180,11 +187,10 @@ def test_delete_non_existent_campaign_fails(self): f"{self.endpoint_url}99/", headers={"Authorization": self.admin_token} ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual( - response_body, {"Error": "Campaign not found", "SubCode": "NotFound"} - ) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], CAMPAIGN_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], CAMPAIGN_NOT_FOUND_SUB_CODE) class TestCampaignsAllAPI(BaseTestCase): diff --git a/tests/backend/integration/api/comments/test_resources.py b/tests/backend/integration/api/comments/test_resources.py index 99012dc0e6..3c0bd70d91 100644 --- a/tests/backend/integration/api/comments/test_resources.py +++ b/tests/backend/integration/api/comments/test_resources.py @@ -6,13 +6,19 @@ generate_encoded_token, return_canned_user, ) -from backend.exceptions import NotFound +from backend.exceptions import NotFound, get_message_from_sub_code from backend.models.postgis.statuses import UserRole from backend.services.messaging.chat_service import ChatService, ChatMessageDTO from backend.services.messaging.message_service import MessageService TEST_MESSAGE = "Test comment" +PROJECT_NOT_FOUND_SUB_CODE = "PROJECT_NOT_FOUND" +TASK_NOT_FOUND_SUB_CODE = "TASK_NOT_FOUND" +MESSAGE_NOT_FOUND_SUB_CODE = "MESSAGE_NOT_FOUND" +PROJECT_NOT_FOUND_MESSAGE = get_message_from_sub_code(PROJECT_NOT_FOUND_SUB_CODE) +TASK_NOT_FOUND_MESSAGE = get_message_from_sub_code(TASK_NOT_FOUND_SUB_CODE) +MESSAGE_NOT_FOUND_MESSAGE = get_message_from_sub_code(MESSAGE_NOT_FOUND_SUB_CODE) class TestCommentsProjectsAllAPI(BaseTestCase): @@ -104,9 +110,10 @@ def test_get_chat_messages_of_non_existent_project_fails(self): """ response = self.client.get(self.non_existent_url) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Project not found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], PROJECT_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_get_project_chat_messages_passes(self): """ @@ -168,10 +175,11 @@ def test_delete_non_existent_comment_fails(self): self.non_existent_url, headers={"Authorization": self.test_author_token} ) response_body = response.get_json() + error_details = response_body["error"] # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Comment not found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], MESSAGE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], MESSAGE_NOT_FOUND_SUB_CODE) def test_returns_403_if_user_not_allowed_to_delete_comment(self): """ @@ -288,9 +296,10 @@ def test_post_comment_to_task_chat_of_non_existent_project_fails(self): json={"comment": TEST_MESSAGE}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Task Not Found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TASK_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_post_comment_to_task_chat_passes(self): """ diff --git a/tests/backend/integration/api/interests/test_resources.py b/tests/backend/integration/api/interests/test_resources.py index 01e8a8994a..b03a22a249 100644 --- a/tests/backend/integration/api/interests/test_resources.py +++ b/tests/backend/integration/api/interests/test_resources.py @@ -6,10 +6,12 @@ generate_encoded_token, create_canned_user, ) -from backend.api.interests.resources import INTEREST_NOT_FOUND +from backend.exceptions import get_message_from_sub_code TEST_INTEREST_NAME = "test_interest" NEW_INTEREST_NAME = "New Interest" +INTEREST_NOT_FOUND_SUB_CODE = "INTEREST_NOT_FOUND" +INTEREST_NOT_FOUND_MESSAGE = get_message_from_sub_code(INTEREST_NOT_FOUND_SUB_CODE) class TestInterestsAllAPI(BaseTestCase): @@ -153,9 +155,10 @@ def test_get_a_non_existent_interest_fails(self): headers={"Authorization": self.session_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], INTEREST_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], INTEREST_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], INTEREST_NOT_FOUND_SUB_CODE) def test_get_an_existing_interest_by_organisation_admin_passes(self): """ @@ -211,9 +214,10 @@ def test_update_a_non_existent_interest_fails(self): json={"name": NEW_INTEREST_NAME}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], INTEREST_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], INTEREST_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], INTEREST_NOT_FOUND_SUB_CODE) def test_update_an_existent_interest_with_invalid_data_fails(self): """ @@ -281,9 +285,10 @@ def test_delete_a_non_existent_interest_fails(self): headers={"Authorization": self.session_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], INTEREST_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], INTEREST_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], INTEREST_NOT_FOUND_SUB_CODE) def test_delete_an_existing_interest_by_organisation_admin_passes(self): """ diff --git a/tests/backend/integration/api/issues/test_resources.py b/tests/backend/integration/api/issues/test_resources.py index 7c11eb2780..99e6e57d60 100644 --- a/tests/backend/integration/api/issues/test_resources.py +++ b/tests/backend/integration/api/issues/test_resources.py @@ -5,9 +5,12 @@ create_canned_mapping_issue, ) +from backend.exceptions import get_message_from_sub_code + TEST_ISSUE_NAME = "Test Issue" TEST_ISSUE_DESCRIPTION = "Test issue description" -ISSUE_NOT_FOUND = "Mapping-issue category not found" +ISSUE_NOT_FOUND_SUB_CODE = "ISSUE_CATEGORY_NOT_FOUND" +ISSUE_NOT_FOUND_MESSAGE = get_message_from_sub_code(ISSUE_NOT_FOUND_SUB_CODE) class TestIssuesRestAPI(BaseTestCase): @@ -35,9 +38,10 @@ def test_get_non_existent_issue_fails(self): """ response = self.client.get(self.non_existent_url) response_json = response.get_json() + error_details = response_json["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_json["Error"], ISSUE_NOT_FOUND) - self.assertEqual(response_json["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ISSUE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ISSUE_NOT_FOUND_SUB_CODE) # patch def test_update_issue_by_unauthenticated_user_fails(self): @@ -78,9 +82,10 @@ def test_update_non_existent_issue_fails(self): json={"description": TEST_ISSUE_DESCRIPTION, "name": TEST_ISSUE_NAME}, ) response_json = response.get_json() + error_details = response_json["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_json["Error"], ISSUE_NOT_FOUND) - self.assertEqual(response_json["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ISSUE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ISSUE_NOT_FOUND_SUB_CODE) def test_update_mapping_issue_passes(self): """ @@ -120,9 +125,10 @@ def test_delete_non_existent_issue_fails(self): self.non_existent_url, headers={"Authorization": self.test_user_token} ) response_json = response.get_json() + error_details = response_json["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_json["Error"], ISSUE_NOT_FOUND) - self.assertEqual(response_json["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ISSUE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ISSUE_NOT_FOUND_SUB_CODE) def test_delete_mapping_issue_passes(self): """ diff --git a/tests/backend/integration/api/licenses/test_actions.py b/tests/backend/integration/api/licenses/test_actions.py index 8cf313fa2a..472ade7c40 100644 --- a/tests/backend/integration/api/licenses/test_actions.py +++ b/tests/backend/integration/api/licenses/test_actions.py @@ -5,6 +5,12 @@ create_canned_license, ) +from backend.exceptions import get_message_from_sub_code + + +LICENSE_NOT_FOUND_SUB_CODE = "LICENSE_NOT_FOUND" +LICENSE_NOT_FOUND_MESSAGE = get_message_from_sub_code(LICENSE_NOT_FOUND_SUB_CODE) + class TestLicensesActionsAcceptAPI(BaseTestCase): def setUp(self): @@ -34,9 +40,10 @@ def test_accept_terms_of_non_existent_license_fails(self): headers={"Authorization": self.test_user_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "User or License not found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], LICENSE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], LICENSE_NOT_FOUND_SUB_CODE) def test_accept_license_terms_by_authenticated_user_passes(self): """ diff --git a/tests/backend/integration/api/licenses/test_resources.py b/tests/backend/integration/api/licenses/test_resources.py index c51772bc4f..f46639837f 100644 --- a/tests/backend/integration/api/licenses/test_resources.py +++ b/tests/backend/integration/api/licenses/test_resources.py @@ -5,12 +5,15 @@ create_canned_license, ) +from backend.exceptions import get_message_from_sub_code + TEST_LICENSE_NAME = "test_license" TEST_LICENSE_DESCRIPTION = "test license" TEST_LICENSE_PLAINTEXT = "test license" NEW_LICENSE_DESCRIPTION = "A new test license" NEW_LICENSE_NAME = "New License" -LICENSE_NOT_FOUND = "License Not Found" +LICENSE_NOT_FOUND_SUB_CODE = "LICENSE_NOT_FOUND" +LICENSE_NOT_FOUND_MESSAGE = get_message_from_sub_code(LICENSE_NOT_FOUND_SUB_CODE) class TestLicensesRestAPI(BaseTestCase): @@ -81,9 +84,10 @@ def test_get_non_existent_license_fails(self): """ response = self.client.get(self.non_existent_url) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], LICENSE_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], LICENSE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], LICENSE_NOT_FOUND_SUB_CODE) def test_get_license_passes(self): """ @@ -143,9 +147,10 @@ def test_update_non_existent_license_by_authenticated_user_fails(self): }, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], LICENSE_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], LICENSE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], LICENSE_NOT_FOUND_SUB_CODE) def test_update_license_by_authenticated_user_passes(self): """ @@ -189,9 +194,10 @@ def test_delete_non_existent_license_by_authenticated_user_fails(self): headers={"Authorization": self.test_user_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], LICENSE_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], LICENSE_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], LICENSE_NOT_FOUND_SUB_CODE) def test_delete_license_by_authenticated_user_passes(self): """ diff --git a/tests/backend/integration/api/notifications/test_resources.py b/tests/backend/integration/api/notifications/test_resources.py index 01a9817c7d..274782edde 100644 --- a/tests/backend/integration/api/notifications/test_resources.py +++ b/tests/backend/integration/api/notifications/test_resources.py @@ -11,10 +11,12 @@ create_canned_project, ) +from backend.exceptions import get_message_from_sub_code + TEST_SUBJECT = "Test subject" TEST_MESSAGE = "This is a test message" -MESSAGES_NOT_FOUND = "No messages found" -NOT_FOUND = "NotFound" +NOT_FOUND_SUB_CODE = "NOT_FOUND" +NOT_FOUND_MESSAGE = get_message_from_sub_code(NOT_FOUND_SUB_CODE) OLDER_TEST_SUBJECT = "Older Test Subject" OLDER_TEST_MESSAGE = "This is an older test message" @@ -66,9 +68,10 @@ def test_get_message_returns_404(self): headers={"Authorization": self.test_sender_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], MESSAGES_NOT_FOUND) - self.assertEqual(response_body["SubCode"], NOT_FOUND) + self.assertEqual(error_details["message"], NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], NOT_FOUND_SUB_CODE) def test_get_message_returns_200(self): """ @@ -114,9 +117,10 @@ def test_delete_message_returns_404(self): headers={"Authorization": self.test_sender_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], MESSAGES_NOT_FOUND) - self.assertEqual(response_body["SubCode"], NOT_FOUND) + self.assertEqual(error_details["message"], NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], NOT_FOUND_SUB_CODE) def test_delete_message_returns_200(self): """ diff --git a/tests/backend/integration/api/organisations/test_campaigns.py b/tests/backend/integration/api/organisations/test_campaigns.py index 4a90eb263b..b0e1aeb861 100644 --- a/tests/backend/integration/api/organisations/test_campaigns.py +++ b/tests/backend/integration/api/organisations/test_campaigns.py @@ -6,6 +6,10 @@ return_canned_user, return_canned_campaign, ) +from tests.backend.integration.api.campaigns.test_resources import ( + CAMPAIGN_NOT_FOUND_MESSAGE, + CAMPAIGN_NOT_FOUND_SUB_CODE, +) CAMPAIGN_NAME = "New Campaign" CAMPAIGN_ID = 2 @@ -152,6 +156,7 @@ def test_delete_non_existent_organisation_campaign_fails(self): headers={"Authorization": self.test_author_session_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Organisation Campaign Not Found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], CAMPAIGN_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], CAMPAIGN_NOT_FOUND_SUB_CODE) diff --git a/tests/backend/integration/api/organisations/test_resources.py b/tests/backend/integration/api/organisations/test_resources.py index 148b046f16..8aec9f9f65 100644 --- a/tests/backend/integration/api/organisations/test_resources.py +++ b/tests/backend/integration/api/organisations/test_resources.py @@ -9,12 +9,15 @@ generate_encoded_token, return_canned_user, ) + +from backend.exceptions import get_message_from_sub_code from backend.services.users.authentication_service import AuthenticationService TEST_USER_ID = 777777 TEST_USERNAME = "Thinkwhere Test" -ORG_NOT_FOUND = "Organisation Not Found" +ORG_NOT_FOUND_SUB_CODE = "ORGANISATION_NOT_FOUND" +ORG_NOT_FOUND_MESSAGE = get_message_from_sub_code(ORG_NOT_FOUND_SUB_CODE) class TestOrganisationAllAPI(BaseTestCase): @@ -164,10 +167,10 @@ def test_get_non_existent_org_by_slug_fails(self): """ response = self.client.get("/api/v2/organisations/random-organisation/") response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(len(response_body), 2) - self.assertEqual(response_body["Error"], ORG_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ORG_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ORG_NOT_FOUND_SUB_CODE) class TestOrganisationsRestAPI(BaseTestCase): @@ -239,9 +242,10 @@ def test_get_non_existent_org_fails(self): headers={"Authorization": self.session_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], ORG_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ORG_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ORG_NOT_FOUND_SUB_CODE) # delete method tests def test_delete_org_by_admin_user_passes(self): @@ -383,7 +387,8 @@ def test_get_non_existent_org_statistics_fails(self): Tests that endpoint returns 404 when retrieving a non existent organisation's statistics """ response = self.client.get("/api/v2/organisations/99/statistics/") - self.assertEqual(response.status_code, 404) response_body = response.get_json() - self.assertEqual(response_body["Error"], ORG_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + error_details = response_body["error"] + self.assertEqual(response.status_code, 404) + self.assertEqual(error_details["message"], ORG_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ORG_NOT_FOUND_SUB_CODE) diff --git a/tests/backend/integration/api/system/test_authentication.py b/tests/backend/integration/api/system/test_authentication.py index 08ce11b5cb..62ff06316c 100644 --- a/tests/backend/integration/api/system/test_authentication.py +++ b/tests/backend/integration/api/system/test_authentication.py @@ -10,7 +10,10 @@ from backend.services.messaging.smtp_service import SMTPService from backend.services.users.authentication_service import AuthenticationService from tests.backend.helpers.test_helpers import return_canned_user - +from tests.backend.integration.api.users.test_resources import ( + USER_NOT_FOUND_MESSAGE, + USER_NOT_FOUND_SUB_CODE, +) USERNAME = "test_user" USER_ID = 1234 @@ -201,10 +204,11 @@ def test_returns_404_if_user_does_not_exist(self): response = self.client.get( self.url, query_string={"username": "non_existent_user"} ) + error_details = response.json["error"] # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "UserNotFound") - self.assertEqual(response.json["Error"], "User not found") + self.assertEqual(error_details["sub_code"], USER_NOT_FOUND_SUB_CODE) + self.assertEqual(error_details["message"], USER_NOT_FOUND_MESSAGE) def test_returns_403_if_invalid_email_token(self): # Arrange diff --git a/tests/backend/integration/api/tasks/test_actions.py b/tests/backend/integration/api/tasks/test_actions.py index 9c0bf3454e..4d7bb034aa 100644 --- a/tests/backend/integration/api/tasks/test_actions.py +++ b/tests/backend/integration/api/tasks/test_actions.py @@ -18,9 +18,17 @@ generate_encoded_token, create_canned_license, ) +from tests.backend.integration.api.users.test_resources import ( + USER_NOT_FOUND_SUB_CODE, + USER_NOT_FOUND_MESSAGE, +) from backend.models.postgis.task import Task, TaskAction +PROJECT_NOT_FOUND_SUB_CODE = "PROJECT_NOT_FOUND" +TASK_NOT_FOUND_SUB_CODE = "TASK_NOT_FOUND" + + class TasksActionsMapAllAPI(BaseTestCase): def setUp(self): super().setUp() @@ -344,7 +352,7 @@ def test_mapping_lock_returns_404_for_invalid_project_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_mapping_lock_returns_404_for_invalid_task_id(self): """Test returns 404 on request with invalid task id.""" @@ -355,7 +363,7 @@ def test_mapping_lock_returns_404_for_invalid_task_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) @patch.object(ProjectService, "is_user_permitted_to_map") def test_mapping_lock_returns_403_for_if_user_not_allowed_to_map( @@ -489,7 +497,7 @@ def test_mapping_unlock_returns_404_for_invalid_project_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_mapping_unlock_returns_404_for_invalid_task_id(self): """Test returns 404 on request with invalid task id.""" @@ -501,7 +509,7 @@ def test_mapping_unlock_returns_404_for_invalid_task_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_mapping_unlock_returns_403_if_task_not_locked_for_mapping(self): """Test returns 403 if task is not locked for mapping.""" @@ -632,7 +640,7 @@ def test_mapping_stop_returns_404_for_invalid_project_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_mapping_stop_returns_404_for_invalid_task_id(self): """Test returns 404 on request with invalid task id.""" @@ -643,7 +651,7 @@ def test_mapping_stop_returns_404_for_invalid_task_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_mapping_stop_returns_403_if_task_not_locked_for_mapping(self): """Test returns 403 if task not locked for mapping.""" @@ -756,7 +764,7 @@ def test_validation_lock_returns_404_for_invalid_project_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_validation_lock_returns_404_for_invalid_task_id(self): """Test returns 404 on request with invalid task id.""" @@ -768,7 +776,7 @@ def test_validation_lock_returns_404_for_invalid_task_id(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_validation_lock_returns_403_if_task_not_ready_for_validation(self): """Test returns 403 if task not ready for validation.""" @@ -954,7 +962,7 @@ def test_validation_unlock_returns_404_if_project_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_validation_unlock_returns_404_if_task_not_found(self): """Test returns 404 if task not found.""" @@ -966,7 +974,7 @@ def test_validation_unlock_returns_404_if_task_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_validation_unlock_returns_403_if_task_not_locked_for_validation(self): """Test returns 403 if task not locked for validation.""" @@ -1138,7 +1146,7 @@ def test_validation_stop_returns_404_if_project_not_found(self): # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_validation_stop_returns_404_if_task_not_found(self): """Test returns 404 if task not found.""" @@ -1150,7 +1158,7 @@ def test_validation_stop_returns_404_if_task_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_validation_stop_returns_403_if_task_not_locked_for_validation(self): """Test returns 403 if task not locked for validation.""" @@ -1262,7 +1270,7 @@ def test_returns_404_if_project_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_returns_404_if_task_not_found(self): """Test returns 404 if task not found.""" @@ -1273,7 +1281,7 @@ def test_returns_404_if_task_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_returns_403_if_task_too_small_to_split(self): """Test returns 403 if task too small to split.""" @@ -1363,7 +1371,7 @@ def test_returns_404_if_project_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_returns_404_if_task_not_found(self): """Test returns 404 if task not found.""" @@ -1374,7 +1382,7 @@ def test_returns_404_if_task_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_returns_403_if_task_in_invalid_state_for_undo(self): """Test returns 403 if task in invalid state for undo.""" @@ -1523,7 +1531,7 @@ def test_returns_404_if_project_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_returns_404_if_task_not_found(self): """Test returns 404 if task not found.""" @@ -1535,7 +1543,7 @@ def test_returns_404_if_task_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], TASK_NOT_FOUND_SUB_CODE) def test_returns_403_if_task_not_locked(self): """Test returns 403 if task not locked.""" @@ -1612,10 +1620,11 @@ def test_returns_404_if_user_not_found(self): headers={"Authorization": self.author_access_token}, query_string={"username": "invalid_user", "action": "VALIDATED"}, ) + error_details = response.json["error"] # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") - self.assertEqual(response.json["Error"], "User not found") + self.assertEqual(error_details["sub_code"], USER_NOT_FOUND_SUB_CODE) + self.assertEqual(error_details["message"], USER_NOT_FOUND_MESSAGE) def test_returns_400_if_action_not_valid(self): """Test returns 400 if action not valid.""" @@ -1639,7 +1648,7 @@ def test_returns_404_if_project_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], PROJECT_NOT_FOUND_SUB_CODE) def test_returns_403_if_user_doesnot_have_PM_permission(self): """Test returns 403 if user doesnot have PM permission.""" diff --git a/tests/backend/integration/api/teams/test_actions.py b/tests/backend/integration/api/teams/test_actions.py index 30d3256050..931ba0412c 100644 --- a/tests/backend/integration/api/teams/test_actions.py +++ b/tests/backend/integration/api/teams/test_actions.py @@ -13,13 +13,18 @@ TeamJoinMethod, TeamMemberFunctions, ) -from backend.api.teams.actions import TEAM_NOT_FOUND +from backend.exceptions import get_message_from_sub_code TEST_ADMIN_USERNAME = "Test Admin" TEST_MESSAGE = "This is a test message" TEST_SUBJECT = "Test Subject" NON_EXISTENT_USER = "Random User" +TEAM_NOT_FOUND_SUB_CODE = "TEAM_NOT_FOUND" +USER_NOT_FOUND_SUB_CODE = "USER_NOT_FOUND" +TEAM_NOT_FOUND_MESSAGE = get_message_from_sub_code(TEAM_NOT_FOUND_SUB_CODE) +USER_NOT_FOUND_MESSAGE = get_message_from_sub_code(USER_NOT_FOUND_SUB_CODE) + class TestTeamsActionsJoinAPI(BaseTestCase): def setUp(self): @@ -63,7 +68,7 @@ def test_request_to_join_non_existent_team_fails(self): ) response_body = response.get_json() self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], TEAM_NOT_FOUND) + self.assertEqual(response_body["error"]["message"], TEAM_NOT_FOUND_MESSAGE) def test_request_to_join_team_with_invite_only_request_fails(self): """ @@ -154,9 +159,10 @@ def test_handle_join_request_to_non_existent_team_fails(self): }, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], TEAM_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TEAM_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TEAM_NOT_FOUND_SUB_CODE) class TestTeamsActionsAddAPI(BaseTestCase): @@ -252,9 +258,9 @@ def test_add_non_existent_members_to_team_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() - self.assertEqual(response.status_code, 500) + self.assertEqual(response.status_code, 404) error_resp = response_body["error"] - self.assertEqual(error_resp["sub_code"], "INTERNAL_SERVER_ERROR") + self.assertEqual(error_resp["sub_code"], "USER_NOT_FOUND") def test_add_members_to_non_existent_team_fails(self): """ @@ -269,10 +275,9 @@ def test_add_members_to_non_existent_team_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() - print(response_body) - self.assertEqual(response.status_code, 500) + self.assertEqual(response.status_code, 404) error_resp = response_body["error"] - self.assertEqual(error_resp["sub_code"], "INTERNAL_SERVER_ERROR") + self.assertEqual(error_resp["sub_code"], "TEAM_NOT_FOUND") class TestTeamsActionsLeaveAPI(BaseTestCase): @@ -357,9 +362,10 @@ def test_remove_non_existent_members_from_team_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "No team member found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], USER_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], USER_NOT_FOUND_SUB_CODE) def test_remove_members_from_non_existent_team_fails(self): """ @@ -373,9 +379,10 @@ def test_remove_members_from_non_existent_team_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "No team member found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TEAM_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TEAM_NOT_FOUND_SUB_CODE) class TestTeamsActionsMessageMembersAPI(BaseTestCase): @@ -403,9 +410,10 @@ def test_message_members_non_existent_team_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], TEAM_NOT_FOUND) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TEAM_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TEAM_NOT_FOUND_SUB_CODE) def test_message_team_members_by_unauthenticated_user_fails(self): """ diff --git a/tests/backend/integration/api/teams/test_resources.py b/tests/backend/integration/api/teams/test_resources.py index a410ea11bb..5ff3baeaa5 100644 --- a/tests/backend/integration/api/teams/test_resources.py +++ b/tests/backend/integration/api/teams/test_resources.py @@ -7,6 +7,16 @@ return_canned_team, create_canned_team, ) +from tests.backend.integration.api.teams.test_actions import ( + TEAM_NOT_FOUND_SUB_CODE, + TEAM_NOT_FOUND_MESSAGE, +) +from tests.backend.integration.api.organisations.test_resources import ( + ORG_NOT_FOUND_MESSAGE, + ORG_NOT_FOUND_SUB_CODE, +) + + from backend.models.postgis.statuses import UserRole TEST_ORGANISATION_NAME = "Kathmandu Living Labs" @@ -32,9 +42,10 @@ def test_get_non_existent_team_by_id_fails(self): """ response = self.client.get("/api/v2/teams/99/") response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Team Not Found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TEAM_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TEAM_NOT_FOUND_SUB_CODE) def test_get_team_by_id_passes(self): """ @@ -143,9 +154,10 @@ def test_delete_non_existent_team_by_id_fails(self): "/api/v2/teams/99/", headers={"Authorization": self.test_user_token} ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual(response_body["Error"], "Team Not Found") - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], TEAM_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], TEAM_NOT_FOUND_SUB_CODE) class TestTeamsAllPI(BaseTestCase): @@ -177,11 +189,10 @@ def test_create_new_team_for_non_existent_org_fails(self): headers={"Authorization": self.admin_token}, ) response_body = response.get_json() + error_details = response_body["error"] self.assertEqual(response.status_code, 404) - self.assertEqual( - response_body["Error"], "Team POST - Organisation does not exist" - ) - self.assertEqual(response_body["SubCode"], "NotFound") + self.assertEqual(error_details["message"], ORG_NOT_FOUND_MESSAGE) + self.assertEqual(error_details["sub_code"], ORG_NOT_FOUND_SUB_CODE) def test_create_new_team_non_admin_fails(self): """ diff --git a/tests/backend/integration/api/users/test_actions.py b/tests/backend/integration/api/users/test_actions.py index a9e9f06e92..0dcc76cfba 100644 --- a/tests/backend/integration/api/users/test_actions.py +++ b/tests/backend/integration/api/users/test_actions.py @@ -279,7 +279,7 @@ def test_returns_404_if_user_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], "USER_NOT_FOUND") def test_returns_200_if_user_role_set(self): """Test that the API returns 200 if user role is set""" diff --git a/tests/backend/integration/api/users/test_resources.py b/tests/backend/integration/api/users/test_resources.py index 5d9f34e9f3..fadd26e264 100644 --- a/tests/backend/integration/api/users/test_resources.py +++ b/tests/backend/integration/api/users/test_resources.py @@ -1,5 +1,7 @@ from backend.models.postgis.task import Task, TaskStatus from backend.models.postgis.statuses import UserGender, UserRole, MappingLevel +from backend.exceptions import get_message_from_sub_code + from tests.backend.base import BaseTestCase from tests.backend.helpers.test_helpers import ( @@ -13,6 +15,8 @@ TEST_USERNAME = "test_user" TEST_USER_ID = 1111111 TEST_EMAIL = "test@hotmail.com" +USER_NOT_FOUND_SUB_CODE = "USER_NOT_FOUND" +USER_NOT_FOUND_MESSAGE = get_message_from_sub_code(USER_NOT_FOUND_SUB_CODE) class TestUsersQueriesOwnLockedDetailsAPI(BaseTestCase): @@ -39,7 +43,7 @@ def test_returns_404_if_no_tasks_locked(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], "TASK_NOT_FOUND") def test_returns_200_if_tasks_locked(self): """Test that the API returns 200 if a task is locked by user""" @@ -84,7 +88,7 @@ def test_returns_404_if_user_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], USER_NOT_FOUND_SUB_CODE) @staticmethod def assert_user_detail_response( @@ -233,7 +237,7 @@ def test_returns_404_if_user_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], USER_NOT_FOUND_SUB_CODE) def test_returns_user_interests_if_interest_found(self): """Test that the API returns user interests if user has interests""" @@ -284,7 +288,7 @@ def test_returns_404_if_no_users_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], USER_NOT_FOUND_SUB_CODE) def test_returns_users_if_users_found(self): """Test that the API returns users if users found""" diff --git a/tests/backend/integration/api/users/test_statistics.py b/tests/backend/integration/api/users/test_statistics.py index 63fc046ac4..d7a095ac5e 100644 --- a/tests/backend/integration/api/users/test_statistics.py +++ b/tests/backend/integration/api/users/test_statistics.py @@ -9,6 +9,7 @@ generate_encoded_token, return_canned_user, ) +from tests.backend.integration.api.users.test_resources import USER_NOT_FOUND_SUB_CODE class TestUsersStatisticsAPI(BaseTestCase): @@ -34,7 +35,7 @@ def test_return_404_if_user_not_found(self): ) # Assert self.assertEqual(response.status_code, 404) - self.assertEqual(response.json["SubCode"], "NotFound") + self.assertEqual(response.json["error"]["sub_code"], USER_NOT_FOUND_SUB_CODE) def test_return_200_if_user_found(self): """Test returns 200 if user found."""