o
    ;a                     @   s  d Z eZddlm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
zddlmZ W n ey9   ddlmZ Y nw ddlZddlmZ ddlmZ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mZmZmZmZm Z m!Z! ddlm"Z" 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(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+Z,G dd de+Z-G dd  d e
j$Z.G d!d" d"e
j$Z/d#d$ Z0dS )%zTests for the Launchpad class.    )contextmanagerN)patch)ServiceRoot)AccessTokenCredentials)uris)	Launchpad)UnencryptedFileCredentialStore)assert_keyring_not_importedBadSaveKeyringfake_keyringFauxSocketModuleInMemoryKeyringNoNetworkAuthorizationEngineNoNetworkLaunchpad)KeyringCredentialStorehttp://api.example.com/c                   @      e Zd ZdZdd ZdS )TestResourceTypeClassesz<launchpadlib must know about restfulclient's resource types.c                 C   s*   t j D ]\}}| tj| | qd S N)r   RESOURCE_TYPE_CLASSESitemsassertEqualr   )selfnamecls r   C/usr/lib/python3/dist-packages/launchpadlib/tests/test_launchpad.pytest_resource_typesB   s   z+TestResourceTypeClasses.test_resource_typesN)__name__
__module____qualname____doc__r   r   r   r   r   r   ?   s    r   c                   @   sl   e Zd ZdZdd Ze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S )TestNameLookupsz0Test the utility functions in the 'uris' module.c                 C   s   t g d| _d S )N)
production	qastagingstagingdogfooddevtest_devedge)sortedaliasesr   r   r   r   setUpL   s   
zTestNameLookups.setUpc                 c   s|    t jdd-}t d d V  | t|d |\}| t|jt | 	dt
| W d    d S 1 s7w   Y  d S )NTrecordalways   zno longer exists)warningscatch_warningssimplefilterr   len
assertTrue
issubclasscategoryDeprecationWarningassertInstr)r   caughtwarningr   r   r   edge_deprecation_errorY   s   
"z&TestNameLookups.edge_deprecation_errorc                 C   s4   |  ttj | j |  ttj | j d S r   )r   r+   r   service_rootskeysr,   	web_rootsr-   r   r   r   test_short_namesf   s   z TestNameLookups.test_short_namesc                 C   F   |    | tdtd W d    d S 1 sw   Y  d S Nr*   r$   r?   r   r   lookup_service_rootr-   r   r   r   $test_edge_service_root_is_productionk      
"z4TestNameLookups.test_edge_service_root_is_productionc                 C   rD   rE   r?   r   r   lookup_web_rootr-   r   r   r    test_edge_web_root_is_productiont   rI   z0TestNameLookups.test_edge_web_root_is_productionc                 C   H   |    | ttjtd W d    d S 1 sw   Y  d S Nr$   r?   r   r   rG   EDGE_SERVICE_ROOTr-   r   r   r   -test_edge_service_root_url_becomes_production}      

"z=TestNameLookups.test_edge_service_root_url_becomes_productionc                 C   rM   rN   )r?   r   r   rK   EDGE_WEB_ROOTr-   r   r   r   )test_edge_web_root_url_becomes_production   rR   z9TestNameLookups.test_edge_web_root_url_becomes_productionc                 C   rM   rN   rO   r-   r   r   r   /test_top_level_edge_constant_becomes_production   rR   z?TestNameLookups.test_top_level_edge_constant_becomes_productionc                 C   rD   )Nzhttps://api.edge.launchpad.net/r$   rF   r-   r   r   r   5test_edge_server_equivalent_string_becomes_production      
"zETestNameLookups.test_edge_server_equivalent_string_becomes_productionc                 C   rD   )Nzhttps://edge.launchpad.net/r$   rJ   r-   r   r   r   9test_edge_web_server_equivalent_string_becomes_production   rW   zITestNameLookups.test_edge_web_server_equivalent_string_becomes_productionc                 C   s   |    | jD ]}| t|tj|  qW d   n1 s!w   Y  |    | jD ]}| t|tj|  q.W d   n1 sGw   Y  d}| t|| | t|| d}| t	tj| | t	tj| dS )z=Ensure that short service names turn into long service names.Nzhttp://some-other-server.comz	not-a-url)
r?   r,   r   r   rG   r@   rK   rB   assertRaises
ValueError)r   alias
other_root	not_a_urlr   r   r   test_lookups   s(   



zTestNameLookups.test_lookupsN)r   r    r!   r"   r.   r   r?   rC   rH   rL   rQ   rT   rU   rV   rX   r^   r   r   r   r   r#   I   s    
		r#   c                   @   r   )"TestServiceNameWithEmbeddedVersiona3  Reject service roots that include the version at the end of the URL.

    If the service root is "http://api.launchpad.net/beta/" and the
    version is "beta", the launchpadlib constructor will raise an
    exception.

    This happens with scripts that were written against old versions
    of launchpadlib. The alternative is to try to silently fix it (the
    fix will eventually break as new versions of the web service are
    released) or to go ahead and make a request to
    http://api.launchpad.net/beta/beta/, and cause an unhelpful 404
    error.
    c              
   C   s   d}t jd | }ztd d d ||d W td ty3 } z| t|d W Y d }~nd }~ww |d7 }| jttd d d ||d t	j
}t jd | d }| jttd d d |d d S )Nzversion-foor&   )service_rootversionzoIt looks like you're using a service root that incorporates the name of the web service version ("version-foo")z*Expected a ValueError that was not thrown!/r`   )r   r@   r   rZ   r7   r<   
startswithAssertionErrorrY   r   DEFAULT_VERSION)r   ra   rootedefault_versionr   r   r   'test_service_name_with_embedded_version   s8   

