U
    ÝÁ]&7  ã                   @   sð   d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
m  mZ ddlmZ ddlmZ ddlmZ ddlmZ ej e d¡¡ZG d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZedkrìe ¡  dS )zTests for certbot.account.é    N)Úmessages)Úerrors)Ú
filesystem)Úmisc)Úoszrsa512_key.pemc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚAccountTestz"Tests for certbot.account.Account.c                 C   s²   ddl m} t ¡ | _|jdtjddddddtjd	d
| _	|| jt
| j	ƒ| _tjdd| j_t d¡@}d|j_t d¡"}| j	j|jj_|| jt
ƒ| _W 5 Q R X W 5 Q R X d S )Nr   ©ÚAccountztest.certbot.orgiß  é   é   é   é
   )Ztzinfo)Zcreation_hostÚcreation_dtZi_am_a_regr)Úreturn_valuezcertbot.account.socketzcertbot.account.datetime)Úcertbot.accountr	   ÚmockÚ	MagicMockÚregrZMetaÚdatetimeÚpytzZUTCÚmetaÚKEYÚaccÚ__repr__ÚpatchZgetfqdnr   r   ZnowÚacc_no_meta)Úselfr	   Zmock_socketZmock_dt© r   ú</usr/lib/python3/dist-packages/certbot/tests/account_test.pyÚsetUp   s*    
      ÿþzAccountTest.setUpc                 C   s8   |   | j| jj¡ |   t| jj¡ |   | j| jj¡ d S ©N)ÚassertEqualr   r   r   Úkeyr   r   ©r   r   r   r   Ú	test_init(   s    zAccountTest.test_initc                 C   s   |   | jjd¡ d S )NZ 7adac10320f585ddf118429c0c4af2cd)r!   r   Úidr#   r   r   r   Útest_id-   s     ÿzAccountTest.test_idc                 C   s   |   | jjd¡ d S )Nz,test.certbot.org@2015-07-04T14:04:10Z (7ada))r!   r   Zslugr#   r   r   r   Ú	test_slug1   s     ÿzAccountTest.test_slugc                 C   s   |   t| jƒ d¡¡ d S )Nz=<Account(i_am_a_regr, 7adac10320f585ddf118429c0c4af2cd, Meta()Ú
