o
    ˷e                     @   s  d dl mZ d dlZd dl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 d dl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 d dlmZ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l0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZBmCZC ddlDmEZEmFZFmGZGmHZH ejIddd d gZJe%d!e<jKjLe<jMjNddd"d#d$d%d&	G d'd( d(e6ZOG d)d* d*e6ZPG d+d, d,e6ZQG d-d. d.e6ZRG d/d0 d0e6ZSG d1d2 d2eZTG d3d4 d4e6ZUG d5d6 d6e6ZVe%d7d8G d9d: d:e6ZWe%d7d8G d;d< d<e6ZXG d=d> d>e6ZYe%d?d@dAiidBdCidDG dEdF dFe6ZZdS )G    )absolute_importN)	timedelta)forms)settings)messages)AbstractUserAnonymousUser)get_messages)MessageMiddleware)SessionMiddleware)mail
validators)ValidationError)models)HttpResponseRedirect)ContextTemplate)ClientRequestFactory)override_settings)reverse)now)BaseSignupFormResetPasswordForm
SignupForm)EmailAddressEmailConfirmationEmailConfirmationHMAC)MockTestCasepatch)get_user_modelget_username_max_length   )app_settings)get_adapter)AuthenticationBackend)user_logged_inuser_logged_out)filter_users_by_usernameurl_str_to_user_pkuser_pk_to_url_struser_usernamez^[a-c]+$znot abc)regexmessageflagshttps/accounts/profile/z/accounts/welcome/z-allauth.account.adapter.DefaultAccountAdapterT)	ACCOUNT_DEFAULT_HTTP_PROTOCOLACCOUNT_EMAIL_VERIFICATIONACCOUNT_AUTHENTICATION_METHODACCOUNT_SIGNUP_FORM_CLASSACCOUNT_EMAIL_SUBJECT_PREFIXLOGIN_REDIRECT_URLACCOUNT_SIGNUP_REDIRECT_URLACCOUNT_ADAPTERACCOUNT_USERNAME_REQUIREDc                   @   s  e Zd Zdd Zeejjddd Zdd Z	dd	 Z
d
d Zeddddd Zeddddd ZdddZdd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eejjdd-d. Zd/d0 Zd1d2 Zd3d4 Zeejjdd5d6 Zd7d8 Z edd9d:d; Z!ed<d=d>d?d@ Z"dAdB Z#eej$j%dCdDdE Z&eej$j%dFdGdHdI Z'eejjej$j(dJdKdLdM Z)eejjej$j(dJdKdNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.eej$j%dCdXdY Z/dZd[ Z0edd\d]d^ Z1ed<d\d_d` Z2dadb Z3eej$j%d<dcddde Z4ed<dfdgdh Z5edidjdkidlgdmdndo Z6eddpdqdr Z7eddpdsdt Z8eddudvdwdx Z9edydzd{d| Z:eejjdd}d~ Z;eejjddd Z<dd Z=dS )AccountTestsc                 C   sX   dt jv r(ddlm} |jjddd}tjjr*ddl	m
} |j|j  d S d S d S )	Nzallauth.socialaccount   )	SocialApptestfbfacebook)nameproviderr   Site)r   INSTALLED_APPSsocialaccount.modelsr=   objectscreateallauthr$   SITES_ENABLEDdjango.contrib.sites.modelsrC   sitesaddget_current)selfr=   sarC    rP   L/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/account/tests.pysetUp?   s   
zAccountTests.setUpr4   c                 C   sd   t  jjdd}|d |  tjj|dddd | jtdddd}| j	|t
jd	d
 d S )Nz@raymond.pennersusernamepsstzraymond.penners@example.comTuseremailprimaryverifiedaccount_loginloginpasswordFfetch_redirect_responser!   rF   rG   set_passwordsaver   clientpostr   assertRedirectsr   r7   rN   rX   resprP   rP   rQ   test_username_containing_atJ   s    

z(AccountTests.test_username_containing_atc                 C   s>   |  dd}| tjj|d d tjjdd|dd d S )Njohn@example.comrX   r#   Tr[   rY   rX   rZ   &_test_signup_email_verified_externallyassertEqualr   rF   filtercountgetrN   rX   rP   rP   rQ   *test_signup_same_email_verified_externally_   s   
z7AccountTests.test_signup_same_email_verified_externallyc                 C   sR   |  dd}| tjj|d d tjjdd|dd tjjdd|dd dS )	z
        John is invited on john@example.org, but signs up via john@example.com.
        E-mail verification is by-passed, their home e-mail address is
        used as a secondary.
        rk   john@example.orgrl   r<   Frm   TNrn   rt   rP   rP   rQ   +test_signup_other_email_verified_externallyh   s   
z8AccountTests.test_signup_other_email_verified_externallyc                 C   s   d}t  td||ddd}tdd | tdd | t |_||jd< dd	l	m
} ||}| |jd
 | |d t | | ttjd t jj|dS )Njohndoeaccount_signuprU   rY   	password1	password2c                 S      d S NrP   requestrP   rP   rQ   <lambda>       zEAccountTests._test_signup_email_verified_externally.<locals>.<lambda>c                 S   r}   r~   rP   r   rP   rP   rQ   r      r   account_verified_emailr#   signup.  locationr   rT   )r   rf   r   r   process_requestr
   r   rX   sessionviewsr   rp   status_coder%   get_signup_redirect_urllenr   outboxr!   rF   rs   )rN   signup_emailverified_emailrU   r   r   ri   rP   rP   rQ   ro   y   s*   

