U
    æÜ›U&$  ã                   @   s  d Z ddlmZ ddlZddlZzddlmZ eZW n$ e	k
rX   ddl
mZm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.é    )ÚdequeN)Údumps)r   Ú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   G   s    zSimulatedResponsesHttp.__init__c                 G   s*   | j  ¡ }| j |¡ | j||jf|žŽ S r   )r   Úpopleftr   ÚappendZretry_on_bad_tokenr   )r   r   Zresponser   r   r   Ú_requestR   s    
zSimulatedResponsesHttp._request)r   r   r   r   r   r!   Ú__classcell__r   r   r   r   r   D   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   ÚtempfileZmkstemp)ÚclsZcredential_save_failedr   r   r   Úcredential_store_factoryb   s    
 ÿz4SimulatedResponsesLaunchpad.credential_store_factoryN)r   r   r   r   r%   Úclassmethodr)   r   r   r   r   r#   X   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   ÚsetUpk   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_responsesq   s
    
 ÿz3SimulatedResponsesTestCase.launchpad_with_responsesN)r   r   r   r   r/   r2   r   r   r   r   r+   h   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)r2   r
   ÚSIMPLE_WADLÚSIMPLE_JSONr.   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.r4   s   This is not WADL.N)ÚassertRaisesÚSyntaxErrorr2   r
   r6   r.   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.r4   s   This is not JSON.N)r8   r   r2   r
   r5   r.   r   r   r   Útest_bad_json—   s     ýz$TestAbilityToParseData.test_bad_jsonN)r   r   r   r   r7   r:   r;   r   r   r   r   r3   €   s   	r3   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.r4   r   r,   r0   r&   N©	r
   r5   r6   r#   r   ZassertEqualr-   Zaccess_tokens_obtainedr1   r.   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.r4   r   r,   r0   é   Nr=   r.   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.r?   ó   Expired token.r4   r   r,   r0   rA   Nr=   r.   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.r?   s   Unknown access token.r4   r   r,   r0   rA   Nr=   r.   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.r4   r?   rC   r   r,   r0   rA   Nr=   r.   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.r?   r@   r4   rC   r   r,   r0   é   Nr=   r.   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.r?   s   Some other error.r,   r0   N)r
   r#   r   r8   r   r1   r-   r.   r   r   r   Útest_other_unauthorizedò   s    ÿ  þz5TestTokenFailureDuringRequest.test_other_unauthorizedN)r   r   r   r   r>   rB   rD   rE   rF   rH   rI   r   r   r   r   r<   Ÿ   s   r<   )r   Úcollectionsr   r'   ZunittestZjsonr   Ú
ValueErrorr   ÚImportErrorZ
simplejsonZlaunchpadlib.errorsr   Zlaunchpadlib.credentialsr   Zlaunchpadlib.launchpadr   r   Zlaunchpadlib.testing.helpersr	   r5   Úencoder6   r
   r   r#   ZTestCaser+   r3   r<   r   r   r   r   Ú<module>   s(   