API - send an email when data export is successful

This commit is contained in:
Sam
2023-03-04 10:49:25 +01:00
parent 32f358b8cb
commit 06d4a53fbb
26 changed files with 627 additions and 25 deletions

View File

@ -0,0 +1,167 @@
# flake8: noqa
expected_en_text_body = """Hi test,
You have requested an export of your account on FitTrackee.
The archive is now ready to be downloaded from your account.
Download your archive: http://localhost/profile/edit/account
If you did not request the export, please change your password immediately or contact your administrator if your account is locked.
Thanks,
The FitTrackee Team
http://localhost"""
expected_fr_text_body = """Bonjour test,
Vous avez demandé un export des données de votre compte sur FitTrackee.
L'archive est maintenant prête à être téléchargée depuis votre compte.
Télécharger votre archive: http://localhost/profile/edit/account
Si vous n'êtes pas à l'origine de cette demande, veuillez changer votre mot de passe immédiatement ou contacter l'administrateur si votre compte est bloqué.
Merci,
L'équipe FitTrackee
http://localhost"""
expected_en_html_body = """ <body>
<span class="preheader">A download link is available in your account.</span>
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table class="email-content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="email-masthead">
<a href="http://localhost" class="f-fallback email-masthead-name">
FitTrackee
</a>
</td>
</tr>
<tr>
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
<table class="email-body-inner" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell">
<div class="f-fallback">
<h1>Hi test,</h1>
<p>You have requested an export of your account on FitTrackee. The archive is now ready to be downloaded from your account.</p>
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" role="presentation">
<tr>
<td align="center">
<a href="http://localhost/profile/edit/account" class="f-fallback button button--green" target="_blank">Download your archive</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p>
If you did not request the export, please change your password immediately or contact your administrator if your account is locked.
</p>
<p>Thanks,
<br>The FitTrackee Team</p>
<table class="body-sub" role="presentation">
<tr>
<td>
<p class="f-fallback sub">If you're having trouble with the button above, copy and paste the URL below into your web browser.</p>
<p class="f-fallback sub">http://localhost/profile/edit/account</p>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell" align="center">
<p class="f-fallback sub align-center">&copy; FitTrackee.</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>"""
expected_fr_html_body = """ <body>
<span class="preheader">Un lien de téléchargement est disponible dans votre compte.</span>
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table class="email-content" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="email-masthead">
<a href="http://localhost" class="f-fallback email-masthead-name">
FitTrackee
</a>
</td>
</tr>
<tr>
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
<table class="email-body-inner" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell">
<div class="f-fallback">
<h1>Bonjour test,</h1>
<p>Vous avez demandé un export des données de votre compte sur FitTrackee. L'archive est maintenant prête à être téléchargée depuis votre compte.</p>
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" role="presentation">
<tr>
<td align="center">
<a href="http://localhost/profile/edit/account" class="f-fallback button button--green" target="_blank">Télécharger votre archive</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p>
Si vous n'êtes pas à l'origine de cette demande, veuillez changer votre mot de passe immédiatement ou contacter l'administrateur si votre compte est bloqué.
</p>
<p>Merci,
<br>L'équipe FitTrackee</p>
<table class="body-sub" role="presentation">
<tr>
<td>
<p class="f-fallback sub">Si vous avez des problèmes avec le bouton, vous pouvez copier et coller le lien suivant dans votre navigateur.</p>
<p class="f-fallback sub">http://localhost/profile/edit/account</p>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0" role="presentation">
<tr>
<td class="content-cell" align="center">
<p class="f-fallback sub align-center">&copy; FitTrackee.</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>"""

View File