z3AccountTests._test_signup_email_verified_externallyT)r:   #ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICEc                 C   s2   | j jtddddddd}| |ddd	 d S )
Nry   rx   rv   janedoerz   dataformr|   z*You must type the same password each time.re   rf   r   assertFormErrorrN   ri   rP   rP   rQ   %test_signup_password_twice_form_error   s   	z2AccountTests.test_signup_password_twice_form_errorr:    ACCOUNT_SIGNUP_EMAIL_ENTER_TWICEc              	   C   s   t  tddddddd}tdd | tdd | t |_dd	lm	} || t
 jjdd
}| |jd d S )Nry   rx   rv   rU   rY   email2r{   r|   c                 S   r}   r~   rP   r   rP   rP   rQ   r      r   z6AccountTests.test_signup_email_twice.<locals>.<lambda>c                 S   r}   r~   rP   r   rP   rP   rQ   r      r   r#   r   rT   )r   rf   r   r   r   r
   r   rX   r   r   r!   rF   rs   rp   rY   )rN   r   r   rX   rP   rP   rQ   test_signup_email_twice   s    
z$AccountTests.test_signup_email_twicejohndoec                 K   s>   t  jjd|dd|}|r|| n|  |  |S )NTrU   	is_activerP   r!   rF   rG   rc   set_unusable_passwordrd   )rN   rU   r_   kwargsrX   rP   rP   rQ   _create_user   s   
zAccountTests._create_userc                 C   s(   |rdnd}| j |d}| j| |S )Nr   F)r_   )r   re   force_login)rN   usable_passwordr_   rX   rP   rP   rQ   _create_user_and_login   s   z#AccountTests._create_user_and_loginc                 C   s0   |    | j}|td}| j|ddd d S )Nr\   r1   Fr`   )r   re   rs   r   rg   rN   cri   rP   rP   rQ    test_redirect_when_authenticated   s   z-AccountTests.test_redirect_when_authenticatedc                 C   s    | j td}| |d d S )Naccount_reset_passwordzaccount/password_reset.html)re   rs   r   assertTemplateUsedr   rP   rP   rQ   test_password_reset_get   s   z$AccountTests.test_password_reset_getc                 C   s$   |  dd}| j|tddd d S )Naccount_set_passwordTaccount_change_passwordFr`    _password_set_or_change_redirectrg   r   r   rP   rP   rQ   test_password_set_redirect      
z'AccountTests.test_password_set_redirectc                 C   sp   |  d}d}| || | jjtd||dd}|  | || | |  | 	|j
d d S )NTz!*123i1uwn12W23r   r{   r|   r   r   )r   assertFalsecheck_passwordre   rf   r   refresh_from_db
assertTruehas_usable_passwordrp   r   )rN   rX   pwdri   rP   rP   rQ   test_set_password_not_allowed   s   
z*AccountTests.test_set_password_not_allowedc                 C      |  dd}| |jd d S )Nr   T   r   rp   r   r   rP   rP   rQ    test_password_change_no_redirect      z-AccountTests.test_password_change_no_redirectc                 C   r   )Nr   Fr   r   r   rP   rP   rQ   test_password_set_no_redirect   r   z*AccountTests.test_password_set_no_redirectc                 C   s$   |  dd}| j|tddd d S )Nr   Fr   r`   r   r   rP   rP   rQ   test_password_change_redirect   r   z*AccountTests.test_password_change_redirectc                 C   s   |  | | jt|S r~   r   re   rs   r   )rN   urlnamer   rP   rP   rQ   r      s   
z-AccountTests._password_set_or_change_redirectc                 C   sl   |    | jjtddddddd}| |d d	 t|jd
}d|d d d d d v s4J d S )Nr   r   z
AbCdEf!123zAbCdEf!123456)oldpasswordr{   r|   XMLHttpRequestr   HTTP_X_REQUESTED_WITHcontent-typeapplication/jsonutf8zsame passwordr   fieldsr|   errorsr   )	r   re   rf   r   rp   jsonloadscontentdecoderN   ri   r   rP   rP   rQ   test_ajax_password_change  s   	$z&AccountTests.test_ajax_password_changec                 C   s&   |   }tjd j}|j|v sJ d S Nr   _request_new_passwordr   r   bodyrU   rN   rX   r   rP   rP   rQ   %test_password_forgotten_username_hint  s   z2AccountTests.test_password_forgotten_username_hintc                 C   s&   |   }tjd j}|j|vsJ d S r   r   r   rP   rP   rQ   (test_password_forgotten_no_username_hint  s   z5AccountTests.test_password_forgotten_no_username_hintc                 C   sj   t  jjdddd}|d |  | jjtdddid | t	t
jd	 | t
jd
 jdg |S )Nr   rv   TrU   rY   r   r   r   rY   r   r#   r   )r!   rF   rG   rc   rd   re   rf   r   rp   r   r   r   tort   rP   rP   rQ   r     s   
z"AccountTests._request_new_passwordc                 C   s   |    tjd j}| |dd ||dd  d }| j|}|j	}| 
 |}| |dtj  | |jd  dS )a9  
        Test the password reset flow when the session is empty:
        requesting a new password, receiving the reset link via email,
        following the link, getting redirected to the
        new link (without the token)
        Copying the link and using it in a DIFFERENT client (Browser/Device).
        r   https:///password/reset/N"account/password_reset_from_key.%s
token_fail)r   r   r   r   assertGreaterfindsplitre   rs   urlclient_classr   r$   TEMPLATE_EXTENSIONr   context_data)rN   r   r   ri   reset_pass_urlrP   rP   rQ   +test_password_reset_flow_with_empty_session-  s   	z8AccountTests.test_password_reset_flow_with_empty_sessionc                 C   s~  |   }tjd j}| |dd ||dd  d }| j|}|j	}| j|}| 
|dtj  | d|jv  | j|ddd}| |td	 t jj|jd
}| |d | j|ddd}| 
|dtj  | |jd  | j|}| 
|dtj  | |jd  | jj|ddddd}| |jd t|jd}d|d d d v sJ dS )z
        Tests the password reset flow: requesting a new password,
        receiving the reset link via email and finally resetting the
        password to a new value.
        r   r   r   Nr   r   
newpass123r   $account_reset_password_from_key_donepkr   r     r   invalidr   r   )r   r   r   r   r   r   r   re   rs   r   r   r$   r   r   r   rf   rg   r   r!   rF   r   r   r   rp   r   r   r   r   r   )rN   rX   r   r   ri   responser   rP   rP   rQ   test_password_reset_flowK  sP   

z%AccountTests.test_password_reset_flowc                 C   s"  |    tjd j}| |dd | jddd}tjj	||j
ddd | jtd|j
d	d
}| ||jd  ||dd  d }| j|}|j}| j|}| |dtj  | d|jv  | jj|ddddd}| |td | ||jd  | t |jd  dS )z
        Tests the password reset flow: if User B requested a password
        reset earlier and now User A is logged in, User B now clicks on
        the link, ensure User A is logged out before continuing.
        r   r   john2john2@example.comrU   rY   TrW   r\   r   r]   rX   r   Nr   r   r   r   followr   )r   r   r   r   r   r   r   r   rF   rG   rY   re   rf   r   rp   contextr   rs   r   r   r$   r   r   r   rg   assertNotEqualr   )rN   r   user2ri   r   rP   rP   rQ   4test_password_reset_flow_with_another_user_logged_in  s8   


zAAccountTests.test_password_reset_flow_with_another_user_logged_inc                 C   s   |   }tjd j}| |dd tjj|dd ||dd 	 d }| j
|}| |dtj  | d|jv  dS )	zr
        Test that the password reset token is invalidated if
        the user email address was changed.
        r   r   zother@email.org)rX   rY   r   Nr   r   )r   r   r   r   r   r   r   rF   rG   r   re   rs   r   r$   r   r   r   rN   rX   r   r   ri   rP   rP   rQ   +test_password_reset_flow_with_email_changed  s   z8AccountTests.test_password_reset_flow_with_email_changed)ACCOUNT_LOGIN_ON_PASSWORD_RESETc                 C   sl   |   }tjd j}||dd   d }| j|}| j|j	ddd}| 
|j | |d d S )Nr   r   r   r   /confirm-email/)r   r   r   r   r   r   re   rs   rf   r   r   is_authenticatedrg   r   rP   rP   rQ   3test_password_reset_ACCOUNT_LOGIN_ON_PASSWORD_RESET  s   z@AccountTests.test_password_reset_ACCOUNT_LOGIN_ON_PASSWORD_RESETF
   )ACCOUNT_EMAIL_CONFIRMATION_HMAC#ACCOUNT_EMAIL_CONFIRMATION_COOLDOWNc                 C   s  t  }|jtdddddddd}| |jd | tjd jdg | tjd j	
d	d | ttjd
 | |dtj  dD ]I}|jtdddddd}| t jjddd  | |dtj  | ttj| | tjjdd | tjjt td
d d qEtjjddd d
  }|td|jgd}| |dtj  |td|jgd |tdddd}| j|tjdd d S )Nry   rx   rk   rz   Tr   r   r   r   r#   zaccount/verification_sent.%s)r#   r<   r\   r]   r   zaccount/verification_sent.)email_address__emaildayssent)email_address__user__usernameaccount_confirm_emailargszaccount/email_confirm.%sFr`   )r   rf   r   rp   r   r   r   r   r   r   r   r   r   r$   r   r   r!   rF   rq   existsr   rr   updater   r   rs   keyrg   r   r7   )rN   r   ri   attemptconfirmationrP   rP   rQ   !test_email_verification_mandatory  s|   


