
    δhYF                         S r SSKrSSKJs  Jr  SSK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g)z*
Unit tests for notifications app models.
    N)TestCase)get_user_model)ValidationError)timezone)NotificationChannelNotificationTemplateNotificationNotificationRulec                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
NotificationChannelModelTest   z)Test cases for NotificationChannel model.c                 L    [         R                  R                  SSSS9U l        g Ntestusertest@example.comtestpass123usernameemailpasswordUserobjectscreate_useruserselfs    PC:\Users\brahi\OneDrive\Desktop\Code\src\apps\notifications\tests\test_models.pysetUp"NotificationChannelModelTest.setUp   &    LL,,$" - 
	    c                 B   [         R                  R                  SSSSS.U R                  S9nU R	                  UR
                  S5        U R	                  UR                  S5        U R                  UR                  5        U R	                  UR                  S   S5        g)	z.Test creating a Telegram notification channel.zTest Telegramtelegram
test_token	123456789	bot_tokenchat_idnamechannel_typeconfiguration
created_byr(   N)
r   r   creater   assertEqualr+   r,   
assertTrue	is_activer-   r   channels     r   test_telegram_channel_creation;NotificationChannelModelTest.test_telegram_channel_creation   s    %--44 #)& yy 5 
 	7--z:))*..{;\Jr"   c           
          [         R                  R                  SSSSSSSS.U R                  S	9nU R	                  UR
                  S5        U R	                  UR                  S
   S5        g)z,Test creating an email notification channel.z
Test Emailr   zsmtp.gmail.comK  ztest@gmail.comapp_passwordT)	smtp_host	smtp_portr   r   use_tlsr*   r:   Nr   r   r/   r   r0   r,   r-   r3   s     r   test_email_channel_creation8NotificationChannelModelTest.test_email_channel_creation-   st    %--44 - ,* yy 5 
 	--w7..{;=MNr"   c                     [         R                  R                  SSSSSS0S.U R                  S9nU R	                  UR
                  S5        U R	                  UR                  S	   S5        g
)z-Test creating a webhook notification channel.zTest Webhookwebhookzhttps://api.example.com/webhookPOSTAuthorizationzBearer token123)urlmethodheadersr*   rD   Nr=   r3   s     r   test_webhook_channel_creation:NotificationChannelModelTest.test_webhook_channel_creation?   st    %--44"8 +->?
 yy 5 	
 	--y9..u57XYr"   c                    [         R                  R                  SSSSS.U R                  S9  U R	                  [
        5         [         R                  R                  SSSS	0U R                  S9  S
S
S
5        g
! , (       d  f       g
= f)z'Test unique constraint on channel name.Test Channelr$   token1123r'   r*   r   r:   smtp.test.comN)r   r   r/   r   assertRaises	Exceptionr   s    r   test_channel_unique_name5NotificationChannelModelTest.test_channel_unique_nameO   sz    ##**#(0UCyy	 	+ 	
 y)''..#$*O<99	 /  *))s   -A99
