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   
'