z.AccountTests.test_email_verification_mandatoryc                 C   s   d}t jjrddlm} |j }d |_}|  t	 jj
ddd}t d}tjj|||jd	d
 | tjd jdd  | d S )N
testserverr   rB   z<enc&"test>testuser@example.comr   /T)confirmr#   )rH   r$   rI   rJ   rC   rF   rM   r@   rd   r!   rG   r   rs   r   	add_emailrY   r   r   r   subject
startswith)rN   	site_namerC   siteur   rP   rP   rQ   test_email_escaping%  s   

$z AccountTests.test_email_escaping)r3   c                 C   sd   t  jjdd}|d |  tjj|dddd | jtdddd	}| j	|t
jdd
 dS )z9Tests login behavior when email verification is optional.r   rT   r   r  TFrW   r\   r]   r`   Nrb   rh   rP   rP   rQ   &test_login_unverified_account_optional2  s   

z3AccountTests.test_login_unverified_account_optional   )r3   ACCOUNT_LOGIN_ATTEMPTS_LIMITc                 C   s   t  jjdd}|d |  tjj|dddd tdD ]*}|d	k}|d
k}| jt	dg d| |r8dndd}| 
|dd |rFdnd qd S )Nr   rT   r   r  TFrW         r#  r\   )r   JohnJOHNJOhnjoHNwrongr]   r   0Too many failed login attempts. Try again later.z;The username and/or password you specified are not correct.)r!   rF   rG   rc   rd   r   rangere   rf   r   r   )rN   rX   iis_valid_attempt	is_lockedri   rP   rP   rQ   #test_login_failed_attempts_exceededD  s0   


z0AccountTests.test_login_failed_attempts_exceededr#   )r4   r3   r$  c                 C   s  t  jjdddd}|d |  tjj|dddd | jtd|j	dd	}| 
|d
d d | jtd|j	dd	}| 
|d
d d | jjtdd|j	id tjd j}| |dd ||dd   d }| j|}|j}| j|}| |dtj  | d|jv  d}| j|||d}| |td t  jj|jd}| || | jtd|j	|d	}| j|tjdd d S )Nr   rv   Tr   r   rW   r\   badr]   r   zAThe e-mail address and/or password you specified are not correct.r,  r   rY   r   r   r   r   r   r   r   r   r   r   Fr`   )r!   rF   rG   rc   rd   r   re   rf   r   rY   r   r   r   r   r   r   r   rs   r   r   r$   r   r   r   rg   r   r   r   r   r7   )rN   rX   ri   r   r   new_passwordrP   rP   rQ   =test_login_failed_attempts_exceeded_cleared_on_password_resetb  sh   



