
    P1i#                     <   d Z ddlZddlZ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mZmZ ddlZddlZej.                  j1                  dd       ej.                  j1                  dd       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) G d" d#e#      Z* G d$ d%e#      Z+e,d&k(  rddl-Z- e-j\                          yy)'z
Comprehensive unit tests for temporary user API flows.
Tests all the scenarios for temporary user creation, data management, and conversion via API.
    N)TestCaseClient)	APIClient)status)reverse)get_user_model)AnonymousUser)timezone)datetimedatetimeDJANGO_SETTINGS_MODULEziching.settings
DJANGO_ENVdevelopment)User)UserProfile)Person)liuyao)create_temporary_userc                       e Zd ZdZd Zy)TempUserAPIFlowTestCasez=Base test case with common setup for temporary user API flowsc           	          t               | _        ddddddd| _        dd	d
dd| _        ddddddddd| _        t
        j                  j                  ddddd      | _        y)zSet up test dataAPI Test PersonM
1990-01-0112:00:00FzAPI test notes)namegender
birth_date
birth_timelunarnoteszAPI Number TestFz
1985-06-15)r   r   r   r!   zAPI test question00001T)questiony1y2y3y4y5y6usecur
1234567890regular@example.comtestpass123Regularr   )phoneemailpassword
first_name	last_nameN)	r   clientbazi_api_datanumber_api_dataliuyao_api_datar   objectscreate_userregular_user)selfs    S/home/cursorai/projects/iching/iching/tests/../../tests/test_temp_user_api_flows.pysetUpzTempUserAPIFlowTestCase.setUp!   s    k &&$%
 &&	 
 ,	 
 !LL44'"  5 
    N)__name__
__module____qualname____doc__rA    rB   r@   r   r      s
    G)
rB   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TempUserCreationAPITestsz.Test temporary user creation via API endpointsc                 v   t         j                  j                         }t        j                  j                         }| j                  j                  t        d      | j                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  t         j                  j                         |       | j                  t        j                  j                         |       y)zBTest that BaZi API only does calculation for unauthenticated usersapi:bazi-listjsonformatmessage	temp_useridN)r   r<   countr   r8   postr   r9   assertEqualstatus_coder   HTTP_200_OKrL   assertInassertNotInr?   initial_user_countinitial_person_countresponseresponse_datas        r@   .test_bazi_api_unauthenticated_calculation_onlyzGTempUserCreationAPITests.test_bazi_api_unauthenticated_calculation_onlyQ   s    !\\//1%~~335;;##O$ $ 
 	--v/A/AB  	i/m4}- 	++-/AB--/1EFrB   c                 .   t         j                  j                         }t        j                  j                         }| j                  j                  t        d      | j                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  t         j                  j                         |       | j                  t        j                  j                         |       y)zOTest that Number API (via BaZi) only does calculation for unauthenticated usersrK   rL   rM   rP   N)r   r<   rR   r   r8   rS   r   r:   rT   rU   r   rV   rL   rX   rY   s        r@   0test_number_api_unauthenticated_calculation_onlyzITempUserCreationAPITests.test_number_api_unauthenticated_calculation_onlyi   s    !\\//1%~~335;;##O$   $ 
 	--v/A/AB  	m4++-/AB--/1EFrB   c                    t         j                  j                         }t        j                  j                         }| j                  j                  t        d      | j                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  t         j                  j                         |       | j                  t        j                  j                         |       y)	zDTest that LiuYao API only does calculation for unauthenticated usersapi:liuyao-calcrL   rM   guar'   rP   rQ   N)r   r<   rR   r   r8   rS   r   r;   rT   rU   r   rV   rL   rW   rX   )r?   rZ   initial_liuyao_countr\   r]   s        r@   0test_liuyao_api_unauthenticated_calculation_onlyzITempUserCreationAPITests.test_liuyao_api_unauthenticated_calculation_only}   s    !\\//1%~~335;;##%&   $ 
 	--v/A/AB  	e]+j-0m4}- 	++-/AB--/1EFrB   c                 R   | j                   j                  t        d      ddid      }| j                  |j                  t
        j                         |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d	|       | j                  |d	          t        j                  j                  |d   
      }| j                  |j                         | j                  |j                  j                  d             y)z/Test creating temp user via direct API endpointapi:temp_user_creater   zDirect API Test UserrL   rM   user_idaccessrefreshis_temporaryrQ   temp_N)r8   rS   r   rT   rU   r   HTTP_201_CREATEDrL   rW   
