-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new templates and email sending for user actions
- Loading branch information
Showing
4 changed files
with
171 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from unittest import mock | ||
|
||
from django.test.utils import override_settings | ||
|
||
from rest_framework import status | ||
|
||
from data.factories import ( | ||
|
@@ -242,10 +246,17 @@ def setUp(self): | |
self.recipient_email = "[email protected]" | ||
self.payload = dict(recipient_email=self.recipient_email, roles=["DeclarantRole"]) | ||
|
||
def test_add_collaborator_without_account_but_invitation_already_sent_ko(self): | ||
# L'invité n'existe pas en base, mais une invitation non traitée a déjà été envoyée. | ||
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"}) | ||
@override_settings(CONTACT_EMAIL="[email protected]") | ||
@override_settings(SECURE=True) | ||
@override_settings(HOSTNAME="hostname") | ||
@mock.patch("config.email.send_sib_template") | ||
def test_add_collaborator_without_account_but_invitation_already_sent_ko(self, mocked_brevo): | ||
self.login(self.adder) | ||
CollaborationInvitationFactory(recipient_email=self.recipient_email, company=self.company) | ||
mocked_brevo.reset_mock() | ||
|
||
# L'invité n'existe pas en base, mais une invitation non traitée a déjà été envoyée. | ||
response = self.post(self.url(pk=self.company.pk), self.payload) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
|
@@ -255,21 +266,52 @@ def test_add_collaborator_without_account_but_invitation_already_sent_ko(self): | |
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_add_collaborator_without_account_ok(self): | ||
mocked_brevo.assert_not_called() | ||
|
||
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"}) | ||
@override_settings(CONTACT_EMAIL="[email protected]") | ||
@override_settings(SECURE=True) | ||
@override_settings(HOSTNAME="hostname") | ||
@mock.patch("config.email.send_sib_template") | ||
def test_add_collaborator_without_account_ok(self, mocked_brevo): | ||
# L'invité n'existe pas en base, et aucune invitation n'a été envoyée | ||
self.login(self.adder) | ||
response = self.post(self.url(pk=self.company.pk), self.payload) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("message", response.data) | ||
|
||
def test_add_collaborator_already_a_collaborator_ko(self): | ||
template_number = 16 | ||
mocked_brevo.assert_called_once_with( | ||
template_number, | ||
{ | ||
"COMPANY_NAME": self.company.social_name, | ||
"SENDER_NAME": self.adder.get_full_name(), | ||
"SIGNUP_LINK": f"https://hostname/inscription?email={self.recipient_email}", | ||
}, | ||
self.recipient_email, | ||
self.recipient_email, | ||
) | ||
|
||
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"}) | ||
@override_settings(CONTACT_EMAIL="[email protected]") | ||
@override_settings(SECURE=True) | ||
@override_settings(HOSTNAME="hostname") | ||
@mock.patch("config.email.send_sib_template") | ||
def test_add_collaborator_already_a_collaborator_ko(self, mocked_brevo): | ||
# L'invité existe en base et fait déjà partie des collaborateurs de l'entreprise. | ||
self.login(self.adder) | ||
DeclarantRoleFactory(user=UserFactory(email=self.recipient_email), company=self.company) | ||
response = self.post(self.url(pk=self.company.pk), self.payload) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_add_collaborator_with_account_ok(self): | ||
mocked_brevo.assert_not_called() | ||
|
||
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"}) | ||
@override_settings(CONTACT_EMAIL="[email protected]") | ||
@override_settings(SECURE=True) | ||
@override_settings(HOSTNAME="hostname") | ||
@mock.patch("config.email.send_sib_template") | ||
def test_add_collaborator_with_account_ok(self, mocked_brevo): | ||
# L'invité existe en base mais n'est pas encore collaborateur de l'entreprise. | ||
self.login(self.adder) | ||
recipient = UserFactory() | ||
|
@@ -280,6 +322,18 @@ def test_add_collaborator_with_account_ok(self): | |
self.assertIn("message", response.data) | ||
self.assertIn(recipient, self.company.collaborators) | ||
|
||
template_number = 18 | ||
mocked_brevo.assert_called_once_with( | ||
template_number, | ||
{ | ||
"SENDER_NAME": self.adder.get_full_name(), | ||
"COMPANY_NAME": self.company.social_name, | ||
"DASHBOARD_LINK": f"https://hostname/tableau-de-bord?company={self.company.id}", | ||
}, | ||
recipient.email, | ||
recipient.get_full_name(), | ||
) | ||
|
||
def test_add_collaborator_not_logged_ko(self): | ||
response = self.post(self.url(pk=self.company.pk), self.payload) | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from django.test.utils import override_settings | ||
|
||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from data.factories import ( | ||
CollaborationInvitationFactory, | ||
|
@@ -18,7 +19,8 @@ | |
class TestListCollaborationInvitations(ProjectAPITestCase): | ||
viewname = "list_collaboration_invitation" | ||
|
||
def setUp(self): | ||
@mock.patch("config.email.send_sib_template") | ||
def setUp(self, _): | ||
self.user = UserFactory() | ||
self.company_1 = CompanyFactory() | ||
self.company_2 = CompanyFactory() | ||
|
@@ -55,6 +57,35 @@ def test_get_collaboration_invitations_unauthenticated(self): | |
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
|
||
class TestCollaborationInvitationEmail(APITestCase): | ||
@override_settings(ANYMAIL={"SENDINBLUE_API_KEY": "fake-api-key"}) | ||
@override_settings(CONTACT_EMAIL="[email protected]") | ||
@override_settings(SECURE=True) | ||
@override_settings(HOSTNAME="hostname") | ||
@mock.patch("config.email.send_sib_template") | ||
def test_account_created_sends_email(self, mocked_brevo): | ||
email = "[email protected]" | ||
company = CompanyFactory() | ||
sender = SupervisorRoleFactory(user=UserFactory(), company=company) | ||
|
||
invitation = CollaborationInvitationFactory(recipient_email=email, company=company, sender=sender.user) | ||
mocked_brevo.reset_mock() | ||
recipient = UserFactory(email=email) | ||
invitation.account_created(processor=recipient) | ||
|
||
template_number = 17 | ||
mocked_brevo.assert_called_once_with( | ||
template_number, | ||
{ | ||
"COMPANY_NAME": company.social_name, | ||
"NEW_COLLABORATOR": recipient.get_full_name(), | ||
"MEMBERS_LINK": f"https://hostname/gestion-des-collaborateurs/{company.id}", | ||
}, | ||
sender.user.email, | ||
sender.user.get_full_name(), | ||
) | ||
|
||
|
||
class TestListCompanyAccessClaims(ProjectAPITestCase): | ||
viewname = "list_company_access_claim" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters