
    *ϴhP                     6   S r SSKrSSKJs  Jr  SSKrSSKJ	r	J
r
  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJrJrJrJr  \" 5       r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r!g)z,
API tests for notifications app endpoints.
    N)patch	MagicMock)TestCase)get_user_model)reverse)	APIClient)status)NotificationChannelNotificationTemplateNotificationNotificationRulec                   X    \ rS rSrSrS rS rS rS rS r	S r
\" S	5      S
 5       rSrg)NotificationChannelAPITest   z1Test cases for NotificationChannel API endpoints.c                    [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	S
.U R
                  S9U l	        g )Ntestusertest@example.comtestpass123usernameemailpassworduserTest Channeltelegramtoken123	bot_tokenchat_idnamechannel_typeconfiguration
created_by)
r   clientUserobjectscreate_userr   force_authenticater
   createchannelselfs    MC:\Users\brahi\OneDrive\Desktop\Code\src\apps\notifications\tests\test_api.pysetUp NotificationChannelAPITest.setUp   s|    kLL,,$" - 
	
 	&&DII&6*2299#(/EByy	 : 
    c                 >   [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        U R                  [        UR                  S   5      S5        U R                  UR                  S   S   S   S5        g)z#Test listing notification channels.notificationchannel-listresults   r   r#   r   N	r   r'   getassertEqualstatus_coder	   HTTP_200_OKlendatar/   urlresponses      r0   test_list_channels-NotificationChannelAPITest.test_list_channels'   s|    01;;??3'--v/A/ABX]]956:y1!4V<nMr3   c                 0   [        S5      nSSSSS.S.nU R                  R                  XSS	9nU R                  UR                  [
        R                  5        U R                  UR                  S
   S5        U R                  UR                  S   S5        g)z!Test creating a Telegram channel.r5   zNew Telegram Channelr   	new_token	987654321r   r#   r$   r%   jsonformatr#   r$   Nr   r'   postr:   r;   r	   HTTP_201_CREATEDr>   r/   r@   r>   rA   s       r0   test_create_telegram_channel7NotificationChannelAPITest.test_create_telegram_channel0   s    01*&(&
 ;;##Cf#=--v/F/FGv.0FG~6
Cr3   c                     [        S5      nSSSSSSSS	.S
.nU R                  R                  XSS9nU R                  UR                  [
        R                  5        U R                  UR                  S   S5        g)zTest creating an email channel.r5   zEmail Channelr   zsmtp.gmail.comiK  ztest@gmail.comapp_passwordT)	smtp_host	smtp_portr   r   use_tlsrG   rH   rI   r$   NrK   rN   s       r0   test_create_email_channel4NotificationChannelAPITest.test_create_email_channelB   s~    01##- ,*

 ;;##Cf#=--v/F/FG~6@r3   c                     [        S5      nSSSSSS0S.S	.nU R                  R                  XS
S9nU R                  UR                  [
        R                  5        U R                  UR                  S   S5        g)z Test creating a webhook channel.r5   zWebhook Channelwebhookzhttps://api.example.com/webhookPOSTAuthorizationzBearer token)r@   methodheadersrG   rH   rI   r$   NrK   rN   s       r0   test_create_webhook_channel6NotificationChannelAPITest.test_create_webhook_channelV   s}    01%%8 +^<
 ;;##Cf#=--v/F/FG~6	Br3   c                     [        S5      nSSSS0S.nU R                  R                  XSS9nU R                  UR                  [
        R                  5        g	)
z1Test creating channel with invalid configuration.r5   zInvalid Channelr   invalid_fieldvaluerG   rH   rI   N)r   r'   rL   r:   r;   r	   HTTP_400_BAD_REQUESTrN   s       r0   "test_invalid_channel_configuration=NotificationChannelAPITest.test_invalid_channel_configurationh   s[    01%&
 ;;##Cf#=--v/J/JKr3   z=apps.notifications.services.TelegramService.send_test_messagec                 8   SUl         [        SSU R                  R                  0S9nU R                  R                  U5      nU R                  UR                  [        R                  5        U R                  UR                  S   5        UR                  5         g)z$Test testing a notification channel.Tznotificationchannel-testpkkwargssuccessN)return_valuer   r-   rg   r'   rL   r:   r;   r	   r<   
