@@ -387,6 +387,80 @@ def test_active_eaps(self):
387387 },
388388 )
389389
390+ @mock .patch ("eap.serializers.send_eap_share_email.delay" )
391+ def test_share_eap (self , send_eap_share_email ):
392+ eap_registration = EAPRegistrationFactory .create (
393+ country = self .country ,
394+ eap_type = EAPType .SIMPLIFIED_EAP ,
395+ national_society = self .national_society ,
396+ disaster_type = self .disaster_type ,
397+ partners = [self .partner1 .id , self .partner2 .id ],
398+ created_by = self .country_admin ,
399+ modified_by = self .country_admin ,
400+ status = EAPStatus .UNDER_REVIEW ,
401+ )
402+ user1 , user2 , user3 = UserFactory .create_batch (3 )
403+
404+ url = f"/api/v2/eap-registration/{ eap_registration .id } /share/"
405+ data = {
406+ "users" : [
407+ user1 .id ,
408+ user3 .id ,
409+ ],
410+ }
411+ self .authenticate ()
412+
413+ with self .capture_on_commit_callbacks (execute = True ):
414+ response = self .client .post (url , data , format = "json" )
415+ self .assertEqual (response .status_code , 200 )
416+
417+ # Check if notification email sent
418+ send_eap_share_email .assert_called_with (
419+ eap_registration_id = eap_registration .id ,
420+ recipient_emails = [user1 .email , user3 .email ],
421+ )
422+
423+ # Check if the users has been added
424+ eap_registration .refresh_from_db ()
425+ self .assertEqual (eap_registration .users .count (), 2 )
426+
427+ # Test removing a user
428+ data = {
429+ "users" : [
430+ user1 .id ,
431+ user2 .id ,
432+ ],
433+ }
434+
435+ with self .capture_on_commit_callbacks (execute = True ):
436+ response = self .client .post (url , data , format = "json" )
437+ self .assertEqual (response .status_code , 200 )
438+ eap_registration .refresh_from_db ()
439+ self .assertEqual (eap_registration .users .count (), 2 , response .data )
440+
441+ # Check notification email sent again with only updated user
442+ send_eap_share_email .assert_called_with (
443+ eap_registration_id = eap_registration .id ,
444+ recipient_emails = [user2 .email ],
445+ )
446+
447+ # NOTE: test list of EAP Share Users
448+ url = "/api/v2/eap-share-users/"
449+ self .authenticate ()
450+ response = self .client .get (url )
451+ self .assertEqual (response .status_code , 200 )
452+ self .assertEqual (response .data ["count" ], 1 , response .data )
453+ returned_user_ids = [user ["id" ] for user in response .data ["results" ][0 ]["users_details" ]]
454+ # count should be 2
455+ self .assertEqual (len (returned_user_ids ), 2 )
456+
457+ # NOTE: test with filter by EAP Registration Id
458+ url = f"/api/v2/eap-share-users/?id={ eap_registration .id } "
459+ self .authenticate ()
460+ response = self .client .get (url )
461+ self .assertEqual (response .status_code , 200 )
462+ self .assertEqual (response .data ["count" ], 1 , response .data )
463+
390464
391465class EAPSimplifiedTestCase (APITestCase ):
392466 def setUp (self ):
0 commit comments