o
    Æ²…aœ"  ã                   @   sª   d Z ddlZddlZddlZddlmZ eeu reZ	neZ	ddl
mZmZ ddlmZmZmZmZ G dd„ dejƒZG dd	„ d	ejƒZG d
d„ deƒZG dd„ deƒZdS )z'Tests for the credential store classes.é    N)Ú	b64decode)Úfake_keyringÚInMemoryKeyring)ÚAccessTokenÚCredentialsÚKeyringCredentialStoreÚUnencryptedFileCredentialStorec                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTestAccessTokenz Tests for the AccessToken class.c                 C   s6   t  d¡}|  d|j¡ |  d|j¡ |  |j¡ d S )Nz;oauth_token_secret=secret%3Dpassword&oauth_token=lock%26keyúlock&keyúsecret=password)r   Úfrom_stringÚassertEqualÚkeyÚsecretÚassertIsNoneÚcontext©ÚselfÚaccess_token© r   úJ/usr/lib/python3/dist-packages/launchpadlib/tests/test_credential_store.pyÚtest_from_string/   s   ÿz TestAccessToken.test_from_stringc                 C   s8   t  d¡}|  d|j¡ |  d|j¡ |  d|j¡ d S )NzNoauth_token_secret=secret%3Dpassword&oauth_token=lock%26key&lp.context=firefoxr
   r   Úfirefox)r   r   r   r   r   r   r   r   r   r   Útest_from_string_with_context7   s   ÿz-TestAccessToken.test_from_string_with_contextN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r	   ,   s    r	   c                   @   ó   e Zd Zdd„ ZdS )ÚCredentialStoreTestCasec                 C   s   t ddt|dƒd�S )z(Helper method to make a fake credential.zapp namezconsumer_secret:42zaccess_secret:168)Úconsumer_secretr   )r   r   )r   Úconsumer_keyr   r   r   Úmake_credentialB   s
   ýz'CredentialStoreTestCase.make_credentialN)r   r   r   r"   r   r   r   r   r   A   ó    r   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú"TestUnencryptedFileCredentialStorez3Tests for the UnencryptedFileCredentialStore class.c                 C   s   t  ¡ \}| _t| jƒ| _d S ©N)ÚtempfileÚmkstempÚfilenamer   Ústore)r   Úignorer   r   r   ÚsetUpN   s   z(TestUnencryptedFileCredentialStore.setUpc                 C   s"   t j | j¡rt  | j¡ d S d S r%   )ÚosÚpathÚexistsr(   Úremove©r   r   r   r   ÚtearDownR   s   ÿz+TestUnencryptedFileCredentialStore.tearDownc                 C   s<   |   d¡}| j |d¡ | j d¡}|  |jj|jj¡ d S ©Núconsumer keyú
unique key©r"   r)   ÚsaveÚloadr   Úconsumerr   ©r   Ú
credentialÚcredential2r   r   r   Útest_save_and_loadV   s   
z5TestUnencryptedFileCredentialStore.test_save_and_loadc                 C   s<   |   d¡}| j |d¡ | j d¡}|  |jj|jj¡ d S )Nr3   zsome keyzsome other keyr5   r9   r   r   r   Útest_unique_id_doesnt_matter]   s   
z?TestUnencryptedFileCredentialStore.test_unique_id_doesnt_matterc                 C   sT   |   d¡}|   d¡}| j |d¡ | j |d¡ | j d¡}|  |jj|jj¡ d S )Nr3   úconsumer key2zunique key 1zunique key 2r5   ©r   Úcredential1r;   Úloadedr   r   r   Ú&test_file_only_contains_one_credentiale   s   

zITestUnencryptedFileCredentialStore.test_file_only_contains_one_credentialN)	r   r   r   r   r+   r1   r<   r=   rB   r   r   r   r   r$   K   s    r$   c                   @   sP   e Zd 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S )ÚTestKeyringCredentialStorez+Tests for the KeyringCredentialStore class.c                 C   s   t ƒ | _tƒ | _d S r%   )r   Úkeyringr   r)   r0   r   r   r   r+   u   s   z TestKeyringCredentialStore.setUpc                 C   sh   t | jƒ�% |  d¡}| j |d¡ | j d¡}|  |jj|jj¡ W d   ƒ d S 1 s-w   Y  d S r2   ©	r   rD   r"   r)   r6   r7   r   r8   r   r9   r   r   r   r<   y   s   
"üz-TestKeyringCredentialStore.test_save_and_loadc                 C   sª   t | jƒ�F |  d¡}| j |d¡ |  d¡}| j |d¡ | j d¡}|  |¡ |  |jj	|jj	¡ | j d¡}|  |jj	|jj	¡ W d   ƒ d S 1 sNw   Y  d S )Núconsumer key1zkey 1r>   zkey 2)
r   rD   r"   r)   r6   r7   Ú
assertTruer   r8   r   )r   r@   r;   Úloaded1Úloaded2r   r   r   Útest_lookup_by_unique_key�   s   


