o
    "4gC                     @   s   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
 d dlmZ d dlmZ d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zejdddgdd Zdd Zejddgejdd d!gd"d# Zd$d% Zd&d' ZdS )(    )ANY)settings)get_user_model)reverseN)assertTemplateUsed)"AUTHENTICATION_METHODS_SESSION_KEY)Authenticatorc                 C   s   ||$}| j tddd}d| v sJ | jtdd|id}W d    n1 s+w   Y  |d tjks9J | jt ttdd	d
dgksIJ d S )Nmfa_login_webauthnXMLHttpRequestHTTP_X_REQUESTED_WITHrequest_options
credentialdatalocationmfaTwebauthn)atidmethodpasswordlesstype)	getr   jsonpostr   LOGIN_REDIRECT_URLsessionr   r   )clientpasskeywebauthn_authentication_bypassr   resp r"   n/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/mfa/webauthn/tests/test_views.pytest_passkey_login   s$   
r$   c                 C   s   |  td}|jdksJ |d tdsJ |  td}t|d ||}|  td}| jtd|dd	d
}W d    n1 sGw   Y  |d dksTJ d S )Nmfa_view_recovery_codes.  r   account_reauthenticatemfa_reauthenticatezmfa/reauthenticate.htmlmfa_reauthenticate_webauthnz/redir)r   nextr   )r   r   status_code
startswithr   r   )auth_clientr   user_with_recovery_codesr    r!   r   r"   r"   r#   test_reauthenticate#   s   

r/   c                 C   s&   |  td}|d tdksJ d S )Nr	   r   account_login)r   r   )r   r!   r"   r"   r#   6test_get_passkey_login_challenge_redirects_if_not_ajax7   s   r1   c                 C   sZ   | j tddd}|jdksJ |d dksJ | }|ddtd	g d
diiks+J d S )Nr	   r
   r      zcontent-typezapplication/jsonr   	publicKey
testserver	preferred)	challengerpIdallowCredentialsuserVerification)r   r   r+   r   r   )r   dbr!   r   r"   r"   r#    test_get_passkey_login_challenge<   s    r;   c                 C   s.   | j tdddid}|d tdksJ d S )Nr	   r   z{}r   r   r0   )r   r   )r   r   r!   r"   r"   r#   test_invalid_passkey_loginO   s   r<   c                 C   s   |  tdd|jid}|d tdsJ | I |  tdd|jid}t|d | jtdd|jidddid	}|d td
ksFJ |  |jd dksSJ t|dks[J W d    d S 1 sfw   Y  d S )Nmfa_edit_webauthnpkkwargsr   r'   zmfa/webauthn/edit_form.htmlnameRenamedr   mfa_list_webauthn)	r   r   r>   r,   r   r   refresh_from_dbr   strr-   r   reauthentication_bypassr!   r"   r"   r#   test_rename_keyT   s   
"rH   c                 C   s   |  tdd|jid}|d tdsJ | 0 |  tdd|jid}t|d | tdd|jid}|d tdksBJ W d    d S 1 sMw   Y  d S )Nmfa_remove_webauthnr>   r?   r   r'   z.mfa/webauthn/authenticator_confirm_delete.htmlrC   )r   r   r>   r,   r   r   rF   r"   r"   r#   test_remove_keyd   s   
"rJ   r   FTc              	   C   s8  |||}| j tdd|id}|d tdsJ W d    n1 s&w   Y  | d | td}t|d |||#}| j td||rKdndd	d}|d td
s\J W d    n1 sfw   Y  tjj|tjjd}|	 j
|ks~J tjj|tjjd  W d    d S 1 sw   Y  d S )Nmfa_add_webauthnr   r   r   r'   zmfa/webauthn/add_form.htmlon )r   r   r%   )userr   )r   r   r,   r   r   r   objectsTypeWEBAUTHNwrapis_passwordlessfilterRECOVERY_CODESexists)r-   rN   webauthn_registration_bypassrG   r   r   r!   authenticatorr"   r"   r#   test_add_keyr   s6   

	"rY   c                 C   s   |  td}t|d d S )NrC   z$mfa/webauthn/authenticator_list.html)r   r   r   )r-   r!   r"   r"   r#   test_list_keys   s   rZ   email_verifiedr   r   r   c                 C   sr   ||d*}|dkr|  td}n| jtdd|id}|d tdks'J W d    d S 1 s2w   Y  d S )NFr   rK   r   r   r   	mfa_index)r   r   r   )r-   rN   rW   rG   r   r   r!   r"   r"   r#   test_add_with_unverified_email   s   "r]   c                 C   s   | j tddddd}|d tdksJ |  |d }|jdks$J t jjdd	}||d
}| j tdd|id}W d    n1 sGw   Y  |d tjksUJ d S )Naccount_signup_by_passkeyzpass@key.orgr   )emailusernamer   r   mfa_signup_webauthnr2   )r_   Tr   )r   r   r+   r   rO   r   r   r   )r   r:   rW   r!   rN   r   r"   r"   r#   test_passkey_signup   s   rb   c                 C   s   |  td|j|d}|jdksJ |d tdksJ || }| td}|jdks0J |  tdd|i}W d    n1 sDw   Y  |jdksPJ |d tjksYJ | jt dt	|jd	d
t	t	t
jjdgkspJ d S )Nr0   )loginpasswordr&   r   mfa_authenticater2   r   rd   )r   r   r`   r   )r   r   r   r   )r   r   r`   r+   r   r   r   r   r   r   r   rP   rQ   )r   user_with_passkeyr   user_passwordr    r!   r   r"   r"   r#   test_webauthn_login   s"   

rh   )unittest.mockr   django.confr   django.contrib.authr   django.urlsr   pytestpytest_django.assertsr   allauth.account.authenticationr   allauth.mfa.modelsr   r$   r/   r1   r;   r<   rH   rJ   markparametrizerY   rZ   r]   rb   rh   r"   r"   r"   r#   <module>   s.    
!