zJTestServiceNameWithEmbeddedVersion.test_service_name_with_embedded_versionN)r   r    r!   r"   rj   r   r   r   r   r_      s    r_   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )#TestRequestTokenAuthorizationEnginez4Tests for the RequestTokenAuthorizationEngine class.c                 C   s   |  ttt d S r   rY   rZ   r   SERVICE_ROOTr-   r   r   r   test_app_must_be_identified   s   z?TestRequestTokenAuthorizationEngine.test_app_must_be_identifiedc                 C      t tdd d S Nr   application_namer   rm   r-   r   r   r   $test_application_name_identifies_app      zHTestRequestTokenAuthorizationEngine.test_application_name_identifies_appc                 C   ro   Nr   consumer_namers   r-   r   r   r   !test_consumer_name_identifies_app   ru   zETestRequestTokenAuthorizationEngine.test_consumer_name_identifies_appc                 C   s,   | j tttddd | j tttddd d S )Nname1name2)rr   rx   r   rl   r-   r   r   r   #test_conflicting_app_identification  s   

zGTestRequestTokenAuthorizationEngine.test_conflicting_app_identificationN)r   r    r!   r"   rn   rt   ry   r|   r   r   r   r   rk      s    rk   c                   @       e Zd ZdZdd Zdd ZdS )%TestLaunchpadLoginWithCredentialsFilez9Tests for Launchpad.login_with() with a credentials file.c                 C   sH   t  \}}tjd|d}t|}| |jj|j	jj t
| d S )Nnot important)rr   credentials_file)tempfilemkstempr   
login_withr   load_from_pathr   consumerkeycredentialsosremove)r   ignorefilename	launchpadr   r   r   r   test_filename  s   
z3TestLaunchpadLoginWithCredentialsFile.test_filenamec                 C   s6   t  \}}t }| jttjd||d t| d S )Nr   )rr   r   credential_store)	r   r   r   rY   rZ   r   r   r   r   )r   r   r   storer   r   r   +test_cannot_specify_both_filename_and_store)  s   zQTestLaunchpadLoginWithCredentialsFile.test_cannot_specify_both_filename_and_storeN)r   r    r!   r"   r   r   r   r   r   r   r~     s    r~   c                   @   s   e Zd Zdd ZdS )4TestLaunchpadLoginWithCredentialsFileFromEnvVariablec                 C   sr   t  \}}|tjd< tjdd}| |jt | 	|jj
| td tjd= | tjd t| d S )NLP_CREDENTIALS_FILEr   rq   )r   r   r   environr   r   assertIsInstancer   r	   r   r   unsetenvassertIsNonegetr   )r   r   r   r   r   r   r   r   :  s   