zJAccountTests.test_login_failed_attempts_exceeded_cleared_on_password_resetc                 C   s   t  jjdddd}|d |  tjj|dddd tjj|dddd | jtd	ddd
}| j	|tddd | 
ttjd tjd jdgksQJ d S )Nr   rv   Tr   r   rW   rk   Fr\   r]   account_email_verification_sentr`   r#   r   )r!   rF   rG   rc   rd   r   re   rf   r   rg   rp   r   r   r   r   rh   rP   rP   rQ   7test_login_using_unverified_email_address_is_prohibited  s*   
zDAccountTests.test_login_using_unverified_email_address_is_prohibitedc                 C   sb   t  jjdd}|d |  tjj|dddd | jtdddd	}| 	|td
 dS )z:Tests login behavior when email verification is mandatory.r   rT   r   r  TFrW   r\   r]   r5  N
r!   rF   rG   rc   rd   r   re   rf   r   rg   rh   rP   rP   rQ   'test_login_unverified_account_mandatory  s   
z4AccountTests.test_login_unverified_account_mandatoryc                 C   s   t  jjddd}|d |  tjj|dddd | jtdddd	}| 	|td
 t  jjddd}|d |  tjj|dddd | jtdddd	}| 	|td
 dS )z
        Tests login behavior with inactive accounts.

        Inactive user accounts should be prevented from performing any actions,
        regardless of their verified state.
        r   Fr   r   rk   TrW   r\   r]   account_inactiver  Nr7  rh   rP   rP   rQ   test_login_inactive_account  s(   

z(AccountTests.test_login_inactive_accountc                 C   sj   t  jjdddd | jjtdddidd}| ttj	d	 | tj	d
 j
dg | |d d d S )Nr   rv   Tr   r   rY   r   r   r#   r   r   r   )r!   rF   rG   re   rf   r   rp   r   r   r   r   r   rP   rP   rQ   test_ajax_password_reset  s   z%AccountTests.test_ajax_password_resetc                 C   s:   | j jtdi dd}| |jd t|jd d S )Nr\   r   r   r   r   )	re   rf   r   rp   r   r   r   r   r   r   rP   rP   rQ   test_ajax_login_fail  s   z!AccountTests.test_ajax_login_failc                 C   st   t  jjddd}|d |  | jjtdddddd}| |j	d	 t
|jd
}| |d d d S )Nr   Tr   r   r\   r]   r   r   r   r   r   r1   )r!   rF   rG   rc   rd   re   rf   r   rp   r   r   r   r   r   )rN   rX   ri   r   rP   rP   rQ   test_ajax_login_success  s   
z$AccountTests.test_ajax_login_successc                 C   s   |    | jtd d S )Naccount_emailr   rN   rP   rP   rQ   test_email_view   s   zAccountTests.test_email_view)ACCOUNT_LOGOUT_ON_GETc                 C   s   |  d\}}| |d d S )Nrs   account/messages/logged_out.txt)_logout_viewr   r   rP   rP   rQ   test_logout_view_on_get%  s   z$AccountTests.test_logout_view_on_getc                 C   sz   |  d\}}| |dtj  t }t| |td}| |d |j	t
 |jt
 jjddtd t| d S )Nrs   zaccount/logout.%saccount_logoutrB  r   rT   )senderr   rX   signal)rC  r   r$   r   r   r(   connectrf   r   assert_called_once_withr!   wsgi_requestrF   rs   
disconnect)rN   r   ri   receiver_mockrP   rP   rQ   test_logout_view_on_post*  s   

z%AccountTests.test_logout_view_on_postc                 C   sT   t  }t jjddd}|d |  t  }|jddd |t||tdfS )Nr   Tr   r   rU   r_   rE  )	r   r!   rF   rG   rc   rd   r^   getattrr   )rN   methodr   rX   rP   rP   rQ   rC  @  s   
zAccountTests._logout_view)r3   r   c                 C   s   t  }|td |tddddd}| j|tjdd | tj	d j
dg | ttj	d |  tjjt tdd	 d
 |tdddd}| j|tjdd | tj	d j
dg | ttj	d d S )Nry   rx   rk   )rU   rY   r{   Fr`   r   r#   r  r
  r\   r]   )r   rs   r   rf   rg   r   r8   rp   r   r   r   r   logoutr   rF   r  r   r   r7   r   rP   rP   rQ    test_optional_email_verificationI  s2   	z-AccountTests.test_optional_email_verification)%ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTSc                 C   s*   |    | jtd}| |jd d S )Nr\   r   )r   re   rs   r   rp   r   r   rP   rP   rQ   3test_account_authenticated_login_redirects_is_falseq  s   z@AccountTests.test_account_authenticated_login_redirects_is_falsez>django.contrib.auth.password_validation.MinimumLengthValidator
