ó
ÝÏkMc           @   s*  d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 d d l
 m Z d d l m Z m Z d d l m Z m Z d	 Z e i  ƒ Z d
 d d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s,   Tests for the LaunchpadOAuthAwareHTTP class.iÿÿÿÿ(   t   dequeN(   t   dumpst   JSONDecodeError(   t   Unauthorized(   t   UnencryptedFileCredentialStore(   t	   Launchpadt   LaunchpadOAuthAwareHttp(   t   NoNetworkAuthorizationEnginet   NoNetworkLaunchpadsÝ  <?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>
t   Responsec           B   s   e  Z d  Z d „  Z RS(   s   A fake HTTP response object.c         C   s   | |  _  | |  _ d  S(   N(   t   statust   content(   t   selfR
   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   __init__>   s    	(   t   __name__t
   __module__t   __doc__R   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR	   <   s   t   SimulatedResponsesHttpc           B   s    e  Z d  Z d „  Z d „  Z RS(   s<   Responds to HTTP requests by shifting responses off a stack.c         G   s5   t  t |  ƒ j | Œ  g  |  _ | |  _ d |  _ d S(   s{   Constructor.

        :param responses: A list of HttpResponse objects to use
            in response to requests.
        N(   t   superR   R   t   sent_responsest   unsent_responsest   Nonet   cache(   R   t	   responsest   args(    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR   F   s    		c         G   s5   |  j  j ƒ  } |  j j | ƒ |  j | | j | Œ S(   N(   R   t   popleftR   t   appendt   retry_on_bad_tokenR   (   R   R   t   response(    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   _requestQ   s    (   R   R   R   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR   C   s   	t   SimulatedResponsesLaunchpadc           B   s&   e  Z g  Z d  „  Z e d „  ƒ Z RS(   c         G   s   t  t |  j ƒ |  |  j | Œ S(   N(   R   R    R   t   authorization_engine(   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   httpFactory]   s    c         C   s   t  t j ƒ  d | ƒ S(   Ni   (   R   t   tempfilet   mkstemp(   t   clst   credential_save_failed(    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   credential_store_factorya   s    (   R   R   R   R    t   classmethodR%   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR   W   s   	t   SimulatedResponsesTestCasec           B   s    e  Z d  Z d „  Z d „  Z RS(   sA   Test cases that give fake responses to launchpad's HTTP requests.c         C   s   g  t  _ t d d ƒ |  _ d S(   s*   Clear out the list of simulated responses.s   http://api.example.com/s   application nameN(   R   R   R   t   engine(   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   setUpj   s    	c         G   s   | t  _ t  j d d |  j ƒS(   sq  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.
        s   application nameR   (   R   R   t
   login_withR(   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   launchpad_with_responsesp   s    
	(   R   R   R   R)   R+   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR'   g   s   	t   TestAbilityToParseDatac           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s_  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(   |  j  t d t ƒ t d t ƒ ƒ } d S(   s5   Make sure that launchpadlib can use the minimal data.iÈ   N(   R+   R	   t   SIMPLE_WADLt   SIMPLE_JSON(   R   t	   launchpad(    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_minimal_data‰   s    c         C   s/   |  j  t |  j t d d ƒ t d t ƒ ƒ d S(   s'   Show that bad WADL causes an exception.iÈ   s   This is not WADL.N(   t   assertRaisest   SyntaxErrorR+   R	   R.   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_bad_wadl   s    	c         C   s/   |  j  t |  j t d t ƒ t d d ƒ ƒ d S(   s'   Show that bad JSON causes an exception.iÈ   s   This is not JSON.N(   R1   R   R+   R	   R-   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_bad_json–   s    	(   R   R   R   R0   R3   R4   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR,      s   		t   TestTokenFailureDuringRequestc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s  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   si   t  d t ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d S(   s/   If our token is good, we never get another one.iÈ   i    s   application nameR   i   N(	   R	   R-   R.   R   R   t   assertEqualsR(   t   access_tokens_obtainedR*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_good_token§   s    c         C   su   t  d d ƒ t  d t ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d S(	   s(   If our token is bad, we get another one.i‘  s   Invalid token.iÈ   i    s   application nameR   i   N(	   R	   R-   R.   R   R   R6   R(   R7   R*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_bad_token²   s    c         C   su   t  d d ƒ t  d t ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d S(	   s,   If our token is expired, we get another one.i‘  s   Expired token.iÈ   i    s   application nameR   i   N(	   R	   R-   R.   R   R   R6   R(   R7   R*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_expired_token¾   s    c         C   su   t  d d ƒ t  d t ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d S(	   s,   If our token is unknown, we get another one.i‘  s   Unknown access token.iÈ   i    s   application nameR   i   N(	   R	   R-   R.   R   R   R6   R(   R7   R*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_unknown_tokenË   s    c         C   su   t  d t ƒ t  d d ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d S(	   s6   We get another token no matter when the error happens.iÈ   i‘  s   Expired token.i    s   application nameR   i   N(	   R	   R-   R.   R   R   R6   R(   R7   R*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_delayed_errorØ   s    c         C   s   t  d d ƒ t  d t ƒ t  d d ƒ t  d d ƒ t  d t ƒ g t _ |  j |  j j d ƒ t j d d |  j ƒ} |  j |  j j d ƒ d	 S(
   s@   We'll keep getting new tokens as long as tokens are the problem.i‘  s   Invalid token.iÈ   s   Expired token.i    s   application nameR   i   N(	   R	   R-   R.   R   R   R6   R(   R7   R*   (   R   R/   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_many_errorsä   s    c         C   s8   t  d d ƒ g t _ |  j t t j d d |  j ƒd S(   s>   If the token is not at fault, a 401 error raises an exception.i‘  s   Some other error.s   application nameR   N(   R	   R   R   R1   R   R*   R(   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   test_other_unauthorizedñ   s    	(
   R   R   R   R8   R9   R:   R;   R<   R=   R>   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyR5   ž   s   						(    (   R   t   collectionsR    R!   t   unittestt
   simplejsonR   R   t   launchpadlib.errorsR   t   launchpadlib.credentialsR   t   launchpadlib.launchpadR   R   t   launchpadlib.testing.helpersR   R   R-   R.   R	   R   R   t   TestCaseR'   R,   R5   (    (    (    s@   /usr/lib/python2.7/dist-packages/launchpadlib/tests/test_http.pyt   <module>   s    