diff --git a/notifications_routing/auditing.py b/notifications_routing/auditing.py index ecfe8b927e8c054570dc56cbf59009ef232a91e9..55621bf0b6328bad24b6f27884db427cce298ed4 100644 --- a/notifications_routing/auditing.py +++ b/notifications_routing/auditing.py @@ -45,11 +45,13 @@ def _external_values(value): filtered_value.update({"total": len(value["targets"])}) if value["event"] == AuditingEvents.EXPANDED_RECIPIENTS: del filtered_value["targets"] - if value["event"] in [AuditingEvents.NOTIFICATION_TARGETS]: + if value["event"] in [AuditingEvents.NOTIFICATION_TARGETS, AuditingEvents.CHANNEL_SNAPSHOT]: if AuditingEvents.TARGET_USERS in filtered_value: for user in filtered_value[AuditingEvents.TARGET_USERS]: del user["user_id"] del user["last_login"] + if "group users" in filtered_value: + del filtered_value["group users"] if "users" in filtered_value: for user in filtered_value["users"]: if "user_id" in user or "last_login" in user: diff --git a/notifications_routing/router.py b/notifications_routing/router.py index ca4156bdd420feecec53e97e1b9e5cca16fc7296..cb91758ebc628094feb188a861219ff60915cea8 100644 --- a/notifications_routing/router.py +++ b/notifications_routing/router.py @@ -87,6 +87,8 @@ class Router(megabus.Listener): users: List[Dict[str, str]], groups: List[str], unsubscribed_users: List[str], + users_from_groups: List[str], + audit: bool, ): """Add users from groups to users list.""" unique_usernames = [channel_user[self.data_source.USERNAME] for channel_user in users] @@ -96,14 +98,16 @@ class Router(megabus.Listener): logging.debug("checking users for group: %s", group) group_users = self.data_source.get_group_users(group[DataSource.GROUP_ID]) - audit_notification( - notification_id, - { - "event": AuditingEvents.GET_GROUP_USERS, - "group": group[DataSource.GROUP_IDENTIFIER], - "users": group_users, - }, - ) + if audit: + audit_notification( + notification_id, + { + "event": AuditingEvents.GET_GROUP_USERS, + "group": group[DataSource.GROUP_IDENTIFIER], + "users": group_users, + }, + ) + logging.debug("channel %s groups users %s", channel_id, group_users) for user in group_users: @@ -115,10 +119,10 @@ class Router(megabus.Listener): try: system_user = self.data_source.get_system_user(user[self.data_source.USERNAME]) - users.append(system_user) + users_from_groups.append(system_user) unique_usernames.append(user[self.data_source.USERNAME]) except NotFoundDataSourceError: - users.append(user) + users_from_groups.append(user) unique_usernames.append(user[self.data_source.USERNAME]) logging.debug("notification %s final users %s", notification_id, users) @@ -143,6 +147,7 @@ class Router(megabus.Listener): users = self.data_source.get_channel_subscribed_users(channel_id) groups = self.data_source.get_channel_groups(channel_id) logging.debug("channel %s groups %s", users, groups) + group_users = [] if groups: self.add_users_from_groups( @@ -151,6 +156,8 @@ class Router(megabus.Listener): users, groups, [unsubscribed[DataSource.USERNAME] for unsubscribed in unsubscribed_users], + group_users, + True, ) audit_notification( @@ -159,6 +166,7 @@ class Router(megabus.Listener): "event": AuditingEvents.CHANNEL_SNAPSHOT, AuditingEvents.TOTAL_COMPUTED_USERS: len(users + unsubscribed_users), "users": users + unsubscribed_users, + "group users": group_users, "groups": [group[DataSource.GROUP_IDENTIFIER] for group in groups], }, external=True, @@ -193,7 +201,13 @@ class Router(megabus.Listener): if target_groups: self.add_users_from_groups( - notification_id, channel_id, subscribed_target_users, target_groups, unsubscribed_users + notification_id, + channel_id, + subscribed_target_users, + target_groups, + unsubscribed_users, + subscribed_target_users, + False, ) return subscribed_target_users diff --git a/notifications_routing/utils.py b/notifications_routing/utils.py index 7bcd11a0475390668b991a41e38c2e0d45ea4854..31bffeec1621dd0ec52b1c6c48de51a82db0f7b4 100644 --- a/notifications_routing/utils.py +++ b/notifications_routing/utils.py @@ -75,14 +75,14 @@ class AuditingEvents: # Router other field constants TARGET_GROUPS = "Target groups" - TOTAL_COMPUTED_USERS = "Total computer users" + TOTAL_COMPUTED_USERS = "Total computed users" CHANNEL_USERS_FOR_INTERSECTION = "Computed users for intersection" # Preferences events DEFAULT_CONSUMER = "Default sent to consumer" SENT_CONSUMER = "Sent to consumer" CRITICAL_CONSUMER = "Critical sent to consumer" - PREFENCE_ADDED = "Preference added to feed" + PREFERENCE_ADDED = "Preference added to feed" def is_time_between(time, start_range, end_range): diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 78f861f93055fd5cbfe67ad65658d576c35024f2..b68c45af99d6d6fc855afbbfda1fa71bd0f07a13 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -143,7 +143,7 @@ def channel(session, group, user, unsubscribed_user): @pytest.fixture(scope="function") def target_notification(session, group, user, unsubscribed_user): - """Insert channel to db.""" + """Insert target notification to db.""" target_notification = Notification() target_notification.id = "c3ccc15b-298f-4dc7-877f-2c8970331caf" target_notification.target_groups = [group] @@ -163,6 +163,38 @@ def target_notification(session, group, user, unsubscribed_user): session.commit() +@pytest.fixture(scope="function") +def normal_notification(): + """Insert normal notification into db.""" + normal_notification = Notification() + normal_notification.id = "9b624b89-b99d-4d1e-9fd8-f713361facf6" + + """ + notification_router_message = { + "id": "36e42a9e-9196-41d1-b6f3-3bad9e0eba42", + "priority": "NORMAL", + "sentAt": "2023-01-17T09:37:32.988Z", + "body": "<p>WOOPDESC</p>\n", + "summary": "woop", + "link": "https://test-notifications-service.web.cern.ch/channels/bfef5252-d83f-4b4f-a294-bd346bed68f5/notifications/36e42a9e-9196-41d1-b6f3-3bad9e0eba42", + "imgUrl": "", + "private": false, + "intersection": false, + "target": { + "id": "bfef5252-d83f-4b4f-a294-bd346bed68f5", + "name": "normal chan", + "slug": "normal-chan", + "category": {"id": "5e75dbed-7321-41c5-a0a7-c4e1760f78d0", "name": "testcat", "info": ""}, + }, + } + """ + + session.add(normal_notification) + session.commit() + + yield normal_notification + + @pytest.fixture(scope="function") def device(session): """Insert device to db.""" diff --git a/tests/integration/test_etcd.py b/tests/integration/test_etcd.py index 0f8e5179217f22009942cf9603e711cfecdaaff6..d58a596f11425855af574feff533dbbf89171bd9 100644 --- a/tests/integration/test_etcd.py +++ b/tests/integration/test_etcd.py @@ -1,6 +1,7 @@ """Integration Tests for ETCD.""" from notifications_routing.auditing import audit_notification, client, get_audit_notification +from notifications_routing.router import Router def test_etcd(appctx): @@ -15,3 +16,70 @@ def test_etcd(appctx): value = get_audit_notification(id, key) assert value["event"] == expected["event"] assert value["date"] + + +def test_normal_notification_audit_internal(appctx, normal_notification, notification_): + """Test internal audit result on normal notification.""" + assert client + + Router.process_message() + + """ + expected_auditing = { + "9b624b89-b99d-4d1e-9fd8-f713361facf6": { + "router": { + "8d7e8567-8602-41f4-8999-a398e3cbc5be": { + "date": "17/01/2023 10:37:33", + "event": "Unsubscribed users", + "targets": [], + "total": 0, + }, + "f29c08d9-ebb5-4afc-bead-60476c53228d": {"date": "17/01/2023 10:37:33", "event": "Start processing"}, + "594662d1-dc3b-45aa-b1db-b0ea2b7e9b7b": { + "date": "17/01/2023 10:37:33", + "event": "Channel snapshot", + "Total computed users": 1, + "users": [{"username": "jlopesda", "email": "jose.semedo@cern.ch"}], + "groups": [], + }, + "e46dc219-3919-4535-b7b9-719a3b1074f8": { + "date": "17/01/2023 10:37:33", + "event": "Expanded recipients", + "total": 1, + }, + } + } + } + """ + + result = get_audit_notification(normal_notification.id) + + # general compare? how to handle date timestamps + assert expected == result + + # test each entry like so: + assert result[""] + + +def test_normal_notification_audit_external(appctx, normal_notification): + """Test external audit result for normal notification.""" + + +def test_intersection_notification_single_user_audit_internal(appctx, single_user_intersection_notification): + """Test internal audit result for single target user intersection notification.""" + + +def test_intersection_notification_single_user_audit_external(appctx, single_user_intersection_notification): + """Test external audit result for single target user intersection notification.""" + + +def test_intersection_notification_group_and_user_audit_internal(): + """Test internal audit result for target user and target group intersection notification.""" + + +def test_unsubscribed_user_notification_audit_internal(): + """Test internal audit result for notification on channel with unsubscribed user.""" + + +def test_unsubscribed_user_notification_audit_external(): + """Test external audit result for notification on channel with unsubscribed user."""