
    {a                     4   d Z eZg dZddlmZ ddlZddlmZ ddl	m
Z
mZmZ  e       Zd Z G d d	e      Z G d
 de      Z G d de      Zed        Z G d d      Z G d d      Z G d d      Z G d d      Z edd      Z edd      Z edd      Zy)zlaunchpadlib testing helpers.)
BadSaveKeyringfake_keyringFauxSocketModuleInMemoryKeyringNoNetworkAuthorizationEngineNoNetworkLaunchpadTestableLaunchpadnopriv_read_nonprivatesalgado_read_nonprivatesalgado_with_full_permissions    )contextmanagerN)	Launchpad)AccessTokenCredentialsRequestTokenAuthorizationEnginec                  \    t        t        j                  dt              t        u sJ d       y )Nkeyringz>During tests the real keyring module should never be imported.)getattrlaunchpadlibcredentialsmissing     >/usr/lib/python3/dist-packages/launchpadlib/testing/helpers.pyassert_keyring_not_importedr   2   s+    (()W=HHGHHr   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )r   a  An authorization engine that doesn't open a web browser.

    You can use this to test the creation of Launchpad objects and the
    storing of credentials. You can't use it to interact with the web
    service, since it only pretends to authorize its OAuth request tokens.
    zaccess_key:84c                 H    t        t        | 
  |i | d| _        d| _        y )Nr   )superr   __init__request_tokens_obtainedaccess_tokens_obtained)selfargskwargs	__class__s      r   r   z%NoNetworkAuthorizationEngine.__init__B   s(    *D:DKFK'($&'#r   c                 .    | xj                   dz  c_         y)zsPretend to get a request token from the server.

        We do this by simply returning a static token ID.
           zrequest_token:42)r    )r"   r   s     r   get_request_tokenz.NoNetworkAuthorizationEngine.get_request_tokenH   s    
 	$$)$!r   c                 d    t        | j                  d      |_        | xj                  dz  c_        y)zPretend to exchange a request token for an access token.

        We do this by simply setting the access_token property.
        zaccess_secret:168r'   N)r   ACCESS_TOKEN_KEYaccess_tokenr!   )r"   r   request_tokens      r   make_end_user_authorize_tokenz:NoNetworkAuthorizationEngine.make_end_user_authorize_tokenP   s0    
 $/!!#6$
  	##q(#r   )	__name__
__module____qualname____doc__r*   r   r(   r-   __classcell__r%   s   @r   r   r   8   s     '(")r   r   c                   &    e Zd ZdZd Zed        Zy)r   zA Launchpad instance for tests with no network access.

    It's only useful for making sure that certain methods were called.
    It can't be used to interact with the API.
    c	                 X    || _         || _        || _        t        |||||      | _        y )Nservice_rootcachetimeout
proxy_infoversion)r   authorization_enginecredential_storedictpassed_in_args)	r"   r   r<   r=   r7   r8   r9   r:   r;   s	            r   r   zNoNetworkLaunchpad.__init__b   s8     '$8! 0"%!
r   c                     t        | S N)r   )clsr#   s     r   authorization_engine_factoryz/NoNetworkLaunchpad.authorization_engine_factoryx   s    +T22r   N)r.   r/   r0   r1   r   classmethodrC   r   r   r   r   r   [   s     
, 3 3r   r   c                   F     e Zd ZdZddddddej
                  f fd	Z xZS )r   z5A base class for talking to the testing root service.Ntest_devc	           
      <    t         t        |   ||||||||       y)a\  Provide test-friendly defaults.

        :param authorization_engine: Defaults to None, since a test
            environment can't use an authorization engine.
        :param credential_store: Defaults to None, since tests
            generally pass in fully-formed Credentials objects.
        :param service_root: Defaults to 'test_dev'.
        r6   N)r   r   r   )
r"   r   r<   r=   r7   r8   r9   r:   r;   r%   s
            r   r   zTestableLaunchpad.__init__   s2    & 	/ %! 	0 		
r   )r.   r/   r0   r1   r   DEFAULT_VERSIONr   r2   r3   s   @r   r   r   }   s-    ?
 "))
 
r   r   c              #     K   t                | t        j                  _        t        t        j                  _        	 d t        j                  `t        j                  `y# t        j                  `t        j                  `w xY ww)zAA context manager which injects a testing keyring implementation.N)r   r   r   r   RuntimeErrorNoKeyringError)fakes    r   r   r      se       !'+L$.:L+4$$,$$3 $$,$$3s   9BA!  !B!"BBc                       e Zd ZdZd Zy)r   z:A socket module replacement that provides a fake hostname.c                      y)NHOSTNAMEr   r"   s    r   gethostnamezFauxSocketModule.gethostname   s    r   N)r.   r/   r0   r1   rQ   r   r   r   r   r      s
    Dr   r   c                       e Zd ZdZd Zd Zy)r   z6A keyring that generates errors when saving passwords.c                      y rA   r   r"   serviceusernames      r   get_passwordzBadSaveKeyring.get_password   s    r   c                     t         rA   )rJ   r"   rU   rV   passwords       r   set_passwordzBadSaveKeyring.set_password   s    r   N)r.   r/   r0   r1   rW   r[   r   r   r   r   r      s    @r   r   c                   "    e Zd ZdZd Zd Zd Zy)r   z.A keyring that saves passwords only in memory.c                     i | _         y rA   datarP   s    r   r   zInMemoryKeyring.__init__   s	    	r   c                 &    || j                   ||f<   y rA   r^   rY   s       r   r[   zInMemoryKeyring.set_password   s    '/		'8#$r   c                 <    | j                   j                  ||f      S rA   )r_   getrT   s      r   rW   zInMemoryKeyring.get_password   s    yy}}gx011r   N)r.   r/   r0   r1   r   r[   rW   r   r   r   r   r      s    802r   r   c                   :    e Zd ZdZd Zdddej                  fdZy)KnownTokensz'Known access token/secret combinations.c                 z    || _         || _        t        ||      | _        t	        d| j                        | _        y )Nzlaunchpad-library)consumer_namer+   )token_stringaccess_secretr   tokenr   r   )r"   rg   rh   s      r   r   zKnownTokens.__init__   s6    (* }=
&-DJJ
r   Nc                 6    t        | j                  ||||      S )z2Create a Launchpad object using these credentials.)r8   r9   r:   r;   )r   r   )r"   r8   r9   r:   r;   s        r   loginzKnownTokens.login   s&     !!
 	
r   )r.   r/   r0   r1   r   r   rH   rk   r   r   r   rd   rd      s#    1
 ))
r   rd   zsalgado-change-anythingtestzsalgado-read-nonprivatesecretznopriv-read-nonprivatemystery)r1   type__metaclass____all__
contextlibr   r   launchpadlib.launchpadr   launchpadlib.credentialsr   r   r   objectr   r   r   r   r   r   r   r   r   rd   r   r
   r	   r   r   r   <module>rv      s   & $  &  ,  (H )#B  )F3 3D
	 
D 
4 
4  
2 
2
 
8 !,,Ev N %&?J $%=yI r   