o
    "4gB                     @   s4  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d Zejdd Zdd Z dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&d d! Z'd"d# Z(d$d% Z)d&d' Z*d(d) Z+d*d+ Z,d,d- Z-dS ).    )patch)get_user_model)AnonymousUser)reverseN)assertFormErrorassertRedirects)app_settings)EmailAddress)
user_emailuser_username)context)complete_social_login)SocialAccountSocialLogin)signupc                        fdd}|S )Nc                    sx     d}t |_t|$ t||}| j}|j D ]\}}|||< q|  |W  d    S 1 s5w   Y  d S )N/)	getr   userr   request_contextr   sessionitemssave)clientsocialloginrequestrespr   kvrequest_factory p/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/socialaccount/tests/test_signup.pyf   s   


$z!setup_sociallogin_flow.<locals>.fr!   )r    r#   r!   r   r"   setup_sociallogin_flow   s   r$   c                    r   )Nc           
         s   t  }| }t|d d}t|| |  tjj||ddd tddd}| }t||  t|| t||t|dgd	} 	d
}t
 |_t| t||}	W d    ||	fS 1 s_w   Y  ||	fS )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_emailr1   r   r   r   r   r   r!   r"   _email_address_clash&   s0   





z1email_address_clash.<locals>._email_address_clashr!   )r    r:   r!   r   r"   email_address_clash$   s   r;   c                 C   s   d| _ d | _tjj| _|dddd}||| t jjdi tj	di}t
jj||jjd s2J tjj|t|d s@J d S )	NTr&   Fr%   r(   email_verifiedr6   )r   r/   )r   r(   r!   )SOCIALACCOUNT_AUTO_SIGNUPACCOUNT_SIGNUP_FORM_CLASSaccount_settingsEmailVerificationMethodNONEACCOUNT_EMAIL_VERIFICATIONr   r4   r   USER_MODEL_USERNAME_FIELDr   filterr1   r/   existsr	   r
   )settingsdbr   r$   sociallogin_factoryr   r   r!   r!   r"   test_email_address_createdE   s   

 rJ   c                 C   s   d|_ d|_d|_d|_d|_|dd\}}|d tdks J d|_dd	d
|_t	| t
|}W d   n1 s=w   Y  |d dksJJ t jjdi tjd	i}t|dks_J dS )z%Test clash on both username and emailTr(   r%   r&   locationsocialaccount_signupPOSTotherother@example.comr6   r(   N/accounts/profile/r!   ACCOUNT_EMAIL_REQUIREDACCOUNT_UNIQUE_EMAILACCOUNT_USERNAME_REQUIREDACCOUNT_AUTHENTICATION_METHODr>   r   methodrM   r   r   r   r   r4   r   r@   USER_MODEL_EMAIL_FIELDr   )rH   r   rG   r;   r   r   r   r!   r!   r"   *test_email_address_clash_username_requiredX   s"   
rY   c                 C   s   d|_ d|_d|_d|_d|_|dd\}}|d tdks J d|_dd	i|_t	| t
|}W d
   n1 s<w   Y  |d dksIJ t jjdi tjd	i}t|dks^J d