Br   N)__name__
__module____qualname____firstlineno____doc__r   r5   r>   rG   rP   __static_attributes__ r"   r   r   r      s!    3
K"O$Z r"   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
NotificationTemplateModelTesta   z*Test cases for NotificationTemplate model.c                 L    [         R                  R                  SSSS9U l        g r   r   r   s    r   r   #NotificationTemplateModelTest.setUpd   r!   r"   c                 8   [         R                  R                  SSSSU R                  S9nU R	                  UR
                  S5        U R	                  UR                  S5        U R                  SUR                  5        U R                  UR                  5        g)zTest basic template creation.Stream Startedstream_startedStream {{channel_name}} Started5Stream {{channel_name}} has started at {{start_time}}r+   template_typesubject_templatebody_templater.   z{{channel_name}}N)r   r   r/   r   r0   r+   re   assertInrf   r1   r2   r   templates     r   test_template_creation4NotificationTemplateModelTest.test_template_creationk   s    '//66!*>Qyy 7 
 	(89//1AB((*C*CD**+r"   c           	         [         R                  R                  SSSSU R                  S9  U R	                  [
        5         [         R                  R                  SSSSU R                  S9  S	S	S	5        g	! , (       d  f       g	= f)
z(Test unique constraint on template type.zStream Started 1ra   z	Subject 1zBody 1rd   zStream Started 2z	Subject 2zBody 2N)r   r   r/   r   rN   rO   r   s    r   test_template_unique_constraint=NotificationTemplateModelTest.test_template_unique_constraintz   sv    $$++#*("yy 	, 	
 y) ((//'.!,&99 0  *))s   ,A66
Bc                     [         R                  R                  SSSSU R                  S9nSSS.nUR	                  U5      nU R                  US	   S
5        U R                  US   S5        g)z%Test template rendering with context.r`   ra   rb   rc   rd   rJ   z2024-01-01 12:00:00)channel_name
start_timesubjectzStream Test Channel Startedbodyz6Stream Test Channel has started at 2024-01-01 12:00:00N)r   r   r/   r   renderr0   )r   rj   contextrendereds       r   test_template_rendering5NotificationTemplateModelTest.test_template_rendering   s{    '//66!*>Qyy 7 
 +/

 ??7+),.KL&)+cdr"   c           	          U R                  [        5         [        SSSSU R                  S9nUR	                  5         SSS5        g! , (       d  f       g= f)zTest template validation.zInvalid Templatetest z
Valid bodyrd   N)rN   r   r   r   
full_cleanri   s     r   test_template_validation6NotificationTemplateModelTest.test_template_validation   sI     /+'$!#*99H ! 0//s   (A
ArR   N)rS   rT   rU   rV   rW   r   rk   rn   rx   r~   rX   rY   r"   r   r[   r[   a   s    4
,&e("r"   r[   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
NotificationModelTest   z"Test cases for Notification model.c                    [         R                  R                  SSSS9U l        [        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   rJ   r$   tokenrL   r'   r*   Test Templater{   Test Subjectz	Test Bodyrd   	r   r   r   r   r   r/   r4   r   rj   r   s    r   r   NotificationModelTest.setUp   s    LL,,$" - 
	 +2299#(/EByy	 : 
 -44;;  +%yy < 
r"   c           	         [         R                  R                  U R                  U R                  SSSSS0S9nU R                  UR                  U R                  5        U R                  UR                  U R                  5        U R                  UR                  S5        U R                  UR                  S5        U R                  UR                  5        g	)
z!Test basic notification creation.r   r   Test Messagekeyvalue)r4   rj   	recipientrs   messagerv   pendingr   N)
r	   r   r/   r4   rj   r0   statusretry_countassertIsNonesent_atr   notifications     r   test_notification_creation0NotificationModelTest.test_notification_creation   s    #++22LL]](""G$ 3 
 	--t||<..>,,i81115,../r"   c                 P   [         R                  R                  U R                  U R                  SSSS9nSUl        UR                  5         U R                  UR
                  S5        SUl        [        R                  " 5       Ul
        UR                  5         U R                  UR
                  S5        U R                  UR                  5        SUl        SUl        UR                  5         U R                  UR
                  S5        U R                  UR                  S5        g	)
z%Test notification status transitions.r   r   r   r4   rj   r   rs   r   
processing	completedfailedz
Test errorN)r	   r   r/   r4   rj   r   saver0   r   nowr   assertIsNotNoneerror_messager   s     r   $test_notification_status_transitions:NotificationModelTest.test_notification_status_transitions   s    #++22LL]]("" 3 
 +,,l; *'||~,,k:\112 '%1",,h733\Br"   c                 ,   [         R                  R                  U R                  U R                  SSSS9nU=R
                  S-  sl        UR                  5         U R                  UR
                  S5        U R                  UR
                  S:  5        g)zTest notification retry logic.r   r   r   r         N)	r	   r   r/   r4   rj   r   r   r0   r1   r   s     r   test_notification_retry_logic3NotificationModelTest.test_notification_retry_logic   s    #++22LL]]("" 3 
 	  A% 1115 	00145r"   c                     [         R                  R                  U R                  U R                  SSSS9nSUR
                   SU R                  R                   S3nU R                  [        U5      U5        g)	z+Test string representation of notification.r   r   r   r   zNotification z - z
 (pending)N)	r	   r   r/   r4   rj   idr+   r0   str)r   r   expected_strs      r   $test_notification_str_representation:NotificationModelTest.test_notification_str_representation  so    #++22LL]]("" 3 
 '|&7s4<<;L;L:MZX\*L9r"   r4   rj   r   N)rS   rT   rU   rV   rW   r   r   r   r   r   rX   rY   r"   r   r   r      s    ,
,0"C:6$:r"   r   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)NotificationRuleModelTesti  z&Test cases for NotificationRule model.c                    [         R                  R                  SSSS9U l        [        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   rJ   r$   r   rL   r'   r*   r   ra   r`   zStream has startedrd   r   r   s    r   r   NotificationRuleModelTest.setUp  s    LL,,$" - 
	 +2299#(/EByy	 : 
 -44;; *-.yy < 
r"   c                    [         R                  R                  SSU R                  SS0U R                  S9nUR
                  R                  U R                  5        U R                  UR                  S5        U R                  UR                  S5        U R                  UR                  U R                  5        U R                  UR                  5        U R                  U R                  UR
                  R                  5       5        g)z&Test basic notification rule creation.zStream Start Rulera   rq   rJ   r+   
event_typerj   
conditionsr.   N)r
   r   r/   rj   r   channelsaddr4   r0   r+   r   r1   r2   rh   allr   rules     r   test_notification_rule_creation9NotificationRuleModelTest.test_notification_rule_creation/  s    ''..$']]&7yy / 
 	$,,'$78*:;6'dllDMM$5$5$78r"   c                 L   [         R                  R                  SSU R                  SS0U R                  S9nSSS.nU R                  UR                  U5      5        SS0nU R                  UR                  U5      5        S	S0nU R                  UR                  U5      5        g
)zTest rule condition matching.zChannel Specific Rulera   rq   zSpecific Channelr   r   )rq   
other_datazDifferent Channelr   N)r
   r   r/   rj   r   r1   matches_conditionsassertFalse)r   r   matching_contextnon_matching_contextpartial_contexts        r   test_rule_condition_matching6NotificationRuleModelTest.test_rule_condition_matchingA  s    ''..(']]&(:;yy / 
 -?gV//0@AB !/0CD001EFG (100ABr"   c                     [         R                  R                  SSU R                  0 U R                  S9nSSS.nU R                  UR                  U5      5        g)z4Test rule without specific conditions (matches all).zGlobal Rulera   r   zAny Channelanything)rq   dataN)r
   r   r/   rj   r   r1   r   )r   r   rv   s      r   test_rule_without_conditions6NotificationRuleModelTest.test_rule_without_conditionsW  sW    ''..']]yy / 
 $1*E//89r"   c                 x   [         R                  R                  SSU R                  SU R                  S9n[         R                  R                  SSU R                  SU R                  S9n[        [         R                  R                  SS95      nU R                  US   U5        U R                  US   U5        g	)
