
    P1iII                     "   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mZmZ ddlZddlZej(                  j+                  d
d       ej(                  j+                  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 ddl 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*jV                          yy)$z
Comprehensive unit tests for temporary user web flows.
Tests all the scenarios for temporary user creation, data management, and conversion.
    N)TestCaseClient)reverse)get_user_model)AnonymousUser)Session)timezone)datetimedatetimeDJANGO_SETTINGS_MODULEziching.settings
DJANGO_ENVdevelopment)User)Person)liuyao)UserProfile)create_temporary_userc                       e Zd ZdZd Zy)TempUserWebFlowTestCasez=Base test case with common setup for temporary user web flowsc           	         t               | _        | j                  j                  }t	        t        |            dd }d| | _        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| _        t        j                  j                  | j                  d| dddd      | _        y)zSet up test dataN99Test PersonM
1990-01-0112:00:00z
Test notes)namegender
birth_date
birth_timenotesTest Number PersonFz
1995-05-15z08:30r   z
1960-03-20z
1965-07-10)r   r   r    r!   	twin_type
father_dob
mother_dobTest question00001)questiony1y2y3y4y5y6qdateregular@example.comtestpass123Regularr   )phoneemailpassword
first_name	last_name)r   client	__class____name__strhash
test_phone	bazi_datanumber_datar	   now	isoformatliuyao_datar   objectscreate_userregular_user)self
class_namephone_suffixs      S/home/cursorai/projects/iching/iching/tests/../../tests/test_temp_user_web_flows.pysetUpzTempUserWebFlowTestCase.setUp$   s    h ^^,,
4
+,RS1|n- "&$!
 )&!&&
 (\\^--/	
 !LL44//L>6"  5 
    N)r?   
__module____qualname____doc__rO    rP   rN   r   r   !   s
    G0
rP   r   c                   "    e Zd ZdZd Zd Zd Zy)TempUserCreationWebTestsz*Test temporary user creation via web formsc                    | j                  t        j                  j                         d       | j                  j                  t        d      | j                        }| j                  t        j                  j                         d       t        j                  j                  d      j                         }| j                  |       | j                  |j                  j                  d             t        j                  j                  |      j                         }| j                  |       | j                  |j                  d       | j                  |j                          y	)
z5Test that creating BaZi record creates temporary user   bazi:calculate_chart   Tis_temporary_usertemp_
created_byr   N)assertEqualr   rH   countr=   postr   rC   filterfirstassertIsNotNone
assertTruer8   
startswithr   r   owner)rK   response	temp_userbazi_persons       rN   test_bazi_create_temp_userz3TempUserCreationWebTests.test_bazi_create_temp_user[   s    	++-q1 ;;##G,B$CT^^T 	++-q1LL''$'?EEG	Y'	227;< nn++y+AGGI[)))=9))*rP   c                    | j                  t        j                  j                         d       | j                  j                  t        d      | j                        }| j                  t        j                  j                         d       t        j                  j                  d      j                         }| j                  |       t        j                  j                  |      j                         }| j                  |       | j                  |j                  d       | j                  |j                         y)	z7Test that creating Number record creates temporary userrX   number:calculaterZ   Tr[   r^   r#   N)r`   r   rH   ra   r=   rb   r   rD   rc   rd   re   r   r   rf   rh   )rK   ri   rj   number_persons       rN   test_number_create_temp_userz5TempUserCreationWebTests.test_number_create_temp_userq   s     	++-q1 ;;##G,>$?AQAQR 	++-q1LL''$'?EEG	Y' ---CIIK]+++-AB++,rP   c           	         t         j                  j                         }t        j                  j                         }ddddddddd}| j                  j                  dt        j                  |      d	      }| j                  |j                  d
       |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  t         j                  j                         |       | j                  t        j                  j                         |       y)zETest that LiuYao API only does calculation for unauthenticated users.r(   r)   r*   r+   T)r,   r-   r.   r/   r0   r1   r2   usecurz/api/liuyao/calc/zapplication/json)datacontent_type   guar,   rj   idN)r   rH   ra   r   r=   rb   jsondumpsr`   status_codeassertInassertNotIn)rK   initial_user_countinitial_liuyao_countrG   ri   response_datas         rN   ,test_liuyao_unauthenticated_calculation_onlyzETempUserCreationWebTests.test_liuyao_unauthenticated_calculation_only   s   !\\//1%~~335 (	
 ;;##$7(,

;(?0B $ D
 	--s3  	e]+j-0m4}- 	++-/AB--/1EFrP   N)r?   rQ   rR   rS   rl   rp   r   rT   rP   rN   rV   rV   X   s    4+,-&"GrP   rV   c                   (    e Zd ZdZd Zd Zd Zd Zy)TempUserOwnershipWebTestsz,Test owner flag behavior for temporary usersc                 P   | j                   j                  t        d      | j                        }t        j
                  j                  d      j                         }t        j
                  j                  |      j                         }| j                  |j                         y)z%Test first BaZi record has owner=TruerY   Tr[   r^   N)r=   rb   r   rC   r   rH   rc   rd   r   rf   rh   )rK   ri   rj   first_records       rN   !test_first_bazi_record_owner_truez;TempUserOwnershipWebTests.test_first_bazi_record_owner_true   sv     ;;##G,B$CT^^TLL''$'?EEG	~~,,	,BHHJ**+rP   c                 \   | j                   j                  t        d      | j                         | j                  j	                         }d|d<   | j                   j                  t        d      |       t
        j                  j                  d      j                         }t        j                  j                  |      j                  d      }| j                  |j                         d       | j                  |d	   j                         | j                  |d
   j                         y)z-Test subsequent BaZi records have owner=FalserY   zSecond Personr   Tr[   r^   
created_atrZ   r   rX   N)r=   rb   r   rC   copyr   rH   rc   rd   r   order_byr`   ra   rf   rh   assertFalse)rK   second_datarj   recordss       rN   (test_subsequent_bazi_records_owner_falsezBTempUserOwnershipWebTests.test_subsequent_bazi_records_owner_false   s     	!78$..I nn))+-F!78+FLL''$'?EEG	..''9'=FF|T!,
(()))*rP   c                      y)z@Test when LiuYao created first, then BaZi - BaZi should be ownerNrT   rK   s    rN   *test_liuyao_first_then_bazi_owner_behaviorzDTempUserOwnershipWebTests.test_liuyao_first_then_bazi_owner_behavior   s     	rP   c                 \   | j                   j                  t        d      | j                         | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }t        j
                  j                  |      }| j                  |j                         d       | j                  |j                  d      j                         d       | j                  |j                  d      j                         d       y	)
z8Test multiple records with same data are not overwrittenrY   Tr[   r^   rZ   )rh   rX   FN)r=   rb   r   rC   r   rH   rc   rd   r   r`   ra   rf   )rK   rj   r   s      rN   /test_multiple_records_same_data_not_overwrittenzITempUserOwnershipWebTests.test_multiple_records_same_data_not_overwritten   s     	!78$..I 	!78$..ILL''$'?EEG	..''9'= 	!,T288:A>U399;Q?rP   N)r?   rQ   rR   rS   r   r   r   r   rT   rP   rN   r   r      s    6,+"@rP   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TempUserRedirectionWebTestsz-Test redirection behavior for temporary usersc                 P    t               }| j                  j                  |       |S )z*Helper to create temp user and set session)r   r=   force_login)rK   rj   s     rN   _create_temp_user_sessionz5TempUserRedirectionWebTests._create_temp_user_session   s"    )+		*rP   c                     | j                         }| j                  j                  t        d            }| j	                  |t        d             y)z@Test temp user accessing profile edit redirects to temp-registeraccounts:profile_editaccounts:temp_registerNr   r=   getr   assertRedirectsrK   rj   ri   s      rN   $test_temp_user_profile_edit_redirectz@TempUserRedirectionWebTests.test_temp_user_profile_edit_redirect   s>    224	;;??7+B#CDXw/G'HIrP   c                     | j                         }| j                  j                  t        d            }| j	                  |t        d             y)z'Test temp user logout redirects to homezaccounts:logoutz
main:indexN)r   r=   rb   r   r   r   s      rN   test_temp_user_logout_redirectz:TempUserRedirectionWebTests.test_temp_user_logout_redirect   s?    224	;;##G,=$>?Xw|'<=rP   c                     | j                         }| j                  j                  t        d            }| j	                  |t        d             y)z<Test temp user accessing register redirects to temp-registerzaccounts:registerr   Nr   r   s      rN    test_temp_user_register_redirectz<TempUserRedirectionWebTests.test_temp_user_register_redirect   s>    224	;;??7+>#?@Xw/G'HIrP   N)r?   rQ   rR   rS   r   r   r   r   rT   rP   rN   r   r      s    7J>JrP   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TempUserRegistrationWebTestsz+Test temporary user registration/conversionc                     | j                   j                  t        d      | j                         t        j
                  j                  d      j                         S )z)Helper to create temp user with some datarY   Tr[   )r=   rb   r   rC   r   rH   rc   rd   r   s    rN   _create_temp_user_with_dataz8TempUserRegistrationWebTests._create_temp_user_with_data  sC     	!78$..I||""T":@@BBrP   c                 >   | j                         }ddd}| j                  j                  t        d      |      }|j	                          | j                  |j                         | j                  |j                  d       | j                  |j                  d       y)z@Test temp user registration with minimal data (phone/email only)
9876543210converted@example.comr8   r9   r   N)
r   r=   rb   r   refresh_from_dbr   r\   r`   r8   r9   rK   rj   register_datari   s       rN   test_temp_register_minimal_dataz<TempUserRegistrationWebTests.test_temp_register_minimal_data	  s    446	 ",

 ;;##G,D$E}U 	!!#445,7*ABrP   c                    | j                         }ddddd}| j                  j                  t        d      |      }|j	                          | j                  |j                         | j                  |j                  d             y)z)Test temp user registration with passwordr   r   