min_length	   )NAMEOPTIONS)AUTH_PASSWORD_VALIDATORSc                 C   sB   | j tdddddd}| |dd g  | |dddg d S )Nry   rx   rk   rz   r   r{   zBThis password is too short. It must contain at least 9 characters.r   r   rP   rP   rQ   test_django_password_validationw  s   	z,AccountTests.test_django_password_validation)r  c                 C   sn   |   }tjj|dddd}t|}t |_|  | j	t
d|jgd tjj|jd}| |j d S )Na@b.comFTrX   rY   r[   rZ   r  r  r   )r   r   rF   rG   r   r   r  rd   re   rf   r   r  rs   r   r   r[   )rN   rX   rY   r  rP   rP   rQ   'test_email_confirmation_hmac_falls_back  s   
z4AccountTests.test_email_confirmation_hmac_falls_backc                 C      |   }tjj|dddd}t|}t d}|j|d | t	t
jd | jtd|jgd	 tjj|jd
}| |j d S Nr[  FTr\  r  r   r#   r  r  r   )r   r   rF   rG   r   r   rs   sendrp   r   r   r   re   rf   r   r  r   r   r[   rN   rX   rY   r  r   rP   rP   rQ   test_email_confirmation_hmac  s   z)AccountTests.test_email_confirmation_hmacr   )r  &ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYSc                 C   r^  r_  )r   r   rF   rG   r   r   rs   r`  rp   r   r   r   re   rf   r   r  r   r   r[   ra  rP   rP   rQ   $test_email_confirmation_hmac_timeout  s   z1AccountTests.test_email_confirmation_hmac_timeoutz.allauth.account.tests.test_username_validators)ACCOUNT_USERNAME_VALIDATORSc                 C   s    t  d | tdd  d S )Nabcc                   S   s   t  dS )Ndef)r%   clean_usernamerP   rP   rP   rQ   r     s    z6AccountTests.test_username_validator.<locals>.<lambda>)r%   rh  assertRaisesr   r?  rP   rP   rQ   test_username_validator  s   z$AccountTests.test_username_validatorc                 C   s0  t  jjdddd}| j| | jtd|jdd | t	t
jd | t
jd	 j|jg | j  t
jd	 j}| |d
d	 | jddd}tjj||jddd | jtd|jdd}| ||jd  ||dd  d	 }| j|}| |d | |d | j|tjdd dS )zTest the email confirmation view. If User B clicks on an email
        verification link while logged in as User A, ensure User A gets
        logged out.r   rv   r   rU   rY   r_   r>   rY   action_sendr#   r   r   r   r   r   TrW   r\   r]   rX   r  NrB  $account/messages/email_confirmed.txtFr`   )r!   rF   create_userre   r   rf   r   rY   rp   r   r   r   r   rQ  r   r   r   r   r   rG   r   r   r   rg   r   	LOGIN_URL)rN   rX   r   r   ri   r   rP   rP   rQ   .test_confirm_email_with_another_user_logged_in  s8   

z;AccountTests.test_confirm_email_with_another_user_logged_inc                 C   s   t  jjdddd}| j| | jtd|jdd | t	t
jd | t
jd	 j|jg t
jd	 j}| |d
d	 ||dd  d	 }| j|}| |d | |d | j|tjdd | ||jj dS )zTest the email confirmation view. If User A clicks on an email
        verification link while logged in, ensure the user
        stayed logged in.r   rv   r   rk  r>  rl  rm  r#   r   r   r  NrB  ro  Fr`   )r!   rF   rp  re   r   rf   r   rY   rp   r   r   r   r   r   r   r   r   assertTemplateNotUsedr   rg   r   r7   rJ  rX   r   rP   rP   rQ   +test_confirm_email_with_same_user_logged_in  s&   z8AccountTests.test_confirm_email_with_same_user_logged_inc                 C   s   t  d}tdd | tdd | t  }t|d d|i}t |t	j
d| t|}|jd j}|j|v sBJ |d S )	Nr  c                 S   r}   r~   rP   r   rP   rP   rQ   r     r   z4AccountTests.test_message_escaping.<locals>.<lambda>c                 S   r}   r~   rP   r   rP   rP   rQ   r     r   z'<8rX   zaccount/messages/logged_in.txtr   )r   rs   r   r   r
   r!   r,   r%   add_messager   SUCCESSr	   _queued_messagesr.   rU   )rN   r   rX   r   msgsactual_messagerP   rP   rQ   test_message_escaping  s   

z"AccountTests.test_message_escapingNr   r   )T)>__name__
__module____qualname__rR   r   r$   AuthenticationMethodUSERNAME_EMAILrj   ru   rw   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   EMAILr   r   r   r   r   r   r  r  r!  EmailVerificationMethodOPTIONALr"  r1  	MANDATORYr4  r6  r8  r:  r;  r<  r=  r@  rD  rM  rC  rR  rT  rZ  r]  rb  rd  rj  rr  rt  rz  rP   rP   rP   rQ   r;   3   s    
	




A
-

E


H




	
$







'
r;   c                   @   s   e Z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edddd Zedddd Zed dd!d" ZdS )#EmailFormTestsc                 C   sv   t  }|jjddd| _| jd | j  tjj| j| jjddd| _tjj| jdddd| _	| j
jddd	 d S )
Nr   john1@example.orgr   r   Tr\  john2@example.orgFrN  )r!   rF   rG   rX   rc   rd   r   rY   email_addressemail_address2re   r^   )rN   UserrP   rP   rQ   rR      s   
zEmailFormTests.setUpc                 C   s>   | j tdddd}tjjd| jddd | |d d S )Nr>  rl  john3@example.org
action_addrY   F)rY   rX   r[   rZ   ,account/messages/email_confirmation_sent.txt)re   rf   r   r   rF   rs   rX   r   r   rP   rP   rQ   test_add0  s   zEmailFormTests.test_addc                 C   sZ   | j jtddd}t|jd}|d | jjdddd| j	jd	d
d
dgks+J d S )Nr>  r   r   r   r   r  T)idrY   rZ   r[   r  F)
re   rs   r   r   r   r   r   r  r   r  r   rP   rP   rQ   test_ajax_get=  s    zEmailFormTests.test_ajax_getc                 C   sF   | j jtdddddd}t|jd}| |d td d S )	Nr>  rl  r  r  r   r   r   r   )re   rf   r   r   r   r   r   rp   r   rP   rP   rQ   test_ajax_addQ  s   zEmailFormTests.test_ajax_addc                 C   sR   | j jtdddddd}t|jd}d|d	 d
 d d d v s'J d S )Nr>  rl  zjohn3#example.orgr  r   r   r   validr   r   rY   r   r   )re   rf   r   r   r   r   r   r   rP   rP   rQ   test_ajax_add_invalidZ  s   $z$EmailFormTests.test_ajax_add_invalidc                 C   s>   | j tdd| jjd}tjj| jjd | 	|d d S )Nr>  rl  action_removerY   r   0account/messages/cannot_delete_primary_email.txt)
