From 3de35c99e02b26cb764952c8d30f6afbd97a3e3a Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Mon, 6 Feb 2023 09:11:25 +0100
Subject: [PATCH] custom_user: provide more generic user-inactivity
 notifications (#74178)

Authentic now exposes a keepalive API endpoint, meaning that the user's
last activity event was not necessarily a user login event.
---
 .../authentic2/unused_account_alert_body.html |  6 +++---
 .../authentic2/unused_account_alert_body.txt  |  6 +++---
 .../unused_account_alert_subject.txt          |  2 +-
 .../unused_account_delete_body.html           |  4 ++++
 .../authentic2/unused_account_delete_body.txt |  6 +++---
 tests/test_commands.py                        | 21 ++++++++++---------
 6 files changed, 25 insertions(+), 20 deletions(-)
 create mode 100644 src/authentic2/templates/authentic2/unused_account_delete_body.html

diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.html b/src/authentic2/templates/authentic2/unused_account_alert_body.html
index 041f17509..74b123caf 100644
--- a/src/authentic2/templates/authentic2/unused_account_alert_body.html
+++ b/src/authentic2/templates/authentic2/unused_account_alert_body.html
@@ -1,6 +1,6 @@
-{% load i18n humanize %}
+{% load i18n %}
 <p>{% blocktrans %}Hi {{ user }},{% endblocktrans %}</p>
 
-<p>{% blocktrans with last_login_date=user.last_login|naturaltime %}Your last logging was {{ last_login_date }}.{% endblocktrans %}</p>
+<p>{% blocktrans %}Your account is inactive and is pending deletion.{% endblocktrans %}</p>
 <p>{% blocktrans %}In order to keep your account, you must <a href="{{ login_url }}">log in</a> within {{ days_to_deletion }} days.{% endblocktrans %}</p>
-<p>{% trans "Otherwise, it will be deleted after this time." %}</p>
+<p>{% trans "Past this delay, it will be deleted." %}</p>
diff --git a/src/authentic2/templates/authentic2/unused_account_alert_body.txt b/src/authentic2/templates/authentic2/unused_account_alert_body.txt
index b509f2e32..dce40946a 100644
--- a/src/authentic2/templates/authentic2/unused_account_alert_body.txt
+++ b/src/authentic2/templates/authentic2/unused_account_alert_body.txt
@@ -1,5 +1,5 @@
-{% load i18n humanize %}{% blocktrans %}Hi {{ user }},{% endblocktrans %}
+{% load i18n %}{% blocktrans %}Hi {{ user }},{% endblocktrans %}
 
-{% blocktrans with last_login_date=user.last_login|naturaltime %}Your last logging was {{ last_login_date }}.{% endblocktrans %}
+{% blocktrans %}Your account is inactive and is pending deletion.{% endblocktrans %}
 {% blocktrans %}In order to keep your account, you must log in within {{ days_to_deletion }} days.{% endblocktrans %}
-{% trans "Otherwise, it will be deleted after this time." %}
+{% trans "Past this delay, it will be deleted." %}
diff --git a/src/authentic2/templates/authentic2/unused_account_alert_subject.txt b/src/authentic2/templates/authentic2/unused_account_alert_subject.txt
index a3c4988e4..da9e4fef3 100644
--- a/src/authentic2/templates/authentic2/unused_account_alert_subject.txt
+++ b/src/authentic2/templates/authentic2/unused_account_alert_subject.txt
@@ -1 +1 @@
-{% load i18n humanize %}{% blocktrans trimmed with last_login_date=user.last_login|naturaltime %}Alert: {{ user.get_full_name }} your last login was {{ last_login_date }}{% endblocktrans %}
+{% load i18n %}{% blocktrans trimmed %}Alert: {{ user }}, your account is inactive and is pending deletion{% endblocktrans %}
diff --git a/src/authentic2/templates/authentic2/unused_account_delete_body.html b/src/authentic2/templates/authentic2/unused_account_delete_body.html
new file mode 100644
index 000000000..13ed55840
--- /dev/null
+++ b/src/authentic2/templates/authentic2/unused_account_delete_body.html
@@ -0,0 +1,4 @@
+{% load i18n %}
+<p>{% blocktrans %}Hi {{ user }},{% endblocktrans %}</p>
+
+<p>{% blocktrans %}Your account was inactive, it has been deleted.{% endblocktrans %}</p>
diff --git a/src/authentic2/templates/authentic2/unused_account_delete_body.txt b/src/authentic2/templates/authentic2/unused_account_delete_body.txt
index cf5fd35cd..83acb966c 100644
--- a/src/authentic2/templates/authentic2/unused_account_delete_body.txt
+++ b/src/authentic2/templates/authentic2/unused_account_delete_body.txt
@@ -1,4 +1,4 @@
-{% load i18n humanize %}{% blocktrans with last_login_date=user.last_login|naturaltime %}
-Hi {{ user }},
+{% load i18n %}
+{% blocktrans %}Hi {{ user }},{% endblocktrans %}
 
-Since your last logging was {{ last_login_date }}, your account has been deleted.{% endblocktrans %}
+{% blocktrans %}Your account was inactive, it has been deleted.{% endblocktrans %}
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 72821af90..ae8a80c0d 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -228,28 +228,29 @@ def test_clean_unused_account_always_alert(db, simple_user, mailoutbox, freezer)
     assert len(mailoutbox) == 1
 
 
-@pytest.mark.parametrize(
-    "deletion_delay,formatted", [(730, '2\xa0years'), (500, '1\xa0year'), (65, '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
+@pytest.mark.parametrize("deletion_delay", [730, 500, 65])
+def test_clean_unused_account_displayed_message(simple_user, mailoutbox, deletion_delay):
+    simple_user.ou.clean_unused_accounts_alert = deletion_delay - 30
     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.last_login = now() - datetime.timedelta(days=deletion_delay + 30)
     simple_user.save()
 
     # alert email
     call_command('clean-unused-accounts')
     mail = mailoutbox[0]
-    assert formatted in mail.body
-    assert formatted in mail.subject and not '\n' in mail.subject
+    assert mail.subject == 'Alert: Jôhn Dôe, your account is inactive and is pending deletion'
+    assert 'Jôhn Dôe' in mail.body
+    assert 'In order to keep your account, you must log in within 30 days.' in mail.body
 
     # deletion email
-    simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=2)
+    simple_user.last_account_deletion_alert = now() - datetime.timedelta(days=31)
     simple_user.save()
     call_command('clean-unused-accounts')
     mail = mailoutbox[1]
-    assert formatted in mail.body
+    assert mail.subject == 'Notification: Jôhn Dôe, your account has been deleted'
+    assert 'Jôhn Dôe' in mail.body
+    assert 'Your account was inactive, it has been deleted.' in mail.body
 
 
 def test_clean_unused_account_login_url(simple_user, mailoutbox):
-- 
2.39.1

