diff --git a/CHANGELOG.md b/CHANGELOG.md index 39f16a92..c4d59f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to - ✨(frontend) integrate configurable Waffle #1795 - ✨ Import of documents #1609 - 🚨(CI) gives warning if theme not updated #1811 +- 🔧(project) add DJANGO_EMAIL_URL_APP environment variable #1825 ### Changed diff --git a/docs/env.md b/docs/env.md index e4ed43d5..186cfb30 100644 --- a/docs/env.md +++ b/docs/env.md @@ -56,6 +56,7 @@ These are the environment variables you can set for the `impress-backend` contai | DJANGO_EMAIL_HOST_USER | User to authenticate with on the email host | | | DJANGO_EMAIL_LOGO_IMG | Logo for the email | | | DJANGO_EMAIL_PORT | Port used to connect to email host | | +| DJANGO_EMAIL_URL_APP | Url used in the email to go to the app | | | DJANGO_EMAIL_USE_SSL | Use ssl for email host connection | false | | DJANGO_EMAIL_USE_TLS | Use tls for email host connection | false | | DJANGO_SECRET_KEY | Secret key | | diff --git a/docs/examples/helm/impress.values.yaml b/docs/examples/helm/impress.values.yaml index ce2fd8b2..57fab24c 100644 --- a/docs/examples/helm/impress.values.yaml +++ b/docs/examples/helm/impress.values.yaml @@ -27,6 +27,7 @@ backend: DJANGO_EMAIL_HOST: "mailcatcher" DJANGO_EMAIL_LOGO_IMG: https://docs.127.0.0.1.nip.io/assets/logo-suite-numerique.png DJANGO_EMAIL_PORT: 1025 + DJANGO_EMAIL_URL_APP: https://docs.127.0.0.1.nip.io DJANGO_EMAIL_USE_SSL: False LOGGING_LEVEL_HANDLERS_CONSOLE: ERROR LOGGING_LEVEL_LOGGERS_ROOT: INFO diff --git a/docs/installation/compose.md b/docs/installation/compose.md index a0a65a8f..f8bb49a9 100644 --- a/docs/installation/compose.md +++ b/docs/installation/compose.md @@ -127,6 +127,7 @@ DJANGO_EMAIL_FROM= DJANGO_EMAIL_BRAND_NAME= # e.g. "La Suite Numérique" DJANGO_EMAIL_LOGO_IMG= # e.g. "https://docs.yourdomain.tld/assets/logo-suite-numerique.png" +DJANGO_EMAIL_URL_APP= # e.g. "https://docs.yourdomain.tld" ``` ### AI diff --git a/env.d/development/common b/env.d/development/common index ab598d89..7f8b1311 100644 --- a/env.d/development/common +++ b/env.d/development/common @@ -20,6 +20,7 @@ DJANGO_EMAIL_BRAND_NAME="La Suite Numérique" DJANGO_EMAIL_HOST="mailcatcher" DJANGO_EMAIL_LOGO_IMG="http://localhost:3000/assets/logo-suite-numerique.png" DJANGO_EMAIL_PORT=1025 +DJANGO_EMAIL_URL_APP="http://localhost:3000" # Backend url IMPRESS_BASE_URL="http://localhost:8072" diff --git a/env.d/production.dist/backend b/env.d/production.dist/backend index c4f70f86..1016bd3f 100644 --- a/env.d/production.dist/backend +++ b/env.d/production.dist/backend @@ -24,7 +24,8 @@ DJANGO_EMAIL_FROM= #DJANGO_EMAIL_USE_SSL=true # A flag to enable or disable SSL for email sending. DJANGO_EMAIL_BRAND_NAME="La Suite Numérique" -DJANGO_EMAIL_LOGO_IMG="https://${DOCS_HOST}/assets/logo-suite-numerique.png" +DJANGO_EMAIL_LOGO_IMG="https://${DOCS_HOST}/assets/logo-suite-numerique.png" +DJANGO_EMAIL_URL_APP="https://${DOCS_HOST}" # Media AWS_S3_ENDPOINT_URL=https://${S3_HOST} diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 25e97bd1..71abd253 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -817,7 +817,7 @@ class Document(MP_Node, BaseModel): def send_email(self, subject, emails, context=None, language=None): """Generate and send email from a template.""" context = context or {} - domain = Site.objects.get_current().domain + domain = settings.EMAIL_URL_APP or Site.objects.get_current().domain language = language or get_language() context.update( { diff --git a/src/backend/core/tests/test_models_documents.py b/src/backend/core/tests/test_models_documents.py index 521b07c1..e5432cdd 100644 --- a/src/backend/core/tests/test_models_documents.py +++ b/src/backend/core/tests/test_models_documents.py @@ -1024,6 +1024,39 @@ def test_models_documents__email_invitation__success(): assert f"docs/{document.id}/" in email_content +@pytest.mark.parametrize( + "email_url_app", + [ + "https://test-example.com", # Test with EMAIL_URL_APP set + None, # Test fallback to Site domain + ], +) +def test_models_documents__email_invitation__url_app_param(email_url_app): + """ + Test that email invitation uses EMAIL_URL_APP when set, or falls back to Site domain. + """ + with override_settings(EMAIL_URL_APP=email_url_app): + document = factories.DocumentFactory() + + sender = factories.UserFactory( + full_name="Test Sender", email="sender@example.com" + ) + document.send_invitation_email( + "guest@example.com", models.RoleChoices.EDITOR, sender, "en" + ) + + # pylint: disable-next=no-member + email = mail.outbox[0] + email_content = " ".join(email.body.split()) + + # Determine expected domain + if email_url_app: + assert f"https://test-example.com/docs/{document.id}/" in email_content + else: + # Default Site domain is example.com + assert f"example.com/docs/{document.id}/" in email_content + + def test_models_documents__email_invitation__success_empty_title(): """ The email invitation is sent successfully. diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index 60bf31fe..75101603 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -459,6 +459,7 @@ class Base(Configuration): EMAIL_HOST_PASSWORD = SecretFileValue(None) EMAIL_LOGO_IMG = values.Value(None) EMAIL_PORT = values.PositiveIntegerValue(None) + EMAIL_URL_APP = values.Value(None) EMAIL_USE_TLS = values.BooleanValue(False) EMAIL_USE_SSL = values.BooleanValue(False) EMAIL_FROM = values.Value("from@example.com") diff --git a/src/helm/env.d/dev/values.impress.yaml.gotmpl b/src/helm/env.d/dev/values.impress.yaml.gotmpl index 7ecf21a2..e6e3332b 100644 --- a/src/helm/env.d/dev/values.impress.yaml.gotmpl +++ b/src/helm/env.d/dev/values.impress.yaml.gotmpl @@ -27,6 +27,7 @@ backend: DJANGO_EMAIL_HOST: "mailcatcher" DJANGO_EMAIL_LOGO_IMG: https://docs.127.0.0.1.nip.io/assets/logo-suite-numerique.png DJANGO_EMAIL_PORT: 1025 + DJANGO_EMAIL_URL_APP: https://docs.127.0.0.1.nip.io DJANGO_EMAIL_USE_SSL: False LOGGING_LEVEL_HANDLERS_CONSOLE: ERROR LOGGING_LEVEL_LOGGERS_ROOT: INFO diff --git a/src/helm/env.d/feature/values.impress.yaml.gotmpl b/src/helm/env.d/feature/values.impress.yaml.gotmpl index 6a0fa58a..1489810d 100644 --- a/src/helm/env.d/feature/values.impress.yaml.gotmpl +++ b/src/helm/env.d/feature/values.impress.yaml.gotmpl @@ -28,6 +28,7 @@ backend: DJANGO_EMAIL_HOST: "mailcatcher" DJANGO_EMAIL_LOGO_IMG: https://{{ .Values.feature }}-docs.{{ .Values.domain }}/assets/logo-suite-numerique.png DJANGO_EMAIL_PORT: 1025 + DJANGO_EMAIL_URL_APP: https://{{ .Values.feature }}-docs.{{ .Values.domain }} DJANGO_EMAIL_USE_SSL: False LOGGING_LEVEL_HANDLERS_CONSOLE: ERROR LOGGING_LEVEL_LOGGERS_ROOT: INFO