o
    "4gP2                     @   s0  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 d dlZd dlmZmZmZ d d	lmZ d d
lmZ d dlmZmZmZ d dlmZ G dd deZejdddejfdddejfg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 ))    )	timedelta)Mock)SESSION_KEYget_user_model)cache)reverse)nowN)assertRedirectsassertTemplateNotUsedassertTemplateUsed)app_settings)DefaultAccountAdapter)EmailAddressEmailConfirmationEmailConfirmationHMAC)user_logged_inc                   @   s   e Zd ZdZdd ZdS )TestEmailVerificationAdapter/foobarc                 C   s   | j S )N)SIGNUP_REDIRECT_URL)selfrequest r   v/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/account/tests/test_email_verification.pyget_signup_redirect_url   s   z4TestEmailVerificationAdapter.get_signup_redirect_urlN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   zadapter,query,expected_location )Nz
?next=/fooz/foozJallauth.account.tests.test_email_verification.TestEmailVerificationAdapterc                 C   s  t jj|_d|_d|_| r| |_| }|jtddd||dd}|j	dks(J |d td	ks2J |
|d }|j	d
ks@J tjj
dd}	t|	j}
t }t| |td|
gd| }|d |kshJ tjj
|	jd}	|	jsuJ |jt |j||	jtd t| d S )NTaccount_signupjohnza@a.comusernameemail	password1	password2)data.  locationaccount_email_verification_sent   r"   account_confirm_emailargspk)senderr   responseusersignal)r   EmailVerificationMethod	MANDATORYACCOUNT_EMAIL_VERIFICATIONACCOUNT_EMAIL_CONFIRMATION_HMAC#ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATIONACCOUNT_ADAPTERpostr   status_codegetr   objectsr   keyr   r   connectr/   verifiedassert_called_once_withr   wsgi_requestr2   
disconnect)adapterclientdbqueryexpected_locationpassword_factorysettingspasswordrespr"   r>   receiver_mockr   r   r   test_login_on_verification!   sD   
	


rN   c                 C   sp   d| _ |ddd |ddd}tj||j}|jrJ tjj|dt d}|	t
d|jgd}t|d	 d S )
NFTzfoo@bar.org)email_verifiedr"   dummy)email_addressr>   sentr+   r,   z.account/messages/email_confirmation_failed.txt)r7   r   r=   get_for_userr"   r@   r   creater   r:   r   r>   r   )rJ   user_factoryrE   unverified_userrQ   confirmationrL   r   r   r   test_email_verification_failed[   s   
rX   c                 C   s  d| _ d| _tjj| _|jtdddddddd}|jd	ks!J |d
 j	dgks+J |d
 j
dd
ks7J t|dks?J t|dtj  dD ]A}|jtdddddd}t jjddd seJ t|dtj  t||ksuJ tjjdd |ksJ t  |  qItjjddd d  }|td|jgd}t|dtj  |td|jgd |tdddd}t|| jdd d S )NF
   r   johndoejohn@example.comr    T)followr)   r   http://   zaccount/verification_sent.%s)r^      account_loginloginrK   )r!   	is_activezaccount/verification_sent.)email_address__email)email_address__user__usernamer+   r,   zaccount/email_confirm.%sfetch_redirect_response)r7   #ACCOUNT_EMAIL_CONFIRMATION_COOLDOWNr   r4   r5   r6   r:   r   r;   tobodyfindlenr   TEMPLATE_EXTENSIONr   r=   filterexistsr   countr   clearlogoutr<   r>   r	   LOGIN_REDIRECT_URL)rJ   rF   rE   
mailoutboxenable_cacherL   attemptrW   r   r   r   !test_email_verification_mandatoryl   st   

	

rw   c                 C   s   d| _ tjj| _d| _|td |tddddd}t	|| j dd |d j
dgks0J t|d	ks8J |  tjjt td	d
 d |tdddd}t	|| jdd |d j
dgksfJ t|d	ksnJ d S )Nz/accounts/welcome/Fr   rZ   r[   )r!   r"   r#   rf   r   r^   )days)rR   r`   ra   )ACCOUNT_SIGNUP_REDIRECT_URLr   r4   OPTIONALr6   #ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICEr<   r   r:   r	   ri   rl   rr   r   r=   updater   r   rs   )rJ   rE   rF   rt   rL   r   r   r    test_optional_email_verification   s2   
	r}   c           	      C   s   d| _ |dd}tj||j}t|}|d}|j|d t|dks'J |	t