assertTruer>   assert_called_once)r/   mock_send_testr@   rA   s       r0   test_test_channel_action3NotificationChannelAPITest.test_test_channel_actionx   sw     '+#0$9PQ;;##C(--v/A/ABi01))+r3   )r-   r'   r   N)__name__
__module____qualname____firstlineno____doc__r1   rB   rO   rV   r^   rd   r   ro   __static_attributes__ r3   r0   r   r      sC    ;
 ND$A(C$L  JK	, L	,r3   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	NotificationTemplateAPITest   z2Test cases for NotificationTemplate API endpoints.c                    [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	U R
                  S
9U l	        g )Nr   r   r   r   r   Test Templatestream_startedzStream {{channel_name}} Startedz#Stream {{channel_name}} has startedr#   template_typesubject_templatebody_templater&   )
r   r'   r(   r)   r*   r   r+   r   r,   templater.   s    r0   r1   !NotificationTemplateAPITest.setUp   sz    kLL,,$" - 
	
 	&&DII&6,44;; *>?yy < 
r3   c                 >   [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        U R                  [        UR                  S   5      S5        U R                  UR                  S   S   S   S5        g)z$Test listing notification templates.notificationtemplate-listr6   r7   r   r#   r|   Nr8   r?   s      r0   test_list_templates/NotificationTemplateAPITest.test_list_templates   s|    12;;??3'--v/A/ABX]]956:y1!4V<oNr3   c                 .   [        S5      nSSSSS.nU R                  R                  X5      nU R                  UR                  [
        R                  5        U R                  UR                  S   S5        U R                  UR                  S   S5        g	)
z&Test creating a notification template.r   zNew Templatead_break_detectedzAd Break in {{channel_name}}z;Ad break detected in {{channel_name}} at {{detection_time}})r#   r   r   r   r#   r   NrK   rN   s       r0   test_create_template0NotificationTemplateAPITest.test_create_template   s    12"0 >Z	
 ;;##C.--v/F/FGv.?79LMr3   c                 V   [        SSU R                  R                  0S9nSSSS.0nU R                  R	                  XSS	9nU R                  UR                  [        R                  5        U R                  UR                  S
   S5        U R                  SUR                  S   5        g)z#Test template preview with context.znotificationtemplate-previewrg   rh   contextr   z2024-01-01 12:00:00)channel_name
start_timerH   rI   subjectzStream Test Channel StartedbodyN)r   r   rg   r'   rL   r:   r;   r	   r<   r>   assertInrN   s       r0   test_template_preview_action8NotificationTemplateAPITest.test_template_preview_action   s    4dDMMDTDT=UV .3
 ;;##Cf#=--v/A/ABy13PQnhmmF&;<r3   )r'   r   r   N)
