
    rϴhM                        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Jr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 services.
    N)patch	MagicMock)TestCase)get_user_model)timezone)NotificationChannelNotificationTemplateNotification)NotificationServiceTelegramServiceEmailServiceWebhookServicec                       \ rS rSrSrS r\" S5      S 5       r\" S5      S 5       r\" S5      \" S5      S 5       5       r	S	 r
S
rg)NotificationServiceTest   z#Test cases for NotificationService.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SSS.U R                  S
9U l        [        R                  R                  SSSSU R                  S9U l	        [        5       U l        g )Ntestusertest@example.comtestpass123usernameemailpasswordzTelegram Channeltelegramtoken123	bot_tokenchat_idnamechannel_typeconfiguration
created_byzEmail Channelr   smtp.test.comK  test@test.comr   	smtp_host	smtp_portr   r   zTest Templatestream_startedzStream {{channel_name}} Startedz5Stream {{channel_name}} has started at {{start_time}}r!   template_typesubject_templatebody_templater$   )Userobjectscreate_useruserr   createtelegram_channelemail_channelr	   templater   serviceselfs    RC:\Users\brahi\OneDrive\Desktop\Code\src\apps\notifications\tests\test_services.pysetUpNotificationServiceTest.setUp   s    LL,,$" - 
	 !4 ; ; B B##(/EByy	 !C !
 188??  , +&	 yy @ 

 -44;; *>Qyy < 
 +,    8apps.notifications.services.TelegramService.send_messagec                    SUl         SSS.nU R                  R                  SUU R                  /S9nU R	                  U5        UR                  5         [        R                  R                  5       nU R                  U5        U R                  UR                  U R                  5        U R                  UR                  S5        g)	z.Test successful Telegram notification sending.TTest Channel2024-01-01 12:00:00)channel_name
start_timer+   r-   contextchannels	completedN)return_valuer8   send_notificationr5   
assertTrueassert_called_oncer
   r1   firstassertIsNotNoneassertEqualchannelstatusr:   	mock_sendrF   resultnotifications        r;   'test_send_notification_telegram_success?NotificationServiceTest.test_send_notification_telegram_success<   s     "&	 +/

 //*++, 0 
 	$$& $++113\*--t/D/DE,,k:r>   c                 h   [        S5      Ul        SS0nU R                  R                  SUU R                  /S9nU R                  U5        [        R                  R                  5       nU R                  U5        U R                  UR                  S5        U R                  SUR                  5        g)z*Test failed Telegram notification sending.zTelegram API errorrC   rA   r+   rE   failedN)	Exceptionside_effectr8   rJ   r5   assertFalser
   r1   rM   rN   rO   rQ   assertInerror_messagerR   s        r;   'test_send_notification_telegram_failure?NotificationServiceTest.test_send_notification_telegram_failureU   s     !**> ?	!>2//*++, 0 
 	  $++113\*,,h7*L,F,FGr>   3apps.notifications.services.EmailService.send_emailc                 h   SUl         SUl         SS0nU R                  R                  SUU R                  U R                  /S9nU R                  U5        UR                  5         UR                  5         [        R                  R                  5       nU R                  UR                  5       S5        g)z/Test sending notification to multiple channels.TrC   rA   r+   rE      N)rI   r8   rJ   r5   r6   rK   rL   r
   r1   allrO   count)r:   mock_telegram
mock_emailrF   rT   notificationss         r;   (test_send_notification_multiple_channels@NotificationServiceTest.test_send_notification_multiple_channelsj   s     &*""&
!>2//*++T-?-?@ 0 
 	((*%%' %,,002,,.2r>   c                     SS0nU R                   R                  SUU R                  /S9nU R                  U5        U R	                  [
        R                  R                  5       S5        g)z5Test sending notification with invalid template type.rC   rA   nonexistent_templaterE   r   N)r8   rJ   r5   r\   rO   r
   r1   re   )r:   rF   rT   s      r;   'test_send_notification_invalid_template?NotificationServiceTest.test_send_notification_invalid_template   sh    !>2//0++, 0 
 	  	--335q9r>   )r6   r8   r5   r7   r3   N)__name__
