|
| 1 | +# tests/test_triggered_mails.py |
| 2 | +from datetime import timedelta |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from django.utils import timezone |
| 6 | + |
| 7 | +from tests.base import OsfTestCase |
| 8 | +from tests.utils import run_celery_tasks, capture_notifications |
| 9 | + |
| 10 | +from osf_tests.factories import UserFactory |
| 11 | +from osf.models import EmailTask, NotificationType |
| 12 | + |
| 13 | +from scripts.triggered_mails import ( |
| 14 | + find_inactive_users_without_enqueued_or_sent_no_login, |
| 15 | + main, |
| 16 | + NO_LOGIN_PREFIX, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def _inactive_time(): |
| 21 | + """Make a timestamp that is definitely 'inactive' regardless of threshold settings.""" |
| 22 | + # Very conservative: 12 weeks ago |
| 23 | + return timezone.now() - timedelta(weeks=12) |
| 24 | + |
| 25 | + |
| 26 | +def _recent_time(): |
| 27 | + """Make a timestamp that is definitely NOT inactive.""" |
| 28 | + return timezone.now() - timedelta(seconds=10) |
| 29 | + |
| 30 | + |
| 31 | +class TestTriggeredMails(OsfTestCase): |
| 32 | + def setUp(self): |
| 33 | + super().setUp() |
| 34 | + self.user = UserFactory() |
| 35 | + self.user.date_last_login = timezone.now() |
| 36 | + self.user.save() |
| 37 | + |
| 38 | + def test_dont_trigger_no_login_mail_for_recent_login(self): |
| 39 | + self.user.date_last_login = _recent_time() |
| 40 | + self.user.save() |
| 41 | + |
| 42 | + with run_celery_tasks(): |
| 43 | + main(dry_run=False) |
| 44 | + |
| 45 | + # No task should be created |
| 46 | + assert EmailTask.objects.filter( |
| 47 | + user=self.user, task_id__startswith=NO_LOGIN_PREFIX |
| 48 | + ).count() == 0 |
| 49 | + |
| 50 | + def test_trigger_no_login_mail_enqueues_and_runs_success_path(self): |
| 51 | + """Inactive user -> EmailTask is enqueued and task runs without failing.""" |
| 52 | + self.user.date_last_login = _inactive_time() |
| 53 | + self.user.save() |
| 54 | + |
| 55 | + # Intercept .emit so we don't depend on template rendering |
| 56 | + with capture_notifications(), run_celery_tasks(): |
| 57 | + main(dry_run=False) |
| 58 | + |
| 59 | + tasks = EmailTask.objects.filter( |
| 60 | + user=self.user, task_id__startswith=NO_LOGIN_PREFIX |
| 61 | + ).order_by('id') |
| 62 | + assert tasks.count() == 1 |
| 63 | + # Current task code sets STARTED and only flips to FAILURE on exception; |
| 64 | + # allow either STARTED (no explicit success mark yet) or SUCCESS if added later. |
| 65 | + assert tasks.latest('id').status in {'STARTED', 'SUCCESS'} |
| 66 | + |
| 67 | + def test_trigger_no_login_mail_failure_marks_task_failure(self): |
| 68 | + """If sending raises, the task should capture the exception and mark FAILURE.""" |
| 69 | + self.user.date_last_login = _inactive_time() |
| 70 | + self.user.save() |
| 71 | + |
| 72 | + # Force the emit call to raise to exercise failure branch |
| 73 | + with mock.patch.object( |
| 74 | + NotificationType.Type.USER_NO_LOGIN.instance, |
| 75 | + 'emit', |
| 76 | + side_effect=RuntimeError('kaboom'), |
| 77 | + ), run_celery_tasks(): |
| 78 | + main(dry_run=False) |
| 79 | + |
| 80 | + task = EmailTask.objects.filter( |
| 81 | + user=self.user, task_id__startswith=NO_LOGIN_PREFIX |
| 82 | + ).latest('id') |
| 83 | + task.refresh_from_db() |
| 84 | + assert task.status == 'FAILURE' |
| 85 | + assert 'kaboom' in (task.error_message or '') |
| 86 | + |
| 87 | + def test_finder_returns_two_inactive_when_none_queued(self): |
| 88 | + """Two inactive users, no prior tasks -> finder returns both.""" |
| 89 | + u1 = UserFactory(fullname='Jordan Mailata') |
| 90 | + u2 = UserFactory(fullname='Jake Elliot') |
| 91 | + u1.date_last_login = _inactive_time() |
| 92 | + u2.date_last_login = _inactive_time() |
| 93 | + u1.save() |
| 94 | + u2.save() |
| 95 | + |
| 96 | + users = list(find_inactive_users_without_enqueued_or_sent_no_login()) |
| 97 | + ids = {u.id for u in users} |
| 98 | + assert ids == {u1.id, u2.id} |
| 99 | + |
| 100 | + def test_finder_excludes_users_with_existing_task(self): |
| 101 | + """Inactive users but one already has a no_login EmailTask -> excluded.""" |
| 102 | + u1 = UserFactory(fullname='Jalen Hurts') |
| 103 | + u2 = UserFactory(fullname='Jason Kelece') |
| 104 | + u1.date_last_login = _inactive_time() |
| 105 | + u2.date_last_login = _inactive_time() |
| 106 | + u1.save() |
| 107 | + u2.save() |
| 108 | + |
| 109 | + # Pretend u2 already had this email flow (SUCCESS qualifies for exclusion) |
| 110 | + EmailTask.objects.create( |
| 111 | + task_id=f"{NO_LOGIN_PREFIX}existing-success", |
| 112 | + user=u2, |
| 113 | + status='SUCCESS', |
| 114 | + ) |
| 115 | + |
| 116 | + users = list(find_inactive_users_without_enqueued_or_sent_no_login()) |
| 117 | + ids = {u.id for u in users} |
| 118 | + assert ids == {u1.id} # u2 excluded because of existing task |
0 commit comments