-
Notifications
You must be signed in to change notification settings - Fork 356
[ENG-9856] create a task to periodically clean up notification sent logs #11562
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
Merged
cslzchen
merged 2 commits into
CenterForOpenScience:feature/notifications-refactor-post-release
from
Ostap-Zherebetskyi:feature/notifications_cleanup_task
Jan 28, 2026
+216
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import pytest | ||
| from osf.models import Notification, NotificationType, EmailTask, NotificationSubscription | ||
| from notifications.tasks import ( | ||
| notifications_cleanup_task | ||
| ) | ||
| from osf_tests.factories import AuthUserFactory | ||
| from website.settings import NOTIFICATIONS_CLEANUP_AGE | ||
| from django.utils import timezone | ||
| from datetime import timedelta | ||
|
|
||
| def create_notification(subscription, sent_date=None): | ||
| return Notification.objects.create( | ||
| subscription=subscription, | ||
| event_context={}, | ||
| sent=sent_date | ||
| ) | ||
|
|
||
| def create_email_task(user, created_date): | ||
| et = EmailTask.objects.create( | ||
| task_id=f'test-{created_date.timestamp()}', | ||
| user=user, | ||
| status='SUCCESS', | ||
| ) | ||
| et.created_at = created_date | ||
| et.save() | ||
| return et | ||
|
|
||
| @pytest.mark.django_db | ||
| class TestNotificationCleanUpTask: | ||
|
|
||
| @pytest.fixture() | ||
| def user(self): | ||
| return AuthUserFactory() | ||
|
|
||
| @pytest.fixture() | ||
| def notification_type(self): | ||
| return NotificationType.objects.get_or_create( | ||
| name='Test Notification', | ||
| subject='Hello', | ||
| template='Sample Template', | ||
| )[0] | ||
|
|
||
| @pytest.fixture() | ||
| def subscription(self, user, notification_type): | ||
| return NotificationSubscription.objects.get_or_create( | ||
| user=user, | ||
| notification_type=notification_type, | ||
| message_frequency='daily', | ||
| )[0] | ||
|
|
||
| def test_dry_run_does_not_delete_records(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| old_notification = create_notification( | ||
| subscription, | ||
| sent_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| old_email_task = create_email_task( | ||
| user, | ||
| created_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
|
|
||
| notifications_cleanup_task(dry_run=True) | ||
|
|
||
| assert Notification.objects.filter(id=old_notification.id).exists() | ||
| assert EmailTask.objects.filter(id=old_email_task.id).exists() | ||
|
|
||
| def test_deletes_old_notifications_and_email_tasks(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| old_notification = create_notification( | ||
| subscription, | ||
| sent_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| new_notification = create_notification( | ||
| subscription, | ||
| sent_date=now - timedelta(days=10), | ||
| ) | ||
|
|
||
| old_email_task = create_email_task( | ||
| user, | ||
| created_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| new_email_task = create_email_task( | ||
| user, | ||
| created_date=now - timedelta(days=10), | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert not Notification.objects.filter(id=old_notification.id).exists() | ||
| assert Notification.objects.filter(id=new_notification.id).exists() | ||
|
|
||
| assert not EmailTask.objects.filter(id=old_email_task.id).exists() | ||
| assert EmailTask.objects.filter(id=new_email_task.id).exists() | ||
|
|
||
| def test_records_at_cutoff_are_not_deleted(self, user, subscription): | ||
| now = timezone.now() | ||
| cutoff = now - NOTIFICATIONS_CLEANUP_AGE + timedelta(hours=1) | ||
|
|
||
| notification = create_notification( | ||
| subscription, | ||
| sent_date=cutoff, | ||
| ) | ||
| email_task = create_email_task( | ||
| user, | ||
| created_date=cutoff, | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert Notification.objects.filter(id=notification.id).exists() | ||
| assert EmailTask.objects.filter(id=email_task.id).exists() | ||
|
|
||
| def test_cleanup_when_only_notifications_exist(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| notification = create_notification( | ||
| subscription, | ||
| sent_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert not Notification.objects.filter(id=notification.id).exists() | ||
|
|
||
| def test_cleanup_when_only_email_tasks_exist(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| email_task = create_email_task( | ||
| user, | ||
| created_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert not EmailTask.objects.filter(id=email_task.id).exists() | ||
|
|
||
| def test_task_is_idempotent(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| create_notification( | ||
| subscription, | ||
| sent_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| create_email_task( | ||
| user, | ||
| created_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
| notifications_cleanup_task() | ||
|
|
||
| assert Notification.objects.count() == 0 | ||
| assert EmailTask.objects.count() == 0 | ||
|
|
||
| def test_recent_records_are_not_deleted(self, user, subscription): | ||
| now = timezone.now() | ||
|
|
||
| create_notification( | ||
| subscription, | ||
| sent_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| create_email_task( | ||
| user, | ||
| created_date=now - NOTIFICATIONS_CLEANUP_AGE - timedelta(days=1), | ||
| ) | ||
| create_notification( | ||
| subscription, | ||
| sent_date=now, | ||
| ) | ||
| create_email_task( | ||
| user, | ||
| created_date=now, | ||
| ) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert Notification.objects.count() == 1 | ||
| assert EmailTask.objects.count() == 1 | ||
|
|
||
| def test_not_sent_notifications_are_not_deleted(self, user, subscription): | ||
| create_notification(subscription) | ||
| create_notification(subscription) | ||
| create_notification(subscription) | ||
|
|
||
| notifications_cleanup_task() | ||
|
|
||
| assert Notification.objects.count() == 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.