assertTruer   r<   getis_temporary_userr3   
startswith)r?   r\   r]   rP   s       r@   test_direct_temp_user_creationz7TempUserCreationAPITests.test_direct_temp_user_creation   s    ;;##*++, $ 
 	--v/F/FG  	i/h.i/nm4n56 LL$$i(@$A		334	227;<rB   N)rC   rD   rE   rF   r^   r`   re   rs   rG   rB   r@   rI   rI   N   s    8G0G(G2=rB   rI   c                   "    e Zd ZdZd Zd Zd Zy)TempUserJWTAuthAPITestsz+Test JWT authentication for temporary usersc                 t    | j                   j                  t        d      ddid      }|j                         S )z<Helper to create temp user via direct API and get JWT tokensrg   r   zJWT Test UserrL   rM   )r8   rS   r   rL   r?   r\   s     r@   _create_temp_user_with_jwtz2TempUserJWTAuthAPITests._create_temp_user_with_jwt   s=    ;;##*+_% $ 

 }}rB   c                    | j                         }| j                  j                  d|d           | j                  j                  t	        d      | j
                  d      }| j                  |j                  t        j                         | j                  j                  t	        d            }| j                  |j                  t        j                         |j                         }| j                  t        |d         d       y	)
z.Test using JWT tokens for subsequent API callsBearer ri   HTTP_AUTHORIZATIONrK   rL   rM   results   N)rx   r8   credentialsrS   r   r9   rT   rU   r   rn   rp   rV   rL   len)r?   temp_user_datar\   r]   s       r@   !test_temp_user_jwt_authenticationz9TempUserJWTAuthAPITests.test_temp_user_jwt_authentication   s     88: 	W^H=U<V3WX ;;##O$ $ 

 	--v/F/FG ;;??7?#;<--v/A/AB ]956:rB   c                    | j                         }| j                  j                  t        d      d|d   id      }| j	                  |j
                  t        j                         |j                         }| j                  d|       | j                  j                  d|d           | j                  j                  t        d      | j                  d      }| j	                  |j
                  t        j                         y	)
z-Test refreshing JWT tokens for temporary userapi:token_refreshrj   rL   rM   ri   rz   r{   rK   N)rx   r8   rS   r   rT   rU   r   rV   rL   rW   r   r9   rn   )r?   r   refresh_responserefresh_datar\   s        r@    test_temp_user_jwt_token_refreshz8TempUserJWTAuthAPITests.test_temp_user_jwt_token_refresh   s     88:  ;;++'(y12 , 
 	)55v7I7IJ',,.h- 	W\(=S<T3UV;;##O$ $ 

 	--v/F/FGrB   N)rC   rD   rE   rF   rx   r   r   rG   rB   r@   ru   ru      s    5;.HrB   ru   c                   "    e Zd ZdZd Zd Zd Zy)TempUserOwnershipAPITestsz4Test owner flag behavior for temporary users via APIc                 H   | j                   j                  t        d      ddid      }|j                         }| j                   j	                  d|d           | j                   j                  t        d	      | j
                  d      }| j                  |j                  t        j                         t        j                  j                  |d
         }t        j                  j                  |      j                         }| j!                  |j"                         y)z;Test first BaZi record via authenticated API has owner=Truerg   r   zOwner Test UserrL   rM   rz   ri   r{   rK   rh   rl   
created_byN)r8   rS   r   rL   r   r9   rT   rU   r   rn   r   r<   rp   r   filterfirstro   owner)r?   temp_user_responser   r\   rP   first_records         r@   %test_first_bazi_record_owner_true_apiz?TempUserOwnershipAPITests.test_first_bazi_record_owner_true_api   s     "[[--*+&' . 

 ,002 	W^H=U<V3WX;;##O$ $ 

 	--v/F/FGLL$$y(A$B	~~,,	,BHHJ**+rB   c                    | j                   j                  t        d      ddid      }|j                         }| j                   j	                  d|d           | j                   j                  t        d	      | j
                  d      }| j                  |j                  t        j                         | j
                  j                         }d
|d<   | j                   j                  t        d	      |d      }| j                  |j                  t        j                         t        j                  j                  |d         }t        j                  j                  |      j!                  d      }| j                  |j#                         d       | j%                  |d   j&                         | j)                  |d   j&                         y)zCTest subsequent BaZi records via authenticated API have owner=Falserg   r   zMultiple Records TestrL   rM   rz   ri   r{   rK   zSecond API Personrh   rl   r   
created_at   r   r~   N)r8   rS   r   rL   r   r9   rT   rU   r   rn   copyr   r<   rp   r   r   order_byrR   ro   r   assertFalse)r?   r   r   r\   second_datarP   recordss          r@   ,test_subsequent_bazi_records_owner_false_apizFTempUserOwnershipAPITests.test_subsequent_bazi_records_owner_false_api  s    "[[--*+,- . 

 ,002 	W^H=U<V3WX ;;##O$ $ 

 	--v/F/FG ((--/1F;;##O$ $ 

 	--v/F/FGLL$$y(A$B	..''9'=FF|T!,
(()))*rB   c                    | j                   j                  t        d      ddid      }|j                         }| j                   j	                  d|d           | j                   j                  t        d	      | j
                  d      }| j                  |j                  t        j                         | j                   j                  t        d	      | j
                  d      }| j                  |j                  t        j                         t        j                  j                  |d
         }t        j                  j                  |      }| j                  |j                         d       | j                  |j                  d      j                         d       | j                  |j                  d      j                         d       y)z<Test multiple API records with same data are not overwrittenrg   r   zDuplicate Test UserrL   rM   rz   ri   r{   rK   rh   rl   r   r   T)r   r~   FN)r8   rS   r   rL   r   r9   rT   rU   r   rn   r   r<   rp   r   r   rR   )r?   r   r   r\   rP   r   s         r@   )test_multiple_api_records_not_overwrittenzCTempUserOwnershipAPITests.test_multiple_api_records_not_overwritten+  s    "[[--*+*+ . 

 ,002 	W^H=U<V3WX;;##O$ $ 

 	--v/F/FG ;;##O$ $ 

 	--v/F/FGLL$$y(A$B	..''9'= 	!,d399;Q?e4::<a@rB   N)rC   rD   rE   rF   r   r   r   rG   rB   r@   r   r      s    >,.$+L!ArB   r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TempUserRegistrationAPITestsz3Test temporary user registration/conversion via APIc                     | j                   j                  t        d      | j                  d   | j                  d   | j                  d   | j                  d   dd      }|j	                         S )	z3Helper to create temp user with data via direct APIrg   r   r   r   r    r   r   r   r    rL   rM   r8   rS   r   r9   rL   rw   s     r@   _create_temp_user_with_data_apiz<TempUserRegistrationAPITests._create_temp_user_with_data_apiR  t    ;;##*+**62,,X6"00>"00>	  $ 	
 }}rB   c                 h   | j                         }| j                  j                  d|d           ddd}| j                  j                  t	        d      |d	      }| j                  |j                  t        j                         |j                         }t        j                  j                  |d
         }| j                  |j                         | j                  |j                  d       | j                  |j                   d       | j#                  d|       | j#                  d|       y)z5Test temp user registration via API with minimal datarz   ri   r{   
9876543210converted@example.comr3   r4   api:temp_registerrL   rM   rh   rl   userrO   N)r   r8   r   rS   r   rT   rU   r   rV   rL   r   r<   rp   r   rq   r3   r4   rW   r?   r   register_datar\   r]   rP   s         r@   #test_temp_register_api_minimal_dataz@TempUserRegistrationAPITests.test_temp_register_api_minimal_data`  s   ==? 	W^H=U<V3WX ",

 ;;##'( $ 
 	--v/A/AB  LL$$y(A$B	445,7*AB 	fm,i/rB   c                    | j                         }| j                  j                  d|d           ddddd}| j                  j                  t	        d      |d	
      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       t        j                  j                  |d         }| j                  |j                  d             y)z1Test temp user registration via API with passwordrz   ri   r{   r   r   
newpass123)r3   r4   r5   	password2r   rL   rM   rj   rh   rl   N)r   r8   r   rS   r   rT   rU   r   rV   rL   rW   r   r<   rp   ro   check_passwordr   s         r@   $test_temp_register_api_with_passwordzATempUserRegistrationAPITests.test_temp_register_api_with_password  s    ==? 	W^H=U<V3WX ",$%	
 ;;##'( $ 
 	--v/A/AB  	h.i/ LL$$y(A$B		00>?rB   c                    | j                         }| j                  j                  d|d           dddddd	d