zTest rule ordering by priority.zHigh Priority Rulera   r   )r+   r   rj   priorityr.   zLow Priority Rule
   )r   r   N)r
   r   r/   rj   r   listfilterr0   )r   high_prioritylow_priorityruless       r   test_rule_priority_ordering5NotificationRuleModelTest.test_rule_priority_orderinge  s    (0077%']]yy 8 
 (//66$']]yy 7 
 %--44@P4QRq=1q<0r"   c                     [         R                  R                  SSU R                  U R                  S9nSnU R                  [        U5      U5        g)z0Test string representation of notification rule.z	Test Rulera   r+   r   rj   r.   z Rule: Test Rule (stream_started)N)r
   r   r/   rj   r   r0   r   )r   r   r   s      r   test_rule_str_representation6NotificationRuleModelTest.test_rule_str_representation|  sL    ''..']]yy	 / 
 :TL1r"   r   N)rS   rT   rU   rV   rW   r   r   r   r   r   r   rX   rY   r"   r   r   r     s$    0
,9$C,:1.
2r"   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
"NotificationModelRelationshipsTesti  z0Test cases for notification model relationships.c           	      x   [         R                  R                  SSSS9U l        [        R                  R                  SSSSS	.U R                  S
9U l        [        R                  R                  SS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   zTelegram Channelr$   r   rL   r'   r*   zEmail Channelr   rM   r8   ztest@test.comr   )r:   r;   r   r   zMulti Channel Templatead_break_detectedzAd Break Detectedz%Ad break detected in {{channel_name}}rd   )
r   r   r   r   r   r/   telegram_channelemail_channelr   rj   r   s    r   r   (NotificationModelRelationshipsTest.setUp  s    LL,,$" - 
	 !4 ; ; B B##(/EByy	 !C !
 188??  , +&	 yy @ 

 -44;;)-0Ayy < 
r"   c                    [         R                  R                  SSU R                  U R                  S9nUR
                  R                  U R                  U R                  5        U R                  UR
                  R                  5       S5        U R                  U R                  UR
                  R                  5       5        U R                  U R                  UR
                  R                  5       5        g)z.Test notification rule with multiple channels.zMulti Channel Ruler   r      N)r
   r   r/   rj   r   r   r   r   r   r0   countrh   r   r   s     r   test_rule_multiple_channels>NotificationModelRelationshipsTest.test_rule_multiple_channels  s    ''..%*]]yy	 / 
 	$//1C1CD,,.2d++T]]->->-@Ad(($--*;*;*=>r"   c                    [         R                  R                  U R                  U R                  SSSS9n[         R                  R                  U R                  U R                  SSSS9nU R                  R
                  R                  5       nU R                  UR                  5       S5        U R                  X5        U R                  X#5        g	)