rq   rr   rs   rt   ru   r1   r   r   r   rv   rw   r3   r0   ry   ry      s    <
"ON =r3   ry   c                   R    \ rS rSrSrS rS rS rS r\	" S5      S 5       r
S	 rS
rg)NotificationAPITest   z*Test cases for Notification API endpoints.c                    [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	S
.U R
                  S9U l	        [        R                  R                  SSSSU R
                  S9U l        [        R                  R                  U R                  U R                  SSSS9U l        g )Nr   r   r   r   r   r   r   r   r   r   r"   r|   testTest Subject	Test Bodyr~   zTest Messager-   r   	recipientr   message)r   r'   r(   r)   r*   r   r+   r
   r,   r-   r   r   r   notificationr.   s    r0   r1   NotificationAPITest.setUp   s    kLL,,$" - 
	
 	&&DII&6*2299#(/EByy	 : 
 -44;;  +%yy < 
 )0077LL]]("" 8 
r3   c                 >   [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        U R                  [        UR                  S   5      S5        U R                  UR                  S   S   S   S5        g)zTest listing notifications.notification-listr6   r7   r   r   r   Nr8   r?   s      r0   test_list_notifications+NotificationAPITest.test_list_notifications   s|    )*;;??3'--v/A/ABX]]956:y1!4Y?Pr3   c           	         [         R                  R                  U R                  U R                  SSSSS9  [        S5      nU R                  R                  USS05      nU R                  UR                  [        R                  5        U R                  [        UR                  S	   5      S
5        U R                  UR                  S	   S   S   S5        g)z'Test filtering notifications by status.test2@example.comzTest Subject 2zTest Message 2	completedr-   r   r   r   r   r	   r   r	   pendingr6   r7   r   N)r   r)   r,   r-   r   r   r'   r9   r:   r;   r	   r<   r=   r>   r?   s      r0   #test_filter_notifications_by_status7NotificationAPITest.test_filter_notifications_by_status   s     	##LL]])$$ 	$ 	
 )*;;??39(=>--v/A/ABX]]956:y1!4X>	Jr3   c                 B   [         R                  R                  SSSS0U R                  S9n[        R                  R                  UU R
                  SSSS	9  [        S
5      nU R                  R                  USU R                  R                  05      nU R                  UR                  [        R                  5        U R                  [        UR                   S   5      S5        U R                  UR                   S   S   S   U R                  R                  5        g)z(Test filtering notifications by channel.zOther Channelr   rS   zsmtp.test.comr"   r   zOther SubjectzOther Messager   r   r-   r6   r7   r   N)r
   r)   r,   r   r   r   r   r'   r9   r-   idr:   r;   r	   r<   r=   r>   )r/   other_channelr@   rA   s       r0   $test_filter_notifications_by_channel8NotificationAPITest.test_filter_notifications_by_channel  s     ,33::  &8yy	 ; 
 	##!]])## 	$ 	
 )*;;??3DLLOO(DE--v/A/ABX]]956:y1!4Y?Qr3   z0apps.notifications.tasks.send_notification.delayc                    SU R                   l        SU R                   l        U R                   R                  5         [	        SS9Ul        [        SSU R                   R                  0S9nU R                  R                  U5      nU R                  UR                  [        R                  5        U R                  SUR                  5        UR                  5         g	)
z$Test retrying a failed notification.failedz
Test errorztest-task-id)r   notification-retryrg   rh   task_idN)r   r	   error_messagesaver   rk   r   rg   r'   rL   r:   r;   r<   r   r>   rm   )r/   	mock_taskr@   rA   s       r0   test_retry_notification_action2NotificationAPITest.test_retry_notification_action  s     $, *6' !*n!=	*D$:K:K:N:N3OP;;##C(--v/A/ABi/$$&r3   c                 (   [        SSU R                  R                  0S9nU R                  R	                  U5      nU R                  UR                  [        R                  5        U R                  SUR                  S   R                  5       5        g)z(Test retrying a non-failed notification.r   rg   rh   zcan only retry failederrorN)r   r   rg   r'   rL   r:   r;   r	   rc   r   r>   lowerr?   s      r0   "test_retry_non_failed_notification6NotificationAPITest.test_retry_non_failed_notification,  sq    *D$:K:K:N:N3OP;;##C(--v/J/JK-x}}W/E/K/K/MNr3   )r-   r'   r   r   r   N)rq   rr   rs   rt   ru   r1   r   r   r   r   r   r   rv   rw   r3   r0   r   r      s=    4
@QK&R2 =>' ?' Or3   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	NotificationRuleAPITesti5  z.Test cases for NotificationRule API endpoints.c                 D   [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	S
.U R
                  S9U l	        [        R                  R                  SSSSU R
                  S9U l        [        R                  R                  SSU R                  U R
                  S9U l        U R                  R                  R                  U R                  5        g )Nr   r   r   r   r   r   r   r   r   r   r"   r|   r}   zStream StartedzStream has startedr~   	Test Ruler#   
event_typer   r&   )r   r'   r(   r)   r*   r   r+   r
   r,   r-   r   r   r   rulechannelsaddr.   s    r0   r1   NotificationRuleAPITest.setUp8  s    kLL,,$" - 
	
 	&&DII&6*2299#(/EByy	 : 
 -44;; *-.yy < 
 %,,33']]yy	 4 
	 			t||,r3   c                 >   [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        U R                  [        UR                  S   5      S5        U R                  UR                  S   S   S   S5        g)z Test listing notification rules.notificationrule-listr6   r7   r   r#   r   Nr8   r?   s      r0   test_list_rules'NotificationRuleAPITest.test_list_rulesX  s|    -.;;??3'--v/A/ABX]]956:y1!4V<kJr3   c                    [        S5      nSSU R                  R                  U R                  R                  /SS0SS.nU R                  R                  XSS	9nU R                  UR                  [        R                  5        U R                  UR                  S
   S5        U R                  UR                  S   S5        g)z"Test creating a notification rule.r   zNew Ruler   r   zSpecific Channel   )r#   r   r   r   
conditionspriorityrH   rI   r#   r   N)r   r   r   r-   r'   rL   r:   r;   r	   rM   r>   rN   s       r0   test_create_rule(NotificationRuleAPITest.test_create_rulea  s    -.-(())+=>
 ;;##Cf#=--v/F/FGv.
;|46IJr3   c                    [         R                  R                  SSU R                  U R                  S9  [        S5      nU R                  R                  USS05      nU R                  UR                  [        R                  5        U R                  [        UR                  S   5      S5        U R                  UR                  S   S	   S   S5        g
)z#Test filtering rules by event type.zAd Break Ruler   r   r   r   r}   r6   r7   r   N)r   r)   r,   r   r   r   r'   r9   r:   r;   r	   r<   r=   r>   r?   s      r0   test_filter_rules_by_event_type7NotificationRuleAPITest.test_filter_rules_by_event_types  s     	  '' *]]yy	 	( 	
 -.;;??37G(HI--v/A/ABX]]956:y1!4\BDTUr3   )r-   r'   r   r   r   N)
rq   rr   rs   rt   ru   r1   r   r   r   rv   rw   r3   r0   r   r   5  s    8-@KK$Vr3   r   c                   *    \ rS rSrSrS rS rS rSrg)NotificationStatisticsAPITesti  z1Test cases for notification statistics endpoints.c           	         [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	S
.U R
                  S9U l	        [        R                  R                  SSSSU R
                  S9U l        [        R                  R                  U R                  U R                  SSSSS9  [        R                  R                  U R                  U R                  SSSSS9  [        R                  R                  U R                  U R                  SSSSS9  g )Nr   r   r   r   r   r   r   r   r   r   r"   r|   r   r   r   r~   user1@example.comzTest 1z	Message 1r   r   user2@example.comzTest 2z	Message 2r   zuser3@example.comzTest 3z	Message 3r   )r   r'   r(   r)   r*   r   r+   r
   r,   r-   r   r   r   r.   s    r0   r1   #NotificationStatisticsAPITest.setUp  sM   kLL,,$" - 
	
 	&&DII&6*2299#(/EByy	 : 
 -44;;  +%yy < 
 	##LL]]) 	$ 	
 	##LL]]) 	$ 	
 	##LL]]) 	$ 	
r3   c                    [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        UR                  nU R                  US   S5        U R                  US   S5        U R                  US   S5        U R                  US   S5        U R                  US   S	5        g
)z&Test notification statistics endpoint.znotification-statisticstotal_notifications   completed_notificationsr7   failed_notificationspending_notificationssuccess_rate
ףp=@@N)r   r'   r9   r:   r;   r	   r<   r>   r/   r@   rA   statss       r0   test_notification_statistics:NotificationStatisticsAPITest.test_notification_statistics  s    /0;;??3'--v/A/AB45q9891=56:67;~.6r3   c                 :   [        SSU R                  R                  0S9nU R                  R	                  U5      nU R                  UR                  [        R                  5        UR                  nU R                  US   S5        U R                  US   S5        g)	z!Test channel-specific statistics.znotificationchannel-statisticsrg   rh   r   r   r   r   N)
r   r-   rg   r'   r9   r:   r;   r	   r<   r>   r   s       r0   test_channel_statistics5NotificationStatisticsAPITest.test_channel_statistics  s~    6dlloo?VW;;??3'--v/A/AB45q9~.6r3   r-   r'   r   r   N)	rq   rr   rs   rt   ru   r1   r   r   rv   rw   r3   r0   r   r     s    ;2
h7	7r3   r   c                   $    \ rS rSrSrS rS rSrg)NotificationThrottlingTesti  z+Test cases for notification API throttling.c                 t   [        5       U l        [        R                  R	                  SSSS9U l        U R                  R                  U R
                  S9  [        R                  R                  SSSS	S
.U R
                  S9U l	        [        R                  R                  SSSSU R
                  S9U l        g )Nr   r   r   r   r   r   r   r   r   r   r"   r|   r   r   r   r~   )r   r'   r(   r)   r*   r   r+   r
   r,   r-   r   r   r.   s    r0   r1    NotificationThrottlingTest.setUp  s    kLL,,$" - 
	
 	&&DII&6*2299#(/EByy	 : 
 -44;;  +%yy < 
r3   c                     [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        g)z2Test that notification API has throttling applied.r   N)r   r'   r9   r:   r;   r	   r<   r?   s      r0    test_notification_api_throttling;NotificationThrottlingTest.test_notification_api_throttling  s;    )*;;??3'--v/A/ABr3   r   N)rq   rr   rs   rt   ru   r1   r   rv   rw   r3   r0   r   r     s    5
0Cr3   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	NotificationPermissionsTesti  z,Test cases for notification API permissions.c                    [        5       U l        [        R                  R	                  SSSS9U l        [        R                  R	                  SSSS9U l        [        R                  R                  SSS	S
S.U R
                  S9U l	        g )Nuser1r   r   r   user2r   zUser1 Channelr   r   r   r   r"   )
r   r'   r(   r)   r*   r   r   r
   r,   r-   r.   s    r0   r1   !NotificationPermissionsTest.setUp  s    k\\--%" . 


 \\--%" . 

 +2299 #(/EBzz	 : 
r3   c                     [        S5      nU R                  R                  U5      nU R                  UR                  [
        R                  5        g)z'Test API access without authentication.r5   N)r   r'   r9   r:   r;   r	   HTTP_401_UNAUTHORIZEDr?   s      r0   test_unauthorized_access4NotificationPermissionsTest.test_unauthorized_access  s;    01;;??3'--v/K/KLr3   c                 Z   U R                   R                  U R                  S9  [        S5      nU R                   R	                  U5      nU R                  UR                  [        R                  5        U R                  [        UR                  S   5      S5        U R                   R                  U R                  S9  U R                   R	                  U5      nU R                  UR                  [        R                  5        U R                  [        UR                  S   5      S5        g)z0Test that users can only see their own channels.r   r5   r6   r7   r   N)r'   r+   r   r   r9   r:   r;   r	   r<   r=   r>   r   r?   s      r0   #test_user_can_only_see_own_channels?NotificationPermissionsTest.test_user_can_only_see_own_channels  s     	&&DJJ&701;;??3'--v/A/ABX]]956: 	&&DJJ&7;;??3'--v/A/ABX]]956:r3   c                    U R                   R                  U R                  S9  [        SSU R                  R
                  0S9nSS0nU R                   R                  X5      nU R                  UR                  [        R                  5        g)z4Test that users cannot modify other users' channels.r   znotificationchannel-detailrg   rh   r#   zHacked ChannelN)r'   r+   r   r   r-   rg   r   r:   r;   r	   HTTP_404_NOT_FOUNDrN   s       r0   'test_user_cannot_modify_others_channelsCNotificationPermissionsTest.test_user_cannot_modify_others_channels,  sp    &&DJJ&72D$,,//;RS();;$$S/--v/H/HIr3   )r-   r'   r   r   N)
rq   rr   rs   rt   ru   r1   r   r   r  rv   rw   r3   r0   r   r     s    6
(M;&	Jr3   r   )"ru   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arrH   unittest.mockr   r   django.testr   django.contrib.authr   django.urlsr   rest_framework.testr   rest_frameworkr	   apps.notifications.modelsr
   r   r   r   r(   r   ry   r   r   r   r   r   rw   r3   r0   <module>r     s      *   .  ) !  n, n,b;=( ;=|oO( oOdMVh MV`N7H N7b C  CJ:J( :Jr3   