ddddd}| j                  j                  t	        d      |d      }| j                  |j                  t        j                         t        j                  j                  |d         }| j                  |j                  d       | j                  |j                  d       | j                  |j                  d       t        j                  j!                  |      \  }}| j                  |j"                  t%        ddd             | j                  |j&                  t)        ddd             | j                  |j*                  d       | j                  |j,                  t%        ddd             | j                  |j.                  t%        ddd             y)z<Test temp user registration via API with all optional fieldsrz   ri   r{   r   r   JohnDoer   r   r   r   z
1965-01-01z
1967-01-01)r   r    	twin_type
father_dob
mother_dobr3   r4   r6   r7   r   profiler   rL   rM   rh   rl   r   i  r~      i  i  N)r   r8   r   rS   r   rT   rU   r   rV   r   r<   rp   r6   r7   r   r   get_or_creater   r   r    r   r   r   r   )r?   r   r   r\   rP   r   createds          r@   +test_temp_register_api_with_optional_fieldszHTempUserRegistrationAPITests.test_temp_register_api_with_optional_fields  s   ==? 	W^H=U<V3WX ", *(**
 ;;##'( $ 
 	--v/A/AB LL$$y(A$B	--v6,,e4))3/ '..<<)<L++T$1-=>++T"a^<**A.++T$1-=>++T$1-=>rB   c                 J   | j                         }| j                  j                  d|d           dddddd	d
