9
9
from django .conf import settings
10
10
from django .core .exceptions import ValidationError
11
11
from edx_toggles .toggles .testutils import override_waffle_flag
12
- from testfixtures import LogCapture
13
12
14
13
from common .djangoapps .student .models import CourseEnrollment
15
14
from common .djangoapps .student .tests .factories import UserFactory
16
15
from xmodule .modulestore .tests .django_utils import ModuleStoreTestCase
17
16
from xmodule .modulestore .tests .factories import CourseFactory
18
17
19
- from ..config .waffle import ENABLE_NOTIFICATIONS , ENABLE_NOTIFICATION_GROUPING , ENABLE_PUSH_NOTIFICATIONS
20
- from ..models import CourseNotificationPreference , Notification , NotificationBrazeCampaigns
18
+ from ..config .waffle import ENABLE_NOTIFICATION_GROUPING , ENABLE_NOTIFICATIONS , ENABLE_PUSH_NOTIFICATIONS
19
+ from ..models import CourseNotificationPreference , Notification
21
20
from ..tasks import (
22
21
create_notification_pref_if_not_exists ,
23
22
delete_notifications ,
26
25
)
27
26
from .utils import create_notification
28
27
29
- LOGGER_NAME = 'openedx.core.djangoapps.notifications.tasks'
30
-
31
28
32
29
@patch ('openedx.core.djangoapps.notifications.models.COURSE_NOTIFICATION_CONFIG_VERSION' , 1 )
33
30
class TestNotificationsTasks (ModuleStoreTestCase ):
@@ -234,7 +231,6 @@ def test_send_notification_with_grouping_enabled(self):
234
231
user_notifications_mock .assert_called_once ()
235
232
236
233
@override_waffle_flag (ENABLE_NOTIFICATIONS , active = True )
237
- @override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True )
238
234
@ddt .data (
239
235
('discussion' , 'new_comment_on_response' ), # core notification
240
236
('discussion' , 'new_response' ), # non core notification
@@ -261,7 +257,6 @@ def test_send_with_app_disabled_notifications(self, app_name, notification_type)
261
257
self .assertIsNone (notification )
262
258
263
259
@override_waffle_flag (ENABLE_NOTIFICATIONS , active = True )
264
- @override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True )
265
260
def test_notification_not_created_when_context_is_incomplete (self ):
266
261
try :
267
262
send_notifications ([self .user .id ], str (self .course_1 .id ), "discussion" , "new_comment" , {}, "" )
@@ -299,11 +294,10 @@ def _create_users(self, num_of_users):
299
294
return users
300
295
301
296
@override_waffle_flag (ENABLE_NOTIFICATIONS , active = True )
302
- @override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True )
303
297
@ddt .data (
304
- (settings .NOTIFICATION_CREATION_BATCH_SIZE , 10 , 6 ),
305
- (settings .NOTIFICATION_CREATION_BATCH_SIZE + 10 , 12 , 9 ),
306
- (settings .NOTIFICATION_CREATION_BATCH_SIZE - 10 , 10 , 5 ),
298
+ (settings .NOTIFICATION_CREATION_BATCH_SIZE , 13 , 6 ),
299
+ (settings .NOTIFICATION_CREATION_BATCH_SIZE + 10 , 15 , 9 ),
300
+ (settings .NOTIFICATION_CREATION_BATCH_SIZE - 10 , 13 , 5 ),
307
301
)
308
302
@ddt .unpack
309
303
def test_notification_is_send_in_batch (self , creation_size , prefs_query_count , notifications_query_count ):
@@ -354,7 +348,7 @@ def test_preference_not_created_for_default_off_preference(self):
354
348
"username" : "Test Author"
355
349
}
356
350
with override_waffle_flag (ENABLE_NOTIFICATIONS , active = True ):
357
- with self .assertNumQueries (11 ):
351
+ with self .assertNumQueries (13 ):
358
352
send_notifications (user_ids , str (self .course .id ), notification_app , notification_type ,
359
353
context , "http://test.url" )
360
354
@@ -374,7 +368,7 @@ def test_preference_created_for_default_on_preference(self):
374
368
}
375
369
with override_waffle_flag (ENABLE_NOTIFICATIONS , active = True ):
376
370
with override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True ):
377
- with self .assertNumQueries (13 ):
371
+ with self .assertNumQueries (16 ):
378
372
send_notifications (user_ids , str (self .course .id ), notification_app , notification_type ,
379
373
context , "http://test.url" )
380
374
@@ -419,79 +413,6 @@ def test_preference_enabled_in_batch_audience(self, notification_type,
419
413
send_notifications (user_ids , str (self .course .id ), app_name , notification_type , context , content_url )
420
414
self .assertEqual (len (Notification .objects .all ()), generated_count )
421
415
422
- @override_waffle_flag (ENABLE_NOTIFICATIONS , active = True )
423
- @override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True )
424
- @patch ('openedx.core.djangoapps.notifications.tasks.get_braze_client' )
425
- def test_mobile_notification_send (self , mock_braze_client ):
426
- mock_braze_client = mock_braze_client .return_value
427
- mock_braze_client .send_campaign_message .return_value = None
428
-
429
- notification_app = "discussion"
430
- notification_type = "new_response"
431
- users = self ._create_users (5 )
432
- user_ids = [user .id for user in users ]
433
- context = {
434
- "post_title" : "Test Post" ,
435
- "author_name" : "Test Author" ,
436
- "replier_name" : "Replier Name"
437
- }
438
- with LogCapture () as log :
439
- send_notifications (user_ids , str (self .course .id ), notification_app ,
440
- notification_type , context , "https://test.url" )
441
-
442
- self .assertFalse (mock_braze_client .send_campaign_message .called )
443
- log .check_present (
444
- (
445
- 'openedx.core.djangoapps.notifications.models' ,
446
- 'INFO' ,
447
- 'No campaign id found for notification type new_response' ,
448
- )
449
- )
450
-
451
- NotificationBrazeCampaigns .objects .create (notification_type = notification_type , braze_campaign_id = "123" )
452
- send_notifications (user_ids , str (self .course .id ), notification_app ,
453
- notification_type , context , "https://test.url" )
454
-
455
- self .assertTrue (mock_braze_client .send_campaign_message .called )
456
- log .check_present (
457
- (
458
- LOGGER_NAME ,
459
- 'INFO' ,
460
- 'Sent mobile notification for new_response with Braze' ,
461
- )
462
- )
463
-
464
- @override_waffle_flag (ENABLE_NOTIFICATIONS , active = True )
465
- @override_waffle_flag (ENABLE_PUSH_NOTIFICATIONS , active = True )
466
- @patch ('openedx.core.djangoapps.notifications.tasks.get_braze_client' )
467
- def test_mobile_notification_braze_error (self , mock_braze_client ):
468
- mock_braze_client = mock_braze_client .return_value
469
- mock_braze_client .send_campaign_message .side_effect = Exception ("Error" )
470
-
471
- notification_app = "discussion"
472
- notification_type = "new_response"
473
- users = self ._create_users (5 )
474
- user_ids = [user .id for user in users ]
475
- NotificationBrazeCampaigns .objects .create (notification_type = notification_type , braze_campaign_id = "123" )
476
-
477
- context = {
478
- "post_title" : "Test Post" ,
479
- "author_name" : "Test Author" ,
480
- "replier_name" : "Replier Name"
481
- }
482
- with LogCapture () as log :
483
- send_notifications (user_ids , str (self .course .id ), notification_app ,
484
- notification_type , context , "https://test.url" )
485
-
486
- self .assertTrue (mock_braze_client .send_campaign_message .called )
487
- log .check_present (
488
- (
489
- LOGGER_NAME ,
490
- 'ERROR' ,
491
- 'Unable to send mobile notification for new_response with Braze. Reason: Error' ,
492
- )
493
- )
494
-
495
416
496
417
class TestDeleteNotificationTask (ModuleStoreTestCase ):
497
418
"""
0 commit comments