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

fix flaky email tests #2546

Merged
merged 2 commits into from
Feb 5, 2024
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
8 changes: 8 additions & 0 deletions changelog/_4567.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Added

- test helper for testing emails
- initial doc on testing

### Fixed

- fixed the flaky test_notify_creator_exclude_moderator test
14 changes: 14 additions & 0 deletions docs/tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more a wip for now


## Backend Tests

We use pytest in combination with pytest-django and factoryboy to write backend
tests.

### Notes

#### Testing Emails

Emails are not guaranteed to be sent in a deterministic order, so checking
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably also check the other tests at some point

against an index will result in flaky tests. If you want to assert
that a mail was sent use one of the helper functions in `tests/helpers.py`.
7 changes: 7 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.core import mail


def get_emails_for_address(email_address):
"""Return all mails send to email_address"""
mails = list(filter(lambda mails: mails.to[0] == email_address, mail.outbox))
return mails
8 changes: 5 additions & 3 deletions tests/notifications/test_creator_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import reverse

from adhocracy4.test.helpers import redirect_target
from tests.helpers import get_emails_for_address


@pytest.mark.django_db
Expand Down Expand Up @@ -33,10 +34,11 @@ def test_notify_creator_exclude_moderator(idea, comment_factory, user):
comment_factory(content_object=idea)

assert len(mail.outbox) == 3
mails = get_emails_for_address(creator_moderator.email)
assert len(mails) == 1
# moderator notification instead of creator notification
assert mail.outbox[1].to[0] == creator_moderator.email
assert not mail.outbox[1].subject.startswith("Reaction to your contribution")
assert mail.outbox[1].subject.startswith("A comment was added to the project")
assert not mails[0].subject.startswith("Reaction to your contribution")
assert mails[0].subject.startswith("A comment was added to the project")


@pytest.mark.django_db
Expand Down