
    дhU                     b   S r SSKrSSKJs  Jr  SSKrSSKrSSK	J
r
J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JrJr  SS
KJrJrJr  SSKJ r J!r!J"r"  SSK#J$r$  SSK%J&r&J'r'  SSK(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)z5
Integration tests for the stream processing system.
    N)patch	MagicMock)TestCaseTransactionTestCase)get_user_model)timezone)	APIClientstatus)ChannelStreamSession
HLSSegment)JingleTemplateJingleDetectionAdBreak)NotificationChannelNotificationTemplateNotificationRule)StreamCaptureService)JingleDetectorAdBreakAnalyzer)NotificationServicec                       \ rS 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
g)StreamProcessingIntegrationTest   z:Integration tests for complete stream processing workflow.c                    [         R                  R                  SSSS9U l        [        R
                  " 5       U l        [        R                  R                  SSSU R                  U R                  S9U l	        [        R                  R                  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        [         R                  R                  SSU R                  U R                  S9U l        U R"                  R$                  R'                  U R                  5        g )Ntestusertest@example.comtestpass123usernameemailpasswordzIntegration Test Channelzintegration-testhttps://example.com/stream.m3u8nameslughls_urloutput_directory
created_byTest Jingleztest-jinglez/tmp/test_jingle.jpgg?)r&   r'   
image_pathsimilarity_thresholdr*   zTest Notificationstelegramtoken123	bot_tokenchat_idr&   channel_typeconfigurationr*   zAd Break Templatead_break_detectedzAd Break Detectedz%Ad break detected in {{channel_name}}r&   template_typesubject_templatebody_templater*   zAd Break Rule)r&   
event_typetemplater*   )Userobjectscreate_userusertempfilemkdtemptemp_dirr   createchannelr   jingle_templater   notification_channelr   notification_templater   notification_rulechannelsaddselfs    BC:\Users\brahi\OneDrive\Desktop\Code\src\tests\test_integration.pysetUp%StreamProcessingIntegrationTest.setUp   sb   LL,,$" - 
	
 !((* --+#5!]]yy . 
  .55<<-!$yy  =  
 %8$?$?$F$F%#(/EByy	 %G %
! &:%A%A%H%H$-0Ayy &I &
" "2!9!9!@!@ *//yy	 "A "
 	''++D,E,EF    c                 B    [         R                  " U R                  SS9  g )NT)ignore_errors)shutilrmtreerD   rM   s    rO   tearDown(StreamProcessingIntegrationTest.tearDownO   s    dmm48rR   zsubprocess.Popenz2apps.jingles.services.JingleDetector.detect_jingle8apps.notifications.services.TelegramService.send_messagec                    [        5       nSUl        SUR                  l        XCl        SSSU R                  4Ul        SUl        [        5       nUR                  U R                  5      nU R                  U5        U R                  UR                  S5        [        R                  R                  USS	S
SS9n[        5       nUR                  S
U5      n	U R                  U	5        U R                  U	R                  U R                  5        [!        5       n
U
R#                  U	5      nU R                  US   S5        US   nU R                  UR                  S5        [%        5       nUR'                  SSU R                  R(                  0U R*                  /S9nU R-                  U5        UR/                  5         U R                  [0        R                  R3                  SS9R5                  5       S5        U R                  [6        R                  R5                  5       S5        U R                  [8        R                  R3                  SS9R5                  5       S5        g)zPTest complete workflow from stream start to ad break detection and notification.90  Nr+   z/tmp/frame.jpgg      ?Tactive   zsegment_001.tsz/tmp/segment_001.tsg      @)sessionsequence_numberfilename	file_pathdurationactionstart_ad_breakad_breakr7   channel_namer9   contextrK   r
   )r   pidpollreturn_valuerG   r   start_capturerF   assertIsNotNoneassertEqualr   r   r?   rE   r   process_detectionr   analyze_detectionr   send_notificationr&   rH   
assertTrueassert_calledr   filtercountr   r   )rN   mock_telegrammock_detect
mock_popenmock_processcapture_servicer^   segmentdetector	detectionanalyzeranalysis_resultre   notification_servicenotification_results                  rO   (test_complete_stream_processing_workflowHStreamProcessingIntegrationTest.test_complete_stream_processing_workflowR   s(    !{ )-&".   	$
  &*" /0!//=W%2 $$++%+ , 
 "#../DgN	Y'22D4H4HI #$"44Y?24DE":.(3  342DD-#T\\%6%67//0 E 
 	+,##% 	..55X5FLLNPQR00668!<//x/@FFH!LrR   c                 r   [        5       nSUl        SUR                  l        X!l        [	        5       nUR                  U R                  5      nU R                  U5        SUR                  l        UR                  U R                  5      nU R                  U5        U R                  UR                  S5        g)z*Test stream failure and recovery workflow.r[   r]   Nr\   )r   ri   rj   rk   r   rl   rF   assertIsNonecapture_with_retryrm   rn   r   )rN   rx   ry   rz   r^   s        rO   test_stream_failure_recovery<StreamProcessingIntegrationTest.test_stream_failure_recovery   s     !{ )*&"..0 "//='" *.&!44T\\BW%2rR   )rF   rG   rH   rJ   rI   rD   rA   N)__name__
__module____qualname____firstlineno____doc__rP   rW   r   r   r   __static_attributes__ rR   rO   r   r      sf    D2Gh9 
?@
EF?M G A ?MB 3 3rR   r   c                   V    \ rS rSrSrS r\" S5      \" S5      S 5       5       rS rSr	g	)
APIIntegrationTest   z$Integration tests for 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    rA   API Test Channelzapi-testr$   z/tmp/api-testr%   )
r	   clientr>   r?   r@   rA   force_authenticater   rE   rF   rM   s    rO   rP   APIIntegrationTest.setUp   sx    kLL,,$" - 
	
 	&&DII&6--#5,yy . 
rR   -apps.streams.tasks.start_stream_capture.delayz,apps.streams.tasks.stop_stream_capture.delayc                    [        SS9Ul        [        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         [        R                  R!                  U R                  S["        R$                  " 5       S	9n[        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 complete stream control via API.zstart-task-ididzstop-task-idchannel-start-streampkkwargstask_idr\   rF   r   
started_atzchannel-stop-streamN)r   rk   reverserF   r   r   postrn   status_coder   HTTP_200_OKassertIndataassert_called_oncer   r?   rE   r   now)rN   mock_stop_taskmock_start_task	start_urlstart_responser^   stop_urlstop_responses           rO    test_stream_control_api_workflow3APIIntegrationTest.test_stream_control_api_workflow   s&    (1O'D$&/>&B# 2D$,,//;RS	)))433V5G5GHi!4!45**,  ''..LL||~ / 
 0$9PQ((222F4F4FGi!3!34))+rR   c                    [        S5      nSSSSS.S.nU R                  R                  XSS	9nU R                  UR                  [
        R                  5        [        S
5      nSSSSS.nU R                  R                  XE5      nU R                  UR                  [
        R                  5        [        S5      nSSUR                  S   UR                  S   /S.nU R                  R                  XxSS	9n	U R                  U	R                  [
        R                  5        [        S5       n
SU
l	        [        S5      nSSS0UR                  S   /S.nU R                  R                  XSS	9nU R                  UR                  [
        R                  5        U
R                  5         SSS5        g! , (       d  f       g= f)z5Test complete notification setup and sending via API.notificationchannel-listr   r.   r/   r0   r1   r&   r5   r6   jsonformatnotificationtemplate-listzAPI Test Template
test_eventzTest {{event}}zTest event {{event}} occurred)r&   r9   r:   r;   notificationrule-listzAPI Test Ruler   )r&   r<   r=   rK   rY   Tznotification-sendeventzAPI Integration Testrg   N)r   r   r   rn   r   r   HTTP_201_CREATEDr   r   rk   r   r   )rN   channel_urlchannel_datachannel_responsetemplate_urltemplate_datatemplate_responserule_url	rule_datarule_response	mock_sendsend_url	send_datasend_responses                 rO   "test_notification_workflow_via_api5APIIntegrationTest.test_notification_workflow_via_api   s    89&&+2uE
  ;;++Kf+U)55v7N7NO :;') 0<	
 !KK,,\I*668O8OP 23#&)..t4)..t45	
	 ((V(L22F4K4KL MNR[%)I"23H!-#%;<-22489I !KK,,X,PM]668J8JK((* ONNs   ,A=F22
G rF   r   rA   N)
r   r   r   r   r   rP   r   r   r   r   r   rR   rO   r   r      s7    .
" :;
9:, ; <,82+rR   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	PerformanceIntegrationTesti  z2Integration tests for performance and scalability.c                     [         R                  R                  SSSS9U l        [	        5       U l        U R
                  R                  U R                  S9  g )Nr   r   r   r    r   )r>   r?   r@   rA   r	   r   r   rM   s    rO   rP    PerformanceIntegrationTest.setUp  sJ    LL,,$" - 
	
  k&&DII&6rR   c           	         / n[        S5       HL  n[        R                  R                  SU 3SU 3SU S3SU 3U R                  S9nUR                  U5        MN     [        S5      nU R                  R                  U5      nU R                  UR                  [        R                  5        U R                  [        UR                  S	   5      S5        g
)z(Test performance with multiple channels.
   zChannel zchannel-https://example.com/stream.m3u8z/tmp/channelr%   channel-listresultsN)ranger   r?   rE   rA   appendr   r   getrn   r   r   r   lenr   )rN   rK   irF   urlresponses         rO   test_bulk_channel_operations7PerformanceIntegrationTest.test_bulk_channel_operations   s     rAoo,,s^s^4QCu=#/s!399 - G OOG$  n%;;??3'--v/A/ABX]]956;rR   c                    [         R                  R                  SSSSS.U R                  S9n[        R                  R                  SSS	S
U R                  S9n[        S5       nSUl        [        5       n[        S5       H(  nUR                  SSU0U/S9nU R                  U5        M*     SSS5        SSKJn  UR                  R                  US9nU R                  UR                  5       S5        g! , (       d  f       NN= f)z-Test performance with multiple notifications.zBulk Test Channelr.   r/   r0   r1   r4   zBulk Template	bulk_testz	Bulk TestzBulk test messager8   rY   T   indexrg   Nr   Notification)r=   )r   r?   rE   rA   r   r   rk   r   r   rq   rr   apps.notifications.modelsr   rt   rn   ru   )	rN   rF   r=   r   servicer   resultr   notificationss	            rO   test_bulk_notification_creation:PerformanceIntegrationTest.test_bulk_notification_creation5  s	    &--44$#(/EByy	 5 
 (//66 %(-yy 7 
 MNR[%)I")+G2Y 22"-$aL%Y 3 
 '  O 	;$,,33X3F,,.3! ONs   $A	C55
Dc           	      >   / n/ n[        S5       H  n[        R                  R                  SU 3SU 3SU S3SU 3U R                  S9nUR                  U5        [        R                  R                  US[        R                  " 5       S	9nUR                  U5        M     [        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        g)z2Test handling multiple concurrent stream sessions.   zConcurrent Channel zconcurrent-r   r   z/tmp/concurrentr%   r\   r   streamsession-listr   r   N)r   r   r?   rE   rA   r   r   r   r   r   r   r   rn   r   r   r   r   r   )rN   rK   sessionsr   rF   r^   r   r   s           rO   test_concurrent_stream_sessions:PerformanceIntegrationTest.test_concurrent_stream_sessionsZ  s    qAoo,,*1#."1#&4QCu=#21#!699 - G OOG$#++22#<<> 3 G
 OOG$ $ *+;;??38(<=--v/A/ABX]]956:rR   r   rA   N)
r   r   r   r   r   rP   r   r   r   r   r   rR   rO   r   r     s    <7<*#4J;rR   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	SecurityIntegrationTestiy  z(Integration tests for security features.c                 j    [        5       U l        [        R                  R	                  SSSS9U l        g )Nr   r   r   r    )r	   r   r>   r?   r@   rA   rM   s    rO   rP   SecurityIntegrationTest.setUp|  s/    kLL,,$" - 
	rR   c                 &   [        S5      [        S5      [        S5      [        S5      [        S5      [        S5      /nU HM  nU R                  R                  U5      nU R                  UR                  [
        R                  SU S35        MO     g	)
z;Test that authentication is required for all API endpoints.r   r   r   r   znotification-listr   z	Endpoint z should require authenticationN)r   r   r   rn   r   r   HTTP_401_UNAUTHORIZED)rN   	endpointsendpointr   s       rO   (test_authentication_required_across_apis@SecurityIntegrationTest.test_authentication_required_across_apis  s     N#().//0'(+,
	 "H{{x0H$$,,H:%CD "rR   c                 4   [         R                  R                  SSSS9n[         R                  R                  SSSS9nU R                  R	                  US9  SS	S
SS.nU R                  R                  [        S5      U5      nU R                  UR                  [        R                  5        U R                  R	                  US9  U R                  R                  [        S5      5      nU R                  UR                  [        R                  5        U R                  [        UR                  S   5      S5        U R                  R                  [        SSUR                  S   0S95      nU R                  UR                  [        R                  5        g)z4Test that users can only access their own resources.user1zuser1@example.comr   r    user2zuser2@example.comr   zUser1 Channelzuser1-channelr$   z
/tmp/user1r&   r'   r(   r)   r   r   r   channel-detailr   r   r   N)r>   r?   r@   r   r   r   r   rn   r   r   r   r   r   r   r   HTTP_404_NOT_FOUND)rN   r   r  r   r   list_responsedetail_responses          rO   $test_user_isolation_across_resources<SecurityIntegrationTest.test_user_isolation_across_resources  sg   ((%" ) 

 ((%" ) 
 	&&E&2 $#8 ,	
  ;;++GN,C\R)55v7N7NO 	&&E&2(?@22F4F4FG]//	:;Q? ++//$d4D4I4I$4O-PQ
 	44f6O6OPrR   c                    U R                   R                  U R                  S9  SSSSS.nU R                   R                  [	        S5      U5      nU R                  UR                  [        R                  5        SS	S
S0S.nU R                   R                  [	        S5      USS9nU R                  UR                  [        R                  5        g)z5Test input validation across different API endpoints.r    zinvalid-channelznot-a-valid-urlz	/tmp/testr  r   zInvalid Channelr.   invalid_fieldvaluer   r   r   r   N)	r   r   rA   r   r   rn   r   r   HTTP_400_BAD_REQUEST)rN   invalid_channel_datar   invalid_notification_datas       rO   !test_input_validation_across_apis9SecurityIntegrationTest.test_input_validation_across_apis  s    &&DII&6 %( +	 
 ;;##GN$;=QR--v/J/JK &&%
! ;;##./% $ 

 	--v/J/JKrR   r   N)
r   r   r   r   r   rP   r   r  r  r   r   rR   rO   r   r   y  s    2
&%QNLrR   r   c                   \    \ rS rSrSrS r\" S5      S 5       rS r\" S5      S 5       r	S	r
g
)ErrorHandlingIntegrationTesti  z2Integration tests for error handling and recovery.c                    [         R                  R                  SSSS9U l        [	        5       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   Error Test Channelz
error-testr$   z/tmp/error-testr%   )
r>   r?   r@   rA   r	   r   r   r   rE   rF   rM   s    rO   rP   "ErrorHandlingIntegrationTest.setUp  sx    LL,,$" - 
	
  k&&DII&6--%5.yy . 
rR   z8apps.streams.services.StreamCaptureService.start_capturec                 X   [        S5      Ul        [        SSU R                  R                  0S9n[        S5       n[        S5      Ul        U R                  R                  U5      nU R                  UR                  [        R                  5        SSS5        g! , (       d  f       g= f)z,Test error handling when stream start fails.zFFmpeg errorr   r   r   r   zTask failedN)	Exceptionside_effectr   rF   r   r   r   r   rn   r   r   HTTP_500_INTERNAL_SERVER_ERROR)rN   mock_capturer   	mock_taskr   s        rO    test_stream_start_error_handling=ErrorHandlingIntegrationTest.test_stream_start_error_handling  s     $-^#< ,dDLLOO5LMBCy$-m$<I!{{'',H X1163X3XY DCCs   AB
B)c                 D   SnU R                   R                  [        SSU0S95      nU R                  UR                  [
        R                  5        U R                   R                  [        SSU0S95      nU R                  UR                  [
        R                  5        g)z&Test accessing non-existent resources.z$00000000-0000-0000-0000-000000000000r  r   r   r   N)r   r   r   rn   r   r   r  r   )rN   	fake_uuidr   s      rO   test_invalid_resource_access9ErrorHandlingIntegrationTest.test_invalid_resource_access  s     ;	;;??$dI->?
 	--v/H/HI ;;##*D)3DE
 	--v/H/HIrR   rY   c                    [         R                  R                  SSSSS.U R                  S9n[        R                  R                  SSS	S
U R                  S9n[        S5      Ul        [        5       nUR                  SSS0U/S9nU R                  U5        SSK
Jn  UR                  R                  5       nU R                  UR                  S5        SUl        SUl        [!        SSUR"                  0S9n[%        S5       n	['        SS9U	l        U R(                  R+                  U5      n
U R                  U
R,                  [        R.                  5        U	R1                  5         SSS5        g! , (       d  f       g= f)z.Test notification failure and retry mechanism.r  r.   r/   r0   r1   r4   zError Test Template
error_testz
Error TestzError test messager8   zTelegram API errortestr   rg   r   r   failedNTznotification-retryr   r   z0apps.notifications.tasks.send_notification.delayzretry-task-idr   )r   r?   rE   rA   r   r  r  r   rq   assertFalser   r   firstrn   r   rk   r   r   r   r   r   r   r   r   r   )rN   r   rF   r=   r   r   r   notification	retry_urlmock_retry_taskretry_responses              rO   "test_notification_failure_recovery?ErrorHandlingIntegrationTest.test_notification_failure_recovery  si    &--44%#(/EByy	 5 
 (//66&&).yy 7 
 !**> ?	%'**&V$Y + 
 	  	;#++113,,h7 !%	!%	0$9PQ	EF/+4+HO(![[--i8N^779K9KL..0 GFFs   A$E00
E>r   N)r   r   r   r   r   rP   r   r  r!  r-  r   r   rR   rO   r  r    sH    <
" EFZ GZJ  EF.1 G.1rR   r  )0r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arrB   rU   unittest.mockr   r   django.testr   r   django.contrib.authr   django.utilsr   rest_framework.testr	   rest_frameworkr   apps.streams.modelsr   r   r   apps.jingles.modelsr   r   r   r   r   r   r   apps.streams.servicesr   apps.jingles.servicesr   r   apps.notifications.servicesr   r>   r   r   r   r   r  r   rR   rO   <module>r@     s       * 5 . ! ) ! B B H H a a 6 A ;R3&9 R3jd+ d+Nb; b;JcLh cLLb18 b1rR   