dddddd}| j                  j                  t	        d      |d      }| j                  |j                  t        j                         |j                         }| j                  d|       |d   }| j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  d|       |d   }| j                  |d   d	       | j                  |d   d
       | j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  |d          t        j                  j                  |d          }t        j                  j                  |!      }| j                  |j                   t#        d"d#d$             | j                  |j$                  t'        d%d&d'             | j                  |j(                  d       | j                  |j*                  t#        d(d)d*             | j                  |j,                  t#        d+d,d-             | j                  |j.                         y.)/z@Test comprehensive profile data update in temp user registrationrz   ri   r{   r   zprofile-test@example.comProfileTestr#   z
1995-06-15z14:30:00r~   z
1970-03-20z
1972-08-10F)r   r    r   r   r   can_regenerate_air   r   rL   rM   r   r6   r7   r   r   r   r    r   r   r   r   rh   rl   r   i              r   i        i     
   N)r   r8   r   rS   r   rT   rU   r   rV   rL   rW   r   r   r<   rp   r   r   r   r    r   r   r   r   r   )	r?   r   r   r\   r]   	user_dataprofile_datarP   r   s	            r@   /test_temp_register_comprehensive_profile_updatezLTempUserRegistrationAPITests.test_temp_register_comprehensive_profile_update  s   ==? 	W^H=U<V3WX "/#*(**%*
  ;;##'( $ 
 	--v/A/AB  	fm,!&)	<0)<;/88,c2 	i+ +l3\Bl3Z@k2A6l3\Bl3\B&9:; LL$$y(A$B	%%))y)9++T$2->?++T"b!_=**A.++T$2->?++T$2->?223rB   c                    | j                         }| j                  j                  d|d           ddddddd	}| j                  j                  t	        d
      |d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       | j                  |d          | j                  |d          | j                  d|d          t        j                  j                  |d         }| j                  |j!                  d             | j#                  |d   |d          | j#                  |d   |d          y)zDTest that providing password in temp register returns new JWT tokensrz   ri   r{   r   zpassword-test@example.comnewpassword123Passwordr   )r3   r4   r5   r   r6   r7   r   rL   rM   rj   znew authentication tokensrO   rh   rl   N)r   r8   r   rS   r   rT   rU   r   rV   rL   rW   assertIsNotNoner   r<   rp   ro   r   assertNotEqualr   s         r@   /test_temp_register_with_password_returns_tokenszLTempUserRegistrationAPITests.test_temp_register_with_password_returns_tokens	  sh   ==? 	W^H=U<V3WX "0()$
 ;;##'( $ 
 	--v/A/AB  	h.i/]845]956 	1=3KL LL$$y(A$B		001ABC 	M(3^H5MNM)4nY6OPrB   c                    | j                         }| j                  j                  d|d           ddddd}| j                  j                  t	        d	      |d
      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       | j                  d|d          | j                  d|       | j                  |d   d   d       y)zPTest that not providing password in temp register does not return new JWT tokensrz   ri   r{   r   zno-password-test@example.com
NoPasswordr   )r3   r4   r6   r7   r   rL   rM   rj   zauthentication tokensrO   r   r6   N)r   r8   r   rS   r   rT   rU   r   rV   rL   rX   rW   )r?   r   r   r\   r]   s        r@   -test_temp_register_without_password_no_tokenszJTempUserRegistrationAPITests.test_temp_register_without_password_no_tokens4  s    ==? 	W^H=U<V3WX "3&	
 ;;##'( $ 
 	--v/A/AB  	=1M2 	0-	2JK 	fm,v.|<lKrB   c                 2   | j                         }| j                  j                  d|d           dddddd	}| j                  j                  t	        d
      |d      }| j                  |j                  t        j                         |j                         }t        j                  j                  |d         }t        j                  j                  |      }| j                  |j                  d       | j                  |j                  t!        ddd             | j#                  |j$                         |d   }| j'                  d|       |d   }| j                  |d   d       | j                  |d   d       | j#                  |d          y)z5Test that partial profile data updates work correctlyrz   ri   r{   r   zpartial-profile@example.comr   z