z+Test channel to notifications relationship.user1zTest 1z	Message 1r   user2zTest 2z	Message 2r   N)
r	   r   r/   r   rj   notificationsr   r0   r   rh   )r   notification1notification2channel_notificationss       r   'test_channel_notifications_relationshipJNotificationModelRelationshipsTest.test_channel_notifications_relationship  s     %,,33))]] 4 
 %,,33))]] 4 
 !% 5 5 C C G G I.446:m;m;r"   c                 6   [         R                  R                  U R                  U R                  SSSS9nU R                  R
                  R                  5       nU R                  UR                  5       S5        U R                  UR                  5       U5        g)z,Test template to notifications relationship.	test_userr   r   r   r   N)
r	   r   r/   r   rj   r   r   r0   r   first)r   r   template_notificationss      r   (test_template_notifications_relationshipKNotificationModelRelationshipsTest.test_template_notifications_relationship  s    #++22))]]!"" 3 
 "&!<!<!@!@!B/557;/557Fr"   c                    U R                   R                  R                  5       nU R                   R                  R                  5       nU R	                  UR                  5       S5        U R	                  UR                  5       S5        U R                  U R                  U5        U R                  U R                  U5        U R                  U R                  U5        g)z+Test user relationships to created objects.r   r   N)
r   notification_channelsr   notification_templatesr0   r   rh   r   r   rj   )r   user_channelsuser_templatess      r   test_user_created_objects<NotificationModelRelationshipsTest.test_user_created_objects  s     		77;;=99==?,,.2--/3d++];d((-8dmm^4r"   )r   r   rj   r   N)rS   rT   rU   rV   rW   r   r   r   r   r   rX   rY   r"   r   r   r     s     : 
D?<2G5r"   r   )rW   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arjsondjango.testr   django.contrib.authr   django.core.exceptionsr   django.utilsr   apps.notifications.modelsr   r   r	   r
   r   r   r[   r   r   r   rY   r"   r   <module>r     s}        . 2 !  L8 L^K"H K"\d:H d:Np2 p2fg5 g5r"   