Ë
    '[”aü$  ã                   óT  — d Z ddlmZ ddlmZ ddlZddlZ	 ddlmZ 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 «      j)                  d«      Z G d„ d«      Z G d„ de«      Z G d„ de«      Z G d„ dej2                  «      Z G d„ de«      Z G d„ de«      Zy# e$ r e	ZY Œˆw xY w)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                   ó   — e Zd ZdZd„ Zy)ÚResponsezA fake HTTP response object.c                 ó    — || _         || _        y ©N)ÚstatusÚcontent)Úselfr   r   s      ú>/usr/lib/python3/dist-packages/launchpadlib/tests/test_http.pyÚ__init__zResponse.__init__@   s   € ØˆŒØˆ�ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   © r   r   r   r   =   s
   „ Ù&ór   r   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚSimulatedResponsesHttpz<Responds to HTTP requests by shifting responses off a stack.c                 óP   •— t        t        | �
  |Ž  g | _        || _        d| _        y)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__s      €r   r   zSimulatedResponsesHttp.__init__H   s,   ø€ ô 	Ô$ dÑ4°dÑ;Ø ˆÔØ )ˆÔØˆ�
r   c                 ó¨   — | j                   j                  «       }| j                  j                  |«        | j                  ||j
                  g|¢­Ž S r   )r   Úpopleftr   ÚappendÚretry_on_bad_tokenr   )r   r"   Úresponses      r   Ú_requestzSimulatedResponsesHttp._requestS   sJ   € Ø×(Ñ(×0Ñ0Ó2ˆØ×Ñ×"Ñ" 8Ô,Ø&ˆt×&Ñ& x°×1AÑ1AÐIÀDÒIÐIr   )r   r   r   r   r   r)   Ú__classcell__)r#   s   @r   r   r   E   s   ø„ ÙFô	öJr   r   c                   ó&   — e Zd Zg Zd„ Zed„ «       Zy)ÚSimulatedResponsesLaunchpadc                 óX   — t        t        | j                  «      | | j                  g|¢­Ž S r   )r   r   r!   Úauthorization_engine)r   r"   s     r   ÚhttpFactoryz'SimulatedResponsesLaunchpad.httpFactory_   s-   € Ü%Ü�$—.‘.Ó! 4¨×)BÑ)Bð
ØEIò
ð 	
r   c                 óD   — t        t        j                  «       d   |«      S )Né   )r   ÚtempfileÚmkstemp)ÚclsÚcredential_save_faileds     r   Úcredential_store_factoryz4SimulatedResponsesLaunchpad.credential_store_factoryd   s#   € ä-Ü×ÑÓ˜qÑ!Ð#9ó
ð 	
r   N)r   r   r   r!   r/   Úclassmethodr6   r   r   r   r,   r,   Y   s"   „ ð €Iò
ð
 ñ
ó ñ
r   r,   c                   ó   — e Zd ZdZd„ Zd„ Zy)ÚSimulatedResponsesTestCasezATest cases that give fake responses to launchpad's HTTP requests.c                 ó<   — g t         _        t        dd«      | _        y)z*Clear out the list of simulated responses.zhttp://api.example.com/úapplication nameN)r,   r!   r
   Úengine©r   s    r   ÚsetUpz SimulatedResponsesTestCase.setUpn   s   € à02Ô#Ô-Ü2Ø%Ð'9ó
