From 1abbf0539f585f7ae4f6dfc18c1364091b8a30bc Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Thu, 15 Aug 2024 15:40:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20send=20email=20invitation?= =?UTF-8?q?=20when=20add=20user=20to=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We send as well an email invitation to the user when we add him to a document. --- CHANGELOG.md | 3 ++- src/backend/core/api/viewsets.py | 11 ++++++++-- .../core/tests/test_api_document_accesses.py | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba72fa1a..134c0075 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,9 @@ and this project adheres to - 🎨(frontend) better conversion editor to pdf #151 - ✨(frontend) Versioning #147 - ✨Export docx (word) #161 -- 🌐 Internationalize invitation email #167 +- 🌐Internationalize invitation email #167 - ✨(frontend) White branding #164 +- ✨Email invitation when add user to doc #171 ## Fixed diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 13af6198..3ded8e54 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -1,5 +1,4 @@ """API endpoints""" -from core.utils import email_invitation from django.contrib.postgres.aggregates import ArrayAgg from django.db.models import ( OuterRef, @@ -23,6 +22,7 @@ from rest_framework import ( ) from core import models +from core.utils import email_invitation from . import permissions, serializers @@ -429,6 +429,13 @@ class DocumentAccessViewSet( resource_field_name = "document" serializer_class = serializers.DocumentAccessSerializer + 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") + email_invitation(language, access.user.email, access.document.id) + class TemplateViewSet( ResourceViewsetMixin, @@ -599,7 +606,7 @@ class InvitationViewset( .distinct() ) return queryset - + def perform_create(self, serializer): """Save invitation to a document then send an email to the invited user.""" invitation = serializer.save() diff --git a/src/backend/core/tests/test_api_document_accesses.py b/src/backend/core/tests/test_api_document_accesses.py index e632ec26..6280842b 100644 --- a/src/backend/core/tests/test_api_document_accesses.py +++ b/src/backend/core/tests/test_api_document_accesses.py @@ -4,6 +4,8 @@ Test document accesses API endpoints for users in impress's core app. import random from uuid import uuid4 +from django.core import mail + import pytest from rest_framework.test import APIClient @@ -303,6 +305,7 @@ def test_api_document_accesses_create_authenticated_administrator( """ Administrators of a document should be able to create document accesses except for the "owner" role. + An email should be sent to the accesses to notify them of the adding. """ user = factories.UserFactory() @@ -342,6 +345,8 @@ def test_api_document_accesses_create_authenticated_administrator( [role[0] for role in models.RoleChoices.choices if role[0] != "owner"] ) + assert len(mail.outbox) == 0 + response = client.post( f"/api/v1.0/documents/{document.id!s}/accesses/", { @@ -362,12 +367,19 @@ def test_api_document_accesses_create_authenticated_administrator( "role": role, "user": other_user, } + assert len(mail.outbox) == 1 + email = mail.outbox[0] + assert email.to == [other_user["email"]] + email_content = " ".join(email.body.split()) + assert "Invitation to join Docs!" in email_content + assert "docs/" + str(document.id) + "/" in email_content @pytest.mark.parametrize("via", VIA) def test_api_document_accesses_create_authenticated_owner(via, mock_user_get_teams): """ Owners of a document should be able to create document accesses whatever the role. + An email should be sent to the accesses to notify them of the adding. """ user = factories.UserFactory() @@ -387,6 +399,8 @@ def test_api_document_accesses_create_authenticated_owner(via, mock_user_get_tea role = random.choice([role[0] for role in models.RoleChoices.choices]) + assert len(mail.outbox) == 0 + response = client.post( f"/api/v1.0/documents/{document.id!s}/accesses/", { @@ -407,6 +421,12 @@ def test_api_document_accesses_create_authenticated_owner(via, mock_user_get_tea "role": role, "abilities": new_document_access.get_abilities(user), } + assert len(mail.outbox) == 1 + email = mail.outbox[0] + assert email.to == [other_user["email"]] + email_content = " ".join(email.body.split()) + assert "Invitation to join Docs!" in email_content + assert "docs/" + str(document.id) + "/" in email_content def test_api_document_accesses_update_anonymous():