__module____qualname____firstlineno____doc__r<   r   rV   r_   ri   rm   __static_attributes__ r>   r;   r   r      sr    -"-H EF; G;0 EFH GH( @A
EF3 G B3*:r>   r   c                       \ rS rSrSrS r\" S5      S 5       r\" S5      S 5       r\" S5      S 5       r	\" S5      S	 5       r
S
rg)TelegramServiceTest   zTest cases for TelegramService.c                 6    [        5       U l        SSS.U l        g )N
test_token	123456789r   )r   r8   configr9   s    r;   r<   TelegramServiceTest.setUp   s    &(%"
r>   zrequests.postc                    [        5       nSUl        SSS0S.UR                  l        X!l        U R                  R                  U R                  SSS9nU R                  U5        UR                  5         UR                  nU R                  S	US
   S
   5        U R                  US   S   S   S5        U R                  SUS   S   S   5        g)z)Test successful Telegram message sending.   T
message_id{   okrT   Test SubjectTest Messager|   subjectmessagesendMessager      jsonr   r{   textN)r   status_coder   rI   r8   send_messager|   rK   rL   	call_argsr]   rO   )r:   	mock_postmock_responserT   r   s        r;   test_send_message_success-TelegramServiceTest.test_send_message_success   s     "$'!15,PSAT*U'!.**;;"" + 
 	$$& ''	mYq\!_51f-i8+Fnil6&:6&BCr>   c                     [        5       nSUl        SSS.UR                  l        X!l        U R                  R                  U R                  SSS9nU R                  U5        g)	z!Test Telegram API error handling.i  FzBad Request)r   descriptionr   r   r   N)r   r   r   rI   r8   r   r|   r\   )r:   r   r   rT   s       r;   test_send_message_api_error/TelegramServiceTest.test_send_message_api_error   sb     "$'!16}*U'!.**;;"" + 
 	 r>   c                     [        S5      Ul        U R                  R                  U R                  SSS9nU R                  U5        g)zTest network error handling.zNetwork errorr   r   r   N)rZ   r[   r8   r   r|   r\   )r:   r   rT   s      r;   test_send_message_network_error3TelegramServiceTest.test_send_message_network_error   sG     !*/ :	**;;"" + 
 	 r>   zrequests.getc                    [        5       nSUl        SSSSSS.S.UR                  l        X!l        U R                  R                  S5      nU R                  U5        U R                  US	   S5        U R                  US
   5        g)zTest getting bot information.r   Ti[zTest Bottest_bot)idis_bot
first_namer   r   rz   r   r   N)	r   r   r   rI   r8   get_bot_inforN   rO   rK   )r:   mock_getr   bot_infos       r;   test_get_bot_info_success-TelegramServiceTest.test_get_bot_info_success   s     "$'!(&	+
' !.<<,,\:X&*-z:*+r>   r|   r8   N)ro   rp   rq   rr   rs   r<   r   r   r   r   r   rt   ru   r>   r;   rw   rw      sp    )
 ?D D, ?! ! ?
