o
    éú{a  ã                   @   sè   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d„ ZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZedd„ ƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZeddƒZeddƒZeddƒZdS )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                   C   s   t tjdtƒtu sJ dƒ‚d S )NÚkeyringz>During tests the real keyring module should never be imported.)ÚgetattrÚlaunchpadlibÚcredentialsÚmissing© r   r   ú>/usr/lib/python3/dist-packages/launchpadlib/testing/helpers.pyÚassert_keyring_not_imported2   s   ÿr   c                       s4   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Z‡  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                    s&   t t| ƒj|i |¤Ž d| _d| _d S )Nr   )Úsuperr   Ú__init__Úrequest_tokens_obtainedÚaccess_tokens_obtained)ÚselfÚargsÚkwargs©Ú	__class__r   r   r   B   s   
z%NoNetworkAuthorizationEngine.__init__c                 C   s   |  j d7  _ dS )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   r   r   r   Úget_request_tokenH   s   z.NoNetworkAuthorizationEngine.get_request_tokenc                 C   s    t | jdƒ|_|  jd7  _dS )z‚Pretend 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_tokenr   r   r   Úmake_end_user_authorize_tokenP   s   ÿz:NoNetworkAuthorizationEngine.make_end_user_authorize_token)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r   r#   r'   Ú__classcell__r   r   r    r   r   8   s    r   c                   @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )r   z²A 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	           	      C   s*   || _ || _|| _t|||||d�| _d S )N©Úservice_rootÚcacheÚtimeoutÚ
proxy_infoÚversion)r   Úauthorization_engineÚcredential_storeÚdictÚpassed_in_args©	r   r   r3   r4   r.   r/   r0   r1   r2   r   r   r   r   b   s   ûzNoNetworkLaunchpad.__init__c                 G   s   t |Ž S ©N)r   )Úclsr   r   r   r   Úauthorization_engine_factoryx   s   z/NoNetworkLaunchpad.authorization_engine_factoryN)r(   r)   r*   r+   r   Úclassmethodr:   r   r   r   r   r   [   s
    r   c                       s2   e Zd ZdZddddddejf‡ fdd„	Z‡  ZS )r   z5A base class for talking to the testing root service.NÚtest_devc	           	   
      s$   t t| ƒj||||||||d� dS )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'.
        r-   N)r   r   r   r7   r    r   r   r   €   s   

øzTestableLaunchpad.__init__)r(   r)   r*   r+   r   ÚDEFAULT_VERSIONr   r,   r   r   r    r   r   }   s    ÷r   c                 c   s@   � t ƒ  | tj_ttj_zdV  W tj`tj`dS tj`tj`w )zAA context manager which injects a testing keyring implementation.N)r   r   r   r   ÚRuntimeErrorÚNoKeyringError)Úfaker   r   r   r   Ÿ   s   €
ÿr   c                   @   s   e Zd ZdZdd„ ZdS )r   z:A socket module replacement that provides a fake hostname.c                 C   s   dS )NÚHOSTNAMEr   ©r   r   r   r   Úgethostname°   ó   zFauxSocketModule.gethostnameN)r(   r)   r*   r+   rC   r   r   r   r   r   ­   s    r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z6A keyring that generates errors when saving passwords.c                 C   s   d S r8   r   ©r   ÚserviceÚusernamer   r   r   Úget_password·   rD   zBadSaveKeyring.get_passwordc                 C   s   t ‚r8   )r>   ©r   rF   rG   Úpasswordr   r   r   Úset_passwordº   rD   zBadSaveKeyring.set_passwordN)r(   r)   r*   r+   rH   rK   r   r   r   r   r   ´   s    r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   z.A keyring that saves passwords only in memory.c                 C   s
   i | _ d S r8   ©ÚdatarB   r   r   r   r   Á   s   
zInMemoryKeyring.__init__c                 C   s   || j ||f< d S r8   rL   rI   r   r   r   rK   Ä   s   zInMemoryKeyring.set_passwordc                 C   s   | j  ||f¡S r8   )rM   ÚgetrE   r   r   r   rH   Ç   s   zInMemoryKeyring.get_passwordN)r(   r)   r*   r+   r   rK   rH   r   r   r   r   r   ¾   s
    r   c                   @   s,   e Zd ZdZdd„ Zdddejfdd„ZdS )ÚKnownTokensz'Known access token/secret combinations.c                 C   s,   || _ || _t||ƒ| _td| jd�| _d S )Nzlaunchpad-library)Úconsumer_namer%   )Útoken_stringÚaccess_secretr   Útokenr   r   )r   rQ   rR   r   r   r   r   Î   s   ÿzKnownTokens.__init__Nc                 C   s   t | j||||d�S )z2Create a Launchpad object using these credentials.)r/   r0   r1   r2   )r   r   )r   r/   r0   r1   r2   r   r   r   ÚloginÖ   s   ûzKnownTokens.login)r(   r)   r*   r+   r   r   r=   rT   r   r   r   r   rO   Ë   s    
ûrO   zsalgado-change-anythingÚtestzsalgado-read-nonprivateÚsecretznopriv-read-nonprivateÚmystery)r+   ÚtypeÚ__metaclass__Ú__all__Ú
contextlibr   r   Úlaunchpadlib.launchpadr   Úlaunchpadlib.credentialsr   r   r   Úobjectr   r   r   r   r   r   r   r   r   rO   r
   r	   r   r   r   r   r   Ú<module>   s*   #""



