Skip to content
Snippets Groups Projects
test_mailutils.py 3.43 KiB
Newer Older
from django.core import mail
from django_q.tasks import result

import pytest
from gregui import mailutils


@pytest.mark.django_db
def test_registration_template(registration_template):
    prefilled_template = {
        "from_email": None,
        "recipient_list": ["test@example.com"],
        "subject": "registration subject",
        "message": """Dette er en automatisk generert melding fra gjesteregistreringstjenesten.
Du har blitt registrert som gjest på InstanceName av Foo Bar.
For å fullføre registreringen av gjestekontoen følg denne lenken: https://example.org/invite#secret-key

This message has been automatically generated by the guest registration system.
You have been registered as a guest at InstanceName by Foo Bar.
To complete the registration of your guest account, please follow this link: https://example.org/invite#secret-key""",
    }
    rendered_template = mailutils.registration_template(
        institution="InstanceName",
        sponsor="Foo Bar",
        mail_to="test@example.com",
        token="secret-key",
    assert rendered_template == prefilled_template


@pytest.mark.django_db
def test_confirmation_template(confirmation_template):
    prefilled_template = {
        "from_email": None,
        "recipient_list": ["test@example.com"],
        "subject": "confirmation subject",
        "message": """Dette er en automatisk generert melding fra gjesteregistreringstjenesten.
Din gjest, Foo Bar, har fullført registrering, bekreft gjesten her: https://example.org/sponsor/guest/123

This message has been automatically generated by the guest registration system.
Your guest, Foo Bar, has completed their registration, please confirm the guest here: https://example.org/sponsor/guest/123""",
    rendered_template = mailutils.confirmation_template(
        guest_name="Foo Bar", mail_to="test@example.com", guest_person_id=123
    )
    assert rendered_template == prefilled_template


@pytest.mark.django_db
def test_registration_mail(registration_template):
    mail.outbox = []
    task_id = mailutils.send_registration_mail(
        mail_to="test@example.no", sponsor="Foo", token="secret-key"
    )
    assert result(task_id) == 1
    assert len(mail.outbox) == 1
    assert mail.outbox[0].to == ["test@example.no"]


@pytest.mark.django_db
def test_confirmation_mail(confirmation_template):
    mail.outbox = []
    task_id = mailutils.send_confirmation_mail(
        mail_to="test@example.no", guest_name="Foo", guest_person_id=123
    )
    assert result(task_id) == 1
    assert len(mail.outbox) == 1
    assert mail.outbox[0].to == ["test@example.no"]
Andreas Ellewsen's avatar
Andreas Ellewsen committed


@pytest.mark.django_db
def test_send_invite_mail(registration_template, invited_person):
    """Verify function queues an email when called"""
    _, link = invited_person
    assert len(mail.outbox) == 0
    assert mailutils.send_invite_mail(link)
    assert len(mail.outbox) == 1


@pytest.mark.django_db
def test_send_invite_mail_no_mail(invited_person):
    """Verify function returns None if an email is not present"""
    person, link = invited_person
    person.private_email.delete()
    assert len(mail.outbox) == 0
    assert mailutils.send_invite_mail(link) is None
    assert len(mail.outbox) == 0


@pytest.mark.django_db
def test_confirmation_mail_from_link(invited_person, confirmation_template):
    mail.outbox = []
    _, link = invited_person
    assert len(mail.outbox) == 0
    assert mailutils.send_confirmation_mail_from_link(link)
    assert len(mail.outbox) == 1