zBTestLaunchpadLoginWithCredentialsFileFromEnvVariable.test_filenameN)r   r    r!   r   r   r   r   r   r   6  s    r   c                   @   r}   )KeyringTestz*Base class for tests that use the keyring.c                 C   s   t   t tj_d S r   )r
   r   launchpadlibr   keyringr-   r   r   r   r.   R  s   zKeyringTest.setUpc                 C   s
   t j`d S r   )r   r   r   r-   r   r   r   tearDownY  s   
zKeyringTest.tearDownN)r   r    r!   r"   r.   r   r   r   r   r   r   O  s    r   c                       s   e Zd ZdZ fd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d&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Z  ZS )0TestLaunchpadLoginWithz!Tests for Launchpad.login_with().c                    s   t t|   t | _d S r   )superr   r.   r   mkdtemptemp_dirr-   	__class__r   r   r.   a  s   zTestLaunchpadLoginWith.setUpc                    s   t t|   t| j d S r   )r   r   r   shutilrmtreer   r-   r   r   r   r   e  s   zTestLaunchpadLoginWith.tearDownc                 C   s   t j| jd}tjdt|d | t j| t j|d}| t j| | t jt j|d t j|d}| 	t j| d S )Nr   r   r`   launchpadlib_dirapi.example.comcacher   )
r   pathjoinr   r   r   rm   r7   isdirassertFalse)r   r   service_pathcredentials_pathr   r   r   test_dirs_createdi  s   z(TestLaunchpadLoginWith.test_dirs_createdc                 C   s   t j| jd}t | t |d | t j| t |}t	|j
}| |tjtjB tjB  tjdt|d t |}t	|j
}| |tjtjB tjB  d S )Nr   i  r   r   )r   r   r   r   mkdirchmodr7   r   statS_IMODEst_modeassertNotEqualS_IWRITES_IREADS_IEXECr   r   rm   r   r   r   statinfomoder   r   r   'test_dirs_created_are_changed_to_secure  s   


z>TestLaunchpadLoginWith.test_dirs_created_are_changed_to_securec                 C   sf   t j| jd}tjdt|d | t j| t 	|}t	
|j}| |t	jt	jB t	jB  d S )Nr   r   r   )r   r   r   r   r   r   rm   r7   r   r   r   r   r   r   r   r   r   r   r   r   test_dirs_created_are_secure  s   
z3TestLaunchpadLoginWith.test_dirs_created_are_securec                 C   s\   t j| jd}tjdt|dd}| |jd d tjdt|dd}| |jd d d S )Nr   r   foo)r`   r   ra   ra   bar)	r   r   r   r   r   r   rm   r   passed_in_argsr   r   r   r   r   r   test_version_is_propagated  s   z1TestLaunchpadLoginWith.test_version_is_propagatedc                 C   sX   t j| jd}tjdt|d}| |jj	j
d tjdt|d}| |jj	j
d d S )Nr   zvery importantr   )r   r   r   r   r   r   rm   r   r   r   rr   r   r   r   r   #test_application_name_is_propagated  s"   
	
z:TestLaunchpadLoginWith.test_application_name_is_propagatedc                 C   s6   t td}tj|d | |jd | |jd d S )Napplication nameauthorization_enginer2   )r   rm   r   r   r   request_tokens_obtainedaccess_tokens_obtainedr   enginer   r   r   'test_authorization_engine_is_propagated  s   
z>TestLaunchpadLoginWith.test_authorization_engine_is_propagatedc                 C   s   |  ttj d S r   )rY   rZ   r   r   r-   r   r   r   )test_login_with_must_identify_application     z@TestLaunchpadLoginWith.test_login_with_must_identify_applicationc                 C      t jdd d S rp   r   r   r-   r   r   r   rt        z;TestLaunchpadLoginWith.test_application_name_identifies_appc                 C   r   rv   r   r-   r   r   r   ry     r   z8TestLaunchpadLoginWith.test_consumer_name_identifies_appc                 C   s"   t td}| jttjd|d dS );Catch an attempt to specify inconsistent application_names.zapplication name1zapplication name2r   Nr   rm   rY   rZ   r   r   r   r   r   r   +test_inconsistent_application_name_rejected  s   
zBTestLaunchpadLoginWith.test_inconsistent_application_name_rejectedc                 C   s&   t tddd}| jttjd|d dS )r   Nconsumer_name1rw   consumer_name2r   r   r   r   r   r   (test_inconsistent_consumer_name_rejected  s   
z?TestLaunchpadLoginWith.test_inconsistent_consumer_name_rejectedc                 C   s.   t tddgd}| jttjdddg|d dS )z=Catch an attempt to specify inconsistent allow_access_levels.r   FOOrx   allow_access_levelsNBAR)rx   r   r   r   r   r   r   r   .test_inconsistent_allow_access_levels_rejected  s   
zETestLaunchpadLoginWith.test_inconsistent_allow_access_levels_rejectedc                 C   s4   dd }t |d}dd }| jttjd||d d S )Nc                   S      d S r   r   r   r   r   r   	callback1     zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback1)credential_save_failedc                   S   r   r   r   r   r   r   r   	callback2  r   zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback2app name)r   r   )r   rY   rZ   r   r   )r   r   r   r   r   r   r   (test_inconsistent_credential_save_failed  s   

z?TestLaunchpadLoginWith.test_inconsistent_credential_save_failedc                 C   sJ   t jddgd}| |jjjd | |jjjd  | |jjdg d S )Nr   r   r   )	r   r   r   r   r   r   rr   r   r   r   r   r   r   r   test_non_desktop_integration)  s   
z3TestLaunchpadLoginWith.test_non_desktop_integrationc                 C   s&   t jddgd}| |jjdg d S )Nr   r   )r   DESKTOP_INTEGRATION)r   r   r   r   r   r   r   r   r   <test_desktop_integration_doesnt_happen_without_consumer_name6  s   zSTestLaunchpadLoginWith.test_desktop_integration_doesnt_happen_without_consumer_namec                 C   s   t  }t  }tjd| jt||d}| |jjjt	j
 | |jjjd | |jjdg tttj| jdd||tjd}| |j| d S )Nr   )r   r`   timeout
