
    P1iC                     8   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zedk(  rd dlZ ej4                          yy)    )TestCaseClient)reverse)timezone)status)	APIClient)User)Person)liuyaoc                       e Zd ZdZd Zy)TempUserIntegrationTestCasez@Base test case with common setup for temp user integration testsc           	      (   t               | _        t               | _        dddddd| _        ddd	d
ddd| _        dddddddt        j                         j                         d| _	        t        j                  j                  ddddd      | _        y)zSet up test dataWeb Test PersonMz
1990-01-01z12:00:00zWeb test notes)namegender
birth_date
birth_timenotesAPI Test PersonFz
1985-06-15z15:30:00FzAPI test notes)r   r   r   r   lunarr   Integration test question10000questiony1y2y3y4y5y6qdate
1234567890zregular@example.comtestpass123Regularr	   )phoneemailpassword
first_name	last_nameN)r   
web_clientr   
api_clientbazi_web_databazi_api_datar   now	isoformatliuyao_datar	   objectscreate_userregular_user)selfs    U/home/cursorai/projects/iching/iching/tests/../../tests/test_temp_user_integration.pysetUpz!TempUserIntegrationTestCase.setUp   s     (#+ &&$%
 &&$%
 4\\^--/	
 !LL44'"  5 
    N)__name__
__module____qualname____doc__r:    r;   r9   r   r      s
    J*
r;   r   c                       e Zd ZdZd Zd Zy)WebToAPIIntegrationTestsz8Test scenarios where user starts with web, then uses APIc                    | j                   j                  t        d      | j                        }t        j
                  j                  d      j                         }| j                  |       | j                   j                  |       ddddd}| j                   j                  t        d      |      }|j                          | j                  |j                         | j                  j                  t        d	      ddd
d      }| j                  |j                  t         j"                         |j%                         }| j                  j'                  d|d           | j                  j                  t        d      | j(                  d      }| j                  |j                  t         j*                         t,        j
                  j                  |      }| j                  |j/                         d       |D 	cg c]  }	|	j0                   }
}	| j3                  d|
       | j3                  d|
       yc c}	w )z8Test creating temp user via web, then using API with JWTbazi:calculate_chartTis_temporary_user
9876543210zwebtoapi@example.com
newpass123r)   r*   r+   	password2accounts:temp_register	api:loginr)   r+   jsonformatBearer accessHTTP_AUTHORIZATIONapi:bazi-list
created_by   r   r   N)r.   postr   r0   r	   r5   filterfirstassertIsNotNoneforce_loginrefresh_from_dbassertFalserF   r/   assertEqualstatus_coder   HTTP_200_OKrN   credentialsr1   HTTP_201_CREATEDr
   countr   assertIn)r8   response	temp_userregister_dataregister_responseapi_login_responsetokensapi_responseuser_recordsrrecord_namess              r9   $test_web_temp_user_then_api_with_jwtz=WebToAPIIntegrationTests.test_web_temp_user_then_api_with_jwt?   s    ??''0F(GI[I[\ LL''$'?EEG	Y' 	##I. "+$%	
 !OO00,-
 	!!#445 "__11K "= 2 
 	+779K9KL#((* 	##AQ@R7S#T++O$ , 
 	1163J3JK ~~,,	,B++-q1(4515'6'6 6s   I c                    | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }| j                  j                  |       | j                  j                  ddddddddt        j                         j                         d	d
       ddd}| j                  j                  t        d      |d
      }| j                  |j                  t        j                          |j#                         d   }| j                  |d   d       | j                  |d   d       t$        j
                  j                  | j&                        }t(        j
                  j                  | j&                        }| j                  |j+                         d       | j                  |j+                         d       y)z9Test web temp user data migration when logging in via APIrD   TrE   user/api/liuyao/liuyao/r      r   r   rN   rO   r&   r'   rM   zapi:temp_logintransfer_summarybazi_recordsliuyao_recordsrV   N)r.   rY   r   r0   r	   r5   rZ   r[   r/   force_authenticater   r2   r3   r`   ra   r   rb   rN   r
   r7   r   re   )r8   rh   