re   rf   r   r  rY   r   rF   rs   r   r   r   rP   rP   rQ   test_remove_primaryc  s   z"EmailFormTests.test_remove_primaryc                 C   sV   | j jtdd| jjddd}| |d t|j	d}| 
|d td d S )	Nr>  rl  r  r   r   r  r   r   )re   rf   r   r  rY   r   r   r   r   r   rp   r   rP   rP   rQ   test_ajax_remove_primarym  s   z'EmailFormTests.test_ajax_remove_primaryc                    sB    j tdd jjd} tj fdd  |d d S )Nr>  rl  r  c                      s   t jj jjdS )Nr   )r   rF   rs   r  r   rP   r?  rP   rQ   r     s    z6EmailFormTests.test_remove_secondary.<locals>.<lambda>z"account/messages/email_deleted.txt)	re   rf   r   r  rY   ri  r   DoesNotExistr   r   rP   r?  rQ   test_remove_secondaryy  s   
z$EmailFormTests.test_remove_secondaryc                 C   sh   | j tdd| jjd}tjj| jj	d}tjj| jj	d}| 
|j | |j | |d d S )Nr>  rl  action_primaryrY   r   z-account/messages/unverified_primary_email.txt)re   rf   r   r  rY   r   rF   rs   r  r   r   rZ   r   r   )rN   ri   r  r  rP   rP   rQ   test_set_primary_unverified  s   z*EmailFormTests.test_set_primary_unverifiedc                 C   s   t jj| jjd}d|_|  | jt	dd| jj
d}t jj| jjd}t jj| jjd}| |j | |j | |d d S )Nr   Tr>  rl  r  z&account/messages/primary_email_set.txt)r   rF   rs   r  r   r[   rd   re   rf   r   rY   r  r   rZ   r   r   )rN   r  ri   r  rP   rP   rQ   test_set_primary  s   zEmailFormTests.test_set_primaryc                 C   s,   | j tdd| jjd}| |d d S )Nr>  rl  rn  rY   r  )re   rf   r   r  rY   r   r   rP   rP   rQ   test_verify  s
   zEmailFormTests.test_verifyc                 C   sT   t jj| jd dksJ | jtdddd t jj| jd dks(J d S )Nrl   r<   r>  rl  zemail@unknown.orgr  )r   rF   rq   rX   rr   re   rf   r   r?  rP   rP   rQ   test_verify_unknown_email  s    z(EmailFormTests.test_verify_unknown_emailr<   )ACCOUNT_MAX_EMAIL_ADDRESSESc                 C   (   | j tdddd}| |d d S Nr>  rl  r  r  r  )re   rf   r   rs  r   rP   rP   rQ   test_add_with_two_limiter     z(EmailFormTests.test_add_with_two_limiterNc                 C   r  r  re   rf   r   r   r   rP   rP   rQ   test_add_with_none_limiter  r  z)EmailFormTests.test_add_with_none_limiterr   c                 C   r  r  r  r   rP   rP   rQ   test_add_with_zero_limiter  r  z)EmailFormTests.test_add_with_zero_limiter)r|  r}  r~  rR   r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  rP   rP   rP   rQ   r    s&    		
	

r  c                   @   s^   e Zd Zeddgddd Zeddgddd Zeddd	d
 Zeddddd ZdS )BaseSignupFormTestsTrU   )r:   ACCOUNT_USERNAME_BLACKLISTc                 C   (   ddd}t |dd}| |  d S )NrU   r  r   Temail_required)r   r   is_validrN   r   r   rP   rP   rQ   test_username_in_blacklist  
   z.BaseSignupFormTests.test_username_in_blacklistc                 C   r  )Ntheusernamer  r   Tr  )r   r   r  r  rP   rP   rQ   test_username_not_in_blacklist  r  z2BaseSignupFormTests.test_username_not_in_blacklist)r:   c                 C   sV   ddd}t |dd}t }|jd }| |j| |j}| |jdt| d S )NrU   r  r   Tr  	maxlength)	r   r"   r   rp   
max_lengthwidgetattrsrs   str)rN   r   r   r  fieldr  rP   rP   rQ   test_username_maxlength  s   
z+BaseSignupFormTests.test_username_maxlengthr   c                 C   sp   ddd}t |dd}| |  dddd}t |dd}| |  d|d< t |dd}| |  d S )	NrU   r  r   Tr  )rU   rY   r   zanotheruser@example.comr   )r   r   r  r   r  rP   rP   rQ   test_signup_email_verification  s   z2BaseSignupFormTests.test_signup_email_verificationN)r|  r}  r~  r   r  r  r  r  rP   rP   rP   rQ   r    s    