proxy_infor   r   r   )r`   r   r   r   ra   )objectr   r   r   rm   r   r   access_tokenr   r   ACCESS_TOKEN_KEYr   rr   r   r   dictr   r   r   rf   r   )r   r   r   r   expected_argumentsr   r   r   *test_no_credentials_creates_new_credentialC  s8   
zATestLaunchpadLoginWith.test_no_credentials_creates_new_credentialc                 C   s`   t jd| jtd}| |jjjd | |jjjd t	j
| jddd}| t	j
| dS )z)Test the anonymous login helper function.zanonymous access)r   r`    r   r   N)r   login_anonymouslyr   rm   r   r   r   r   secretr   r   r   r   exists)r   r   r   r   r   r   test_anonymous_logine  s   
z+TestLaunchpadLoginWith.test_anonymous_loginc              
   C   s   t t j| jdd t j| jddd}tddtddd}|| t }t }d}t	j
d| jt|||d	}tt|||t j| jdd
d}| D ]\}}	|j| }
| |
|	 qMd S )Nr   r   r   zconsumer_secret:42zaccess_key:84zaccess_secret:168)consumer_secretr   r   )r   r`   r   r   ra   r   )r`   r   r   ra   r   )r   makedirsr   r   r   r   r   save_to_pathr   r   r   rm   r   r   r   r   )r   credentials_file_pathr   r   r   ra   r   r   r   expectedactualr   r   r   -test_existing_credentials_arguments_passed_onu  sD   


zDTestLaunchpadLoginWith.test_existing_credentials_arguments_passed_onc              	   C   s   t jd}| jt jd< tjdtd}|d ur|t jd< nt jd= |jd }t j	t j
|dd}| |t j
| jd | t jt j
|dd d S )NHOMEr   rc   r   z..z.launchpadlibr   )r   r   r   r   r   r   rm   r   r   abspathr   r   r7   r   )r   old_homer   	cache_dirr   r   r   r   test_None_launchpadlib_dir  s$   
z1TestLaunchpadLoginWith.test_None_launchpadlib_dirc                 C   sb   t dd}| |jd d t dtjd }| |jd tjd  d}| tt jdd d S )Nr   r&   r`   z"https://api.staging.launchpad.net/)r   zhttps://r   )r   r   r   r   r   r@   rY   rZ   r   r   r   r   test_short_service_name  s    

z.TestLaunchpadLoginWith.test_short_service_namec                 C   s   t jddd d S )Nr      )max_failed_attemptsr   r-   r   r   r   !test_max_failed_attempts_accepted  r   z8TestLaunchpadLoginWith.test_max_failed_attempts_accepted)r   r    r!   r"   r.   r   r   r   r   r   r   r   r   rt   ry   r   r   r   r   r   r   r   r   r   r   r   r  __classcell__r   r   r   r   r   ^  s2    	"&r   c                   @   r}   )TestDeprecatedLoginMethodsz2Make sure the deprecated login methods still work.c                 C   sx   t jdd,}t d t dt tddd | t|d | |d	 jt	 W d    d S 1 s5w   Y  d S )
NTr/   r1   r   r   tokenr   r2   r   )
r3   r4   r5   PendingDeprecationWarningr   loginr   r6   r9   r:   r   r=   r   r   r   test_login_is_deprecated  s   
"z3TestDeprecatedLoginMethods.test_login_is_deprecatedc                 C   s   t jdd0}t d t dt t dd td | t|d | |d j	t
 W d    d S 1 s9w   Y  d S )	NTr/   r1   r   z.*next release of cryptographyr   r2   r   )r3   r4   r5   r  filterwarningsr   get_token_and_loginr   r6   r9   r:   r  r   r   r   &test_get_token_and_login_is_deprecated  s   