login_datarg   transferrx   ry   s          r9   &test_web_temp_user_api_login_migrationz?WebToAPIIntegrationTests.test_web_temp_user_api_login_migrationw   s    	W%;<d>P>PQ LL''$'?EEG	**	*:231AQaq\\^--/5
 	 	 	 "%


 ??''$% ( 
 	--v/A/AB ==?#56.115"23Q7 ~~,,8I8I,J..D4E4E.F++-q1--/3r;   N)r<   r=   r>   r?   rq   r}   r@   r;   r9   rB   rB   <   s    B67p&4r;   rB   c                       e Zd ZdZd Zy)APIToWebIntegrationTestsz8Test scenarios where user starts with API, then uses webc                    | j                   j                  t        d      | j                  d      }|j	                         }t
        j                  j                  |d         }| j                  j                  |       dddd	d
}| j                  j                  t        d      |      }|j                          | j                  |j                         | j                  |j                  d       | j                  |j                  d	       t         j                  j#                  |d      j%                         }| j'                  |       y)z@Test creating temp user via direct API, then registering via webapi:temp_user_createrN   rO   user_ididrG   zapitoweB@example.comAPIr	   )r)   r*   r,   r-   rK   zAPI User)rW   r   N)r/   rY   r   r1   rN   r	   r5   getr.   r]   r^   r_   rF   r`   r,   r-   r
   rZ   r[   r\   )r8   rm   temp_user_datarh   ri   web_response
api_records          r9   $test_api_temp_user_then_web_registerz=APIToWebIntegrationTests.test_api_temp_user_then_web_register   s)    ++*+ , 
 &**,LL$$y(A$B	 	##I. "+	
 ++,-
 	!!#445--u5,,f5 ^^**ij*QWWY
Z(r;   N)r<   r=   r>   r?   r   r@   r;   r9   r   r      s
    B%)r;   r   c                       e Zd ZdZd Zd Zy)!CrossPlatformDataConsistencyTestsz2Test data consistency across web and API platformsc                    | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }| j                  j                  |       | j                  j                  t        d      | j                  d      }| j                  |j                  t        j                         t        j
                  j                  |      }| j                  |j!                         d	       | j#                  |j                  d
      j%                                | j#                  |j                  d
      j%                                y)z@Test creating records via both web and API maintains consistencyrD   TrE   rs   rU   rN   rO   rV   rX   )ownerFN)r.   rY   r   r0   r	   r5   rZ   r[   r/   rz   r1   r`   ra   r   rd   r
   re   
assertTrueexists)r8   rh   rm   temp_user_recordss       r9   #test_mixed_platform_record_creationzECrossPlatformDataConsistencyTests.test_mixed_platform_record_creation   s&    	W%;<d>P>PQLL''$'?EEG	 	**	*: ++O$ , 
 	1163J3JK #NN11Y1G*002A6 	)00t0<CCEF)00u0=DDFGr;   c                 b   | j                   j                  t        d      | j                  d      }|j	                         }t
        j                  j                  |d         }| j                   j                  d|d           | j                   j                  t        d	      | j                  d      }| j                  |j                  t        j                         | j                  j                  |       | j                  j!                         }d
|d<   | j                  j                  t        d      |       | j                  j!                         }d|d<   | j                   j                  t        d	      |d       t"        j                  j%                  |      j'                  d      }| j                  |j)                         d       | j+                  |d   j,                         | j/                  |d   j,                         | j/                  |d   j,                         y)zDTest that ownership flags remain consistent when switching platformsr   rN   rO   r   r   rQ   rR   rS   rU   zSecond Web Recordr   rD   zThird API RecordrV   
created_at   r   rv   rX   N)r/   rY   r   r1   rN   r	   r5   r   rc   r`   ra   r   rd   r.   r]   r0   copyr
   rZ   order_byre   r   r   r_   )r8   rm   r   rh   bazi_responseweb_data
third_datarecordss           r9   )test_platform_switching_owner_consistencyzKCrossPlatformDataConsistencyTests.test_platform_switching_owner_consistency   s    ++*+ , 

 &**,LL$$y(A$B	 	##PXAY@Z7[#\,,O$ - 

 	22F4K4KL 	##I.%%**,.W%;<hG '',,.
/
6O$ 	 	
 ..''9'=FF|T!, 	
(()))*))*r;   N)r<   r=   r>   r?   r   r   r@   r;   r9   r   r      s    <H4*+r;   r   c                       e Zd ZdZd Zy)TempUserSessionSharingTestsz>Test session sharing between web and API (simulated scenarios)c                 ,   | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }| j                   j                  |       ddddd}| j                   j                  t        d      |       | j                  j                  t        d	      ddd
d      }|j                         }| j                  j                  d|d           | j                  j                  t        d            }| j                  |j                  t        j                          |j                         d   }| j                  t#        |      d       | j                  |d   d   d       y)z'Test session continuity from web to APIrD   TrE   rG   zsession@example.comsessionpass123rI   rK   rL   rM   rN   rO   rQ   rR   rS   rU   resultsrv   r   r   r   N)r.   rY   r   r0   r	   r5   rZ   r[   r]   r/   rN   rc   r   r`   ra   r   rb   len)r8   rh   ri   	api_loginrl   rm   r   s          r9   "test_session_continuity_web_to_apiz>TempUserSessionSharingTests.test_session_continuity_web_to_api  s`    	W%;<d>P>PQLL''$'?EEG	 	##I. "*()	
 	W%=>N OO((K "0@A ) 
	 !##AQ@R7S#T **7?+CD1163E3EF##%i0Wq)F+->?r;   N)r<   r=   r>   r?   r   r@   r;   r9   r   r     s    H"@r;   r   c                       e Zd ZdZd Zd Zy)%TempUserErrorHandlingIntegrationTestsz/Test error handling in cross-platform scenariosc                 l   | j                   j                  t        d      | j                  d      }|j	                         }| j
                  j                  }d|d<   |j                          ddd}| j
                  j                  t        d	      |      }| j                  |j                  g d
       y)z3Test handling of invalid/expired temp user sessionsr   rN   rO   i temp_user_idrG   zinvalid@example.comr)   r*   rK   )i.  i  i  i  N)
