From f5e3f2d75f3078eba3d8e0121b8128f8cdbb23dc Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 12 Mar 2020 12:22:57 +0100 Subject: [PATCH] commands: human duration in unused account email (#40521) --- .../commands/clean-unused-accounts.py | 10 ++++---- .../authentic2/unused_account_alert_body.txt | 2 +- .../authentic2/unused_account_delete_body.txt | 2 +- tests/test_commands.py | 24 +++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/authentic2/management/commands/clean-unused-accounts.py b/src/authentic2/management/commands/clean-unused-accounts.py index 4868c333..ffe07f87 100644 --- a/src/authentic2/management/commands/clean-unused-accounts.py +++ b/src/authentic2/management/commands/clean-unused-accounts.py @@ -22,6 +22,7 @@ import smtplib from datetime import timedelta from django.contrib.auth import get_user_model +from django.contrib.humanize.templatetags.humanize import naturaltime from django.core.management.base import BaseCommand, CommandError from django.utils import timezone from django.template.loader import render_to_string @@ -89,12 +90,11 @@ class Command(BaseCommand): self.delete_user(user) def send_alert(self, user): - alert_delay = user.ou.clean_unused_accounts_alert - days_to_deletion = user.ou.clean_unused_accounts_deletion - alert_delay + days_to_deletion = user.ou.clean_unused_accounts_deletion - user.ou.clean_unused_accounts_alert ctx = { 'user': user, - 'threshold': alert_delay, - 'clean_threshold': days_to_deletion, + 'days_to_deletion': days_to_deletion, + 'last_login_date': naturaltime(user.last_login) } try: self.send_mail('authentic2/unused_account_alert', user, ctx) @@ -115,7 +115,7 @@ class Command(BaseCommand): def delete_user(self, user): ctx = { 'user': user, - 'threshold': user.ou.clean_unused_accounts_deletion + 'last_login_date': naturaltime(user.last_login) } try: self.send_mail('authentic2/unused_account_delete', user, ctx) diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt index b65fb6a4..3488c45c 100644 --- a/src/authentic2/templates/authentic2/unused_account_alert_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_alert_body.txt @@ -1,4 +1,4 @@ {% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, -You have not logged since {{ threshold }} days. In {{ clean_threshold }} days your account +Your last logging was {{ last_login_date }}. In {{ days_to_deletion }} days your account will be deleted.{% endblocktrans %}{% endautoescape %} diff --git a/src/authentic2/templates/authentic2/unused_account_delete_body.txt b/src/authentic2/templates/authentic2/unused_account_delete_body.txt index abb74c84..e327cb5e 100644 --- a/src/authentic2/templates/authentic2/unused_account_delete_body.txt +++ b/src/authentic2/templates/authentic2/unused_account_delete_body.txt @@ -1,3 +1,3 @@ {% load i18n %}{% autoescape off %}{% blocktrans %}Hi {{ user }}, -You have not logged since {{ threshold }} days so your account has been deleted.{% endblocktrans %}{% endautoescape %} +Since your last logging was {{ last_login_date }}, your account has been deleted.{% endblocktrans %}{% endautoescape %} diff --git a/tests/test_commands.py b/tests/test_commands.py index fbcce590..b2806b40 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -18,6 +18,8 @@ import datetime import importlib import json +import pytest + from django.core import management from django.utils import six from django.utils.timezone import now @@ -128,6 +130,28 @@ def test_clean_unused_account_always_alert(db, simple_user, mailoutbox, freezer) assert len(mailoutbox) == 1 +@pytest.mark.parametrize("deletion_delay,formatted", + [(730, u'2\xa0years'), (500, u'1\xa0year'), (65, u'2\xa0months')]) +def test_clean_unused_account_human_duration_format(simple_user, mailoutbox, deletion_delay, formatted): + simple_user.ou.clean_unused_accounts_alert = deletion_delay - 1 + simple_user.ou.clean_unused_accounts_deletion = deletion_delay + simple_user.ou.save() + simple_user.last_login = now() - datetime.timedelta(days=deletion_delay + 1) + simple_user.save() + + # alert email + management.call_command('clean-unused-accounts') + mail = mailoutbox[0] + assert formatted in mail.body + + # deletion email + simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=2) + simple_user.save() + management.call_command('clean-unused-accounts') + mail = mailoutbox[1] + assert formatted in mail.body + + def test_cleanupauthentic(db): management.call_command('cleanupauthentic') -- 2.20.1