diff --git a/gregsite/settings/base.py b/gregsite/settings/base.py index 8ff941739add0caa06383b1f00a714428c9239fe..4d8d8b63c07c6dcf222ebf369d219b26267dd8c0 100644 --- a/gregsite/settings/base.py +++ b/gregsite/settings/base.py @@ -290,6 +290,9 @@ INSTANCE_NAME = "local" ENVIRONMENT = "unknown" INTERNAL_RK_PREFIX = "no.{instance}.greg".format(instance=INSTANCE_NAME) +# No trailing slash +BASE_URL = "https://example.org" + FEIDE_SOURCE = "feide" diff --git a/gregui/mailutils.py b/gregui/mailutils.py index 3d47e0bba0fa7c29552d577111a4b10a97d76fdf..83c8dc655d48b6455e58b61e6846933c3294626d 100644 --- a/gregui/mailutils.py +++ b/gregui/mailutils.py @@ -1,7 +1,6 @@ import logging -from typing import Optional -from typing import Union +from typing import Optional, Union from django.conf import settings from django.template.context import Context from django_q.tasks import async_task @@ -24,8 +23,22 @@ def prepare_arguments( } +def make_registration_url(token: str) -> str: + return "{base}/invite#{token}".format( + base=settings.BASE_URL, + token=token, + ) + + +def make_guest_profile_url(person_id: Union[str, int]) -> str: + return "{base}/sponsor/guest/{person_id}".format( + base=settings.BASE_URL, + person_id=person_id, + ) + + def registration_template( - institution: str, sponsor: str, mail_to: str + institution: str, sponsor: str, mail_to: str, token: str ) -> dict[str, Union[str, list[str]]]: """ Prepare email for registration @@ -36,17 +49,20 @@ def registration_template( template = EmailTemplate.objects.get( template_key=EmailTemplate.EmailType.GUEST_REGISTRATION ) + registration_link = make_registration_url(token) context = Context( { "institution": institution, "sponsor": sponsor, - "registration_link": "www.google.com", + "registration_link": registration_link, } ) return prepare_arguments(template, context, mail_to) -def confirmation_template(guest: str, mail_to: str) -> dict[str, Union[str, list[str]]]: +def confirmation_template( + guest_name: str, mail_to: str, guest_person_id: Union[str, int] +) -> dict[str, Union[str, list[str]]]: """ Prepare email for confirmation @@ -56,7 +72,8 @@ def confirmation_template(guest: str, mail_to: str) -> dict[str, Union[str, list template = EmailTemplate.objects.get( template_key=EmailTemplate.EmailType.SPONSOR_CONFIRMATION ) - context = Context({"guest": guest, "confirmation_link": "www.google.com"}) + confirmation_link = make_guest_profile_url(guest_person_id) + context = Context({"guest": guest_name, "confirmation_link": confirmation_link}) return prepare_arguments(template, context, mail_to) @@ -68,13 +85,17 @@ def reminder_template(mail_to: str, num_roles: int) -> dict[str, Union[str, list return prepare_arguments(template, context, mail_to) -def send_registration_mail(mail_to: str, sponsor: str) -> str: - arguments = registration_template(settings.INSTANCE_NAME, sponsor, mail_to) +def send_registration_mail(mail_to: str, sponsor: str, token: str) -> str: + arguments = registration_template(settings.INSTANCE_NAME, sponsor, mail_to, token) return async_task("django.core.mail.send_mail", **arguments) -def send_confirmation_mail(mail_to: str, guest: str) -> str: - arguments = confirmation_template(guest, mail_to) +def send_confirmation_mail( + mail_to: str, guest_name: str, guest_person_id: Union[int, str] +) -> str: + arguments = confirmation_template( + guest_name=guest_name, mail_to=mail_to, guest_person_id=guest_person_id + ) return async_task("django.core.mail.send_mail", **arguments) @@ -93,7 +114,9 @@ def send_invite_mail(link: InvitationLink) -> Optional[str]: sponsor = link.invitation.role.sponsor return send_registration_mail( - email_address.value, f"{sponsor.first_name} {sponsor.last_name}" + mail_to=email_address.value, + sponsor=f"{sponsor.first_name} {sponsor.last_name}", + token=link.uuid, ) @@ -101,4 +124,6 @@ def send_confirmation_mail_from_link(link: InvitationLink) -> Optional[str]: email_address = link.invitation.role.sponsor.work_email guest = link.invitation.role.person guest_name = f"{guest.first_name} {guest.last_name}" - return send_confirmation_mail(email_address, guest_name) + return send_confirmation_mail( + mail_to=email_address, guest_name=guest_name, guest_person_id=guest.id + ) diff --git a/gregui/tests/test_mailutils.py b/gregui/tests/test_mailutils.py index 6e1f0482a52ef05f4ca7b0a00f562de76fe37653..ce5f66cdea9a5bc9066b0ea157a06ebf2b627156 100644 --- a/gregui/tests/test_mailutils.py +++ b/gregui/tests/test_mailutils.py @@ -13,14 +13,17 @@ def test_registration_template(registration_template): "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: www.google.com +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: www.google.com""", +To complete the registration of your guest account, please follow this link: https://example.org/invite#secret-key""", } rendered_template = mailutils.registration_template( - "InstanceName", "Foo Bar", "test@example.com" + institution="InstanceName", + sponsor="Foo Bar", + mail_to="test@example.com", + token="secret-key", ) assert rendered_template == prefilled_template @@ -32,19 +35,23 @@ def test_confirmation_template(confirmation_template): "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: www.google.com +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: www.google.com""", +Your guest, Foo Bar, has completed their registration, please confirm the guest here: https://example.org/sponsor/guest/123""", } - rendered_template = mailutils.confirmation_template("Foo Bar", "test@example.com") + 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("test@example.no", "Foo") + 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"] @@ -53,7 +60,9 @@ def test_registration_mail(registration_template): @pytest.mark.django_db def test_confirmation_mail(confirmation_template): mail.outbox = [] - task_id = mailutils.send_confirmation_mail("test@example.no", "Foo") + 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"]