U
    ¶[Öeü$  ã                   @   s   d Z ddlmZ ddlmZ ddlZddlZzddlmZ W n ek
rT   e	ZY nX ddl
mZ ddlmZ ddlmZmZ dd	lmZ d
Zei ƒ d¡ZG dd„ dƒ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ƒZG dd„ deƒZdS )z,Tests for the LaunchpadOAuthAwareHTTP class.é    )Údeque)ÚdumpsN)ÚJSONDecodeError)ÚUnauthorized)ÚUnencryptedFileCredentialStore)Ú	LaunchpadÚLaunchpadOAuthAwareHttp)ÚNoNetworkAuthorizationEnginesÝ  <?xml version="1.0"?>
<application xmlns="http://research.sun.com/wadl/2006/10">
  <resources base="http://www.example.com/">
    <resource path="" type="#service-root"/>
  </resources>

  <resource_type id="service-root">
    <method name="GET" id="service-root-get">
      <response>
        <representation href="#service-root-json"/>
      </response>
    </method>
  </resource_type>

  <representation id="service-root-json" mediaType="application/json"/>
</application>
zutf-8c                   @   s   e Zd ZdZdd„ ZdS )ÚResponsezA fake HTTP response object.c                 C   s   || _ || _d S ©N)ÚstatusÚcontent)Úselfr   r   © r   ú^/var/www/html/services/stratfitenv/lib/python3.8/site-packages/launchpadlib/tests/test_http.pyÚ__init__@   s    zResponse.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r
   =   s   r
   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚSimulatedResponsesHttpz<Responds to HTTP requests by shifting responses off a stack.c                    s&   t t| ƒj|Ž  g | _|| _d| _dS )z{Constructor.

        :param responses: A list of HttpResponse objects to use
            in response to requests.
        N)Úsuperr   r   Úsent_responsesÚunsent_responsesÚcache)r   Ú	responsesÚargs©Ú	__class__r   r   r   H   s    zSimulatedResponsesHttp.__init__c                 G   s*   | j  ¡ }| j |¡ | j||jf|žŽ S r   )r   Úpopleftr   ÚappendZretry_on_bad_tokenr   )r   r   Úresponser   r   r   Ú_requestS   s    
zSimulatedResponsesHttp._request)r   r   r   r   r   r"   Ú__classcell__r   r   r   r   r   E   s   r   c                   @   s$   e Zd Zg Zdd„ Zedd„ ƒZdS )ÚSimulatedResponsesLaunchpadc                 G   s   t t| jƒ| | jf|žŽ S r   )r   r   r   Úauthorization_engine)r   r   r   r   r   ÚhttpFactory_   s      ÿÿz'SimulatedResponsesLaunchpad.httpFactoryc                 C   s   t t ¡ d |ƒS )Né   )r   ÚtempfileÚmkstemp)ÚclsZcredential_save_failedr   r   r   Úcredential_store_factoryd   s    
 ÿz4SimulatedResponsesLaunchpad.credential_store_factoryN)r   r   r   r   r&   Úclassmethodr+   r   r   r   r   r$   Y   s   r$   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSimulatedResponsesTestCasezATest cases that give fake responses to launchpad's HTTP requests.c                 C   s   g t _tddƒ| _dS )z*Clear out the list of simulated responses.zhttp://api.example.com/úapplication nameN)r$   r   r	   Úengine©r   r   r   r   ÚsetUpn   s
     ÿz SimulatedResponsesTestCase.setUpc                 G   s   |t _t jd| jdS )aq  Use simulated HTTP responses to get a Launchpad object.

        The given Response objects will be sent, in order, in response
        to launchpadlib's requests.

        :param responses: Some number of Response objects.
        :return: The Launchpad object, assuming that errors in the
            simulated requests didn't prevent one from being created.
        r.   ©r%   )r$   r   Ú