1975-12-25)r   r   )r3   r4   r   r   rL   rM   rh   rl   r   i  r      r   r   r   r   r   N)r   r8   r   rS   r   rT   rU   r   rV   rL   r   r<   rp   r   r   r   r   assertIsNoner   rW   )	r?   r   r   r\   r]   rP   r   r   r   s	            r@   )test_temp_register_partial_profile_updatezFTempUserRegistrationAPITests.test_temp_register_partial_profile_updateW  s   ==? 	W^H=U<V3WX "2*
 ;;##'( $ 
 	--v/A/AB  LL$$y(A$B	%%))y)9**A.++T$B-?@',,- "&)	i+ +k2A6l3\B,|45rB   N)rC   rD   rE   rF   r   r   r   r   r   r   r   r   rG   rB   r@   r   r   O  s6    =0@@@+?Z:4x)QV!LF)6rB   r   c                       e Zd ZdZd Zd Zy)TempUserLoginMigrationAPITestszFTest data migration when temp user logs in to existing account via APIc                 Z   | j                   j                  t        d      | j                  d      }|j	                         }| j                   j                  d|d           | j                   j                  t        d      | j                  d      }| j                  |j                  t        j                         | j                   j                  t        d      | j                  d      }| j                  |j                  t        j                         d	d
