U
    [e                     @   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k
rt   ddlmZ Y nX 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                   @   s   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   ZRESOURCE_TYPE_CLASSESitemsassertEqualr   )selfnamecls r   c/var/www/html/services/stratfitenv/lib/python3.8/site-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 dddddddg| _d S )N
productionZ	qastagingstagingZdogfooddevZtest_devedge)sortedaliasesr   r   r   r   setUpL   s    zTestNameLookups.setUpc              	   c   sd   t jddN}t d d V  | t|d |\}| t|jt | 	dt
| W 5 Q R X 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   s0   |    | tdtd W 5 Q R X d S Nr%   r"   r:   r   r   lookup_service_rootr(   r   r   r   $test_edge_service_root_is_productionk   s
    
z4TestNameLookups.test_edge_service_root_is_productionc              	   C   s0   |    | tdtd W 5 Q R X d S r?   r:   r   r   lookup_web_rootr(   r   r   r    test_edge_web_root_is_productiont   s
    
z0TestNameLookups.test_edge_web_root_is_productionc              	   C   s2   |     | ttjtd W 5 Q R X d S Nr"   r:   r   r   rA   ZEDGE_SERVICE_ROOTr(   r   r   r   -test_edge_service_root_url_becomes_production}   s
    

z=TestNameLookups.test_edge_service_root_url_becomes_productionc              	   C   s2   |     | ttjtd W 5 Q R X d S rF   )r:   r   r   rD   ZEDGE_WEB_ROOTr(   r   r   r   )test_edge_web_root_url_becomes_production   s
    

z9TestNameLookups.test_edge_web_root_url_becomes_productionc              	   C   s2   |     | ttjtd W 5 Q R X d S rF   rG   r(   r   r   r   /test_top_level_edge_constant_becomes_production   s
    

z?TestNameLookups.test_top_level_edge_constant_becomes_productionc              	   C   s0   |    | tdtd W 5 Q R X d S )Nzhttps://api.edge.launchpad.net/r"   r@   r(   r   r   r   5test_edge_server_equivalent_string_becomes_production   s
    
zETestNameLookups.test_edge_server_equivalent_string_becomes_productionc              	   C   s0   |    | tdtd W 5 Q R X d S )Nzhttps://edge.launchpad.net/r"   rC   r(   r   r   r   9test_edge_web_server_equivalent_string_becomes_production   s
    
zITestNameLookups.test_edge_web_server_equivalent_string_becomes_productionc              	   C   s   |   * | jD ]}| t|tj|  qW 5 Q R X |   * | jD ]}| t|tj|  qHW 5 Q R X d}| t|| | t|| d}| t	tj| | t	tj| dS )z=Ensure that short service names turn into long service names.zhttp://some-other-server.comz	not-a-urlN)
r:   r'   r   r   rA   r;   rD   r=   assertRaises
ValueError)r   aliasZ
other_rootZ	not_a_urlr   r   r   test_lookups   s$    

 

 zTestNameLookups.test_lookupsN)r   r   r   r    r)   r   r:   r>   rB   rE   rH   rI   rJ   rK   rL   rP   r   r   r   r   r!   I   s   
		r!   c                   @   s   e Zd ZdZdd ZdS )"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 n6 tk
r^ } z| t|d W 5 d }~X Y n
X td|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!/rR   )r   r;   r   rN   r2   r7   
startswithAssertionErrorrM   r   DEFAULT_VERSION)r   rS   rooteZdefault_versionr   r   r   'test_service_name_with_embedded_version   s>         zJTestServiceNameWithEmbeddedVersion.test_service_name_with_embedded_versionN)r   r   r   r    r[   r   r   r   r   rQ      s   rQ   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   rM   rN   r   SERVICE_ROOTr(   r   r   r   test_app_must_be_identified   s
      z?TestRequestTokenAuthorizationEngine.test_app_must_be_identifiedc                 C   s   t tdd d S Nr   application_namer   r^   r(   r   r   r   $test_application_name_identifies_app   s    zHTestRequestTokenAuthorizationEngine.test_application_name_identifies_appc                 C   s   t tdd d S Nr   consumer_namerc   r(   r   r   r   !test_consumer_name_identifies_app   s    zETestRequestTokenAuthorizationEngine.test_consumer_name_identifies_appc                 C   s,   | j tttddd | j tttddd d S )NZname1Zname2)rb   rg   r   r]   r(   r   r   r   #test_conflicting_app_identification  s    
zGTestRequestTokenAuthorizationEngine.test_conflicting_app_identificationN)r   r   r   r    r_   rd   rh   ri   r   r   r   r   r\      s
   r\   c                   @   s    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)rb   credentials_file)tempfilemkstempr   
