
    fB                     D   d dl mZ d dlmZ d dlmZ d dlm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mZ d d
lmZ d dlmZ d dlmZmZ d dlmZ ej:                  d        Zej:                  d        Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-y)    )patch)get_user_model)AnonymousUser)reverseN)assertFormErrorassertRedirects)app_settings)EmailAddress)
user_emailuser_username)context)complete_social_login)SocialAccountSocialLogin)signupc                       fd}|S )Nc                 <   j                  d      }t               |_        t        j                  |      5  t        ||      }| j                  }|j                  j                         D ]
  \  }}|||<    |j                          |cd d d        S # 1 sw Y   y xY w)N/)	getr   userr   request_contextr   sessionitemssave)clientsocialloginrequestrespr   kvrequest_factorys          b/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/allauth/socialaccount/tests/test_signup.pyfz!setup_sociallogin_flow.<locals>.f   s    !%%c*$$$W- 	(+>DnnG--/ 1
LLN	 	 	s   ABB )r!   r#   s   ` r"   setup_sociallogin_flowr%      s    
 H    c                       fd}|S )Nc                    t               } |       }t        |d       d}t        ||       |j                          t        j
                  j                  ||dd       t        dd      } |       }t        ||        t        ||       t        ||t	        |      g	      }
j                  d
      }t               |_        t        j                  |      5  t        ||      }	d d d        ||	fS # 1 sw Y   |	fS xY w)Ntesttest@example.comTr   emailverifiedprimarytwitter123provideruidr,   )r   accountemail_addresses!/accounts/twitter/login/callback/)r   r   r   r   r
   objectscreater   r   r   r   r   r   r   r   )usernamer,   Userexi_userexi_user_emailr5   r   r   r   r   r!   s             r"   _email_address_clashz1email_address_clash.<locals>._email_address_clash&   s    6h'+8^,##$ 	$ 	

  >vdH%4!wE9R8S

 "%%&IJ$$$W- 	?(+>D	?}	?}s   C33C?r$   )r!   r>   s   ` r"   email_address_clashr?   $   s    8  r&   c                    d| _         d | _        t        j                  j                  | _         |ddd      } |||        t               j                  j                  di t        j                  di}t        j                  j                  ||j                  j                        j                         sJ t        j                  j                  |t!        |            j                         sJ y )	NTr*   Fr)   r,   email_verifiedr:   )r   r3   )r   r,   r$   )SOCIALACCOUNT_AUTO_SIGNUPACCOUNT_SIGNUP_FORM_CLASSaccount_settingsEmailVerificationMethodNONEACCOUNT_EMAIL_VERIFICATIONr   r8   r   USER_MODEL_USERNAME_FIELDr   filterr5   r3   existsr
   r   )settingsdbr   r%   sociallogin_factoryr   r   s          r"   test_email_address_createdrO   E   s     *.H&)-H&*:*R*R*W*WH'% K 6;/'>##'' 55v
>D   ''T{7J7J7N7N'OVVXXX&&D
48H&IPPRRRr&   c                    d|_         d|_        d|_        d|_        d|_         |dd      \  }}|d   t        d      k(  sJ d|_        dd	d
|_        t        j                  |      5  t        |      }ddd       |d   dk(  sJ  t               j                  j                  di t        j                  d	i}t!        |      dk(  sJ y# 1 sw Y   XxY w)z%Test clash on both username and emailTr,   r)   r*   locationsocialaccount_signupPOSTotherother@example.comr:   r,   N/accounts/profile/r$   ACCOUNT_EMAIL_REQUIREDACCOUNT_UNIQUE_EMAILACCOUNT_USERNAME_REQUIREDACCOUNT_AUTHENTICATION_METHODrC   r   methodrS   r   r   r   r   r8   r   rE   USER_MODEL_EMAIL_FIELDr   )rM   r   rL   r?   r   r   r   s          r"   *test_email_address_clash_username_requiredr_   X   s     '+H#$(H!)-H&-4H*)-H&'0BCMGT
w'=>>>> GN '2EFGL		 	 	) g
3333'>##'' 224G
HD '))) s   )CCc                    d|_         d|_        d|_        d|_        d|_         |dd      \  }}|d   t        d      k(  sJ d|_        dd	i|_        t        j                  |      5  t        |      }d
d
d
       |d   dk(  sJ  t               j                  j                  di t        j                  d	i}t!        |      dk7  sJ y