assertTrueÚreprr   Ú
startswithr#   r   r   r   Ú	test_repr5   s    ÿzAccountTest.test_reprN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r&   r'   r+   r   r   r   r   r      s   r   c                   @   s<   e Zd ZdZdd„ Ze d¡dd„ ƒZe d¡dd„ ƒZd	S )
ÚReportNewAccountTestz-Tests for certbot.account.report_new_account.c                 C   s   ddl m} || jƒ d S )Nr   )Úreport_new_account)r   r1   Úconfig)r   r1   r   r   r   Ú_call<   s    zReportNewAccountTest._callz+certbot.account.zope.component.queryUtilityc                 C   s   d |_ |  ¡  d S r    )r   r3   )r   Ú	mock_zoper   r   r   Útest_no_reporter@   s    z%ReportNewAccountTest.test_no_reporterc                 C   s4   |   ¡  |ƒ jj}|  | jj|d d d k¡ d S )Nr   )r3   Zadd_messageZcall_args_listr(   r2   Z
config_dir)r   r4   Z	call_listr   r   r   Útest_itE   s    
zReportNewAccountTest.test_itN)	r,   r-   r.   r/   r3   r   r   r5   r6   r   r   r   r   r0   9   s   
r0   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚAccountMemoryStorageTestz/Tests for certbot.account.AccountMemoryStorage.c                 C   s   ddl m} |ƒ | _d S )Nr   )ÚAccountMemoryStorage)r   r8   Ústorage)r   r8   r   r   r   r   O   s    zAccountMemoryStorageTest.setUpc                 C   sŽ   t jdd}|  g | j ¡ ¡ |  tj| jjd¡ | j 	|d ¡ |  |g| j ¡ ¡ |  || j d¡¡ | j 	|d ¡ |  |g| j ¡ ¡ d S )NÚx)r%   )
r   ZMockr!   r9   Úfind_allÚassertRaisesr   ÚAccountNotFoundÚloadÚsave)r   Úaccountr   r   r   r6   S   s    z AccountMemoryStorageTest.test_itN)r,   r-   r.   r/   r   r6   r   r   r   r   r7   L   s   r7   c                       s  e Zd ZdZ‡ f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 d!„ Zd"d#„ Ze d$¡d%d&„ ƒZd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Z d9d:„ Z!d;d<„ Z"d=d>„ Z#d?d@„ Z$‡  Z%S )AÚAccountFileStorageTestz-Tests for certbot.account.AccountFileStorage.c                    sl   t t| ƒ ¡  ddlm} || jƒ| _ddlm} d}|tj	d t 
¡ |dtd| _t ¡ | _|| jj_d S )Nr   ©ÚAccountFileStorager   Úhi)ZuriZbodyÚnew_authzr_uri)r   r"   )ÚsuperrA   r   r   rC   r2   r9   r	   r   ZRegistrationResourceZRegistrationr   r   r   r   Úmock_clientZ	directoryZ	new_authz)r   rC   r	   rE   ©Ú	__class__r   r   r   b   s     þü
zAccountFileStorageTest.setUpc                 C   s    |   tj t | jj¡¡¡ d S r    )r(   r   ÚpathÚisdirr   Z.underscores_for_unsupported_characters_in_pathr2   Úaccounts_dirr#   r   r   r   Útest_init_creates_dirr   s    
ÿz,AccountFileStorageTest.test_init_creates_dirc              	   C   sž   | j  | j| j¡ tj | jj| jj	¡}|  
tj |¡¡ dD ] }|  
tj tj ||¡¡¡ q>|  
t tj |d¡d¡¡ | j  | jj	¡}|  | j|¡ d S )N)ú	regr.jsonú	meta.jsonúprivate_key.jsonrP   é   )r9   r?   r   rG   r   rJ   Újoinr2   rL   r%   r(   Úexistsr   Z
check_moder>   r!   )r   Úaccount_pathÚ	file_nameZloadedr   r   r   Útest_save_and_restorev   s    
ÿÿz,AccountFileStorageTest.test_save_and_restorec              	   C   s\   | j  | j| j¡ tj | jj| jj	d¡}t
|dƒ}t |¡}W 5 Q R X |  d|k¡ dS )z=Saved regr should include a new_authzr_uri for older CertbotsrN   ÚrrE   N)r9   r?   r   rG   r   rJ   rR   r2   rL   r%   ÚopenÚjsonr>   r(   )r   rJ   Úfr   r   r   r   Ú!test_save_and_restore_old_version„   s
    z8AccountFileStorageTest.test_save_and_restore_old_versionc              	   C   s€   | j  | j| j¡ tj | jj| jj	¡}|  