login_withr   Zload_from_pathr   consumerkeycredentialsosremove)r   ignorefilename	launchpadrr   r   r   r   test_filename  s     
 z3TestLaunchpadLoginWithCredentialsFile.test_filenamec                 C   s6   t  \}}t }| jttjd||d t| d S )Nrk   )rb   rl   credential_store)	rm   rn   r   rM   rN   r   ro   rs   rt   )r   ru   rv   storer   r   r   +test_cannot_specify_both_filename_and_store)  s    zQTestLaunchpadLoginWithCredentialsFile.test_cannot_specify_both_filename_and_storeN)r   r   r   r    rx   r{   r   r   r   r   rj     s   rj   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 )NZLP_CREDENTIALS_FILErk   ra   )rm   rn   rs   environr   ro   ZassertIsInstancery   r	   r   rv   unsetenvZassertIsNonegetrt   )r   ru   rv   rw   r   r   r   rx   :  s    
 
zBTestLaunchpadLoginWithCredentialsFileFromEnvVariable.test_filenameN)r   r   r   rx   r   r   r   r   r|   6  s   r|   c                   @   s    e Zd ZdZdd Zdd ZdS )KeyringTestz*Base class for tests that use the keyring.c                 C   s   t   t tj_d S r   )r
   r   launchpadlibrr   keyringr(   r   r   r   r)   R  s    zKeyringTest.setUpc                 C   s
   t j`d S r   )r   rr   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)   rm   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   rk   rR   launchpadlib_dirapi.example.comcacherr   )
rs   pathjoinr   r   ro   r^   r2   isdirassertFalse)r   r   Z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  rk   r   )rs   r   r   r   mkdirchmodr2   r   statS_IMODEst_modeassertNotEqualS_IWRITES_IREADS_IEXECr   ro   r^   r   r   r   Z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   rk   r   )rs   r   r   r   r   ro   r^   r2   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   rk   foo)rR   r   rS   rS   bar)	rs   r   r   r   r   ro   r^   r   passed_in_argsr   r   rw   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   )rs   r   r   r   r   ro   r^   r   rr   rp   rb   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_enginer-   )r   r^   r   ro   r   Zrequest_tokens_obtainedZaccess_tokens_obtainedr   Zenginer   r   r   'test_authorization_engine_is_propagated  s    
z>TestLaunchpadLoginWith.test_authorization_engine_is_propagatedc                 C   s   |  ttj d S r   )rM   rN   r   ro   r(   r   r   r   )test_login_with_must_identify_application  s    z@TestLaunchpadLoginWith.test_login_with_must_identify_applicationc                 C   s   t jdd d S r`   r   ro   r(   r   r   r   rd     s    z;TestLaunchpadLoginWith.test_application_name_identifies_appc                 C   s   t jdd d S re   r   r(   r   r   r   rh     s    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   r^   rM   rN   r   ro   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   NZconsumer_name1rf   Z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.rp   FOOrg   allow_access_levelsNZBAR)rg   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   s   d S r   r   r   r   r   r   	callback1  s    zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback1)credential_save_failedc                   S   s   d S r   r   r   r   r   r   	callback2  s    zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback2app name)ry   r   )r   rM   rN   r   ro   )r   r   rz   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 )Nrp   r   r   )	r   ro   r   rr   rp   rq   rb   r   r   r   rw   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   ro   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   rR   timeout
proxy_infor   r   r   )rR   r   r   r   rS   )objectr   ro   r   r^   r   rr   access_tokenrq   r   ZACCESS_TOKEN_KEYrp   rb   r   r   dictrs   r   r   rX   r   )r   r   r   rw   expected_argumentsr   r   r   *test_no_credentials_creates_new_credentialC  s:     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   rR    r   rr   N)r   Zlogin_anonymouslyr   r^   r   rr   r   rq   secretrs   r   r   r   exists)r   rw   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| }
| |
|	 qd S )Nr   rr   r   zconsumer_secret:42zaccess_key:84zaccess_secret:168)Zconsumer_secretr   r   )r   rR   r   r   rS   r   )rR   r   r   rS   r   )rs   makedirsr   r   r   r   r   Zsave_to_pathr   r   ro   r^   r   r   r   r   )r   Zcredentials_file_pathrr   r   r   rS   rw   r   rq   expectedactualr   r   r   -test_existing_credentials_arguments_passed_onu  sH       

