|
| 1 | +from datetime import date, timedelta |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from outdated.notifications.notifier import Notifier |
| 7 | +from outdated.outdated.models import Maintainer |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("nonprimary_maintainers", [False, True]) |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "days_until_outdated,template,sent", |
| 13 | + [ |
| 14 | + (200, None, False), |
| 15 | + (60, "test-foo", True), |
| 16 | + (50, "test-foo", True), |
| 17 | + (10, "test-bar", True), |
| 18 | + (-20, "test-baz", True), |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_send_notification( |
| 22 | + setup_notifications, |
| 23 | + days_until_outdated: int, |
| 24 | + template: Optional[str], |
| 25 | + sent: bool, |
| 26 | + nonprimary_maintainers: bool, |
| 27 | + maintainer, |
| 28 | + maintainer_factory, |
| 29 | + mailoutbox, |
| 30 | + version_factory, |
| 31 | + release_version_factory, |
| 32 | +): |
| 33 | + project = maintainer.project |
| 34 | + release_version = release_version_factory( |
| 35 | + end_of_life=date.today() + timedelta(days=days_until_outdated) |
| 36 | + ) |
| 37 | + version = version_factory(release_version=release_version) |
| 38 | + project.versioned_dependencies.add(version) |
| 39 | + project.save() |
| 40 | + if nonprimary_maintainers: |
| 41 | + maintainer_factory(project=project) |
| 42 | + maintainer_factory(project=project) |
| 43 | + maintainer_factory(project=project) |
| 44 | + nonprimary_maintainers = list(Maintainer.objects.filter(is_primary=False)) |
| 45 | + notification_queue = list(project.notification_queue.all()) |
| 46 | + Notifier(project).notify() |
| 47 | + if sent: |
| 48 | + mail = mailoutbox[0] |
| 49 | + assert mail.subject == template.replace("test-", "") |
| 50 | + assert ( |
| 51 | + mail.body |
| 52 | + == f"Project: {project.name}\nRepo: {project.repo}\n{template}.txt contents\n" |
| 53 | + ) |
| 54 | + assert mail.to[0] == maintainer.user.email |
| 55 | + assert mail.cc == [m.user.email for m in nonprimary_maintainers] |
| 56 | + assert notification_queue[1:] == list(project.notification_queue.all()) |
| 57 | + else: |
| 58 | + assert not mailoutbox |
0 commit comments