o
    '[”aü$  ã                   @   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y)   e	ZY nw 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   ú>/usr/lib/python3/dist-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g|¢R Ž S r   )r   Úpopleftr   ÚappendÚ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g|¢R Ž 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)ÚclsÚ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.
        r0   ©r&   )r%   r   Ú
login_withr1   )r   r   r   r   r   Úlaunchpad_with_responsesu   s   
ÿz3SimulatedResponsesTestCase.launchpad_with_responsesN)r   r   r   r   r3   r6   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)r6   r
   ÚSIMPLE_WADLÚSIMPLE_JSONr2   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.r8   s   This is not WADL.N)ÚassertRaisesÚSyntaxErrorr6   r
   r:   r2   r   r   r   Útest_bad_wadl•   ó   ü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.r8   s   This is not JSON.N)r<   r   r6   r
   r9   r2   r   r   r   Útest_bad_jsonž   r?   z$TestAbilityToParseData.test_bad_jsonN)r   r   r   r   r;   r>   r@   r   r   r   r   r7   …   s
    		r7   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.r8   r   r0   r4   r(   N©	r
   r9   r:   r%   r   ÚassertEqualr1   Úaccess_tokens_obtainedr5   r2   r   r   r   Útest_good_token±   s   þÿz-TestTokenFailureDuringRequest.test_good_tokenc                 C   óR   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.r8   r   r0   r4   é   NrB   r2   r   r   r   Útest_bad_token¾   ó   ýÿz,TestTokenFailureDuringRequest.test_bad_tokenc                 C   rF   )	z,If our token is expired, we get another one.rG   ó   Expired token.r8   r   r0   r4   rI   NrB   r2   r   r   r   Útest_expired_tokenÌ   ó   ýÿz0TestTokenFailureDuringRequest.test_expired_tokenc                 C   rF   )	z,If our token is unknown, we get another one.rG   s   Unknown access token.r8   r   r0   r4   rI   NrB   r2   r   r   r   Útest_unknown_tokenÛ   rN   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.r8   rG   rL   r   r0   r4   rI   NrB   r2   r   r   r   Útest_delayed_errorê   rK   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.rG   rH   r8   rL   r   r0   r4   é   NrB   r2   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.rG   s   Some other error.r0   r4   N)r
   r%   r   r<   r   r5   r1   r2   r   r   r   Útest_other_unauthorized  s   ÿ
üz5TestTokenFailureDuringRequest.test_other_unauthorizedN)r   r   r   r   rE   rJ   rM   rO   rP   rR   rS   r   r   r   r   rA   ¨   s    rA   )r   Úcollectionsr   Újsonr   r)   Úunittestr   ÚImportErrorÚ
ValueErrorÚlaunchpadlib.errorsr   Úlaunchpadlib.credentialsr   Úlaunchpadlib.launchpadr   r   Úlaunchpadlib.testing.helpersr	   r9   Úencoder:   r
   r   r%   ÚTestCaser/   r7   rA   r   r   r   r   Ú<module>   s,   ÿ#