@ -0,0 +1,89 @@
import pytest
from flask import Flask
from fittrackee.emails.email import EmailTemplate
from .template_results.email_data_export_ready import (
expected_en_html_body,
expected_en_text_body,
expected_fr_html_body,
expected_fr_text_body,
)
class TestEmailTemplateForDataExport:
EMAIL_DATA = {
'username': 'test',
'account_url': 'http://localhost/profile/edit/account',
'fittrackee_url': 'http://localhost',
}
@pytest.mark.parametrize(
'lang, expected_subject',
[
('en', 'FitTrackee - Your archive is ready to be downloaded'),
('fr', 'FitTrackee - Votre archive est prête à être téléchargée'),
],
)
def test_it_gets_subject(
self, app: Flask, lang: str, expected_subject: str
) -> None:
email_template = EmailTemplate(
app.config['TEMPLATES_FOLDER'],
app.config['TRANSLATIONS_FOLDER'],
app.config['LANGUAGES'],
)
subject = email_template.get_content(
'data_export_ready', lang, 'subject.txt', {}
)
assert subject == expected_subject
@pytest.mark.parametrize(
'lang, expected_text_body',
[
('en', expected_en_text_body),
('fr', expected_fr_text_body),
],
)
def test_it_gets_text_body(
self, app: Flask, lang: str, expected_text_body: str
) -> None:
email_template = EmailTemplate(
app.config['TEMPLATES_FOLDER'],
app.config['TRANSLATIONS_FOLDER'],
app.config['LANGUAGES'],
)
text_body = email_template.get_content(
'data_export_ready', lang, 'body.txt', self.EMAIL_DATA
)
assert text_body == expected_text_body
def test_it_gets_en_html_body(self, app: Flask) -> None:
email_template = EmailTemplate(
app.config['TEMPLATES_FOLDER'],
app.config['TRANSLATIONS_FOLDER'],
app.config['LANGUAGES'],
)
text_body = email_template.get_content(
'data_export_ready', 'en', 'body.html', self.EMAIL_DATA
)
assert expected_en_html_body in text_body
def test_it_gets_fr_html_body(self, app: Flask) -> None:
email_template = EmailTemplate(
app.config['TEMPLATES_FOLDER'],
app.config['TRANSLATIONS_FOLDER'],
app.config['LANGUAGES'],
)
text_body = email_template.get_content(
'data_export_ready', 'fr', 'body.html', self.EMAIL_DATA
)
assert expected_fr_html_body in text_body

View File

@ -105,11 +105,6 @@ class TestEmailTemplateForEmailUpdateToCurrentEmail:
'email_update_to_current_email', 'fr', 'body.html', self.EMAIL_DATA
)
print('')
print(expected_fr_current_email_html_body)
print('')
print(text_body)
assert expected_fr_current_email_html_body in text_body

View File

@ -52,3 +52,9 @@ def user_email_updated_to_new_address_mock() -> Iterator[MagicMock]:
def account_confirmation_email_mock() -> Iterator[MagicMock]:
with patch('fittrackee.users.auth.account_confirmation_email') as mock:
yield mock
@pytest.fixture()
def data_export_email_mock() -> Iterator[MagicMock]:
with patch('fittrackee.users.export_data.data_export_email') as mock:
yield mock

View File

@ -375,6 +375,7 @@ class TestExportUserData:
self,
generate_archive_mock: Mock,
logger_mock: Mock,
data_export_email_mock: Mock,
app: Flask,
user_1: User,
) -> None:
@ -414,3 +415,53 @@ class TestExportUserData:
assert export_request.updated_at is not None
assert export_request.file_name is None
assert export_request.file_size is None
def test_it_does_not_call_data_export_email_when_export_failed(
self,
generate_archive_mock: Mock,
logger_mock: Mock,
data_export_email_mock: Mock,
app: Flask,
user_1: User,
) -> None:
export_request = UserDataExport(user_id=user_1.id)
db.session.add(export_request)
db.session.commit()
generate_archive_mock.return_value = (None, None)
export_user_data(export_request_id=export_request.id)
data_export_email_mock.send.assert_not_called()
def test_it_calls_data_export_email_when_export_is_successful(
self,
generate_archive_mock: Mock,
logger_mock: Mock,
data_export_email_mock: Mock,
app: Flask,
user_1: User,
) -> None:
export_request = UserDataExport(user_id=user_1.id)
db.session.add(export_request)
db.session.commit()
archive_name = random_string()
generate_archive_mock.return_value = (random_string(), archive_name)
archive_size = random_int()
with patch(
'fittrackee.users.export_data.os.path.getsize',
return_value=archive_size,
):
export_user_data(export_request_id=export_request.id)
data_export_email_mock.send.assert_called_once_with(
{
'language': 'en',
'email': user_1.email,
},
{
'username': user_1.username,
'account_url': 'http://0.0.0.0:5000/profile/edit/account',
'fittrackee_url': 'http://0.0.0.0:5000',
},
)