From 3a9aa98aed59c1753cc21fe011ac5c709c9aff38 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Thu, 6 Jan 2022 17:40:34 +0100
Subject: [PATCH 2/3] journal: define profile type management events (#60082)

---
 src/authentic2/journal_event_types.py | 51 +++++++++++++++++++++++
 tests/test_manager_journal.py         | 58 +++++++++++++++++++++++++--
 2 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/authentic2/journal_event_types.py b/src/authentic2/journal_event_types.py
index 166ef6cc..c2d46b69 100644
--- a/src/authentic2/journal_event_types.py
+++ b/src/authentic2/journal_event_types.py
@@ -435,3 +435,54 @@ class UserProfileDelete(EventTypeDefinition):
         profile_type = event.get_data('profile_type')
         object_user = event.get_data('object_user')
         return _(f'profile of type "{profile_type}" deleted for user "{object_user}"')
+
+
+class ProfileTypeAdd(EventTypeDefinition):
+    name = 'user.profile_type.add'
+    label = 'user profile type creation'
+
+    @classmethod
+    def record(cls, user, profile_type, api=False):
+        data = {
+            'profile_type': profile_type.name or profile_type.slug,
+        }
+        return super().record(user=user, data=data, api=api)
+
+    @classmethod
+    def get_message(cls, event, context):
+        profile_type = event.get_data('profile_type')
+        return _('creation of profile type {0}').format(profile_type)
+
+
+class ProfileTypeUpdate(EventTypeDefinition):
+    name = 'user.profile_type.update'
+    label = 'user profile type update'
+
+    @classmethod
+    def record(cls, user, profile_type, api=False):
+        data = {
+            'profile_type': profile_type.name or profile_type.slug,
+        }
+        return super().record(user=user, data=data, api=api)
+
+    @classmethod
+    def get_message(cls, event, context):
+        profile_type = event.get_data('profile_type')
+        return _('update of profile type {0}').format(profile_type)
+
+
+class ProfileTypeDeletion(EventTypeDefinition):
+    name = 'user.profile_type.delete'
+    label = 'user profile type deletion'
+
+    @classmethod
+    def record(cls, user, profile_type, api=False):
+        data = {
+            'profile_type': profile_type.name or profile_type.slug,
+        }
+        return super().record(user=user, data=data, api=api)
+
+    @classmethod
+    def get_message(cls, event, context):
+        profile_type = event.get_data('profile_type')
+        return _('deletion of profile type {0}').format(profile_type)
diff --git a/tests/test_manager_journal.py b/tests/test_manager_journal.py
index 8c0bac10..bf83e34e 100644
--- a/tests/test_manager_journal.py
+++ b/tests/test_manager_journal.py
@@ -290,6 +290,22 @@ def events(db, freezer):
         object_user=user,
     )
 
+    make(
+        'user.profile_type.add',
+        user=user,
+        profile_type=profile_type,
+    )
+    make(
+        'user.profile_type.update',
+        user=user,
+        profile_type=profile_type,
+    )
+    make(
+        'user.profile_type.delete',
+        user=user,
+        profile_type=profile_type,
+    )
+
     # verify we created at least one event for each type
     assert set(Event.objects.values_list("type__name", flat=True)) == set(_registry)
 
@@ -636,6 +652,24 @@ def test_global_journal(app, superuser, events):
             'user': 'agent',
             'message': 'profile of type "One Type" deleted for user "Johnny doe"',
         },
+        {
+            'timestamp': 'Jan. 3, 2020, 1 a.m.',
+            'type': 'user.profile_type.add',
+            'user': 'Johnny doe',
+            'message': 'creation of profile type One Type',
+        },
+        {
+            'timestamp': 'Jan. 3, 2020, 2 a.m.',
+            'type': 'user.profile_type.update',
+            'user': 'Johnny doe',
+            'message': 'update of profile type One Type',
+        },
+        {
+            'timestamp': 'Jan. 3, 2020, 3 a.m.',
+            'type': 'user.profile_type.delete',
+            'user': 'Johnny doe',
+            'message': 'deletion of profile type One Type',
+        },
     ]
 
     agent_page = response.click('agent', index=1)
@@ -856,6 +890,24 @@ def test_user_journal(app, superuser, events):
             'type': 'user.service.sso.denial',
             'user': 'Johnny doe',
         },
+        {
+            'timestamp': 'Jan. 3, 2020, 1 a.m.',
+            'type': 'user.profile_type.add',
+            'user': 'Johnny doe',
+            'message': 'creation of profile type One Type',
+        },
+        {
+            'timestamp': 'Jan. 3, 2020, 2 a.m.',
+            'type': 'user.profile_type.update',
+            'user': 'Johnny doe',
+            'message': 'update of profile type One Type',
+        },
+        {
+            'timestamp': 'Jan. 3, 2020, 3 a.m.',
+            'type': 'user.profile_type.delete',
+            'user': 'Johnny doe',
+            'message': 'deletion of profile type One Type',
+        },
     ]
 
 
@@ -1135,6 +1187,9 @@ def test_search(app, superuser, events):
 
     table_content = [text_content(p) for p in response.pyquery('tbody td.journal-list--message-column')]
     assert table_content == [
+        'deletion of profile type One Type',
+        'update of profile type One Type',
+        'creation of profile type One Type',
         'profile of type "One Type" deleted for user "Johnny doe"',
         'profile of type "One Type" updated for user "Johnny doe"',
         'profile of type "One Type" created for user "Johnny doe"',
@@ -1153,9 +1208,6 @@ def test_search(app, superuser, events):
         'edit of user "Johnny doe" (first name)',
         'password reset',
         'password reset failure with email "USER@example.com"',
-        'password reset request with email "user@example.com"',
-        'user deletion',
-        'profile edit (first name)',
     ]
 
     response.form['event_type'].select(text='Role management')
-- 
2.34.1