ˆ�r   c                 óZ   — |t         _        t         j                  d| j                  ¬«      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!   s     r   Úlaunchpad_with_responsesz3SimulatedResponsesTestCase.launchpad_with_responsesu   s.   € ð 1:Ô#Ô-Ü*×5Ñ5Ø°T·[±[ð 6ó 
ð 	
r   N)r   r   r   r   r>   rB   r   r   r   r9   r9   k   s   „ ÙKò
ó
r   r9   c                   ó"   — e Zd ZdZd„ Zd„ Zd„ Zy)Ú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                 ó`   — | j                  t        dt        «      t        dt        «      «       y)z5Make sure that launchpadlib can use the minimal data.éÈ   N)rB   r   ÚSIMPLE_WADLÚSIMPLE_JSONr=   s    r   Útest_minimal_dataz(TestAbilityToParseData.test_minimal_data�   s#   € à×%Ñ%Ü�Sœ+Ó&¬°´kÓ(Bõ	
r   c           	      óx   — | j                  t        | j                  t        dd«      t        dt        «      «       y)z'Show that bad WADL causes an exception.rF   s   This is not WADL.N)ÚassertRaisesÚSyntaxErrorrB   r   rH   r=   s    r   Útest_bad_wadlz$TestAbilityToParseData.test_bad_wadl•   s1   € à×ÑÜØ×)Ñ)Ü�SÐ.Ó/Ü�Sœ+Ó&õ		
r   c           	      óx   — | j                  t        | j                  t        dt        «      t        dd«      «       y)z'Show that bad JSON causes an exception.rF   s   This is not JSON.N)rK   r   rB   r   rG   r=   s    r   Útest_bad_jsonz$TestAbilityToParseData.test_bad_jsonž   s1   € à×ÑÜØ×)Ñ)Ü�Sœ+Ó&Ü�SÐ.Ó/õ		
r   N)r   r   r   r   rI   rM   rO   r   r   r   rD   rD   …   s   „ ñò
ò
ó
r   rD   c                   ó:   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
y	)
Ú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                 ó0  — t        dt        «      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y)z/If our token is good, we never get another one.rF   r   r;   r@   r1   N©	r   rG   rH   r,   r!   ÚassertEqualr<   Úaccess_tokens_obtainedrA   r=   s    r   Útest_good_tokenz-TestTokenFailureDuringRequest.test_good_token±   sw   € ô �Sœ+Ó&Ü�Sœ+Ó&ð1
Ô#Ô-ð
 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 óF  — t        dd«      t        dt        «      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y)	z(If our token is bad, we get another one.é‘  ó   Invalid token.rF   r   r;   r@   é   NrS   r=   s    r   Útest_bad_tokenz,TestTokenFailureDuringRequest.test_bad_token¾   s‚   € ô �SÐ+Ó,Ü�Sœ+Ó&Ü�Sœ+Ó&ð1
Ô#Ô-ð 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 óF  — t        dd«      t        dt        «      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y)	z,If our token is expired, we get another one.rX   ó   Expired token.rF   r   r;   r@   rZ   NrS   r=   s    r   Útest_expired_tokenz0TestTokenFailureDuringRequest.test_expired_tokenÌ   s‚   € ô �SÐ+Ó,Ü�Sœ+Ó&Ü�Sœ+Ó&ð1
Ô#Ô-ð 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 óF  — t        dd«      t        dt        «      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y)	z,If our token is unknown, we get another one.rX   s   Unknown access token.rF   r   r;   r@   rZ   NrS   r=   s    r   Útest_unknown_tokenz0TestTokenFailureDuringRequest.test_unknown_tokenÛ   s‚   € ô �SÐ2Ó3Ü�Sœ+Ó&Ü�Sœ+Ó&ð1
Ô#Ô-ð 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 óF  — t        dt        «      t        dd«      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y)	z6We get another token no matter when the error happens.rF   rX   r]   r   r;   r@   rZ   NrS   r=   s    r   Útest_delayed_errorz0TestTokenFailureDuringRequest.test_delayed_errorê   s‚   € ô �Sœ+Ó&Ü�SÐ+Ó,Ü�Sœ+Ó&ð1
Ô#Ô-ð 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 ór  — t        dd«      t        dt        «      t        dd«      t        dd«      t        dt        «      gt        _        | j                  | j                  j                  d«       t        j                  d| j                  ¬«       | j                  | j                  j                  d«       y	)
z@We'll keep getting new tokens as long as tokens are the problem.rX   rY   rF   r]   r   r;   r@   é   NrS   r=   s    r   Útest_many_errorsz.TestTokenFailureDuringRequest.test_many_errorsø   s˜   € ô �SÐ+Ó,Ü�Sœ+Ó&Ü�SÐ+Ó,Ü�SÐ+Ó,Ü�Sœ+Ó&ð1
Ô#Ô-ð 	×Ñ˜Ÿ™×;Ñ;¸QÔ?Ü#×.Ñ.Ø°T·[±[ð 	/ô 	
ð 	×Ñ˜Ÿ™×;Ñ;¸QÕ?r   c                 ó’   — t        dd«      gt        _        | j                  t        t        j
                  d| j                  ¬«       y)z>If the token is not at fault, a 401 error raises an exception.rX   s   Some other error.r;   r@   N)r   r,   r!   rK   r   rA   r<   r=   s    r   Útest_other_unauthorizedz5TestTokenFailureDuringRequest.test_other_unauthorized  sE   € ô �SÐ.Ó/ð1
Ô#Ô-ð 	×ÑÜÜ'×2Ñ2ØØ!%§¡ð	 	õ 	
r   N)r   r   r   r   rV   r[   r^   r`   rb   re   rg   r   r   r   rQ   rQ   ¨   s0   „ ñò@ò@ò@ò@ò@ò@ó
r   rQ   )r   Úcollectionsr   Újsonr   r2   Úunittestr   ÚImportErrorÚ
ValueErrorÚlaunchpadlib.errorsr   Úlaunchpadlib.credentialsr   Úlaunchpadlib.launchpadr   r	   Úlaunchpadlib.testing.helpersr
   rG   ÚencoderH   r   r   r,   ÚTestCaser9   rD   rQ   r   r   r   ú<module>rs      s¹   ðñ" 3å Ý Û Û ð!Ý$õ -Ý C÷õ Fð€ñ& �B‹i×Ñ˜wÓ'€÷ñ ôJÐ4ô Jô(
 )ô 
ô$
 ×!2Ñ!2ô 
ô4 
Ð7ô  
ôFk
Ð$>õ k
øð] ò !Ø ‚Oð!ús   ˜B ÂB'Â&B'