# 1 sw Y   XxY w)z)Test clash while username is not requiredTFr,   r)   r*   rQ   rR   rS   rU   NrW   r$   rX   rM   rL   r?   r   r   r   s         r"   .test_email_address_clash_username_not_requiredrb   p   s    &*H#$(H!).H&-4H*)-H&'0BCMGT
w'=>>>> GN01GL		 	 	) g
3333'>##'' 224G
HD &((( s   (CCc                     d|_         d|_        d|_        d|_        d|_         |dd      \  }}|d   dk(  sJ  t               j                  j                  di t        j                  di}t        |      dk7  sJ y )	NTFr,   r)   rU   rQ   rW   r$   )rY   rZ   r[   r\   rC   r   r8   r   rE   r^   r   ra   s         r"   -test_email_address_clash_username_auto_signuprd      s    &*H#$(H!).H&-4H*)-H&'0CDMGT
3333'>##'' 224G
HD &(((r&   c                    d|_         g d|_        d|_        d|_        d|_        d|_        |j                  d      }t               |_        t               } |       }t        |t        j                  d       t        |t        j                  d       t        dd	      }t        ||
      }t!        j"                  |      5  t%        ||       d d d        |j                  j&                  t        j(                  vsJ y # 1 sw Y   2xY w)NT)r:   	username1	username2r,   r7   r:   zusername@example.comr/   r0   r1   )r   r5   )rY   ACCOUNT_USERNAME_BLACKLISTrZ   r[   r\   rC   r   r   r   r   setattrrE   rI   r^   r   r   r   r   r   r:   USERNAME_BLACKLIST)rM   rL   r!   r   r;   r   r5   r   s           r"   #test_populate_username_in_blacklistrk      s    &*H#*PH'$(H!)-H&-4H*)-H&!!"EFG ?GLD6DD"<<jI// YE:G49K		 	 	) 4g{34 <<  (8(K(KKKK4 4s   DD
c                    d|_         d|_        d|_        d|_        d|_        d|_         |d      } |||       |j                  t        d            }|j                  d   }|d   j                         dk(  sJ |j                  t        d      dd	i
      }t        |dd       t               j                  j                         d   }t        |      dk(  sJ t         j                  j#                  |ddd      j%                         sJ t         j                  j#                  |d	dd      j%                         sJ y)z
    Test scenario for when the user changes email at social signup. Current
    behavior is that both the unverified and verified email are added, and
    that the user is allowed to pass because he did provide a verified one.
    T	mandatoryFr,   zverified@example.comr4   rR   formunverified@example.orgdatarW   )fetch_redirect_responser   r+   N)rY   rH   rZ   r[   r\   rC   r   r   r   valuepostr   r   r8   allr   r
   rJ   rK   )	rM   r   rL   rN   r%   r   r   rn   r   s	            r"   $test_verified_email_change_at_signuprv      sg    '+H#*5H'$(H!).H&-4H*).H&%,BCK6;/::g456D<<D= $::::;;&'/0  D D.N##'')!,Dd5555&&$	 ' 
 fh   &&&	 ' 
 fh  r&   c                    d|_         d|_        d|_        d|_        d|_        d|_        t               } |dd      } |||       |j                  t        d            }|j                  d   }|d   j                         dk(  sJ |j                  t        d      dd	i
      }t        |t        d             |j                  j                         d   }	t        |	      d	k(  sJ t         j                  j#                  |	ddd      j%                         sJ t         j                  j#                  |	d	dd      j%                         sJ y)z
    Test scenario for when the user changes email at social signup, while
    his provider did not provide a verified email. In that case, email
    verification will kick in. Here, both email addresses are added as
    well.
    Trm   Fr,   zunverified@example.comr,   rB   rR   rn   ro   rp   account_email_verification_sentr   r+   N)rY   rH   rZ   r[   r\   rC   r   r   r   r   rs   rt   r   r8   ru   r   r
   rJ   rK   )
rM   r   rL   rN   r%   r;   r   r   rn   r   s
             r"   &test_unverified_email_change_at_signuprz      ss    '+H#*5H'$(H!).H&-4H*).H&D%&uK 6;/::g456D<<D= $<<<<;;&'/0  D
 D'"CDE<<a Dd7777&&&	 ' 
 fh   &&&	 ' 
 fh  r&   c                    d|_         d|_        d|_        d|_        d|_        d|_        d|_        t               }d}|j                  j                  |      }t        j                  j                  ||d        |d      } |||       |j                  t        d            }	|	j                  d	   }
|
d   j                         |k(  sJ |j                  t        d      d|i
      }	t!        |	j                  d	   dd       y )NFTrm   r,   zme@example.comr4   )r,   r   r-   rR   rn   rp   zAn account already exists with this email address. Please sign in to that account first, then connect your Unittest Server account.)ACCOUNT_PREVENT_ENUMERATIONrY   rH   rZ   r[   r\   rC   r   r8   r9   r
   r   r   r   rs   rt   r   )rM   r   rN   rL   r%   r;   r,   r   r   r   rn   s              r"   #test_unique_email_validation_signupr}     s    ,1H(&*H#*5H'$(H!).H&-4H*).H&DE<<U+De$F%,<=K6;/::g456D<<D= E)));;w56gu=M;NDV	)r&   c                 V   d|_         d|_        d|_        d|_        d|_        d|_        t               } |dd      } |||       |j                  t        d            }|j                  d   }|d   j                         dk(  sJ |j                  t        d      d	dd
      }|j                  dk(  sJ |j                  j                         dk(  sJ t        j                  j                         dk(  sJ |j                  t        d            }t!        |t        d             y)a  
    Test scenario for when the user signs up with a social account
    and uses email address in that social account. But upon seeing the
    verification screen, they realize that email address is somehow
    unusable for them, and so backs up and enters a different email
    address (and is forced to choose a new username) while providing
    the same social account token which is owned by their first attempt.
    Trm   r,   Fzme1@example.comrx   rR   rn   me1rV   rp   i.     account_loginN)rY   rH   rZ   r[   r\   rC   r   r   r   r   rs   rt   status_coder8   countr   r   )	rM   r   rN   rL   r%   r;   r   r   rn   s	            r"   #test_social_account_taken_at_signupr   $  s'    '+H#*5H'$(H!)-H&-4H*).H&D%,=eTK6;/::g456D<<D= $5555;;&'*;<  D s"""<<1$$$  &&(A---::g456DD'/23r&   c                    d|_         d|_        d|_        d|_        d|_        d|_         |d      } |||      }|d   t        d      k(  sJ |j                  t        d      ddi      }|d   t        d	      k(  sJ y
)zgTests that when the email address is missing from the sociallogin email
    verification kicks in.
    TFr,   rm   