d}| j                   j                  t        d      |d      }| j                  |j                  t        j                         |j	                         }| j                  d|       |d   }| j                  |d   d       | j                  |d   d       t        j                  j                  | j                         }	t"        j                  j                  | j                         }
| j                  |	j%                         d       | j                  |
j%                         d       | j'                  t(        j*                        5  t(        j                  j-                  |d          ddd       y# 1 sw Y   yxY w)z+Test temp user data migration via API loginrg   rL   rM   rz   ri   r{   rK   rb   r/   r1   r3   r5   api:temp_logintransfer_summarybazi_recordsr~   liuyao_recordsr   r   rh   rl   N)r8   rS   r   r9   rL   r   rT   rU   r   rn   r;   rV   rW   r   r<   r   r>   r   rR   assertRaisesr   DoesNotExistrp   )r?   r   r   bazi_responseliuyao_response
login_datar\   r]   transferr   r   s              r@   "test_temp_login_api_data_migrationzATempUserLoginMigrationAPITests.test_temp_login_api_data_migration  sU    "[[--*+ . 

 ,002 	W^H=U<V3WX ((O$ ) 

 	22F4K4KL ++**%&   + 

 	44f6M6MN "%


 ;;##$% $ 
 	--v/A/AB  	(-8 !34.115"23Q7 ~~,,8I8I,J..D4E4E.F++-q1--/3 t001LLy 9: 211s   4$J!!J*c           	         t         j                  j                  ddt        ddd      | j                  d       | j
                  j                  t        d      | j                  d	      }|j                         }| j
                  j                  d
|d           | j
                  j                  t        d      | j                  d	      }| j                  |j                  t        j                         ddd}| j
                  j                  t        d      |d	      }|j                         d   }| j                  |d          t         j                  j!                  | j                  d      }| j                  |j#                         d       t         j                  j!                  | j                  d      j%                         }| j'                  |j(                         y)z/Test owner flag handling in API login migrationzRegular User Recordr   i  r~   T)r   r   r   r   r   rg   rL   rM   rz   ri   r{   rK   r/   r1   r   r   r   owner_flag_cleared)r   r   r   )r   r   N)r   r<   creater   r>   r8   rS   r   r9   rL   r   rT   rU   r   rn   ro   r   rR   r   r   r   )	r?   r   r   r   r   r\   r   owner_recordsmigrated_records	            r@   -test_temp_login_api_owner_conflict_resolutionzLTempUserLoginMigrationAPITests.test_temp_login_api_owner_conflict_resolution  s    	&D!Q'(( 	 	
 "[[--*+ . 

 ,002 	W^H=U<V3WX((O$ ) 

 	22F4K4KL "%


 ;;##$% $ 
 ==?#56!567 --9J9JRV-W,,.2 !..//((" 0 
 %' 	 	../rB   N)rC   rD   rE   rF   r   r   rG   rB   r@   r   r     s    P;;z50rB   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TempUserAPIEdgeCasesTestsz,Test edge cases and error conditions for APIc                    | j                         }| j                  j                  d|d           ddd}| j                  j                  t	        d      |d	      }| j                  |j                  t        j                         | j                  d
|j                                | j                  d|j                                y)z(Test temp register API with invalid datarz   ri   r{   123zinvalid-emailr   r   rL   rM   r3   r4   N)r   r8   r   rS   r   rT   rU   r   HTTP_400_BAD_REQUESTrX   rL   rW   )r?   r   invalid_datar\   s       r@   #test_invalid_temp_register_api_dataz=TempUserAPIEdgeCasesTests.test_invalid_temp_register_api_data  s    ==?W^H=U<V3WX $

 ;;##'( $ 
 	--v/J/JK(--/2gx}}/rB   c                    | j                         }| j                  j                  d|d           ddd}| j                  j                  t	        d      |d	      }| j                  |j                  t        j                         y
)z/Test API registration with existing phone/emailrz   ri   r{   r/   r0   r   r   rL   rM   N)	r   r8   r   rS   r   rT   rU   r   r   )r?   r   duplicate_datar\   s       r@   +test_duplicate_phone_email_api_registrationzETempUserAPIEdgeCasesTests.test_duplicate_phone_email_api_registration  s    ==?W^H=U<V3WX "*

 ;;##'( $ 
 	--v/J/JKrB   c                     | j                   j                  t        d      dddd      }| j                  |j                  t
        j                         y)z=Test accessing temp user API endpoints without authenticationr   r   test@example.comr   rL   rM   N)r8   rS   r   rT   rU   r   HTTP_401_UNAUTHORIZEDrw   s     r@   test_unauthorized_api_accessz6TempUserAPIEdgeCasesTests.test_unauthorized_api_access'  sP     ;;##'(&89 $ 
 	--v/K/KLrB   c                     | j                   j                  | j                         | j                   j                  t	        d      dddd      }| j                  |j                  t        j                         y)	z1Test regular user trying to access temp user APIsr   r   r   r   r   rL   rM   N)	r8   force_authenticater>   rS   r   rT   rU   r   HTTP_403_FORBIDDENrw   s     r@   !test_regular_user_temp_api_accessz;TempUserAPIEdgeCasesTests.test_regular_user_temp_api_access2  sm     	&&D,=,=&> ;;##'(&89 $ 
 	--v/H/HIrB   c                     | j                   j                  t        d      | j                  d   | j                  d   | j                  d   | j                  d   dd      }|j	                         S 	z)Helper method for creating temp user datarg   r   r   r   r    r   rL   rM   r   rw   s     r@   r   z9TempUserAPIEdgeCasesTests._create_temp_user_with_data_apiA  r   rB   N)	rC   rD   rE   rF   r   r   r   r  r   rG   rB   r@   r   r     s!    60,L&	MJrB   r   c                       e Zd ZdZd Zd Zy)TempUserAPINormalFlowTestsz+Test normal user API flows are not affectedc                    | j                   j                  | j                         | j                   j                  t	        d      | j
                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       t        j                  j                  | j                        j                         }| j!                  |       | j                  |j"                  d       t$        j                  j                  d	      }| j                  |j'                         d
       y)z@Test regular logged-in user creating BaZi via API works normallyr   rK   rL   rM   rP   r   r   T)rq   r   N)r8   r   r>   rS   r   r9   rT   rU   r   rn   rL   rX   r   r<   r   r   r   r   r   rR   )r?   r\   r]   bazi_record