login_withr/   )r   r   r   r   r   Úlaunchpad_with_responsesu   s
    
 ÿz3SimulatedResponsesTestCase.launchpad_with_responsesN)r   r   r   r   r1   r4   r   r   r   r   r-   k   s   r-   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTestAbilityToParseDataa_  Test launchpadlib's ability to handle the sample data.

    To create a Launchpad object, two HTTP requests must succeed and
    return usable data: the requests for the WADL and JSON
    representations of the service root. This test shows that the
    minimal data in SIMPLE_WADL and SIMPLE_JSON is good enough to
    create a Launchpad object.
    c                 C   s   |   tdtƒtdtƒ¡ dS )z5Make sure that launchpadlib can use the minimal data.éÈ   N)r4   r
   ÚSIMPLE_WADLÚSIMPLE_JSONr0   r   r   r   Útest_minimal_data   s     ÿz(TestAbilityToParseData.test_minimal_datac                 C   s"   |   t| jtddƒtdtƒ¡ dS )z'Show that bad WADL causes an exception.r6   s   This is not WADL.N)ÚassertRaisesÚSyntaxErrorr4   r
   r8   r0   r   r   r   Útest_bad_wadl•   s    üz$TestAbilityToParseData.test_bad_wadlc                 C   s"   |   t| jtdtƒtddƒ¡ dS )z'Show that bad JSON causes an exception.r6   s   This is not JSON.N)r:   r   r4   r
   r7   r0   r   r   r   Útest_bad_jsonž   s    üz$TestAbilityToParseData.test_bad_jsonN)r   r   r   r   r9   r<   r=   r   r   r   r   r5   …   s   		r5   c                   @   sH   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S )ÚTestTokenFailureDuringRequesta  Test access token failures during a request.

    launchpadlib makes two HTTP requests on startup, to get the WADL
    and JSON representations of the service root. If Launchpad
    receives a 401 error during this process, it will acquire a fresh
    access token and try again.
    c                 C   sJ   t dtƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ dS )z/If our token is good, we never get another one.r6   r   r.   r2   r'   N©	r
   r7   r8   r$   r   ÚassertEqualr/   Zaccess_tokens_obtainedr3   r0   r   r   r   Útest_good_token±   s    þ ÿz-TestTokenFailureDuringRequest.test_good_tokenc                 C   sR   t ddƒt dtƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ dS )	z(If our token is bad, we get another one.é‘  ó   Invalid token.r6   r   r.   r2   é   Nr?   r0   r   r   r   Útest_bad_token¾   s    ý ÿz,TestTokenFailureDuringRequest.test_bad_tokenc                 C   sR   t ddƒt dtƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ dS )	z,If our token is expired, we get another one.rB   ó   Expired token.r6   r   r.   r2   rD   Nr?   r0   r   r   r   Útest_expired_tokenÌ   s    ý ÿz0TestTokenFailureDuringRequest.test_expired_tokenc                 C   sR   t ddƒt dtƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ dS )	z,If our token is unknown, we get another one.rB   s   Unknown access token.r6   r   r.   r2   rD   Nr?   r0   r   r   r   Útest_unknown_tokenÛ   s    ý ÿz0TestTokenFailureDuringRequest.test_unknown_tokenc                 C   sR   t dtƒt ddƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ dS )	z6We get another token no matter when the error happens.r6   rB   rF   r   r.   r2   rD   Nr?   r0   r   r   r   Útest_delayed_errorê   s    ý ÿz0TestTokenFailureDuringRequest.test_delayed_errorc                 C   sb   t ddƒt dtƒt ddƒt ddƒt dtƒgt_|  | jjd¡ tjd| jd |  | jjd¡ d	S )
z@We'll keep getting new tokens as long as tokens are the problem.rB   rC   r6   rF   r   r.   r2   é   Nr?   r0   r   r   r   Útest_many_errorsø   s    û ÿz.TestTokenFailureDuringRequest.test_many_errorsc                 C   s(   t ddƒgt_| jttjd| jd dS )z>If the token is not at fault, a 401 error raises an exception.rB   s   Some other error.r.   r2   N)r
   r$   r   r:   r   r3   r/   r0   r   r   r   Útest_other_unauthorized  s    ÿüz5TestTokenFailureDuringRequest.test_other_unauthorizedN)r   r   r   r   rA   rE   rG   rH   rI   rK   rL   r   r   r   r   r>   ¨   s   r>   )r   Úcollectionsr   Újsonr   r(   Zunittestr   ÚImportErrorÚ
ValueErrorZlaunchpadlib.errorsr   Zlaunchpadlib.credentialsr   Zlaunchpadlib.launchpadr   r   Zlaunchpadlib.testing.helpersr	   r7   Úencoder8   r
   r   r$   ZTestCaser-   r5   r>   r   r   r   r   Ú<module>   s(   
#