r/   rY   r   r1   rN   r.   sessionsaverf   ra   )r8   rm   r   r   ri   rg   s         r9   test_invalid_session_handlingzCTempUserErrorHandlingIntegrationTests.test_invalid_session_handling@  s     ++*+ , 

 &**, //))"' "*

 ??''0H(I=Y 	h**,@Ar;   c                    | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }| j                  j                  |       | j                   j                  |       ddd}| j                   j                  t        d      |      }| j                  j                  t        d	      | j                  d
      }| j                  |j                  ddg       y)z?Test handling concurrent modifications from different platformsrD   TrE   rs   rG   zconcurrent@example.comr   rK   rU   rN   rO      i  N)r.   rY   r   r0   r	   r5   rZ   r[   r/   rz   r]   r1   rf   ra   )r8   rh   ri   r   rm   s        r9   &test_concurrent_platform_modificationszLTempUserErrorHandlingIntegrationTests.test_concurrent_platform_modificationsZ  s     	W%;<d>P>PQLL''$'?EEG	 	**	*: 	##I. "-

 ++,-
 ++O$ , 
 	l..c
;r;   N)r<   r=   r>   r?   r   r   r@   r;   r9   r   r   =  s    9B4<r;   r   c                       e Zd ZdZd Zy)TempUserCompleteWorkflowTestsz/Test complete workflows spanning both platformsc                 D   | j                   j                  t        d      | j                  d      }|j	                         }t
        j                  j                  |d         }| j                   j                  d|d           | j                   j                  t        d	      | j                  d      }| j                  |j                  t        j                         | j                   j                  d
| j                  d      }t
        j                  j                  |d         }| j                  j                  |       ddddddd}| j                  j                  t        d      |      }| j                   j                  t        d      dddd      }| j                  |j                  t        j                          |j	                         }	| j                   j                  d|	d           | j                   j                  t        d	            }
| j                   j                  t        d            }| j                  |
j                  t        j                          | j                  |j                  t        j                          | j                  t#        |
j	                         d         d       | j                  t#        |j	                         d         d       | j$                  j'                         }d|d<   | j                  j)                  dd       | j                  j                  t        d      |       | j                   j                  t        d	            }| j                  t#        |j	                         d         d       y)zHTest complete workflow: mobile app -> web registration -> back to mobiler   rN   rO   r   r   rQ   rR   rS   rU   ru   
5551234567zmobile@example.commobilepass123Mobiler	   )r)   r*   r+   rJ   r,   r-   rK   rL   rM   zapi:liuyao-listr   rv   zPost-Registration Web Recordr   rD   rX   N)r/   rY   r   r1   rN   r	   r5   r   rc   r`   ra   r   rd   r4   r.   r]   rb   r   r0   r   login)r8   temp_user_responser   rh   mobile_bazimobile_liuyaori   web_registermobile_loginrl   	bazi_listliuyao_listweb_baziupdated_lists                 r9   !test_complete_mobile_web_workflowz?TempUserCompleteWorkflowTests.test_complete_mobile_web_workflow~  s"    "__11*+ 2 

 ,002LL$$y(A$B	 	##PXAY@Z7[#\oo**O$ + 

 	00&2I2IJ,,! - 
 LL$$y(A$B	##I. ")'("
 ++,-
 ++K "@ , 
 	1163E3EF""$ 	##AQ@R7S#TOO''(@A	oo))'2C*DE..0B0BC00&2D2DEY^^-i891=[--/	:;Q? %%**,9 	L?KW%;<hG **7?+CD\..0;<a@r;   N)r<   r=   r>   r?   r   r@   r;   r9   r   r   {  s    9LAr;   r   __main__N)django.testr   r   django.urlsr   django.utilsr   rest_frameworkr   rest_framework.testr   main.user.userr	   bazi.modelsr
   liuyao.modelsr   r   rB   r   r   r   r   r   r<   unittestmainr@   r;   r9   <module>r      s    (  ! ! )    -
( -
ba4: a4H(): ()VG+(C G+T%@"= %@P;<,G ;<|OA$? OAd zHMMO r;   