Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Add username in email #314

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/backend/core/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,15 @@ def perform_create(self, serializer):
"""Add a new access to the document and send an email to the new added user."""
access = serializer.save()
language = self.request.headers.get("Content-Language", "en-us")

username_sender = self.request.user.full_name
email_sender = self.request.user.email
Comment on lines +580 to +581
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
username_sender = self.request.user.full_name
email_sender = self.request.user.email
sender_name = self.request.user.full_name
sender_email = self.request.user.email

if(not username_sender):
username_sender = self.request.user.email
email_sender = ""

access.document.email_invitation(
language, access.user.email, access.role, self.request.user.email
language, access.user.email, access.role, username_sender, email_sender
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantically, shouldn't it be:

Suggested change
language, access.user.email, access.role, username_sender, email_sender
language, access.user.email, access.role, sender_name, sender_email

The term "username" is usually used with another meaning.

)


Expand Down Expand Up @@ -778,6 +785,13 @@ def perform_create(self, serializer):
invitation = serializer.save()

language = self.request.headers.get("Content-Language", "en-us")

username_sender = self.request.user.full_name
email_sender = self.request.user.email
Comment on lines +789 to +790
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
username_sender = self.request.user.full_name
email_sender = self.request.user.email
sender_name = self.request.user.full_name
sender_email = self.request.user.email

if(not username_sender):
username_sender = self.request.user.email
email_sender = ""

invitation.document.email_invitation(
language, invitation.email, invitation.role, self.request.user.email
language, invitation.email, invitation.role, username_sender, email_sender
)
3 changes: 2 additions & 1 deletion src/backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def get_abilities(self, user):
"versions_retrieve": can_get_versions,
}

def email_invitation(self, language, email, role, username_sender):
def email_invitation(self, language, email, role, username_sender, email_sender):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def email_invitation(self, language, email, role, username_sender, email_sender):
def email_invitation(self, language, email, role, username_sender, sender_email):

"""Send email invitation."""

domain = Site.objects.get_current().domain
Expand All @@ -537,6 +537,7 @@ def email_invitation(self, language, email, role, username_sender):
"document": self,
"link": f"{domain}/docs/{self.id}/",
"username": username_sender,
"email": email_sender,
"role": RoleChoices(role).label.lower(),
}
msg_html = render_to_string("mail/html/invitation.html", template_vars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_api_document_accesses_create_authenticated_administrator(via, mock_user
email = mail.outbox[0]
assert email.to == [other_user["email"]]
email_content = " ".join(email.body.split())
assert f"{user.email} shared a document with you: {document.title}" in email_content
assert f"{user.full_name} shared a document with you: {document.title}" in email_content
assert "docs/" + str(document.id) + "/" in email_content


Expand Down Expand Up @@ -225,5 +225,5 @@ def test_api_document_accesses_create_authenticated_owner(via, mock_user_teams):
email = mail.outbox[0]
assert email.to == [other_user["email"]]
email_content = " ".join(email.body.split())
assert f"{user.email} shared a document with you: {document.title}" in email_content
assert f"{user.full_name} shared a document with you: {document.title}" in email_content
assert "docs/" + str(document.id) + "/" in email_content
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_api_document_invitations__create__privileged_members(
assert email.to == ["[email protected]"]
email_content = " ".join(email.body.split())
assert (
f"{user.email} shared a document with you: {document.title}"
f"{user.full_name} shared a document with you: {document.title}"
in email_content
)
else:
Expand Down Expand Up @@ -201,8 +201,49 @@ def test_api_document_invitations__create__email_from_content_language_not_suppo

assert email.to == ["[email protected]"]

email_content = " ".join(email.body.split())
assert f"{user.full_name} shared a document with you: {document.title}" in email_content


def test_api_document_invitations__create__email__full_name_empty():
"""
If the full name of the user is empty, it will display the email address.
"""
user = factories.UserFactory(full_name="")
document = factories.DocumentFactory()
factories.UserDocumentAccessFactory(document=document, user=user, role="owner")

invitation_values = {
"email": "[email protected]",
"role": "reader",
}

assert len(mail.outbox) == 0

client = APIClient()
client.force_login(user)
response = client.post(
f"/api/v1.0/documents/{document.id}/invitations/",
invitation_values,
format="json",
headers={"Content-Language": "not-supported"},
)

assert response.status_code == status.HTTP_201_CREATED
assert response.json()["email"] == "[email protected]"
assert models.Invitation.objects.count() == 1
assert len(mail.outbox) == 1

email = mail.outbox[0]

assert email.to == ["[email protected]"]

email_content = " ".join(email.body.split())
assert f"{user.email} shared a document with you: {document.title}" in email_content
assert (
f"{user.email} invited you as an owner on the following document : {document.title}"
in email_content
)


def test_api_document_invitations__create__issuer_and_document_override():
Expand Down
10 changes: 5 additions & 5 deletions src/backend/core/tests/test_models_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def test_models_documents__email_invitation__success():
assert len(mail.outbox) == 0

document.email_invitation(
"en", "[email protected]", models.RoleChoices.EDITOR, "[email protected]"
"en", "[email protected]", models.RoleChoices.EDITOR, "Test Sender", "[email protected]"
)

# pylint: disable-next=no-member
Expand All @@ -377,7 +377,7 @@ def test_models_documents__email_invitation__success():
email_content = " ".join(email.body.split())

assert (
f"[email protected] invited you as an editor on the following document : {document.title}"
f"Test Sender ([email protected]) invited you as an editor on the following document : {document.title}"
in email_content
)
assert f"docs/{document.id}/" in email_content
Expand All @@ -393,7 +393,7 @@ def test_models_documents__email_invitation__success_fr():
assert len(mail.outbox) == 0

document.email_invitation(
"fr-fr", "[email protected]", models.RoleChoices.OWNER, "[email protected]"
"fr-fr", "[email protected]", models.RoleChoices.OWNER, "Test Sender2", "[email protected]"
)

# pylint: disable-next=no-member
Expand All @@ -406,7 +406,7 @@ def test_models_documents__email_invitation__success_fr():
email_content = " ".join(email.body.split())

assert (
f"[email protected] vous a invité en tant que propriétaire "
f"Test Sender2 ([email protected]) vous a invité en tant que propriétaire "
f"sur le document suivant : {document.title}" in email_content
)
assert f"docs/{document.id}/" in email_content
Expand All @@ -425,7 +425,7 @@ def test_models_documents__email_invitation__failed(mock_logger, _mock_send_mail
assert len(mail.outbox) == 0

document.email_invitation(
"en", "[email protected]", models.RoleChoices.ADMIN, "[email protected]"
"en", "[email protected]", models.RoleChoices.ADMIN, "sender3", "sender3@example.com"
)

# No email has been sent
Expand Down
Binary file modified src/backend/locale/en_US/LC_MESSAGES/django.mo
Binary file not shown.
Loading
Loading