r  c                   @   s(   e Zd Zeddddd Zdd ZdS )CustomSignupFormTestsT)r   r   c                    sP   g d G  fdddt j}G dd dt|}| }| t|j   d S )N)rY   r   r{   r|   rU   	last_name
first_namec                       s(   e Zd ZejddZejddZ ZdS )zJCustomSignupFormTests.test_custom_form_field_order.<locals>.TestSignupForm   )r  N)r|  r}  r~  r   	CharFieldr  r  field_orderrP   expected_field_orderrP   rQ   TestSignupForm  s    r  c                   @      e Zd ZdS )zLCustomSignupFormTests.test_custom_form_field_order.<locals>.CustomSignupFormNr|  r}  r~  rP   rP   rP   rQ   CustomSignupForm      r  )r   Formr   rp   listr   keys)rN   r  r  r   rP   r  rQ   test_custom_form_field_order   s
   
z2CustomSignupFormTests.test_custom_form_field_orderc                 C   st   ddl m} ddlm} G dd dt}| }ddddd	}||d
d}t|j|s,J |  t|j|s8J d S )Nr   )r!   )DeferredAttributec                   @   r  )zICustomSignupFormTests.test_user_class_attribute.<locals>.CustomSignupFormNr  rP   rP   rP   rQ   r  #  r  r  rU   r  zvery-secretrz   Tr  )django.contrib.authr!   django.db.models.query_utilsr  r   
isinstancerU   r  )rN   r!   r  r  r  r   r   rP   rP   rQ   test_user_class_attribute  s   z/CustomSignupFormTests.test_user_class_attributeN)r|  r}  r~  r   r  r  rP   rP   rP   rQ   r    s    
r  c                   @   sV   e Zd Zdd Zeejjddd Zeejj	ddd Z
eejjddd	 Zd
S )AuthenticationBackendTestsc                 C   s2   t  jjdddd}||j |  || _d S NTrk   r   r   rY   rU   )r!   rF   rG   rc   rU   rd   rX   rt   rP   rP   rQ   rR   8  s   
z AuthenticationBackendTests.setUprS   c                 C   sL   | j }t }| |jd |j|jdj|j | |jd |j|jdd  d S N)r   rU   r_   )rX   r&   rp   authenticaterU   r   rY   rN   rX   backendrP   rP   rQ   test_auth_by_username@     

z0AuthenticationBackendTests.test_auth_by_usernamec                 C   sL   | j }t }| |jd |j|jdj|j | |jd |j|jdd  d S r  rX   r&   rp   r  rY   rU   r   r  rP   rP   rQ   test_auth_by_emailS  r  z-AuthenticationBackendTests.test_auth_by_emailc                 C   sP   | j }t }| |jd |j|jdj|j | |jd |j|jdj|j d S r  r  r  rP   rP   rQ   test_auth_by_username_or_emailf  s    

z9AuthenticationBackendTests.test_auth_by_username_or_emailN)r|  r}  r~  rR   r   r$   r  USERNAMEr  r  r  r  r  rP   rP   rP   rQ   r  7  s    

r  c                   @   s0   e Zd ZejdejddZG dd dej	Z	dS )UUIDUserTF)primary_keydefaulteditablec                   @   s   e Zd ZdZdS )zUUIDUser.MetaAUTH_USER_MODELN)r|  r}  r~  	swappablerP   rP   rP   rQ   Meta}  s    r  N)
r|  r}  r~  r   	UUIDFielduuiduuid4r  r   r  rP   rP   rP   rQ   r  z  s    r  c                   @   sP   e Zd Zdd Zdd Zdd Zeddd	d
 Zedddd Zdd Z	dS )
UtilsTestsc                 C   s   t  j| _d S r~   )r  r  hexuser_idr?  rP   rP   rQ   rR     s   zUtilsTests.setUpc                 C   sN   t d}t|_| t| jt| j W d    d S 1 s w   Y  d S )N$allauth.account.utils.get_user_model)r    r  return_valuerp   r*   r  r  UUID)rN   
mocked_gumrP   rP   rQ   0test_url_str_to_pk_identifies_UUID_as_stringlike  s   
"z;UtilsTests.test_url_str_to_pk_identifies_UUID_as_stringlikec                 C   s(   t dddd}| t|t|j d S r  )r  rp   r+   r  r   rt   rP   rP   rQ   3test_pk_to_url_string_identifies_UUID_as_stringlike  s   z>UtilsTests.test_pk_to_url_string_identifies_UUID_as_stringlikeF) ACCOUNT_PRESERVE_USERNAME_CASINGc                 C   s0   t   }t|d | t|d tdd d S )N	CamelCase	camelcaseFooBarr!   r,   rp   r)   rt   rP   rP   rQ   test_username_lower_cased     
z$UtilsTests.test_username_lower_casedTc                 C   s0   t   }t|d | t|d tdd d S )Nr  r  foobarr  rt   rP   rP   rQ   test_username_case_preserved  r  z'UtilsTests.test_username_case_preservedc                 C   sL   t  dd}d}ddg}|D ]}t|}|td|i}| || qd S )Nzjohn<br/>doerT   zjohn&lt;br/&gt;doez){% load account %}{% user_display user %}z5{% load account %}{% user_display user as x %}{{ x }}rX   )r!   r   renderr   rp   )rN   rX   expected_name	templatestemplatetr   rP   rP   rQ   test_user_display  s   zUtilsTests.test_user_displayN)
r|  r}  r~  rR   r  r  r   r  r  r  rP   rP   rP   rQ   r    s    

r  c                   @   sR   e Zd ZdddZeddddd Zeddded	ed
eddd ZdS )ConfirmationViewTestsr   r   c                 C   s6   t  jj|dd}|r|| n|  |  |S )NTr   r   )rN   rU   r_   rX   rP   rP   rQ   r     s   z"ConfirmationViewTests._create_userT)r  #ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATIONc                 C   s   |   }tjj|dddd}t|j}t }t| | j	j
}t||d< |  | j	td|gd}tjj|jd}| |j |jt |j|t jjd	d
td t| d S )Nr[  FTr\  account_userr  r  r   r   rT   )rF  r   r   rX   rG  )r   r   rF   rG   r   r  r   r'   rH  re   r   r+   rd   rf   r   rs   r   r   r[   rI  r!   rJ  rK  )rN   rX   rY   r  rL  r   ri   rP   rP   rQ   test_login_on_confirm  s*   