with_emailrQ   rR   zother@example.orgry   N)rY   rZ   r[   r\   rH   rC   r   rt   )rM   rL   rN   r   r%   r   r   s          r"   4test_email_address_required_missing_from_socialloginr   H  s     '+H#$(H!).H&-4H**5H')-H&%7K!&+6D
w'=>>>>;;w56BU8VWD
w'HIIIIr&   c                 L   d|_         d|_        d|_        d|_        d|_        d|_         |       } |d      } |||      }	|	d   t        d      k(  sJ |j                  t        d      d|j                  i      }	|d   j                  d	k(  sJ |	d   t        d
      k(  sJ y)yTests that when an already existing email is given at the social signup
    form, enumeration preventation kicks in.
    TFr,   rm   r   rQ   rR   r   $[example.com] Account Already Existsry   N)
rY   rZ   r[   r\   rH   rC   r   rt   r,   subject)
rM   rL   user_factoryrN   r   r%   
mailoutboxr   r   r   s
             r"   1test_email_address_conflict_at_social_signup_formr   ]  s     '+H#$(H!).H&-4H**5H')-H&>D%7K!&+6D
w'=>>>> ;;w56$**8MNDa=  $JJJJ
w'HIIIIr&   c                     d|_         d|_        d|_        d|_        d|_        d|_         |       } ||j                  d      } |||      }	|	d   t        d      k(  sJ |d   j                  d	k(  sJ y
)ztTests that when an already existing email is received from the provider,
    enumeration preventation kicks in.
    TFr,   rm   )r,   r   rQ   ry   r   r   N)	rY   rZ   r[   r\   rH   rC   r,   r   r   
rM   rL   r   rN   r   r   r%   r   r   r   s
             r"   .test_email_address_conflict_during_auto_signupr   }  s     '+H#$(H!).H&-4H**5H')-H&>D%DJJ4HK!&+6D
w'HIIIIa=  $JJJJr&   c                    d|_         d|_        d|_        d|_        d|_        d|_        d|_         |d      } ||j                  d      } |||      }	|	d   t        d      k(  sJ |j                  t        d      dd	i      }	|d
   j                  dk(  sJ |	d   |j                  k(  sJ t        j                  j                  |j                        j                         dk(  sJ y)r   TFr,   optional)rB   rx   rQ   rR   zother@email.orgr   z/[example.com] Please Confirm Your Email Addressr4   r   N)rY   rZ   r[   r\   rH   rC   "SOCIALACCOUNT_EMAIL_AUTHENTICATIONr,   r   rt   r   LOGIN_REDIRECT_URLr
   r8   rJ   r   r   s
             r"   5test_email_address_conflict_removes_conflicting_emailr     s     '+H#$(H!).H&-4H**4H')-H&27H/u-D%DJJuMK!&+6D 
w'=>>>> ;;w56BS8TUDa=  $UUUU
x:::::&&TZZ&8>>@AEEEr&   c                      |ddd      }t        d      5 }d|_         |||      }d d d        dj                  v sJ t               j                  j                         rJ y # 1 sw Y   >xY w)Nr*   Fr)   rA   zLallauth.socialaccount.adapter.DefaultSocialAccountAdapter.is_open_for_signups   Sign Up Closed)r   return_valuecontentr   r8   rK   )rL   rM   r   r%   rN   r   iofsr   s           r"   test_signup_closedr     s     & K 
V
 ;	!%fk:	;
 ,,,''..0000; ;s   A%%A.).unittest.mockr   django.contrib.authr   django.contrib.auth.modelsr   django.urlsr   pytestpytest_django.assertsr   r   allauth.accountr	   rE   allauth.account.modelsr
   allauth.account.utilsr   r   allauth.corer   allauth.socialaccount.helpersr   allauth.socialaccount.modelsr   r   allauth.socialaccount.viewsr   fixturer%   r?   rO   r_   rb   rd   rk   rv   rz   r}   r   r   r   r   r   r   r$   r&   r"   <module>r      s     . 4   B < / ;   ? C .       @S&*0),)L6&R+\:!4HJ*J@K4 FF1r&   