! 
! >, ,r>   rw   c                   r    \ rS rSrSrS r\" S5      S 5       r\" S5      S 5       r\" S5      S 5       r	Sr
g	)
EmailServiceTest   zTest cases for EmailService.c                 <    [        5       U l        SSSSSS.U l        g )Nr%   r&   r'   r   T)r)   r*   r   r   use_tls)r   r8   r|   r9   s    r;   r<   EmailServiceTest.setUp   s#    #~('"
r>   zsmtplib.SMTPc                 f   [        5       nX!l        U R                  R                  U R                  SSSS9nU R                  U5        UR                  R                  5         UR                  R                  SS5        UR                  R                  5         UR                  R                  5         g)zTest successful email sending.recipient@example.comr   r   r|   	recipientr   r   r'   r   N)r   rI   r8   
send_emailr|   rK   starttlsrL   loginassert_called_once_withr   quit)r:   mock_smtp_class	mock_smtprT   s       r;   test_send_email_success(EmailServiceTest.test_send_email_success   s     K	'0$((;;-""	 ) 
 	 	--///L113))+r>   c                     [        S5      Ul        U R                  R                  U R                  SSSS9nU R                  U5        g)zTest SMTP error handling.zSMTP connection failedr   r   r   r   N)rZ   r[   r8   r   r|   r\   )r:   r   rT   s      r;   test_send_email_smtp_error+EmailServiceTest.test_send_email_smtp_error  sK     '00H&I#((;;-""	 ) 
 	 r>   c                     [        5       nX!l        U R                  R                  5       nSUS'   U R                  R                  USSSS9nU R                  U5        UR                  R                  5         g)zTest email sending without TLS.Fr   r   r   r   r   N)	r   rI   r|   copyr8   r   rK   r   assert_not_called)r:   r   r   config_no_tlsrT   s        r;   test_send_email_without_tls,EmailServiceTest.test_send_email_without_tls  st     K	'0$((*#(i (( -""	 ) 
 	 	,,.r>   r   N)ro   rp   rq   rr   rs   r<   r   r   r   r   rt   ru   r>   r;   r   r      sU    &
 >, ,( >! ! >/ /r>   r   c                   x    \ rS rSrSrS r\" S5      S 5       r\" S5      S 5       r\" S5      S 5       r	S r
S	rg
)WebhookServiceTesti,  zTest cases for WebhookService.c                 <    [        5       U l        SSSS0S.U l        g )Nhttps://api.example.com/webhookPOSTAuthorizationzBearer token123)urlmethodheaders)r   r8   r|   r9   s    r;   r<   WebhookServiceTest.setUp/  s$    %'4'):;
r>   requests.requestc                 z   [        5       nSUl        SUl        X!l        U R                  R                  U R                  SSSS0S9nU R                  U5        UR                  5         UR                  nU R                  US   S   S	5        U R                  US   S
   S5        U R                  SUS
   S   5        g)z Test successful webhook sending.r   OKr   r   keyvalue)r|   r   r   rF   r   r   r   r   r   r   N)r   r   r   rI   r8   send_webhookr|   rK   rL   r   rO   r]   )r:   mock_requestr   rT   r   s        r;   test_send_webhook_success,WebhookServiceTest.test_send_webhook_success7  s     "$'!!$1!**;;""G$	 + 
 	 	'') **	1a&11a*KLoy|I'>?r>   c                     [        5       nSUl        SUl        X!l        U R                  R                  U R                  SSS9nU R                  U5        g)z!Test webhook HTTP error handling.i  z	Not Foundr   r   r   N)r   r   r   rI   r8   r   r|   r\   )r:   r   r   rT   s       r;   test_send_webhook_http_error/WebhookServiceTest.test_send_webhook_http_errorP  sW     "$'!($1!**;;"" + 
 	 r>   c                     [        S5      Ul        U R                  R                  U R                  SSS9nU R                  U5        g)z$Test webhook network error handling.zConnection timeoutr   r   r   N)rZ   r[   r8   r   r|   r\   )r:   r   rT   s      r;   test_send_webhook_network_error2WebhookServiceTest.test_send_webhook_network_error`  sH     $--A#B **;;"" + 
 	 r>   c                 Z   U R                   R                  5       nSUS'   [        S5       n[        5       nSUl        X2l        U R                  R                  USSS9nU R                  U5        UR                  nU R                  US   S   S5        S	S	S	5        g	! , (       d  f       g	= f)
zTest webhook with GET method.GETr   r   r   r   r   r   r   N)r|   r   r   r   r   rI   r8   r   rK   r   rO   )r:   
config_getr   r   rT   r   s         r;   test_send_webhook_get_method/WebhookServiceTest.test_send_webhook_get_methodm  s    [[%%'
$
8%&,%KM(+M%(5%\\..!&& / F OOF# %..IYq\!_e4 '&&s   A(B
B*r   N)ro   rp   rq   rr   rs   r<   r   r   r   r   r   rt   ru   r>   r;   r   r   ,  s_    (
 @ @0 ! ! 
! 
!5r>   r   c                       \ rS rSrSrS r\" S5      \" S5      \" S5      S 5       5       5       r\" S5      S 5       rS	 r	S
 r
Srg)"NotificationServiceIntegrationTesti  zBIntegration tests for notification service with all channel types.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SSS.U R                  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
        [        5       U l        g )Nr   r   r   r   Telegramr   r   r   r   r    Emailr   r%   r&   r'   r   r(   Webhookwebhookr   r   r   r   zMulti Channel Templatead_break_detectedzAd Break in {{channel_name}}z;Ad break detected in {{channel_name}} at {{detection_time}}r,   )r0   r1   r2   r3   r   r4   r5   r6   webhook_channelr	   r7   r   r8   r9   s    r;   r<   (NotificationServiceIntegrationTest.setUp  s   LL,,$" - 
	 !4 ; ; B B#(/EByy	 !C !
 188?? , +&	 yy @ 

  3::AA"8  yy  B  
 -44;;)-;Wyy < 
 +,r>   z7apps.notifications.services.WebhookService.send_webhookra   r?   c                    SUl         SUl         SUl         SSS.nU R                  R                  SUU R                  U R                  U R
                  /S9nU R                  U5        UR                  5         UR                  5         UR                  5         [        R                  R                  5       nU R                  UR                  5       S5        UR                  SS	9nU R                  UR                  5       S5        g
)z/Test sending notification to all channel types.TTest StreamrB   )rC   detection_timer   rE      rH   )rQ   N)rI   r8   rJ   r5   r6   r   rK   rL   r
   r1   rd   rO   re   filter)r:   rf   rg   mock_webhookrF   rT   rh   completed_notificationss           r;   test_send_to_all_channel_typesANotificationServiceIntegrationTest.test_send_to_all_channel_types  s    
 &*""&
$(! *3

 //-++T-?-?AUAUV 0 
 	 	((*%%''') %,,002,,.2 #0"6"6k"6"J0668!<r>   c                    SUl         [        S5       n[        S5      Ul        SS0nU R                  R                  SUU R                  U R                  /S9nU R                  U5        [        R                  R                  5       nU R                  UR                  5       S5        UR                  U R                  S	9R                  5       nUR                  U R                  S	9R                  5       nU R                  UR                   S
5        U R                  UR                   S5        SSS5        g! , (       d  f       g= f)z0Test notification sending with partial failures.Tra   z
SMTP errorrC   r   r   rE   rc   )rP   rH   rY   N)rI   r   rZ   r[   r8   rJ   r5   r6   r\   r
   r1   rd   rO   re   r   rM   rQ   )r:   rf   rg   rF   rT   rh   telegram_notificationemail_notifications           r;   &test_send_notification_partial_failureINotificationServiceIntegrationTest.test_send_notification_partial_failure  s!    &*"HIZ%.|%<J"%}5G\\331//1C1CD 4 F V$ )00446M]002A6$1$8$8AVAV$8$W$]$]$_!!.!5!5d>P>P!5!Q!W!W!Y299;G/66A- JIIs   DD77
Ec                    U R                   R                  S5      nU R                  U[        5        U R                   R                  S5      nU R                  U[        5        U R                   R                  S5      nU R                  U[
        5        U R                   R                  S5      nU R                  U5        g)z3Test service selection for different channel types.r   r   r   invalidN)r8   get_service_for_channel_typeassertIsInstancer   r   r   assertIsNone)r:   telegram_serviceemail_servicewebhook_serviceinvalid_services        r;   !test_get_service_for_channel_typeDNotificationServiceIntegrationTest.test_get_service_for_channel_type  s    <<DDZP.@AA'Jm\:,,CCINo~> ,,CCIN/*r>   c                 D   SSS.nU R                  U R                  R                  SU5      5        SS0nU R                  U R                  R                  SU5      5        SSSS	S
.nU R                  U R                  R                  SU5      5        SSS.nU R                  U R                  R                  SU5      5        SSS.nU R                  U R                  R                  SU5      5        SS0nU R                  U R                  R                  SU5      5        g)z&Test channel configuration validation.r   r   r   r   r   r%   r&   r'   r   r(   r   )r*   r   r   r   r   r   r   N)rK   r8   validate_channel_configurationr\   )r:   valid_telegraminvalid_telegramvalid_emailinvalid_emailvalid_webhookinvalid_webhooks          r;   #test_validate_channel_configurationFNotificationServiceIntegrationTest.test_validate_channel_configuration  s$    !
 	CCJP^_` 
 	DDZQabc )'"	
 	CCG[YZ '
 	DDWm\] 5
 	CCI}]^ f
 	DDYP_`ar>   )r6   r8   r5   r7   r3   r   N)ro   rp   rq   rr   rs   r<   r   r   r   r  r  rt   ru   r>   r;   r   r     sm    L--^ DE
@A
EF= G B F=@ EFB GB:+*br>   r   ) rs   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr   unittest.mockr   r   django.testr   django.contrib.authr   django.utilsr   apps.notifications.modelsr   r	   r
   apps.notifications.servicesr   r   r   r   r0   r   rw   r   r   r   ru   r>   r;   <module>r     s      *   . !   y:h y:xR,( R,jC/x C/LU5 U5plb lbr>   