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

tests: use MagicMock for mailer tests. #679

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
123 changes: 83 additions & 40 deletions backend/test/unit/test_mailer.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,127 @@
import datetime

import pytest

from appointment.controller.mailer import ConfirmationMail, RejectionMail, ZoomMeetingFailedMail, InvitationMail, \
NewBookingMail, Attachment
from unittest.mock import patch, MagicMock
from appointment.controller.mailer import (
ConfirmationMail,
RejectionMail,
ZoomMeetingFailedMail,
InvitationMail,
NewBookingMail,
Attachment,
)
from appointment.database import schemas


class TestMailer:
def test_invite(self, with_l10n):
fake_email = '[email protected]'

@patch("appointment.controller.mailer.add_task", new_callable=MagicMock)
def test_invite(self, mock_add_task, with_l10n):
fake_email = "[email protected]"
mailer = InvitationMail(
to=fake_email,
name='fake',
email='[email protected]',
name="fake",
email="[email protected]",
date=datetime.datetime.now(),
duration=30,
attachments=[Attachment(
mime=('text', 'calendar'),
filename='test.ics',
data=b''
)]
attachments=[
Attachment(mime=("text", "calendar"), filename="test.ics", data=b"")
],
)
assert mailer.html()
assert mailer.text()

mock_add_task.assert_called_with(
"InvitationMail",
fake_email,
"fake",
"[email protected]",
mailer.date,
30,
"test.ics",
)

def test_confirm(self, faker, with_l10n):
confirm_url = 'https://example.org/yes'
deny_url = 'https://example.org/no'
fake_email = '[email protected]'
@patch("appointment.controller.mailer.add_task", new_callable=MagicMock)
def test_confirm(self, mock_add_task, faker, with_l10n):
confirm_url = "https://example.org/yes"
deny_url = "https://example.org/no"
fake_email = "[email protected]"
now = datetime.datetime.now()
attendee = schemas.AttendeeBase(email=faker.email(), name=faker.name(), timezone='Europe/Berlin')

mailer = ConfirmationMail(confirm_url, deny_url, attendee.name, attendee.email, now, to=fake_email, duration=30, schedule_name='test')
attendee = schemas.AttendeeBase(
email=faker.email(), name=faker.name(), timezone="Europe/Berlin"
)
mailer = ConfirmationMail(
confirm_url,
deny_url,
attendee.name,
attendee.email,
now,
to=fake_email,
duration=30,
schedule_name="test",
)
assert mailer.html()
assert mailer.text()

for idx, content in enumerate([mailer.text(), mailer.html()]):
fault = 'text' if idx == 0 else 'html'
fault = "text" if idx == 0 else "html"
assert confirm_url in content, fault
assert deny_url in content, fault
assert attendee.name in content, fault
assert attendee.email in content, fault

def test_new_booking(self, faker, with_l10n):
fake_email = '[email protected]'
now = datetime.datetime.now()
attendee = schemas.AttendeeBase(email=faker.email(), name=faker.name(), timezone='Europe/Berlin')
mock_add_task.assert_called_with(
"ConfirmationMail",
confirm_url,
deny_url,
attendee.name,
attendee.email,
now,
30,
"test schedule",
)

mailer = NewBookingMail(attendee.name, attendee.email, now, 30, 'test schedule', to=fake_email)
@patch("appointment.controller.mailer.add_task", new_callable=MagicMock)
def test_new_booking(self, mock_add_task, faker, with_l10n):
fake_email = "[email protected]"
now = datetime.datetime.now()
attendee = schemas.AttendeeBase(
email=faker.email(), name=faker.name(), timezone="Europe/Berlin"
)
mailer = NewBookingMail(
attendee.name, attendee.email, now, 30, "test schedule", to=fake_email
)
assert mailer.html()
assert mailer.text()

for idx, content in enumerate([mailer.text(), mailer.html()]):
fault = 'text' if idx == 0 else 'html'
fault = "text" if idx == 0 else "html"
assert attendee.name in content, fault
assert attendee.email in content, fault

def test_reject(self, faker, with_l10n, make_pro_subscriber):
mock_add_task.assert_called_with(
"NewBookingMail", attendee.name, attendee.email, now, 30, "test schedule"
)

@patch("appointment.controller.mailer.add_task", new_callable=MagicMock)
def test_reject(self, mock_add_task, faker, with_l10n, make_pro_subscriber):
subscriber = make_pro_subscriber()
now = datetime.datetime.now()
fake_email = '[email protected]'

fake_email = "[email protected]"
mailer = RejectionMail(owner_name=subscriber.name, date=now, to=fake_email)
assert mailer.html()
assert mailer.text()

for idx, content in enumerate([mailer.text(), mailer.html()]):
fault = 'text' if idx == 0 else 'html'
fault = "text" if idx == 0 else "html"
assert subscriber.name in content, fault

def test_zoom_invite_failed(self, faker, with_l10n):
fake_title = faker.name()
fake_email = '[email protected]'
mock_add_task.assert_called_with("RejectionMail", subscriber.name, now)

@patch("appointment.controller.mailer.add_task", new_callable=MagicMock)
def test_zoom_invite_failed(self, mock_add_task, faker, with_l10n):
fake_title = faker.name()
fake_email = "[email protected]"
mailer = ZoomMeetingFailedMail(appointment_title=fake_title, to=fake_email)
assert mailer.html()
assert mailer.text()

for idx, content in enumerate([mailer.text(), mailer.html()]):
fault = 'text' if idx == 0 else 'html'
fault = "text" if idx == 0 else "html"
assert fake_title in content, fault

mock_add_task.assert_called_with("ZoomMeetingFailedMail", fake_title)