import pytest from app.services.notification_preferences_service import NotificationPreferencesService class FakeCrudClient: async def get_notification_subscriptions(self, customer_profile_id: int): return [ { "notificationSubscriptionId": "sub-1", "notificationCategory": {"notificationCategoryId": 1, "name": "Reizen", "groupName": "Mijn passen"}, "customerProfileId": customer_profile_id, "ovPayTokenId": 112, "subscriptionActivities": [{"timestamp": "2026-01-01T00:00:00Z", "isActive": True}], "notificationPreferences": [ # Default push=true, this row switches device-2 off only. {"notificationPreferenceId": "pref-1", "eventTypeChannelId": "etc-push", "resourceIdentifier": "device-2"}, # Default email=false, this row switches customer email on. {"notificationPreferenceId": "pref-2", "eventTypeChannelId": "etc-email", "resourceIdentifier": "42"}, ], } ] async def get_notification_category_defaults(self, notification_category_id: int): return { "notificationCategoryId": 1, "name": "Reizen", "groupName": "Mijn passen", "eventTypes": [ { "eventTypeId": 2, "name": "GBO", "subName": "CI", "prettyName": "Normal Check-in", "eventTypeChannels": [ { "eventTypeChannelId": "etc-push", "channel": {"channelId": 1, "name": "push", "resourceName": {"resourceNameId": 8, "name": "devices"}}, "isDefault": True, "isMandatory": False, }, { "eventTypeChannelId": "etc-email", "channel": {"channelId": 2, "name": "email", "resourceName": {"resourceNameId": 4, "name": "customers"}}, "isDefault": False, "isMandatory": False, }, ], } ], } async def get_ovpay_token(self, ovpay_token_id): return {"ovPayToken": {"ovPayTokenId": ovpay_token_id, "alias": "Mijn ING bankpas"}} async def get_devices(self, customer_profile_id: int): return [ {"deviceId": "device-1", "alias": "Mijn iPhone"}, {"deviceId": "device-2", "alias": "Mijn Pixel"}, ] async def get_persons(self, customer_profile_id: int): return [{"customerProfileId": 42, "emailAddress": "customer@example.nl"}] @pytest.mark.asyncio async def test_normalizes_defaults_and_customer_exceptions(): result = await NotificationPreferencesService(FakeCrudClient()).get_customer_notification_preferences(42) event_type = result.notificationCategories[0].notificationSubscriptions[0].eventTypes[0] push_resources = event_type.eventTypeChannels[0].resources assert [r.isActive for r in push_resources] == [True, False] email_resources = event_type.eventTypeChannels[1].resources assert email_resources[0].isActive is True @pytest.mark.asyncio async def test_device_filter_only_returns_that_device_resource(): result = await NotificationPreferencesService(FakeCrudClient()).get_customer_notification_preferences(42, device_id="device-1") event_type = result.notificationCategories[0].notificationSubscriptions[0].eventTypes[0] assert len(event_type.eventTypeChannels[0].resources) == 1 assert event_type.eventTypeChannels[0].resources[0].resourceIdentifier == "device-1"