S )z)Test clash while username is not requiredTFr(   r%   r&   rK   rL   rM   rO   NrQ   r!   rR   rH   rG   r;   r   r   r   r!   r!   r"   .test_email_address_clash_username_not_requiredp   s"   

r[   c                 C   sj   d|_ d|_d|_d|_d|_|dd\}}|d dksJ t jjdi tj	di}t
|dks3J d S )	NTFr(   r%   rO   rK   rQ   r!   )rS   rT   rU   rV   r>   r   r4   r   r@   rX   r   rZ   r!   r!   r"   -test_email_address_clash_username_auto_signup   s   r\   c                 C   s   d|_ g d|_d|_d|_d|_d|_|d}t |_t	 }| }t
|tjd t
|tjd tddd	}t||d
}t| t|| W d    n1 sRw   Y  |jjtjvs`J d S )NT)r6   	username1	username2r(   r3   r6   zusername@example.comr+   r,   r-   )r   r1   )rS   ACCOUNT_USERNAME_BLACKLISTrT   rU   rV   r>   r   r   r   r   setattrr@   rD   rX   r   r   r   r   r   r6   USERNAME_BLACKLIST)rH   rG   r    r   r7   r   r1   r   r!   r!   r"   #test_populate_username_in_blacklist   s,   

rb   c           	      C   s   d|_ d|_d|_d|_d|_d|_|dd}||| |td}|jd }|d 	 dks2J |j
tddd	id
}t|ddd t j d }t|dksTJ tjj|dddd sbJ tjj|d	ddd spJ dS )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.comr0   rL   formunverified@example.orgdatarQ   )fetch_redirect_responser   r'   N)rS   rC   rT   rU   rV   r>   r   r   r   valuepostr   r   r4   allr
   r	   rE   rF   )	rH   r   rG   rI   r$   r   r   rd   r   r!   r!   r"   $test_verified_email_change_at_signup   sD   



rl   c           
      C   s   d|_ d|_d|_d|_d|_d|_t }|ddd}||| |td}|j	d }|d 
 dks6J |jtddd	id
}t|td |j d }	t|	d	ksWJ tjj|	dddd seJ tjj|	d	ddd ssJ dS )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.
    Trc   Fr(   zunverified@example.comr(   r=   rL   rd   re   rf   account_email_verification_sentr   r'   N)rS   rC   rT   rU   rV   r>   r   r   r   r   ri   rj   r   r4   rk   r
   r	   rE   rF   )
rH   r   rG   rI   r$   r7   r   r   rd   r   r!   r!   r"   &test_unverified_email_change_at_signup   sJ   	


ro   c                 C   s   d|_ d|_d|_d|_d|_d|_d|_t }d}|jj	|d}t
jj	||dd |dd}||| |td}	|	jd	 }
|
d  |ksJJ |jtdd|id
}	t|	jd	 dd d S )NFTrc   r(   zme@example.comr0   )r(   r   r)   rL   rd   rf   zAn account already exists with this email address. Please sign in to that account first, then connect your Unittest Server account.)ACCOUNT_PREVENT_ENUMERATIONrS   rC   rT   rU   rV   r>   r   r4   r5   r	   r   r   r   ri   rj   r   )rH   r   rI   rG   r$   r7   r(   r   r   r   rd   r!   r!   r"   #test_unique_email_validation_signup  s,   


rq   c           	      C   s   d|_ d|_d|_d|_d|_d|_t }|ddd}||| |td}|j	d }|d 
 dks6J |jtdd	dd
d}|jdksIJ |j dksRJ tj dks[J |td}t|td dS )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.
    Trc   r(   Fzme1@example.comrm   rL   rd   me1rP   rf   i.     account_loginN)rS   rC   rT   rU   rV   r>   r   r   r   r   ri   rj   status_coder4   countr   r   )	rH   r   rI   rG   r$   r7   r   r   rd   r!   r!   r"   #test_social_account_taken_at_signup$  s*   

rw   c                 C   sx   d|_ d|_d|_d|_d|_d|_|dd}|||}|d tdks&J |tdddi}|d td	ks:J d
S )zgTests that when the email address is missing from the sociallogin email
    verification kicks in.
    TFr(   rc   
with_emailrK   rL   zother@example.orgrn   N)rS   rT   rU   rV   rC   r>   r   rj   )rH   rG   rI   r   r$   r   r   r!   r!   r"   4test_email_address_required_missing_from_socialloginH  s   

rz   c           
      C   s   d|_ d|_d|_d|_d|_d|_| }|dd}|||}	|	d tdks)J |tdd|ji}	|d j	d	ks=J |	d td
ksGJ dS )yTests that when an already existing email is given at the social signup
    form, enumeration preventation kicks in.
    TFr(   rc   rx   rK   rL   r   $[example.com] Account Already Existsrn   N)
rS   rT   rU   rV   rC   r>   r   rj   r(   subject)
rH   rG   user_factoryrI   r   r$   
mailoutboxr   r   r   r!   r!   r"   1test_email_address_conflict_at_social_signup_form]  s   

r   c           
      C   sl   d|_ d|_d|_d|_d|_d|_| }||jdd}|||}	|	d tdks+J |d jd	ks4J d
S )ztTests that when an already existing email is received from the provider,
    enumeration preventation kicks in.
    TFr(   rc   )r(   ry   rK   rn   r   r|   N)	rS   rT   rU   rV   rC   r>   r(   r   r}   
rH   rG   r~   rI   r   r   r$   r   r   r   r!   r!   r"   .test_email_address_conflict_during_auto_signup}  s   
r   c           
      C   s   d|_ d|_d|_d|_d|_d|_d|_|dd}||jdd}|||}	|	d tdks0J |	tddd	i}	|d
 j
dksCJ |	d |jksLJ tjj|jd dksZJ dS )r{   TFr(   optional)r=   rm   rK   rL   zother@email.orgr   z/[example.com] Please Confirm Your Email Addressr0   rs   N)rS   rT   rU   rV   rC   r>   "SOCIALACCOUNT_EMAIL_AUTHENTICATIONr(   r   rj   r}   LOGIN_REDIRECT_URLr	   r4   rE   rv   r   r!   r!   r"   5test_email_address_conflict_removes_conflicting_email  s   

 r   c                 C   sh   |dddd}t d}d|_|||}W d    n1 sw   Y  d|jv s*J t j r2J d S )Nr&   Fr%   r<   zLallauth.socialaccount.adapter.DefaultSocialAccountAdapter.is_open_for_signups   Sign Up Closed)r   return_valuecontentr   r4   rF   )rG   rH   r   r$   rI   r   iofsr   r!   r!   r"   test_signup_closed  s   r   ).unittest.mockr   django.contrib.authr   django.contrib.auth.modelsr   django.urlsr   pytestpytest_django.assertsr   r   allauth.accountr   r@   allauth.account.modelsr	   allauth.account.utilsr
   r   allauth.corer   allauth.socialaccount.helpersr   allauth.socialaccount.modelsr   r   allauth.socialaccount.viewsr   fixturer$   r;   rJ   rY   r[   r\   rb   rl   ro   rq   rw   rz   r   r   r   r   r!   r!   r!   r"   <module>   s>    

 ).$ #