"ôz4TestKeyringCredentialStore.test_lookup_by_unique_keyc                 C   s€   t | jƒ�1 |  d¡}| j |d¡ |  d¡}| j |d¡ | j d¡}|  |jj|jj¡ W d   ƒ d S 1 s9w   Y  d S )NrF   zthe only keyr>   rE   r?   r   r   r   Ú/test_reused_unique_id_overwrites_old_credential’   s   

"øzJTestKeyringCredentialStore.test_reused_unique_id_overwrites_old_credentialc                 C   sB   t | jƒ� |  | j d¡¡ W d   ƒ d S 1 sw   Y  d S )Nzno such key)r   rD   r   r)   r7   r0   r   r   r   Útest_bad_unique_id_returns_none    s   "ÿz:TestKeyringCredentialStore.test_bad_unique_id_returns_nonec                    s¬   G ‡ fdd„dt ƒ‰ ˆ ƒ | _t| jƒ�9 |  d¡}|  |¡ | j |d¡ | j d¡}|  |¡ |  |j	j
|j	j
¡ |  |j	j|j	j¡ W d   ƒ d S 1 sOw   Y  d S )Nc                       ó   e Zd Z‡‡ fdd„Z‡  ZS )zWTestKeyringCredentialStore.test_keyring_returns_unicode.<locals>.UnicodeInMemoryKeyringc                    s*   t ˆ | ƒ ||¡}t|tƒr| d¡}|S )Nzutf-8)ÚsuperÚget_passwordÚ
isinstanceÚunicode_typeÚencode)r   ÚserviceÚusernameÚpassword)ÚUnicodeInMemoryKeyringÚ	__class__r   r   rO   «   s   
ÿ

zdTestKeyringCredentialStore.test_keyring_returns_unicode.<locals>.UnicodeInMemoryKeyring.get_password©r   r   r   rO   Ú__classcell__r   ©rV   ©rW   r   rV   ª   ó    rV   r3   r4   )r   rD   r   r"   rG   r)   r6   r7   r   r8   r   r   r9   r   rZ   r   Útest_keyring_returns_unicode¦   s   	


ÿ"øz7TestKeyringCredentialStore.test_keyring_returns_unicodec                    s˜   G ‡ fdd„dt ƒ‰ ˆ ƒ | _t| jƒ�/ |  d¡}| j |d¡ | j d¡}|  |jj	|jj	¡ |  |jj
|jj
¡ W d   ƒ d S 1 sEw   Y  d S )Nc                       rM   )zXTestKeyringCredentialStore.test_nonencoded_key_handled.<locals>.UnencodedInMemoryKeyringc                    s"   t ˆ | ƒ ||¡}t|dd … ƒS )Né   )rN   rO   r   )r   rS   rT   Úpw)ÚUnencodedInMemoryKeyringrW   r   r   rO   Ä   s   
ÿzeTestKeyringCredentialStore.test_nonencoded_key_handled.<locals>.UnencodedInMemoryKeyring.get_passwordrX   r   ©r`   r[   r   r`   Ã   r\   r`   r3   r4   )r   rD   r   r"   r)   r6   r7   r   r8   r   r   r9   r   ra   r   Útest_nonencoded_key_handledÀ   s   
ÿ"ûz6TestKeyringCredentialStore.test_nonencoded_key_handledc                 C   sv   G dd„ dt ƒ}|ƒ | _t| jƒ�  |  d¡}| j |d¡ | j d¡}|  |¡ W d   ƒ d S 1 s4w   Y  d S )Nc                   @   r   )zWTestKeyringCredentialStore.test_corrupted_key_handled.<locals>.CorruptedInMemoryKeyringc                 S   s   dS )NÚbadr   )r   rS   rT   r   r   r   rO   Ø   s   zdTestKeyringCredentialStore.test_corrupted_key_handled.<locals>.CorruptedInMemoryKeyring.get_passwordN)r   r   r   rO   r   r   r   r   ÚCorruptedInMemoryKeyring×   r#   rd   r3   r4   )r   rD   r   r"   r)   r6   r7   r   )r   rd   r:   r;   r   r   r   Útest_corrupted_key_handledÔ   s   
"üz5TestKeyringCredentialStore.test_corrupted_key_handledN)r   r   r   r   r+   r<   rJ   rK   rL   r]   rb   re   r   r   r   r   rC   r   s    rC   )r   r,   r&   ÚunittestÚbase64r   ÚbytesÚstrÚunicoderQ   Úlaunchpadlib.testing.helpersr   r   Úlaunchpadlib.credentialsr   r   r   r   ÚTestCaser	   r   r$   rC   r   r   r   r   Ú<module>   s   
'