"zATestDeprecatedLoginMethods.test_get_token_and_login_is_deprecatedN)r   r    r!   r"   r	  r  r   r   r   r   r    s    	r  c                   @   sv   e Zd Zdd Zdd Zeededd dd	 Z	eeded
d dd Z
eededd dd ZdS )!TestCredenitialSaveFailedCallbackc                 C      t  tj_t | _d S r   r   r   r   socketr   r   r   r-   r   r   r   r.        
z'TestCredenitialSaveFailedCallback.setUpc                 C      t tj_ t| j d S r   r  r   r   r   r   r   r-   r   r   r   r        z*TestCredenitialSaveFailedCallback.tearDown_is_sudoc                   C      dS NFr   r   r   r   r   <lambda>      z*TestCredenitialSaveFailedCallback.<lambda>c                    sv   g   fdd}t j| jd}d}tt  tjd|||d | t	 d W d    d S 1 s4w   Y  d S )Nc                      s     d  d S r   )appendr   callback_calledr   r   callback  s   zPTestCredenitialSaveFailedCallback.test_credentials_save_failed.<locals>.callbackr   r   r   )r`   r   r   r2   )
r   r   r   r   r   r   r   r   r   r6   )r   r  r   r`   r   r  r   test_credentials_save_failed  s   "z>TestCredenitialSaveFailedCallback.test_credentials_save_failedc                   C   r  r  r   r   r   r   r   r    r  c                 C   sZ   t j| jd}d}tt  | jttj	d||d W d    d S 1 s&w   Y  d S Nr   r   r   r   )
r   r   r   r   r   r   rY   RuntimeErrorr   r   r   r   r`   r   r   r   :test_default_credentials_save_failed_is_to_raise_exception  s   "z\TestCredenitialSaveFailedCallback.test_default_credentials_save_failed_is_to_raise_exceptionc                   C   r  )NTr   r   r   r   r   r  !  r  c                 C   sT   t j| jd}d}tt  tjd||d W d    d S 1 s#w   Y  d S r  )r   r   r   r   r   r   r   r   r!  r   r   r   >test_credentials_save_fail_under_sudo_does_not_raise_exception!  s   "z`TestCredenitialSaveFailedCallback.test_credentials_save_fail_under_sudo_does_not_raise_exceptionN)r   r    r!   r.   r   r   r   r   staticmethodr  r"  r#  r   r   r   r   r    s    

r  c                   @   sX   e Zd Zdd Zdd Zeededd dd	 Z	eeded
d dd Z
dS )TestMultipleSitesc                 C   r  r   r  r-   r   r   r   r.   4  r  zTestMultipleSites.setUpc                 C   r  r   r  r-   r   r   r   r   ;  r  zTestMultipleSites.tearDownr  c                   C   r  r  r   r   r   r   r   r  ?  r  zTestMultipleSites.<lambda>c                 C   s   t j| jd}t }d}d}t| tj|||d}|jj	j
}W d    n1 s+w   Y  t|j d d }| || | || | ||d |  d S )Nr   r   zSuper App 3000r   r   r2   @)r   r   r   r   r   r   r   r   r   r   r   listdatarA   r;   r   )r   r   r   r`   rr   r   rx   application_keyr   r   r   "test_components_of_application_key?  s    
z4TestMultipleSites.test_components_of_application_keyc                   C   r  r  r   r   r   r   r   r  Z  r  c                 C   s   t j| jd}t }|jrJ dt| tjdd|d tjdd|d W d    n1 s1w   Y  t	|j
 dksAJ t|j
 d d	 }t|j
 d	 d	 }| || d S )
Nr   z$oops, a fresh keyring has data in itr   zhttp://alpha.example.com/r   zhttp://beta.example.com/   r   r2   )r   r   r   r   r   r(  r   r   r   r6   rA   r'  r   )r   r   r   application_key_1application_key_2r   r   r   test_same_app_different_serversZ  s&   
z1TestMultipleSites.test_same_app_different_serversN)r   r    r!   r.   r   r   r   r   r$  r*  r.  r   r   r   r   r%  /  s    
r%  c                   C   s   t  tS r   )unittest
TestLoaderloadTestsFromNamer   r   r   r   r   
test_suitew  s   r2  )1r"   type__metaclass__
contextlibr   r   r   r  r   r   r/  unittest.mockr   ImportErrormockr3   lazr.restfulclient.resourcer   launchpadlib.credentialsr   r   r   r   launchpadlib.launchpadr   r	   launchpadlib.testing.helpersr
   r   r   r   r   r   r   r   rm   TestCaser   r#   r_   rk   r~   r   r   r   r  r  r%  r2  r   r   r   r   <module>   sL   $	
t8#  wCH