Skip to content

Commit

Permalink
Update test cases after migraton to new NotFound exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadesh-Baral committed Aug 7, 2023
1 parent 9d28969 commit 4e009cd
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 108 deletions.
22 changes: 14 additions & 8 deletions tests/backend/integration/api/campaigns/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
23 changes: 16 additions & 7 deletions tests/backend/integration/api/comments/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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"], PROJECT_NOT_FOUND_MESSAGE)
self.assertEqual(error_details["sub_code"], PROJECT_NOT_FOUND_SUB_CODE)

def test_post_comment_to_task_chat_passes(self):
"""
Expand Down
19 changes: 12 additions & 7 deletions tests/backend/integration/api/interests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -168,9 +170,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):
"""
Expand Down Expand Up @@ -226,9 +229,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):
"""
Expand Down Expand Up @@ -311,9 +315,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):
"""
Expand Down
20 changes: 13 additions & 7 deletions tests/backend/integration/api/issues/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
11 changes: 9 additions & 2 deletions tests/backend/integration/api/licenses/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down
20 changes: 13 additions & 7 deletions tests/backend/integration/api/licenses/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
16 changes: 10 additions & 6 deletions tests/backend/integration/api/notifications/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "MESSAGE_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"

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
9 changes: 7 additions & 2 deletions tests/backend/integration/api/organisations/test_campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Loading

0 comments on commit 4e009cd

Please sign in to comment.