zDTestLaunchpadLoginWith.test_existing_credentials_arguments_passed_onc              	   C   s   t jd}| jt jd< tjdtd}|d k	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   rU   r   z..z.launchpadlibr   )rs   r}   r   r   r   ro   r^   r   r   abspathr   r   r2   r   )r   Zold_homerw   	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#   rR   z"https://api.staging.launchpad.net/)r   zhttps://r   )r   ro   r   r   r   r;   rM   rN   r   r   r   r   test_short_service_name  s(        z.TestLaunchpadLoginWith.test_short_service_namec                 C   s   t jddd d S )Nrk      )Zmax_failed_attemptsr   r(   r   r   r   !test_max_failed_attempts_accepted  s    z8TestLaunchpadLoginWith.test_max_failed_attempts_accepted)r   r   r   r    r)   r   r   r   r   r   r   r   r   rd   rh   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   ^  s0   	"&r   c                   @   s    e Zd ZdZdd Zdd ZdS )TestDeprecatedLoginMethodsz2Make sure the deprecated login methods still work.c              	   C   sb   t jddL}t d t dt tddd | t|d | |d	 jt	 W 5 Q R X d S )
NTr*   r,   ru   rp   tokenr   r-   r   )
r.   r/   r0   PendingDeprecationWarningr   loginr   r1   r4   r5   r   r8   r   r   r   test_login_is_deprecated  s    
z3TestDeprecatedLoginMethods.test_login_is_deprecatedc              	   C   sj   t jddT}t d t dt t dd td | t|d | |d j	t
 W 5 Q R X d S )	NTr*   r,   ru   z.*next release of cryptographyrp   r-   r   )r.   r/   r0   r   filterwarningsr   Zget_token_and_loginr   r1   r4   r5   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   s   t  tj_t | _d S r   r   r   rw   socketrm   r   r   r(   r   r   r   r)     s    
z'TestCredenitialSaveFailedCallback.setUpc                 C   s   t tj_ t| j d S r   r   r   rw   r   r   r   r(   r   r   r   r     s    z*TestCredenitialSaveFailedCallback.tearDown_is_sudoc                   C   s   dS NFr   r   r   r   r   <lambda>      z*TestCredenitialSaveFailedCallback.<lambda>c              	      s`   g   fdd}t j| jd}d}tt ( tjd|||d | t	 d W 5 Q R X d S )Nc                      s     d  d S r   )appendr   Zcallback_calledr   r   callback  s    zPTestCredenitialSaveFailedCallback.test_credentials_save_failed.<locals>.callbackr   r   rk   )rR   r   r   r-   )
rs   r   r   r   r   r   r   ro   r   r1   )r   r   r   rR   r   r   r   test_credentials_save_failed  s    z>TestCredenitialSaveFailedCallback.test_credentials_save_failedc                   C   s   dS r   r   r   r   r   r   r     r   c              	   C   sD   t j| jd}d}tt  | jttj	d||d W 5 Q R X d S Nr   r   rk   r   )
rs   r   r   r   r   r   rM   RuntimeErrorr   ro   r   r   rR   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   s   dS )NTr   r   r   r   r   r   !  r   c              	   C   s>   t j| jd}d}tt  tjd||d W 5 Q R X d S r   )rs   r   r   r   r   r   r   ro   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   s   t  tj_t | _d S r   r   r(   r   r   r   r)   4  s    
zTestMultipleSites.setUpc                 C   s   t tj_ t| j d S r   r   r(   r   r   r   r   ;  s    zTestMultipleSites.tearDownr   c                   C   s   dS 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 5 Q R X t|j d d }| || | || | ||d |  d S )Nr   r   zSuper App 3000r   r   r-   @)rs   r   r   r   r   r   r   ro   rr   rp   rq   listdatar<   r6   r   )r   r   r   rR   rb   rw   rg   Zapplication_keyr   r   r   "test_components_of_application_key?  s    
z4TestMultipleSites.test_components_of_application_keyc                   C   s   dS r   r   r   r   r   r   r   Z  r   c              	   C   s   t j| jd}t }|jr$tdt|& tj	dd|d tj	dd|d W 5 Q R X t
|j dksnt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   r-   )rs   r   r   r   r   r   rW   r   r   ro   r1   r<   r   r   )r   r   r   Zapplication_key_1Z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   )unittestZ
TestLoaderZloadTestsFromNamer   r   r   r   r   
test_suitew  s    r   )1r    typeZ__metaclass__
contextlibr   rs   r   r   r   rm   r   Zunittest.mockr   ImportErrorZmockr.   Zlazr.restfulclient.resourcer   Zlaunchpadlib.credentialsr   r   r   r   Zlaunchpadlib.launchpadr   r	   Zlaunchpadlib.testing.helpersr
   r   r   r   r   r   r   r   r^   ZTestCaser   r!   rQ   r\   rj   r|   r   r   r   r   r   r   r   r   r   r   <module>   sH   $	
t8#  wCH