temp_userss        r@   #test_regular_user_api_bazi_creationz>TempUserAPINormalFlowTests.test_regular_user_api_bazi_creationS  s    	&&D,=,=&> ;;##O$ $ 
 	--v/F/FG  	m4 nn++t7H7H+IOOQ[)))+<= \\((4(@
))+Q/rB   c                 d   | j                   j                  t        d      dddd      }|j                         }| j                   j                  t        d      d|d   id      }| j	                  |j
                  t        j                         | j                  d	|j                                y
)z2Test regular user JWT token refresh works normallyz	api:loginr/   r1   r   rL   rM   r   rj   ri   N)	r8   rS   r   rL   rT   rU   r   rV   rW   )r?   login_responsetokensr   s       r@   #test_regular_user_api_token_refreshz>TempUserAPINormalFlowTests.test_regular_user_api_token_refreshn  s     ))K "> * 
  $$&  ;;++'(y)* , 
 	)55v7I7IJh 0 5 5 78rB   N)rC   rD   rE   rF   r
  r  rG   rB   r@   r  r  P  s    5069rB   r  c                   "    e Zd ZdZd Zd Zd Zy)TempUserAPISecurityTestsz+Test security aspects of temporary user APIc                    | j                   j                  t        d      ddid      }|j                         }| j                   j	                  d|d           | j                   j                  t        d	      | j
                  d      }| j                  |j                  t        j                         | j                   j	                          | j                   j                  t        d      dd
id      }|j                         }| j                   j	                  d|d           | j
                  j                         }d|d<   | j                   j                  t        d	      |d      }| j                  |j                  t        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 temp users can only access their own datarg   r   zFirst Temp UserrL   rM   rz   ri   r{   rK   zSecond Temp UserzSecond Temp User Recordr}   r~   r   r   N)r8   rS   r   rL   r   r9   rT   rU   r   rn   r   rp   rV   r   )
r?   temp1_response
temp1_databazi1_responsetemp2_response
temp2_datatemp2_bazi_databazi2_responser\   r}   s
             r@   test_temp_user_jwt_isolationz5TempUserAPISecurityTests.test_temp_user_jwt_isolation  s    ))*+&' * 

 $((*
 	WZ=Q<R3ST))O$ * 

 	33V5L5LM 	!))*+'( * 

 $((*
 	WZ=Q<R3ST,,113";))O$ * 

 	33V5L5LM 	WZ=Q<R3ST ;;??7?#;<--v/A/AB--/),Wq)F+->?rB   c                     | j                         }| j                  j                  d       | j                  j                  t	        d            }| j                  |j                  t        j                         y)z+Test behavior with expired temp user tokenszBearer invalid_token_123r{   rK   N)	r   r8   r   rp   r   rT   rU   r   r   )r?   r   r\   s      r@   $test_temp_user_token_expiry_handlingz=TempUserAPISecurityTests.test_temp_user_token_expiry_handling  s_     ==? 	3MN ;;??7?#;<--v/K/KLrB   c                     | j                   j                  t        d      | j                  d   | j                  d   | j                  d   | j                  d   dd      }|j	                         S r  r   rw   s     r@   r   z8TempUserAPISecurityTests._create_temp_user_with_data_api  r   rB   N)rC   rD   rE   rF   r  r  r   rG   rB   r@   r  r    s    50@dMrB   r  __main__)/rF   osdjangodjango.testr   r   rest_framework.testr   rest_frameworkr   django.urlsr   django.contrib.authr   django.contrib.auth.modelsr	   django.utilsr
   r   r   r   rL   uuidenviron
setdefaultmain.modelsr   accounts.modelsr   bazi.modelsr   liuyao.modelsr   accounts.utilsr   r   rI   ru   r   r   r   r   r  r  rC   unittestmainrG   rB   r@   <module>r1     s   
  ( ) !  . 4 ! ) )   

  .0A B 

  lM 2  '    0,
h ,
`]=6 ]=@:H5 :HzaA 7 aAHq6#: q6h	u0%< u0pR 7 Rj19!8 19hN6 Nb zHMMO rB   