Skip to content

Commit

Permalink
Adapt notification integration tests to work w/o tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Apr 9, 2024
1 parent e01025a commit 7796d66
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions test/integration/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def notification_broadcast_test_data(subject: Optional[str] = None, message: Opt
}


class TestNotificationsIntegration(IntegrationTestCase):
class NotificationsIntegrationBase(IntegrationTestCase):
dataset_populator: DatasetPopulator
task_based = False
framework_tool_and_types = False
Expand All @@ -67,12 +67,18 @@ def test_notification_status(self):
before_creating_notifications = datetime.utcnow()

# Only user1 will receive this notification
created_response_1 = self._send_test_notification_to([user1["id"]], message="test_notification_status 1")
assert created_response_1["total_notifications_sent"] == 1
subject1 = f"notification_{uuid4()}"
created_response_1 = self._send_test_notification_to(
[user1["id"]], subject=subject1, message="test_notification_status 1"
)
self._assert_notifications_sent(created_response_1, expected_count=1)

# Both user1 and user2 will receive this notification
created_response_2 = self._send_test_notification_to([user1["id"], user2["id"]], "test_notification_status 2")
assert created_response_2["total_notifications_sent"] == 2
subject2 = f"notification_{uuid4()}"
created_response_2 = self._send_test_notification_to(
[user1["id"], user2["id"]], subject=subject2, message="test_notification_status 2"
)
self._assert_notifications_sent(created_response_2, expected_count=2)

# All users will receive this broadcasted notification
created_response_3 = self._send_broadcast_notification("test_notification_status 3")
Expand Down Expand Up @@ -114,7 +120,8 @@ def test_notification_status(self):
assert len(status["broadcasts"]) == 0

# Updating a notification association value should return that notification in the next request
notification_id = created_response_2["notification"]["id"]
notification_id = self._get_notification_id_by_subject(subject2)
assert notification_id is not None
self._update_notification(notification_id, update_state={"seen": True})
status = self._get_notifications_status_since(after_creating_notifications)
assert status["total_unread_count"] == 0
Expand All @@ -132,16 +139,21 @@ def test_user_cannot_access_other_users_notifications(self):
user1 = self._create_test_user()
user2 = self._create_test_user()

subject = f"notification_{uuid4()}"
created_response = self._send_test_notification_to(
[user1["id"]], message="test_user_cannot_access_other_users_notifications"
[user1["id"]], subject=subject, message="test_user_cannot_access_other_users_notifications"
)
notification_id = created_response["notification"]["id"]
self._assert_notifications_sent(created_response, expected_count=1)
notification_id = None

with self._different_user(user1["email"]):
notification_id = self._get_notification_id_by_subject(subject)
assert notification_id is not None
response = self._get(f"notifications/{notification_id}")
self._assert_status_code_is_ok(response)

with self._different_user(user2["email"]):
assert notification_id is not None
response = self._get(f"notifications/{notification_id}")
self._assert_status_code_is(response, 404)

Expand All @@ -151,13 +163,15 @@ def test_delete_notification_by_user(self):

before_creating_notifications = datetime.utcnow()

subject = f"notification_{uuid4()}"
created_response = self._send_test_notification_to(
[user1["id"], user2["id"]], message="test_delete_notification_by_user"
[user1["id"], user2["id"]], subject=subject, message="test_delete_notification_by_user"
)
assert created_response["total_notifications_sent"] == 2
notification_id = created_response["notification"]["id"]
self._assert_notifications_sent(created_response, expected_count=2)

with self._different_user(user1["email"]):
notification_id = self._get_notification_id_by_subject(subject)
assert notification_id is not None
response = self._get(f"notifications/{notification_id}")
self._assert_status_code_is_ok(response)
self._delete(f"notifications/{notification_id}")
Expand All @@ -170,6 +184,7 @@ def test_delete_notification_by_user(self):
assert len(status["broadcasts"]) == 0

with self._different_user(user2["email"]):
assert notification_id is not None
response = self._get(f"notifications/{notification_id}")
self._assert_status_code_is_ok(response)

Expand All @@ -196,11 +211,14 @@ def test_non_admin_users_cannot_create_notifications(self):

def test_update_notifications(self):
recipient_user = self._create_test_user()
created_user_notification_response = self._send_test_notification_to(
[recipient_user["id"]], subject="User Notification to update"
)
assert created_user_notification_response["total_notifications_sent"] == 1
user_notification_id = created_user_notification_response["notification"]["id"]
subject = f"User Notification to update {uuid4()}"
created_user_notification_response = self._send_test_notification_to([recipient_user["id"]], subject=subject)
self._assert_notifications_sent(created_user_notification_response, expected_count=1)

with self._different_user(recipient_user["email"]):
user_notification_id = self._get_notification_id_by_subject(subject)

assert user_notification_id is not None

created_broadcast_notification_response = self._send_broadcast_notification(
subject="Broadcasted Notification to update"
Expand Down Expand Up @@ -403,3 +421,36 @@ def _send_broadcast_notification(
def _update_notification(self, notification_id: str, update_state: Dict[str, Any]):
update_response = self._put(f"notifications/{notification_id}", data=update_state, json=True)
self._assert_status_code_is(update_response, 204)

def _assert_notifications_sent(self, response, expected_count: int = 0):
if self.task_based:
task_id = response["id"]
assert task_id is not None
self.dataset_populator.wait_on_task_id(task_id)
else:
assert response["total_notifications_sent"] == expected_count

def _get_notification_id_by_subject(self, subject: str) -> Optional[str]:
notifications = self._get("notifications").json()
for notification in notifications:
if notification["content"]["subject"] == subject:
return notification["id"]
return None


class TestNotificationsIntegration(NotificationsIntegrationBase):
task_based = False

@classmethod
def handle_galaxy_config_kwds(cls, config):
super().handle_galaxy_config_kwds(config)
config["enable_celery_tasks"] = False


class TestNotificationsIntegrationTaskBased(NotificationsIntegrationBase):
task_based = True

@classmethod
def handle_galaxy_config_kwds(cls, config):
super().handle_galaxy_config_kwds(config)
config["enable_celery_tasks"] = True

0 comments on commit 7796d66

Please sign in to comment.