From 39485c8e02c0f5648adb9d7c70a622e5e8c4d56f Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Tue, 23 Apr 2019 11:14:58 +0200 Subject: [PATCH] Fix #806: Use proper site name/domain in emails --- .../email/email_confirmation_message.txt | 4 +-- .../registration/password_reset_email.html | 4 +-- api/funkwhale_api/users/adapters.py | 13 +++++++- api/funkwhale_api/users/serializers.py | 4 +-- api/tests/users/test_views.py | 32 +++++++++++++++++-- changes/changelog.d/806.bugfix | 1 + 6 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 changes/changelog.d/806.bugfix diff --git a/api/funkwhale_api/templates/account/email/email_confirmation_message.txt b/api/funkwhale_api/templates/account/email/email_confirmation_message.txt index 8aec540fe..8464e057d 100644 --- a/api/funkwhale_api/templates/account/email/email_confirmation_message.txt +++ b/api/funkwhale_api/templates/account/email/email_confirmation_message.txt @@ -1,8 +1,8 @@ -{% load account %}{% user_display user as user_display %}{% load i18n %}{% autoescape off %}{% blocktrans with site_name=current_site.name site_domain=current_site.domain %}Hello from {{ site_name }}! +{% load account %}{% user_display user as user_display %}{% load i18n %}{% autoescape off %}{% blocktrans with site_name=funkwhale_site_name site_domain=funkwhale_site_domain %}Hello from {{ site_name }}! You're receiving this e-mail because user {{ user_display }} at {{ site_domain }} has given yours as an e-mail address to connect their account. To confirm this is correct, go to {{ funkwhale_url }}/auth/email/confirm?key={{ key }} {% endblocktrans %}{% endautoescape %} -{% blocktrans with site_name=current_site.name site_domain=current_site.domain %}Thank you from {{ site_name }}! +{% blocktrans with site_name=funkwhale_site_name site_domain=funkwhale_site_domain %}Thank you from {{ site_name }}! {{ site_domain }}{% endblocktrans %} diff --git a/api/funkwhale_api/templates/registration/password_reset_email.html b/api/funkwhale_api/templates/registration/password_reset_email.html index 7a587d720..0b6b1384d 100644 --- a/api/funkwhale_api/templates/registration/password_reset_email.html +++ b/api/funkwhale_api/templates/registration/password_reset_email.html @@ -1,5 +1,5 @@ {% load i18n %}{% autoescape off %} -{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %} +{% blocktrans with site_name=funkwhale_site_name %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %} {% trans "Please go to the following page and choose a new password:" %} {{ funkwhale_url }}/auth/password/reset/confirm?uid={{ uid }}&token={{ token }} @@ -7,6 +7,6 @@ {% trans "Thanks for using our site!" %} -{% blocktrans %}The {{ site_name }} team{% endblocktrans %} +{% blocktrans with site_name=funkwhale_site_name %}The {{ site_name }} team{% endblocktrans %} {% endautoescape %} diff --git a/api/funkwhale_api/users/adapters.py b/api/funkwhale_api/users/adapters.py index 6d8c365d5..77171ff1b 100644 --- a/api/funkwhale_api/users/adapters.py +++ b/api/funkwhale_api/users/adapters.py @@ -3,11 +3,22 @@ from django.conf import settings from dynamic_preferences.registries import global_preferences_registry +def get_email_context(): + context = {} + context["funkwhale_url"] = settings.FUNKWHALE_URL + manager = global_preferences_registry.manager() + context["funkwhale_site_name"] = ( + manager["instance__name"] or settings.FUNKWHALE_HOSTNAME + ) + context["funkwhale_site_domain"] = settings.FUNKWHALE_HOSTNAME + return context + + class FunkwhaleAccountAdapter(DefaultAccountAdapter): def is_open_for_signup(self, request): manager = global_preferences_registry.manager() return manager["users__registration_enabled"] def send_mail(self, template_prefix, email, context): - context["funkwhale_url"] = settings.FUNKWHALE_URL + context.update(get_email_context()) return super().send_mail(template_prefix, email, context) diff --git a/api/funkwhale_api/users/serializers.py b/api/funkwhale_api/users/serializers.py index 79ef04561..a45d414b4 100644 --- a/api/funkwhale_api/users/serializers.py +++ b/api/funkwhale_api/users/serializers.py @@ -1,6 +1,5 @@ import re -from django.conf import settings from django.core import validators from django.utils.deconstruct import deconstructible from django.utils.translation import gettext_lazy as _ @@ -12,6 +11,7 @@ from versatileimagefield.serializers import VersatileImageFieldSerializer from funkwhale_api.activity import serializers as activity_serializers from funkwhale_api.common import serializers as common_serializers +from . import adapters from . import models @@ -133,4 +133,4 @@ class MeSerializer(UserReadSerializer): class PasswordResetSerializer(PRS): def get_email_options(self): - return {"extra_email_context": {"funkwhale_url": settings.FUNKWHALE_URL}} + return {"extra_email_context": adapters.get_email_context()} diff --git a/api/tests/users/test_views.py b/api/tests/users/test_views.py index 92e9922bf..45e967e12 100644 --- a/api/tests/users/test_views.py +++ b/api/tests/users/test_views.py @@ -168,15 +168,20 @@ def test_changing_password_updates_secret_key(logged_in_api_client): assert user.password != password -def test_can_request_password_reset(factories, api_client, mailoutbox): +def test_can_request_password_reset( + factories, preferences, settings, api_client, mailoutbox +): user = factories["users.User"]() payload = {"email": user.email} - emails = len(mailoutbox) url = reverse("rest_password_reset") + preferences["instance__name"] = "Hello world" response = api_client.post(url, payload) assert response.status_code == 200 - assert len(mailoutbox) > emails + + confirmation_message = mailoutbox[-1] + assert "Hello world" in confirmation_message.body + assert settings.FUNKWHALE_HOSTNAME in confirmation_message.body def test_user_can_patch_his_own_settings(logged_in_api_client): @@ -287,3 +292,24 @@ def test_creating_user_creates_actor_as_well( user = User.objects.get(username="test1") assert user.actor == actor + + +def test_creating_user_sends_confirmation_email( + api_client, db, settings, preferences, mailoutbox +): + url = reverse("rest_register") + data = { + "username": "test1", + "email": "test1@test.com", + "password1": "testtest", + "password2": "testtest", + } + preferences["users__registration_enabled"] = True + preferences["instance__name"] = "Hello world" + response = api_client.post(url, data) + + assert response.status_code == 201 + + confirmation_message = mailoutbox[-1] + assert "Hello world" in confirmation_message.body + assert settings.FUNKWHALE_HOSTNAME in confirmation_message.body diff --git a/changes/changelog.d/806.bugfix b/changes/changelog.d/806.bugfix new file mode 100644 index 000000000..7a2f0b518 --- /dev/null +++ b/changes/changelog.d/806.bugfix @@ -0,0 +1 @@ +Use proper site name/domain in emails (#806)