z+ConfirmationViewTests.test_login_on_confirmz#allauth.account.views.perform_loginr  )r  c                 C   s   t dddd}| jj}t||d< |  td|d}d}tt|d	}||_||j_	t
d
d|_	td| | jtd|gd W d    n1 sKw   Y  |jsUJ d S )NTrk   r   r  r  F)r[   rX   mockkey)autospecr  r  )redirect_toz+allauth.account.views.EmailConfirmationHMACr  r  )r  re   r   r+   rd   r   r   r  from_keyr  r   r    rf   r   called)rN   r  mock_perform_loginrX   r   rY   r  r  rP   rP   rQ   test_login_on_confirm_uuid_user  s   z5ConfirmationViewTests.test_login_on_confirm_uuid_userNr{  )	r|  r}  r~  r   r   r  r    r  r  rP   rP   rP   rQ   r    s    
	

r  F)ACCOUNT_PREVENT_ENUMERATIONc                   @      e Zd Zdd ZdS )TestResetPasswordFormc                 C   s<   t  }|jjddddd ddi}t|}| |  d S )Nmike123mike@ixample.orgtest123F)r   rY   r!   rF   rp  r   r   r  rN   r  r   r   rP   rP   rQ   &test_user_email_not_sent_inactive_user  s   z<TestResetPasswordForm.test_user_email_not_sent_inactive_userN)r|  r}  r~  r  rP   rP   rP   rQ   r    s    r  c                   @   s8   e Zd Ze dZdd Zdd Zdd Zdd	 Z	d
S )TestCVE2019_19844r  c                 C   |   t  }|jddd |jddd ddi}t|}| |  || j | t	t
jd | t
jd jdg d S )	Nr  mike@example.orgr  mike456   mıke@example.orgrY   r#   r   r!   rF   rp  r   r   r  rd   global_requestrp   r   r   r   r   r  rP   rP   rQ   !test_user_email_unicode_collision     z3TestCVE2019_19844.test_user_email_unicode_collisionc                 C   r  )	Nr  r  r  r     mike@ıxample.orgrY   r#   r   r  r  rP   rP   rQ   (test_user_email_domain_unicode_collision  r  z:TestCVE2019_19844.test_user_email_domain_unicode_collisionc                 C   8   t  }|jddd ddi}t|}| |  d S )Nr  r  r  rY   r  r  r  rP   rP   rQ   -test_user_email_unicode_collision_nonexistent  
   z?TestCVE2019_19844.test_user_email_unicode_collision_nonexistentc                 C   r"  )Nr  r  r  rY   r   r  r  rP   rP   rQ   4test_user_email_domain_unicode_collision_nonexistent   r$  zFTestCVE2019_19844.test_user_email_domain_unicode_collision_nonexistentN)
r|  r}  r~  r   rs   r  r  r!  r#  r%  rP   rP   rP   rQ   r    s    r  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
RequestAjaxTestsc                 K   s&   | j jtdddddddfi |S )Nry   rx   rv   r   )re   rf   r   )rN   r   rP   rP   rQ   _send_post_request)  s   	z#RequestAjaxTests._send_post_requestc                 C   s,   |   }| d|j | j|tjdd d S )Nr   Fr`   )r'  rp   r   rg   r   r7   r   rP   rP   rQ   test_no_ajax_header6  s
   
z$RequestAjaxTests.test_no_ajax_headerc                 C   4   | j dd}| d|j | tj| d  d S )Nr   r   r   r   r'  rp   r   r   r7   r   r   rP   rP   rQ   !test_ajax_header_x_requested_with=     z2RequestAjaxTests.test_ajax_header_x_requested_withc                 C   r)  )Nr   )HTTP_ACCEPTr   r   r*  r   rP   rP   rQ   test_ajax_header_http_acceptB  r,  z-RequestAjaxTests.test_ajax_header_http_acceptN)r|  r}  r~  r'  r(  r+  r.  rP   rP   rP   rQ   r&  (  s
    r&  r  BACKENDz-django.core.cache.backends.locmem.LocMemCachereset_password_emailz1/m)CACHESACCOUNT_RATE_LIMITSc                   @   r  )RateLimitTestsc                 C   s`   t  jjdd | jjtdddid}|jdksJ | jjtdddid}|jdks.J d S )	Nr[  )rY   r   rY   r   r   zA@B.COMi  )r!   rF   rG   re   rf   r   r   r   rP   rP   rQ   $test_case_insensitive_password_resetQ  s   z3RateLimitTests.test_case_insensitive_password_resetN)r|  r}  r~  r4  rP   rP   rP   rQ   r3  H  s    	r3  )[
__future__r   r   r  datetimer   djangor   django.confr   django.contribr   django.contrib.auth.modelsr   r   django.contrib.messages.apir	   "django.contrib.messages.middlewarer
   "django.contrib.sessions.middlewarer   django.corer   r   django.core.exceptionsr   	django.dbr   django.httpr   django.templater   r   django.test.clientr   r   django.test.utilsr   django.urlsr   django.utils.timezoner   allauth.app_settingsrH   allauth.account.formsr   r   r   allauth.account.modelsr   r   r   allauth.testsr   r   r    allauth.utilsr!   r"   rl  r$   adapterr%   auth_backendsr&   signalsr'   r(   utilsr)   r*   r+   r,   RegexValidatortest_username_validatorsr  r  r  r  r;   r  r  r  r  r  r  r  r  r  r&  r3  rP   rP   rP   rQ   <module>   s    	       h %<8C*G) 