tj |¡¡ |  
tj tj |d¡¡¡ dD ] }|  tj tj ||¡¡¡ qZd S )NrN   )rO   rP   )r9   Z	save_regrr   rG   r   rJ   rR   r2   rL   r%   r(   rS   ÚassertFalse)r   rT   rU   r   r   r   Útest_save_regrŒ   s     ÿ
ÿz%AccountFileStorageTest.test_save_regrc                 C   s,   | j  | j| j¡ |  | jg| j  ¡ ¡ d S r    )r9   r?   r   rG   r!   r;   r#   r   r   r   Útest_find_all–   s    z$AccountFileStorageTest.test_find_allc                 C   s   |   g | j ¡ ¡ d S r    )r!   r9   r;   r#   r   r   r   Útest_find_all_none_empty_listš   s    z4AccountFileStorageTest.test_find_all_none_empty_listc                 C   s$   t  | jj¡ |  g | j ¡ ¡ d S r    )r   Úrmdirr2   rL   r!   r9   r;   r#   r   r   r   Ú!test_find_all_accounts_dir_absent   s    z8AccountFileStorageTest.test_find_all_accounts_dir_absentc              	   C   sT   t jdtjdgd| j_t  d¡(}dddg|_|  ddg| j 	¡ ¡ W 5 Q R X d S )Nr:   Úz©Úside_effectzcertbot.account.os.listdirÚy)
r   r   r   ÚAccountStorageErrorr9   Ú_load_for_server_pathr   r   r!   r;   )r   Zmock_listdirr   r   r   Útest_find_all_load_skips¡   s    
ÿ
z/AccountFileStorageTest.test_find_all_load_skipsc                 C   s   |   tj| jjd¡ d S )NZmissing)r<   r   r=   r9   r>   r#   r   r   r   Ú#test_load_non_existent_raises_error©   s    z:AccountFileStorageTest.test_load_non_existent_raises_errorc                 C   s$   || j _ddlm} || j ƒ| _d S )Nr   rB   )r2   Úserverr   rC   r9   )r   rj   rC   r   r   r   Ú_set_server¬   s    z"AccountFileStorageTest._set_serverc                 C   sH   |   d¡ |  g | j ¡ ¡ |  g | j ¡ ¡ |  tj | jj	¡¡ d S ©Nú6https://acme-staging-v02.api.letsencrypt.org/directory)
rk   r!   r9   r;   r\   r   rJ   Úislinkr2   rL   r#   r   r   r   Útest_find_all_neither_exists±   s    
z3AccountFileStorageTest.test_find_all_neither_existsc                 C   s’   |   d¡ |  g | j ¡ ¡ | j | j| j¡ |  | jg| j ¡ ¡ |  | jg| j ¡ ¡ |  tj	 
| jj¡¡ d}|  tj	 | j |¡¡¡ d S ©Nrm   ú2https://acme-staging.api.letsencrypt.org/directory)rk   r!   r9   r;   r?   r   rG   r\   r   rJ   rn   r2   rL   rK   Úaccounts_dir_for_server_path©r   Zprev_server_pathr   r   r   Útest_find_all_find_before_save·   s    
z5AccountFileStorageTest.test_find_all_find_before_savec                 C   s–   |   d¡ | j | j| j¡ |  | jg| j ¡ ¡ |  | jg| j ¡ ¡ |  tj	 
| jj¡¡ |  tj	 | jj¡¡ d}|  tj	 | j |¡¡¡ d S rp   )rk   r9   r?   r   rG   r!   r;   r\   r   rJ   rn   r2   rL   r(   rK   rr   rs   r   r   r   Útest_find_all_save_before_findÂ   s    
z5AccountFileStorageTest.test_find_all_save_before_findc                 C   sd   |   d¡ |  g | j ¡ ¡ | j | j| j¡ |  | jg| j ¡ ¡ |   d¡ |  g | j ¡ ¡ d S rp   )rk   r!   r9   r;   r?   r   rG   r#   r   r   r   Útest_find_all_server_downgradeÌ   s    

z5AccountFileStorageTest.test_find_all_server_downgradec                 C   s@   |   d¡ | j | j| j¡ |   d¡ |  | jg| j ¡ ¡ d S ©Nrq   rm   ©rk   r9   r?   r   rG   r!   r;   r#   r   r   r   Útest_upgrade_version_stagingÕ   s    

z3AccountFileStorageTest.test_upgrade_version_stagingc                 C   s@   |   d¡ | j | j| j¡ |   d¡ |  | jg| j ¡ ¡ d S )Nz.https://acme-v01.api.letsencrypt.org/directoryz.https://acme-v02.api.letsencrypt.org/directoryrx   r#   r   r   r   Útest_upgrade_version_productionÛ   s    

z6AccountFileStorageTest.test_upgrade_version_productionzcertbot.compat.os.rmdirc                 C   sT   |   d¡ | j | j| j¡ t|_tjt	j
d| j_|   d¡ |  g | j ¡ ¡ d S )Nrq   rc   rm   )rk   r9   r?   r   rG   ÚOSErrorrd   r   r   r   rf   rg   r!   r;   )r   Z
mock_rmdirr   r   r   Útest_corrupted_accountá   s    
ÿ

z-AccountFileStorageTest.test_corrupted_accountc                 C   sV   |   d¡ | j | j| j¡ | j | jj¡}|   d¡ | j | jj¡}|  ||¡ d S rw   )rk   r9   r?   r   rG   r>   r%   r!   ©r   Zprev_accountr@   r   r   r   Útest_upgrade_loadì   s    

z(AccountFileStorageTest.test_upgrade_loadc                 C   sV   |   d¡ | j | j| j¡ | j | jj¡}|  d¡ | j | jj¡}|  ||¡ d S rw   )	rk   r9   r?   r   rG   r>   r%   Ú_set_server_and_stop_symlinkr!   r}   r   r   r   Ú test_upgrade_load_single_accountô   s    

