From cb4e148afc4ab0915c8938e6ea63b21d2ad4dcdb Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Tue, 4 Mar 2025 09:09:02 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(email)=20adapt=20email=20whe?= =?UTF-8?q?n=20no=20title?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Default title is not set when we create a document anymore. We need to adapt the email to handle this case. --- src/backend/core/models.py | 9 ++++-- .../core/tests/test_models_documents.py | 31 +++++++++++++++++++ src/mail/mjml/invitation.mjml | 2 +- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 17b6e2b0..8d21b85b 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -697,6 +697,7 @@ class Document(MP_Node, BaseModel): "document": self, "domain": domain, "link": f"{domain}/docs/{self.id}/", + "document_title": self.title or str(_("Untitled Document")), "logo_img": settings.EMAIL_LOGO_IMG, } ) @@ -738,8 +739,12 @@ class Document(MP_Node, BaseModel): '{name} invited you with the role "{role}" on the following document:' ).format(name=sender_name_email, role=role.lower()), } - subject = _("{name} shared a document with you: {title}").format( - name=sender_name, title=self.title + subject = ( + context["title"] + if not self.title + else _("{name} shared a document with you: {title}").format( + name=sender_name, title=self.title + ) ) self.send_email(subject, [email], context, language) diff --git a/src/backend/core/tests/test_models_documents.py b/src/backend/core/tests/test_models_documents.py index ca885256..d0ea0f68 100644 --- a/src/backend/core/tests/test_models_documents.py +++ b/src/backend/core/tests/test_models_documents.py @@ -636,6 +636,37 @@ def test_models_documents__email_invitation__success(): assert f"docs/{document.id}/" in email_content +def test_models_documents__email_invitation__success_empty_title(): + """ + The email invitation is sent successfully. + """ + document = factories.DocumentFactory(title=None) + + # pylint: disable-next=no-member + assert len(mail.outbox) == 0 + + 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 + assert len(mail.outbox) == 1 + + # pylint: disable-next=no-member + email = mail.outbox[0] + + assert email.to == ["guest@example.com"] + email_content = " ".join(email.body.split()) + + assert "Test sender shared a document with you!" in email.subject + assert ( + "Test Sender (sender@example.com) invited you with the role "editor" " + "on the following document: Untitled Document" in email_content + ) + assert f"docs/{document.id}/" in email_content + + def test_models_documents__email_invitation__success_fr(): """ The email invitation is sent successfully in french. diff --git a/src/mail/mjml/invitation.mjml b/src/mail/mjml/invitation.mjml index 2512274a..93fa89c6 100644 --- a/src/mail/mjml/invitation.mjml +++ b/src/mail/mjml/invitation.mjml @@ -22,7 +22,7 @@ {{message|capfirst}} - {{document.title}} + {{document_title}}