U
    [e"                     @   s   d Z ddlZddlZddlZddlmZ eekr6e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/var/www/html/services/stratfitenv/lib/python3.8/site-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   Z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                   @   s   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)Zconsumer_secretr   )r   r   )r   Zconsumer_keyr   r   r   make_credentialB   s
    z'CredentialStoreTestCase.make_credentialN)r   r   r   r   r   r   r   r   r   A   s   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 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 )Nr.   zsome keyzsome other keyr0   r4   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 )Nr.   consumer key2zunique key 1zunique key 2r0   r   credential1r6   Zloadedr   r   r   &test_file_only_contains_one_credentiale   s    

zITestUnencryptedFileCredentialStore.test_file_only_contains_one_credentialN)	r   r   r   r   r&   r,   r7   r8   r<   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$   r+   r   r   r   r&   u   s    z TestKeyringCredentialStore.setUpc              	   C   sR   t | j> | d}| j|d | jd}| |jj|jj W 5 Q R X d S r-   	r   r>   r   r$   r1   r2   r   r3   r   r4   r   r   r   r7   y   s
    
z-TestKeyringCredentialStore.test_save_and_loadc              	   C   s   t | j | d}| j|d | d}| j|d | jd}| | | |jj	|jj	 | jd}| |jj	|jj	 W 5 Q R X d S )Nconsumer key1zkey 1r9   zkey 2)
r   r>   r   r$   r1   r2   
assertTruer   r3   r   )r   r;   r6   Zloaded1Zloaded2r   r   r   test_lookup_by_unique_key   s    


z4TestKeyringCredentialStore.test_lookup_by_unique_keyc              	   C   sj   t | jV | d}| j|d | d}| j|d | jd}| |jj|jj W 5 Q R X d S )Nr@   zthe only keyr9   r?   r:   r   r   r   /test_reused_unique_id_overwrites_old_credential   s    

zJTestKeyringCredentialStore.test_reused_unique_id_overwrites_old_credentialc              	   C   s,   t | j | | jd W 5 Q R X d S )Nzno such key)r   r>   r   r$   r2   r+   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f | d}| | | j|d | jd}| | | |j	j
|j	j
 | |j	j|j	j W 5 Q R X d S )Nc                       s   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   rF      s    
 

zdTestKeyringCredentialStore.test_keyring_returns_unicode.<locals>.UnicodeInMemoryKeyring.get_passwordr   r   r   rF   __classcell__r   rM   rN   r   rM      s   rM   r.   r/   )r   r>   r   r   rA   r$   r1   r2   r   r3   r   r   r4   r   rQ   r   test_keyring_returns_unicode   s    	


 z7TestKeyringCredentialStore.test_keyring_returns_unicodec              	      s   G  fdddt    | _t| jR | d}| j|d | jd}| |jj	|jj	 | |jj
|jj
 W 5 Q R X d S )Nc                       s   e Zd Z fddZ  ZS )zXTestKeyringCredentialStore.test_nonencoded_key_handled.<locals>.UnencodedInMemoryKeyringc                    s"   t  | ||}t|dd  S )N   )rE   rF   r   )r   rJ   rK   pw)UnencodedInMemoryKeyringrN   r   r   rF      s
    
 zeTestKeyringCredentialStore.test_nonencoded_key_handled.<locals>.UnencodedInMemoryKeyring.get_passwordrO   r   rV   rR   r   rV      s   rV   r.   r/   )r   r>   r   r   r$   r1   r2   r   r3   r   r   r4   r   rW   r   test_nonencoded_key_handled   s    
 z6TestKeyringCredentialStore.test_nonencoded_key_handledc              	   C   s`   G dd dt }| | _t| j4 | d}| j|d | jd}| | W 5 Q R X d S )Nc                   @   s   e Zd Zdd ZdS )zWTestKeyringCredentialStore.test_corrupted_key_handled.<locals>.CorruptedInMemoryKeyringc                 S   s   dS )Nbadr   )r   rJ   rK   r   r   r   rF      s    zdTestKeyringCredentialStore.test_corrupted_key_handled.<locals>.CorruptedInMemoryKeyring.get_passwordN)r   r   r   rF   r   r   r   r   CorruptedInMemoryKeyring   s   rZ   r.   r/   )r   r>   r   r   r$   r1   r2   r   )r   rZ   r5   r6   r   r   r   test_corrupted_key_handled   s    
z5TestKeyringCredentialStore.test_corrupted_key_handledN)r   r   r   r   r&   r7   rB   rC   rD   rS   rX   r[   r   r   r   r   r=   r   s   r=   )r   r'   r!   Zunittestbase64r   bytesstrunicoderH   Zlaunchpadlib.testing.helpersr   r   Zlaunchpadlib.credentialsr   r   r   r   ZTestCaser	   r   r   r=   r   r   r   r   <module>   s   
'