(backend) send email invitation when add user to doc

We send as well an email invitation to the user
when we add him to a document.
This commit is contained in:
Anthony LC
2024-08-15 15:40:56 +02:00
committed by Anthony LC
parent 2f8c5637f4
commit 1abbf0539f
3 changed files with 31 additions and 3 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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():