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,   #