
    '[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4d;  )
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1A1AID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& Biw' J4 J(
) 
$
!2!2 
4 
7  
Fk
$> k
]  ! O!s   B B'&B'