newpass123)r8   r9   r:   	password2r   N)	r   r=   rb   r   r   r   r\   rf   check_passwordr   s       rN    test_temp_register_with_passwordz=TempUserRegistrationWebTests.test_temp_register_with_password  s|    446	 ",$%	
 ;;##G,D$E}U 	!!#445	00>?rP   c           	         | j                         }ddddddddd	}| j                  j                  t        d
      |      }|j	                          | j                  |j                         | j                  |j                  d       | j                  |j                  d       |j                  }| j                  |j                  j                  d      d       | j                  |j                  j                  d      d       y)z0Test temp user registration with optional fieldsr   r   JohnDoer   r   r   r)   )r8   r9   r;   r<   r   r    r!   r%   r   z%Y-%m-%dz%H:%M:%SN)r   r=   rb   r   r   r   r\   r`   r;   r<   profiler    strftimer!   )rK   rj   r   ri   r   s        rN   'test_temp_register_with_optional_fieldszDTempUserRegistrationWebTests.test_temp_register_with_optional_fields,  s    446	 ", &$	
 ;;##G,D$E}U 	!!#445--v6,,e4 ##++44Z@,O++44Z@*MrP   N)r?   rQ   rR   rS   r   r   r   r   rT   rP   rN   r   r      s    5CC"@$NrP   r   c                       e Zd ZdZd Zy)TempUserLoginMigrationWebTestsz>Test data migration when temp user logs in to existing accountc                    | j                   j                  t        d      | j                        }t        j
                  j                  d      j                         }| j                  |       t        j
                  j                  |      j                         }| j                  |d       | j                  dd}| j                   j                  t        d      |      }| j                  |j                  d	       t        j
                  j                  | j                        }| j                  |j                         d       t        j
                  j                  |
      }| j                  |j                         d       y)zATest that temp user data migrates when logging in as regular userrY   Tr[   r^   rX   r6   )usernamer:   zaccounts:login.  )userr   N)r=   rb   r   rC   r   rH   rc   rd   re   r   ra   r`   rB   rz   rJ   r   )rK   ri   rj   initial_bazi_count
login_databazi_records	temp_bazis          rN   test_login_data_migrationz8TempUserLoginMigrationWebTests.test_login_data_migrationL  s5    ;;##G,B$CT^^TLL''$'?EEG	Y' $^^22i2HNNP+Q/ %

 ;;##G,<$=zJ--s3 ~~,,8I8I,J++-q1 NN))y)9	*A.rP   N)r?   rQ   rR   rS   r   rT   rP   rN   r   r   I  s
    H/rP   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TempUserEdgeCasesWebTestsz$Test edge cases and error conditionsc                    | j                   j                  t        d      | j                         ddd}| j                   j                  t        d      |      }| j	                  |j
                  d       | j                  d|j                  j                         v xs d|j                  j                         v d	       y
)z$Test temp register with invalid datarY   123zinvalid-emailr   r   ru   u   手机号码只能包含数字zEnter a valid email addressz(Should contain validation error messagesN)	r=   rb   r   rC   r`   rz   rf   contentdecode)rK   invalid_datari   s      rN   test_invalid_temp_register_dataz9TempUserEdgeCasesWebTests.test_invalid_temp_register_datak  s     	!78$..I $

 ;;##G,D$E|T 	--s3,0@0@0G0G0II G)X-=-=-D-D-FF6	
rP   c                    | j                   j                  t        d      | j                         | j                  dt        t        | j                  j                              dd  dd}| j                   j                  t        d      |      }| j                  |j                  d       | j                  d	|j                  j                         v d
       y)z+Test registration with existing phone/emailrY   r4   r   Nr5   r   r   ru   u   已被使用z1Should contain duplicate validation error message)r=   rb   r   rC   rB   r@   rA   r>   r?   r`   rz   rf   r   r   )rK   duplicate_datari   s      rN   'test_duplicate_phone_email_registrationzATempUserEdgeCasesWebTests.test_duplicate_phone_email_registration  s     	!78$..I __s4(?(?#@A"#FG|T

 ;;##G,D$E~V 	--s3h..5577?	
