From 93d77f86d62797058805e4fe92c69222b2663b02 Mon Sep 17 00:00:00 2001
From: Stein Elgethun <stein.elgethun@usit.uio.no>
Date: Tue, 5 Oct 2021 17:18:07 +0200
Subject: [PATCH] Add mail sending with templates

Issue: GREG-42
---
 gregui/mailutils.py            | 37 ++++++++++++++++++++++++++
 gregui/tests/test_mailutils.py | 48 ++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 gregui/mailutils.py
 create mode 100644 gregui/tests/test_mailutils.py

diff --git a/gregui/mailutils.py b/gregui/mailutils.py
new file mode 100644
index 00000000..4968c978
--- /dev/null
+++ b/gregui/mailutils.py
@@ -0,0 +1,37 @@
+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("UiO", 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/tests/test_mailutils.py b/gregui/tests/test_mailutils.py
new file mode 100644
index 00000000..3e664d37
--- /dev/null
+++ b/gregui/tests/test_mailutils.py
@@ -0,0 +1,48 @@
+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 gjestregistreringstjenesten.
+Du har blitt registrert som gjest på UiO 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 UiO by Foo Bar.
+To complete the registration of your guest account, please follow this link: www.google.com
+"""
+    rendered_template = mailutils.registration_template("UiO", "Foo Bar")
+    assert rendered_template == prefilled_template
+
+
+@pytest.mark.django_db
+def test_confirmation_template():
+
+    prefilled_template = """Dette er en automatisk generert melding fra gjestregistreringstjenesten.
+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"]
-- 
GitLab