1
1
import pytest
2
+ import json
2
3
from unittest .mock import AsyncMock , MagicMock
4
+ from lcfs .web .api .base import NotificationTypeEnum
3
5
from lcfs .db .models .user .Role import RoleEnum
4
6
from lcfs .db .models .notification import (
5
7
NotificationChannelSubscription ,
8
10
from lcfs .web .api .notification .services import NotificationService
9
11
from lcfs .web .api .notification .repo import NotificationRepository
10
12
from lcfs .web .api .notification .schema import (
13
+ NotificationRequestSchema ,
11
14
SubscriptionSchema ,
12
15
NotificationMessageSchema ,
13
16
)
17
+ from lcfs .web .api .email .services import CHESEmailService
18
+
14
19
15
20
# Mock common data for reuse
16
21
mock_notification_message = NotificationMessage (
34
39
@pytest .fixture
35
40
def notification_service ():
36
41
mock_repo = MagicMock (spec = NotificationRepository )
37
- service = NotificationService (repo = mock_repo )
38
- return service , mock_repo
42
+
43
+ mock_email_service = MagicMock (spec = CHESEmailService )
44
+ mock_email_service .send_notification_email = AsyncMock ()
45
+
46
+ service = NotificationService (repo = mock_repo , email_service = mock_email_service )
47
+ return service , mock_repo , mock_email_service
39
48
40
49
41
50
@pytest .mark .anyio
42
51
async def test_get_notifications_by_user_id (notification_service ):
43
- service , mock_repo = notification_service
52
+ service , mock_repo , mock_email_service = notification_service
44
53
user_id = 1
45
54
46
55
mock_repo .get_notification_messages_by_user = AsyncMock (
@@ -66,7 +75,7 @@ async def test_get_notifications_by_user_id(notification_service):
66
75
67
76
@pytest .mark .anyio
68
77
async def test_get_notification_by_id (notification_service ):
69
- service , mock_repo = notification_service
78
+ service , mock_repo , mock_email_service = notification_service
70
79
notification_id = 1
71
80
72
81
mock_notification = NotificationMessage (
@@ -95,7 +104,7 @@ async def test_get_notification_by_id(notification_service):
95
104
96
105
@pytest .mark .anyio
97
106
async def test_count_unread_notifications_by_user_id (notification_service ):
98
- service , mock_repo = notification_service
107
+ service , mock_repo , mock_email_service = notification_service
99
108
user_id = 1
100
109
expected_unread_count = 5
101
110
@@ -115,7 +124,7 @@ async def test_count_unread_notifications_by_user_id(notification_service):
115
124
116
125
@pytest .mark .anyio
117
126
async def test_mark_notification_as_read (notification_service ):
118
- service , mock_repo = notification_service
127
+ service , mock_repo , mock_email_service = notification_service
119
128
notification_id = 1
120
129
121
130
mock_notification = NotificationMessage (
@@ -143,7 +152,7 @@ async def mock_mark_as_read(notification_id):
143
152
144
153
@pytest .mark .anyio
145
154
async def test_create_notification_message (notification_service ):
146
- service , mock_repo = notification_service
155
+ service , mock_repo , mock_email_service = notification_service
147
156
148
157
notification_data = NotificationMessageSchema (
149
158
message = "Test notification" ,
@@ -190,7 +199,7 @@ async def test_create_notification_message(notification_service):
190
199
191
200
@pytest .mark .anyio
192
201
async def test_update_notification_message (notification_service ):
193
- service , mock_repo = notification_service
202
+ service , mock_repo , mock_email_service = notification_service
194
203
195
204
updated_data = NotificationMessageSchema (
196
205
notification_message_id = 1 ,
@@ -241,7 +250,7 @@ async def test_update_notification_message(notification_service):
241
250
242
251
@pytest .mark .anyio
243
252
async def test_delete_notification_message (notification_service ):
244
- service , mock_repo = notification_service
253
+ service , mock_repo , mock_email_service = notification_service
245
254
246
255
user_id = 1
247
256
notification_id = 123
@@ -269,7 +278,7 @@ async def test_delete_notification_message(notification_service):
269
278
270
279
@pytest .mark .anyio
271
280
async def test_create_notification_channel_subscription (notification_service ):
272
- service , mock_repo = notification_service
281
+ service , mock_repo , mock_email_service = notification_service
273
282
274
283
subscription_data = SubscriptionSchema (
275
284
is_enabled = True ,
@@ -312,7 +321,7 @@ async def test_create_notification_channel_subscription(notification_service):
312
321
313
322
@pytest .mark .anyio
314
323
async def test_get_notification_channel_subscriptions_by_user_id (notification_service ):
315
- service , mock_repo = notification_service
324
+ service , mock_repo , mock_email_service = notification_service
316
325
317
326
user_id = 1
318
327
# Mock subscription data
@@ -349,7 +358,7 @@ async def test_get_notification_channel_subscriptions_by_user_id(notification_se
349
358
350
359
@pytest .mark .anyio
351
360
async def test_get_notification_channel_subscription_by_id (notification_service ):
352
- service , mock_repo = notification_service
361
+ service , mock_repo , mock_email_service = notification_service
353
362
354
363
subscription_id = 123
355
364
expected_subscription = NotificationChannelSubscription (
@@ -374,7 +383,7 @@ async def test_get_notification_channel_subscription_by_id(notification_service)
374
383
375
384
@pytest .mark .anyio
376
385
async def test_delete_notification_channel_subscription (notification_service ):
377
- service , mock_repo = notification_service
386
+ service , mock_repo , mock_email_service = notification_service
378
387
379
388
user_profile_id = 1
380
389
subscription_id = 456
@@ -403,7 +412,7 @@ async def test_delete_notification_channel_subscription(notification_service):
403
412
404
413
@pytest .mark .anyio
405
414
async def test_service_delete_subscriptions_for_user_role (notification_service ):
406
- service , mock_repo = notification_service
415
+ service , mock_repo , mock_email_service = notification_service
407
416
user_profile_id = 1
408
417
role_enum = RoleEnum .ANALYST
409
418
@@ -418,7 +427,7 @@ async def test_service_delete_subscriptions_for_user_role(notification_service):
418
427
419
428
@pytest .mark .anyio
420
429
async def test_service_add_subscriptions_for_user_role (notification_service ):
421
- service , mock_repo = notification_service
430
+ service , mock_repo , mock_email_service = notification_service
422
431
user_profile_id = 1
423
432
role_enum = RoleEnum .DIRECTOR
424
433
@@ -429,3 +438,55 @@ async def test_service_add_subscriptions_for_user_role(notification_service):
429
438
mock_repo .add_subscriptions_for_user_role .assert_awaited_once_with (
430
439
user_profile_id , role_enum
431
440
)
441
+
442
+
443
+ @pytest .mark .anyio
444
+ async def test_send_notification_skip_analyst (notification_service ):
445
+ service , mock_repo , mock_email_service = notification_service
446
+
447
+ # Create the notification request
448
+ message_data = {
449
+ "service" : "Transfer" ,
450
+ "status" : "recorded" ,
451
+ "toOrganizationId" : 2 ,
452
+ }
453
+ notif_msg_schema = NotificationMessageSchema (
454
+ related_organization_id = 1 ,
455
+ message = json .dumps (message_data ),
456
+ )
457
+ notification_req = NotificationRequestSchema (
458
+ notification_types = [
459
+ NotificationTypeEnum .BCEID__TRANSFER__DIRECTOR_DECISION ,
460
+ NotificationTypeEnum .IDIR_ANALYST__TRANSFER__DIRECTOR_RECORDED ,
461
+ ],
462
+ notification_data = notif_msg_schema ,
463
+ )
464
+
465
+ # Analyst
466
+ mock_analyst_sub = MagicMock (spec = NotificationChannelSubscription )
467
+ mock_analyst_sub .user_profile_id = 2
468
+ mock_analyst_sub .user_profile = MagicMock ()
469
+ mock_analyst_sub .user_profile .user_roles = [MagicMock (name = RoleEnum .ANALYST )]
470
+
471
+ # Non-analyst
472
+ mock_non_analyst_sub = MagicMock (spec = NotificationChannelSubscription )
473
+ mock_non_analyst_sub .user_profile_id = 101
474
+ mock_non_analyst_sub .user_profile = MagicMock ()
475
+ mock_non_analyst_sub .user_profile .user_roles = [MagicMock (name = RoleEnum .TRANSFER )]
476
+
477
+ # Configure the repo mock
478
+ mock_repo .get_subscribed_users_by_channel = AsyncMock (
479
+ return_value = [mock_analyst_sub , mock_non_analyst_sub ]
480
+ )
481
+ mock_repo .create_notification_messages = AsyncMock ()
482
+
483
+ # Call the method
484
+ await service .send_notification (notification_req )
485
+
486
+ # Verify that the email service was not called
487
+ called_args , _ = mock_repo .create_notification_messages .await_args
488
+ created_notifications = called_args [0 ]
489
+
490
+ # Ensure that the analyst was skipped
491
+ assert len (created_notifications ) == 2
492
+ assert created_notifications [0 ].related_user_profile_id == 2
0 commit comments