rP   c                     | j                   j                  t        d            }| j                  |j                  g d       y)z8Test accessing temp user features without proper sessionr   )r   i  i  N)r=   r   r   r{   rz   rK   ri   s     rN   %test_temp_user_access_without_sessionz?TempUserEdgeCasesWebTests.test_temp_user_access_without_session  s2     ;;??7+C#DE 	h**O<rP   c                    | j                   j                  t        d      | j                         t        j
                  j                  d      j                         }| j                   j                  j                          | j                  j                         }d|d<   | j                   j                  t        d      |       t        j
                  j                  d      }| j                  |j                         d       y)	z6Test behavior with multiple temp users in same sessionrY   Tr[   zSecond Tempr   rn   rZ   N)r=   rb   r   rC   r   rH   rc   rd   sessionflushrD   r   r`   ra   )rK   
first_temprD   
temp_userss       rN   %test_multiple_temp_users_same_sessionz?TempUserEdgeCasesWebTests.test_multiple_temp_users_same_session  s     	!78$..I\\((4(@FFH
 	!!#&&++-+F!34kB \\((4(@
))+Q/rP   N)r?   rQ   rR   rS   r   r   r   r   rT   rP   rN   r   r   h  s    .
,
*=0rP   r   c                       e Zd ZdZd Zd Zy)TempUserNormalFlowWebTestsz'Test normal user flows are not affectedc                    | j                   j                  | j                         | j                   j                  t	        d      | j
                        }t        j                  j                  | j                        j                         }| j                  |       | j                  |j                  d       t        j                  j                  d      }| j                  |j                         d       y)z8Test regular logged-in user creating BaZi works normallyrY   r^   r   Tr[   r   N)r=   r   rJ   rb   r   rC   r   rH   rc   rd   re   r`   r   r   ra   )rK   ri   bazi_recordr   s       rN   test_regular_user_bazi_creationz:TempUserNormalFlowWebTests.test_regular_user_bazi_creation  s     	 1 12 ;;##G,B$CT^^T nn++t7H7H+IOOQ[)))=9 \\((4(@
))+Q/rP   c                 4   | j                   j                  | j                         | j                   j                  t	        d            }| j                  |j                  d       | j                  t        |d      r|j                  ndt	        d             y)z-Test regular user can access profile normallyr   ru   urlNr   )
r=   r   rJ   r   r   r`   rz   assertNotEqualhasattrr   r   s     rN    test_regular_user_profile_accessz;TempUserNormalFlowWebTests.test_regular_user_profile_access  sv     	 1 12 ;;??7+B#CD 	--s3GHe,DHLL$"#;<	>rP   N)r?   rQ   rR   rS   r   r   rT   rP   rN   r   r     s    10">rP   r   __main__),rS   osdjangodjango.testr   r   django.urlsr   django.contrib.authr   django.contrib.auth.modelsr   django.contrib.sessions.modelsr   django.utilsr	   r
   r   r   rx   environ
setdefaultmain.modelsr   bazi.modelsr   liuyao.modelsr   accounts.modelsr   accounts.utilsr   uuidr   rV   r   r   r   r   r   r   r?   unittestmainrT   rP   rN   <module>r      s   
  (  . 4 2 ! ) )   

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

  lM 2     ' 1 3
h 3
nNG6 NGb1@ 7 1@h J"9  JFFN#: FNR/%< />D0 7 D0N>!8 >D zHMMO rP   