Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification API tests #5715

Merged
merged 4 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/api/notifications/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ def get(self):
- in: query
name: messageType
type: string
description: Optional message-type filter; leave blank to retrieve all
description: Optional message-type filter; leave blank to retrieve all\n
Accepted values are 1 (System), 2 (Broadcast), 3 (Mention), 4 (Validation),
5 (Invalidation), 6 (Request team), \n
7 (Invitation), 8 (Task comment), 9 (Project chat),
10 (Project Activity), and 11 (Team broadcast)
- in: query
name: from
description: Optional from username filter
Expand Down
1 change: 1 addition & 0 deletions backend/services/notification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def update(user_id: int):
raise NotFound()

notifications.update()
return notifications.unread_count

@staticmethod
def get_unread_message_count(user_id: int):
Expand Down
27 changes: 26 additions & 1 deletion tests/backend/helpers/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from backend.models.postgis.project import Project, ProjectTeams
from backend.models.postgis.campaign import Campaign
from backend.models.postgis.statuses import MappingLevel, TaskStatus
from backend.models.postgis.message import Message, MessageType
from backend.models.postgis.notification import Notification
from backend.models.postgis.task import Task
from backend.models.postgis.team import Team, TeamMembers
from backend.models.postgis.user import User
Expand All @@ -39,6 +41,8 @@
TEST_TEAM_NAME = "Test Team"
TEST_CAMPAIGN_NAME = "Test Campaign"
TEST_CAMPAIGN_ID = 1
TEST_MESSAGE_SUBJECT = "Test subject"
TEST_MESSAGE_DETAILS = "This is a test message"


def get_canned_osm_user_details():
Expand Down Expand Up @@ -359,8 +363,29 @@ def create_canned_license(name="test_license") -> int:


def create_canned_mapping_issue(name="Test Issue") -> int:
""""""
issue_dto = MappingIssueCategoryDTO()
issue_dto.name = name
test_issue_id = MappingIssueCategoryService.create_mapping_issue_category(issue_dto)
return test_issue_id


def create_canned_message(
subject=TEST_MESSAGE_SUBJECT,
message=TEST_MESSAGE_DETAILS,
message_type=MessageType.SYSTEM.value,
) -> Message:
test_message = Message()
test_message.subject = subject
test_message.message = message
test_message.message_type = message_type
test_message.save()
return test_message


def create_canned_notification(user_id, unread_count, date) -> Notification:
test_notification = Notification()
test_notification.user_id = user_id
test_notification.unread_count = unread_count
test_notification.date = date
test_notification.save()
return test_notification
Empty file.
52 changes: 52 additions & 0 deletions tests/backend/integration/api/notifications/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from tests.backend.base import BaseTestCase
from tests.backend.helpers.test_helpers import (
create_canned_user,
generate_encoded_token,
create_canned_message,
)

TEST_SUBJECT = "Test subject"
TEST_MESSAGE = "This is a test message"


class TestNotificationsActionsDeleteMultipleAPI(BaseTestCase):
def setUp(self):
super().setUp()
self.test_user = create_canned_user()
self.test_message_one = create_canned_message(
subject=TEST_SUBJECT, message=TEST_MESSAGE
)
self.test_message_two = create_canned_message(
subject=TEST_SUBJECT, message=TEST_MESSAGE
)
self.test_message_one.from_user_id = self.test_user.id
self.test_message_two.from_user_id = self.test_user.id
self.test_user_token = generate_encoded_token(self.test_user.id)
self.url = "/api/v2/notifications/delete-multiple/"

# delete
def test_delete_multiple_messages_returns_401(self):
"""
Test that endpoint returns 401 for unauthenticated user deleting multiple messages
"""
response = self.client.delete(
self.url,
json={"messageIds": [self.test_message_one.id, self.test_message_two.id]},
)
response_body = response.get_json()

self.assertEqual(response.status_code, 401)
self.assertEqual(response_body["SubCode"], "InvalidToken")

def test_delete_multiple_messages_returns_200(self):
"""
Test that endpoint returns 200 for authenticated user deleting multiple messages
"""
response = self.client.delete(
self.url,
headers={"Authorization": self.test_user_token},
json={"messageIds": [self.test_message_one.id, self.test_message_two.id]},
)
response_body = response.get_json()
self.assertEqual(response.status_code, 200)
self.assertEqual(response_body["Success"], "Messages deleted")
Loading