diff --git a/gregsite/settings/dev.py b/gregsite/settings/dev.py index 7d8c2eb6fa33d69786197828f5ae8f2f111a5c5c..6cc23c5aa1f99a96b5e389400fb16703e3c0bf1b 100644 --- a/gregsite/settings/dev.py +++ b/gregsite/settings/dev.py @@ -3,6 +3,12 @@ from .base import * # This is the default configuration file when running manage.py ALLOWED_HOSTS += ["localhost", "127.0.0.1"] +# EMAIL_HOST = "smtp.uio.no" +# EMAIL_PORT = "468" +# EMAIL_USE_SSL = True +# EMAIL_TIMEOUT = 2 +# DEFAULT_FROM_EMAIL = "noreply@uio.no" +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" ORGREG_CLIENT = { "endpoints": {"base_url": "https://example.com/fake/"}, diff --git a/gregui/mailutils.py b/gregui/mailutils.py new file mode 100644 index 0000000000000000000000000000000000000000..06b624d6d1a303810f059d465a0d2a68bb7db1e4 --- /dev/null +++ b/gregui/mailutils.py @@ -0,0 +1,38 @@ +from django.conf import settings +from django.core.mail import send_mail +from django.template.loader import render_to_string + + +def registration_template(institution, sponsor) -> str: + keywords = { + "institution": institution, + "sponsor": sponsor, + "registration_link": "www.google.com" + } + return render_to_string("guest_registration.txt", keywords) + + +def confirmation_template(guest) -> str: + keywords = { + "guest": guest, + "confirmation_link": "www.google.com" + } + return render_to_string("sponsor_confirmation.txt", keywords) + + +def send_registration_mail(mail_to, sponsor) -> int: + return send_mail( + subject="Subject", + message=registration_template(settings.INSTANCE_NAME, sponsor), + from_email=None, + recipient_list=[mail_to] + ) + + +def send_confirmation_mail(mail_to, guest) -> int: + return send_mail( + subject="Subject", + message=confirmation_template(guest), + from_email=None, + recipient_list=[mail_to] + ) diff --git a/gregui/templates/guest_registration.txt b/gregui/templates/guest_registration.txt new file mode 100644 index 0000000000000000000000000000000000000000..cfc7666b00c0f33c69ae99fc8a572662273f4538 --- /dev/null +++ b/gregui/templates/guest_registration.txt @@ -0,0 +1,7 @@ +Dette er en automatisk generert melding fra gjesteregistreringstjenesten. +Du har blitt registrert som gjest på {{ institution }} av {{ sponsor }}. +For å fullføre registreringen av gjestekontoen følg denne lenken: {{ registration_link }} + +This message has been automatically generated by the guest registration system. +You have been registered as a guest at {{ institution }} by {{ sponsor }}. +To complete the registration of your guest account, please follow this link: {{ registration_link }} diff --git a/gregui/templates/sponsor_confirmation.txt b/gregui/templates/sponsor_confirmation.txt new file mode 100644 index 0000000000000000000000000000000000000000..eb64b83c272bf3b6a8c25886307965c922dfe555 --- /dev/null +++ b/gregui/templates/sponsor_confirmation.txt @@ -0,0 +1,5 @@ +Dette er en automatisk generert melding fra gjesteregistreringstjenesten. +Din gjest, {{ guest }}, har fullført registrering, bekreft gjesten her: {{ confirmation_link }} + +This message has been automatically generated by the guest registration system. +Your guest, {{ guest }}, has completed their registration, please confirm the guest here: {{ confirmation_link }} diff --git a/gregui/tests/test_mailutils.py b/gregui/tests/test_mailutils.py new file mode 100644 index 0000000000000000000000000000000000000000..25391f5d12898a832f6da139a94f77fb47f46e1a --- /dev/null +++ b/gregui/tests/test_mailutils.py @@ -0,0 +1,46 @@ +from django.core import mail + +import pytest +from gregui import mailutils + + +@pytest.mark.django_db +def test_registration_template(): + prefilled_template = """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: www.google.com + +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: www.google.com +""" + rendered_template = mailutils.registration_template("InstanceName", "Foo Bar") + assert rendered_template == prefilled_template + + +@pytest.mark.django_db +def test_confirmation_template(): + prefilled_template = """Dette er en automatisk generert melding fra gjesteregistreringstjenesten. +Din gjest, Foo Bar, har fullført registrering, bekreft gjesten her: www.google.com + +This message has been automatically generated by the guest registration system. +Your guest, Foo Bar, has completed their registration, please confirm the guest here: www.google.com +""" + rendered_template = mailutils.confirmation_template("Foo Bar") + assert rendered_template == prefilled_template + + +@pytest.mark.django_db +def test_registration_mail(): + mail.outbox = [] + assert mailutils.send_registration_mail("test@example.no", "Foo") == 1 + assert len(mail.outbox) == 1 + assert mail.outbox[0].to == ["test@example.no"] + + +@pytest.mark.django_db +def test_confirmation_mail(): + mail.outbox = [] + assert mailutils.send_confirmation_mail("test@example.no", "Foo") == 1 + assert len(mail.outbox) == 1 + assert mail.outbox[0].to == ["test@example.no"]