-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️(backend) change email invitation content
Change the email invitation content. More document related variables are added. To benefit of the document inheritance, we moved the function email_invitation to the document model.
- Loading branch information
Showing
7 changed files
with
148 additions
and
154 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
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
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 |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
Unit tests for the Document model | ||
""" | ||
|
||
import smtplib | ||
from logging import Logger | ||
from unittest import mock | ||
|
||
from django.contrib.auth.models import AnonymousUser | ||
from django.core import mail | ||
from django.core.exceptions import ValidationError | ||
from django.core.files.storage import default_storage | ||
|
||
|
@@ -322,3 +327,90 @@ def test_models_documents_version_duplicate(): | |
Bucket=default_storage.bucket_name, Prefix=file_key | ||
) | ||
assert len(response["Versions"]) == 2 | ||
|
||
|
||
def test_models_documents__email_invitation__success(): | ||
""" | ||
The email invitation is sent successfully. | ||
""" | ||
document = factories.DocumentFactory() | ||
|
||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 0 | ||
|
||
document.email_invitation( | ||
"en", "[email protected]", models.RoleChoices.EDITOR, "[email protected]" | ||
) | ||
|
||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 1 | ||
|
||
# pylint: disable-next=no-member | ||
email = mail.outbox[0] | ||
|
||
assert email.to == ["[email protected]"] | ||
email_content = " ".join(email.body.split()) | ||
|
||
assert ( | ||
"[email protected] has granted you access to the document as: Editor" | ||
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. | ||
""" | ||
document = factories.DocumentFactory() | ||
|
||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 0 | ||
|
||
document.email_invitation( | ||
"fr-fr", "[email protected]", models.RoleChoices.OWNER, "[email protected]" | ||
) | ||
|
||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 1 | ||
|
||
# pylint: disable-next=no-member | ||
email = mail.outbox[0] | ||
|
||
assert email.to == ["[email protected]"] | ||
email_content = " ".join(email.body.split()) | ||
assert "Invitation à rejoindre Docs !" in email_content | ||
assert f"docs/{document.id}/" in email_content | ||
|
||
|
||
@mock.patch( | ||
"core.models.send_mail", | ||
side_effect=smtplib.SMTPException("Error SMTPException"), | ||
) | ||
@mock.patch.object(Logger, "error") | ||
def test_models_documents__email_invitation__failed(mock_logger, _mock_send_mail): | ||
"""Check mail behavior when an SMTP error occurs when sent an email invitation.""" | ||
document = factories.DocumentFactory() | ||
|
||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 0 | ||
|
||
document.email_invitation( | ||
"en", "[email protected]", models.RoleChoices.ADMIN, "[email protected]" | ||
) | ||
|
||
# No email has been sent | ||
# pylint: disable-next=no-member | ||
assert len(mail.outbox) == 0 | ||
|
||
# Logger should be called | ||
mock_logger.assert_called_once() | ||
|
||
( | ||
_, | ||
email, | ||
exception, | ||
) = mock_logger.call_args.args | ||
|
||
assert email == "[email protected]" | ||
assert isinstance(exception, smtplib.SMTPException) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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