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 8aec540fe15c602e99505231e2292cb09839930c..8464e057d4f497b4c0af8b7f5c6a6472f1d38116 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 7a587d7204b1ae31eecc995bcca3d9a7e68a0e4d..0b6b1384d89036f8a3eb3f8b708559e721c8cca4 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 6d8c365d52be08db4551dc05044dd6cbb63cfa06..77171ff1b03f3c466673db9dfd9d809f6dd9da16 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 79ef045618310cf908776180f1ea95c743867481..a45d414b4d9b6eaad6308aff70710df1f3e825c2 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 92e9922bf0a6679641e10835a1b22c6eb2160ddb..45e967e1256fb32972cd1c6f156dc99d2f11f883 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 0000000000000000000000000000000000000000..7a2f0b518632387969f79afdcda97f9696172cd9 --- /dev/null +++ b/changes/changelog.d/806.bugfix @@ -0,0 +1 @@ +Use proper site name/domain in emails (#806)