d|jgd tjj|jd	}|js?J d S )
NTFrO   /r   r^   r+   r,   r.   )r7   r   r=   rS   r"   r   r<   sendrl   r:   r   r>   r/   r@   )	rJ   rE   rU   rt   rfr2   r"   rW   r   r   r   r   test_email_verification_hmac   s   

r   c           	      C   s   d| _ d| _|dd}tj||j}t|}|d}|j|d t	|dks*J |
td|jgd	 tjj|jd
}|jrBJ d S )NTr   Fr~   r   r   r^   r+   r,   r.   )r7   &ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYSr   r=   rS   r"   r   r<   r   rl   r:   r   r>   r/   r@   )	rJ   rU   rE   rt   r   r2   r"   rW   r   r   r   r   $test_email_verification_hmac_timeout   s   

r   c           	      C   s  t jj| _|dd}|| |td|jdd t|dks#J |d j	|jgks.J |
  |d j}|ddks@J |dd	d
}|td|jd	d}||jd ks[J ||dd  d }||}t|d t|d t|| jdd dS )zTest the email verification view. If User B clicks on an email
    verification link while logged in as User A, ensure User A gets
    logged out.Fr~   account_emailr   r"   action_sendr^   r   r]   doe)rO   rK   r`   ra   r2   /accounts/confirm-email/Naccount/messages/logged_out.txt$account/messages/email_confirmed.txtrf   )r   AuthenticationMethodEMAILACCOUNT_AUTHENTICATION_METHODforce_loginr:   r   r"   rl   ri   rr   rj   rk   contextsplitr   r	   	LOGIN_URL)	rJ   rU   rE   rt   r2   rj   user2rL   urlr   r   r   -test_verify_email_with_another_user_logged_in   s,   






r   c                 C   s   t jj| _|dd}|| |td|jdd t|dks#J |d j	|jgks.J |d j
}|ddks<J ||d	d
  d }||}t|d t|d t|| jdd ||jjkshJ d
S )zkIf the user clicks on an email verification link while logged in, ensure
    the user stays logged in.
    Fr~   r   r   r   r^   r   r]   r   Nr   r   rf   )r   r   r   r   r   r:   r   r"   rl   ri   rj   rk   r   r
   r   r	   rs   rB   r2   )rJ   rU   rE   rt   r2   rj   r   rL   r   r   r   *test_verify_email_with_same_user_logged_in  s   






r   c                 C   sh   d|_ |dd}| jt t|jksJ tjj|dd}| tddt	|j
id | jtr2J dS )z
    When a user is signed in, and you follow an email confirmation link of
    another user within the same browser/session, be sure to sign out the signed
    in user.
    Fr~   )r2   r@   r+   r>   )kwargsN)ACCOUNT_CONFIRM_EMAIL_ON_GETsessionr   strr/   r   r=   r<   r   r   r>   )auth_clientrJ   r2   rU   confirming_userr"   r   r   r   test_verify_logs_out_user3  s   
r   c                 C   sl   t jj|_| }| tddd||d}|jdksJ |d tdks&J | td}|jd	ks4J d S )
Nr   rZ   user@email.orgr    r&   r'   r(   r`   r)   )r   r4   r5   r6   r:   r   r;   r<   )rE   rF   rJ   rI   rK   rL   r   r   r   &test_email_verification_login_redirectE  s   
	r   c                 C   s   t jj|_d|_d|_d}| tdd|||d}|jdks!J |d td	ks+J t	t
jj|d
}| td|jgd}|jdksFJ |d dksNJ d S )Nr   Tr   r   rZ   r    r&   r'   r(   r*   r+   r,   )r   r4   r5   r6   1ACCOUNT_EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URLr   r:   r   r;   r   r   r=   r<   r>   )rE   rF   rJ   user_passwordr"   rL   rW   r   r   r   $test_email_verification_redirect_urlW  s$   
	r   )+datetimer   unittest.mockr   django.contrib.authr   r   django.core.cacher   django.urlsr   django.utils.timezoner   pytestpytest_django.assertsr	   r
   r   allauth.accountr   allauth.account.adapterr   allauth.account.modelsr   r   r   allauth.account.signalsr   r   markparametrizer   rN   rX   rw   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   sD    

.G%#