z7AccountFileStorageTest.test_upgrade_load_single_accountc              	   C   sT   | j  | j| j¡ t ¡ }t|_t d|¡ |  	t
j| j j| jj¡ W 5 Q R X d S ©Nzsix.moves.builtins.open)r9   r?   r   rG   r   Ú	mock_openÚIOErrorrd   r   r<   r   rf   r>   r%   ©r   r‚   r   r   r   Útest_load_ioerrorü   s      ÿz(AccountFileStorageTest.test_load_ioerrorc              	   C   sD   t  ¡ }t|_t  d|¡  |  tj| jj	| j
| j¡ W 5 Q R X d S r   )r   r‚   rƒ   rd   r   r<   r   rf   r9   r?   r   rG   r„   r   r   r   Útest_save_ioerrors  s      þz)AccountFileStorageTest.test_save_ioerrorsc                 C   s>   | j  | j| j¡ | j  | jj¡ |  tj| j j	| jj¡ d S r    )
r9   r?   r   rG   Údeleter%   r<   r   r=   r>   r#   r   r   r   Útest_delete  s    z"AccountFileStorageTest.test_deletec                 C   s   |   tj| jj| jj¡ d S r    )r<   r   r=   r9   r‡   r   r%   r#   r   r   r   Útest_delete_no_account  s    z-AccountFileStorageTest.test_delete_no_accountc              	   C   sZ   |   d¡ | j | j| j¡ t di ¡( |   d¡ |  tj	| jj
| jj¡ W 5 Q R X d S )Nrq   z"certbot.constants.LE_REUSE_SERVERSrm   )rk   r9   r?   r   rG   r   r   r<   r   r=   r>   r%   r#   r   r   r   Ú!_assert_symlinked_account_removed  s
    

z8AccountFileStorageTest._assert_symlinked_account_removedc                 C   s˜   |   d¡ | j | j| j¡ |   d¡ | j | jj¡ |   |¡ | j | jj¡ |   d¡ |  t	j
| jj| jj¡ |   d¡ |  t	j
| jj| jj¡ d S rw   )rk   r9   r?   r   rG   r>   r%   r‡   r<   r   r=   )r   Z
server_urlr   r   r   Ú_test_delete_folders  s    




z+AccountFileStorageTest._test_delete_foldersc                 C   s   |   d¡ |  ¡  d S )Nrq   ©r‹   rŠ   r#   r   r   r   Útest_delete_folders_up.  s    
z-AccountFileStorageTest.test_delete_folders_upc                 C   s   |   d¡ |  ¡  d S rl   rŒ   r#   r   r   r   Útest_delete_folders_down2  s    
z/AccountFileStorageTest.test_delete_folders_downc              	   C   s<   |   |¡ ttj | jjd¡dƒ}| d¡ W 5 Q R X d S )NZfooÚwZbar)rk   rX   r   rJ   rR   r2   rL   Úwrite)r   Zserver_pathrZ   r   r   r   r   6  s    
z3AccountFileStorageTest._set_server_and_stop_symlinkc                 C   s   |   d¡ |  d¡ d S rp   ©r   r‹   r#   r   r   r   Útest_delete_shared_account_up;  s    
z4AccountFileStorageTest.test_delete_shared_account_upc                 C   s   |   d¡ |  d¡ d S rl   r‘   r#   r   r   r   Útest_delete_shared_account_down?  s    
z6AccountFileStorageTest.test_delete_shared_account_down)&r,   r-   r.   r/   r   rM   rV   r[   r]   r^   r_   ra   rh   ri   rk   ro   rt   ru   rv   ry   rz   r   r   r|   r~   r€   r…   r†   rˆ   r‰   rŠ   r‹   r   rŽ   r   r’   r“   Ú__classcell__r   r   rH   r   rA   ^   sB   

	

	rA   Ú__main__) r/   r   rY   ZunittestZjosepyZjoser   r   Zacmer   Zcertbot.tests.utilZtestsÚutilZ	test_utilZcertbotr   Zcertbot.compatr   r   r   ZJWKRSAr>   Zload_vectorr   ZTestCaser   ZConfigTestCaser0   r7   rA   r,   Úmainr   r   r   r   Ú<module>   s(   $ g