3
S,Z$                 @   s   d dl mZ d dlZd dljjZd dl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 d dlmZmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZmZ e
j
 Zdd ZG dd deZ dS )    )unicode_literalsN)	b64encode)deepcopy)TestCase)	urlencode)TokenExpiredErrorOAuth2Error)MismatchingStateError)WebApplicationClientMobileApplicationClient)LegacyApplicationClientBackendApplicationClient)OAuth2SessionTokenUpdatedc                s    fdd}|S )Nc                s   t j }tj |_|S )N)mock	MagicMockjsondumpstext)rkwargsresp)token K/Users/singingwolfboy/clones/requests-oauthlib/tests/test_oauth2_session.py	fake_send   s    zfake_token.<locals>.fake_sendr   )r   r   r   )r   r   
fake_token   s    r   c               @   s   e Zd Zdd Zdd Zdd Zejddd	 d
dd Zejddd	 d
dd Z	ejddd	 d
dd Z
dd Zdd Zdd Zdd Zdd Zdd Zejddd	 d
d d! Zd"S )#OAuth2SessionTestc                sn   t  ds fdd _ddddtd d	 _d
 _t jddt jt jg _ jt	 jg  _
d S )NassertInc                s    j | |kS )N)
assertTrue)ab)selfr   r   <lambda>"   s    z)OAuth2SessionTest.setUp.<locals>.<lambda>Bearerasdfoiw37850234lkjsdfsdfZsldvafkjw34509s8dfsdf3600i  )
token_typeaccess_tokenrefresh_token
expires_in
expires_atfooZ
asdf345xdf)code)hasattrr   	fake_timer   	client_idr
   r   r   clientsr   all_clients)r"   r   )r"   r   setUp   s    
zOAuth2SessionTest.setUpc                sN   d j d   fdd}x, jD ]"}t| j d}||_|jd q$W d S )NzBearer r(   c                s0   | j jtdd } j| tj }g |_|S )NAuthorization)headersgetstrassertEqualr   r   Zcookes)r   r   auth_headerr   )r"   r   r   r   verifier6   s
    z2OAuth2SessionTest.test_add_token.<locals>.verifier)clientr   zhttps://i.b)r   r2   r   sendr6   )r"   r:   r;   authr   )r"   r   r   test_add_token3   s    z OAuth2SessionTest.test_add_tokenc             C   s   d}t | j}t|d}|j|\}}| j|| | j| j| | jd| t| j}t|d}|j|\}}| j|| | j| j| | jd| d S )Nz%https://example.com/authorize?foo=bar)r;   zresponse_type=codezresponse_type=token)r
   r0   r   authorization_urlr   r   )r"   urlZwebsauth_urlstatemobiler   r   r   test_authorization_urlB   s    



z(OAuth2SessionTest.test_authorization_urlz	time.timec               C   s   t S )N)r/   r   r   r   r   r#   S   s    zOAuth2SessionTest.<lambda>)newc                s  t  j _d jd<  jd=  fdd}x, jD ]"}t| jd} jt|jd q2W x4 jD ]*}t| jdd	}||_ jt	|jd q`W  fd
d}x0 jD ]&}t| jd|d}||_|jd qW  fdd}x6 jD ],}t| jd|d}||_|jdddd qW d S )Nz-1r*   r+   c                s2   d| j kr jd| j tj }tj j|_|S )Nz/refreshr4   )	r@   assertNotInr5   r   r   r   r   r   r   )r   r   r   )r"   r   r   fake_refreshY   s
    
zBOAuth2SessionTest.test_refresh_token_request.<locals>.fake_refresh)r;   r   zhttps://i.bzhttps://i.b/refresh)r;   r   auto_refresh_urlc                s    j |  j d S )N)r8   r   )r   )r"   r   r   token_updaterm   s    zCOAuth2SessionTest.test_refresh_token_request.<locals>.token_updater)r;   r   rI   rJ   c                sZ   d| j kr@ jd| j td}d| jd} j| jd | tj }tj	 j
|_|S )Nz/refreshr4   s   foo:bars   Basic latin1)r@   r   r5   r   decoder8   r   r   r   r   r   r   )r   r   encodedcontentr   )r"   r   r   fake_refresh_with_authw   s    
zLOAuth2SessionTest.test_refresh_token_request.<locals>.fake_refresh_with_authr,   bar)r0   client_secret)
dictr   Zexpired_tokenr1   r   assertRaisesr   r6   r<   r   )r"   rH   r;   r=   rJ   rO   r   )r"   r   test_refresh_token_requestS   s4    

z,OAuth2SessionTest.test_refresh_token_requestc               C   s   t S )N)r/   r   r   r   r   r#      s    c             C   s>   t | j}dt| jj  }t|d}| j|j|| j d S )Nzhttps://i.b/callback#)r;   )r   r0   r   r   itemsr   r8   token_from_fragment)r"   rD   Zresponse_urlr=   r   r   r   test_token_from_fragment   s    

z*OAuth2SessionTest.test_token_from_fragmentc               C   s   t S )N)r/   r   r   r   r   r#      s    c             C   s   d}x<| j D ]2}t|| jd}t| j|_| j|j|| j qW ddi}x6| j D ],}t|| jd}t||_| jt|j| qRW d S )Nzhttps://example.com/token)r;   r   errorinvalid_request)	r1   r   r   r   r<   r8   fetch_tokenrS   r   )r"   r@   r;   r=   rX   r   r   r   test_fetch_token   s    
z"OAuth2SessionTest.test_fetch_tokenc                s   t | j}tj d }tj  || jd<  d |d< d}tjd fdd@ x8| jD ].}t|| jd}t||_| j	|j
|| qVW W d	Q R X d	S )
a   Makes sure the previous token is cleaned before fetching a new one.

        The reason behind it is that, if the previous token is expired, this
        method shouldn't fail with a TokenExpiredError, since it's attempting
        to get a new one (which shouldn't be expired).

        i   r+   i  zhttps://example.com/tokenz	time.timec                  s    S )Nr   r   )nowr   r   r#      s    zVOAuth2SessionTest.test_cleans_previous_token_before_fetching_new_one.<locals>.<lambda>)r;   r   N)r   r   timer   patchr1   r   r   r<   r8   rZ   )r"   Z	new_tokenZpastr@   r;   r=   r   )r\   r   2test_cleans_previous_token_before_fetching_new_one   s    


zDOAuth2SessionTest.test_cleans_previous_token_before_fetching_new_onec             C   s$   t ddd}| jt|jddd d S )Nr,   Z	somestate)rC   zhttps://i.b/tokenzhttps://i.b/no-state?code=abc)authorization_response)r   rS   r	   rZ   )r"   r;   r   r   r   test_web_app_fetch_token   s    
z*OAuth2SessionTest.test_web_app_fetch_tokenc             C   sT   t d}| j|jd d|_| j|jd d|j_| j|jd |`| j|j d S )Nztest-idzdifferent-idzsomething-else)r   r8   r0   _clientassertIsNone)r"   sessr   r   r   test_client_id_proxy   s    z&OAuth2SessionTest.test_client_id_proxyc             C   sR   t d}| j|j d|_| j|jd d|j_| j|jd |`| j|j d S )Nztest-idz
test-tokenzdifferent-token)r   rc   r(   r8   rb   )r"   rd   r   r   r   test_access_token_proxy   s    z)OAuth2SessionTest.test_access_token_proxyc          	   C   s   ddi}t d|d}| j|jd | j|j| d|d< ||_| j|jd | j|j| d|j_d|d< | j|jd | j|j| | jt
 |`W d Q R X d S )Nr(   ztest-accessztest-id)r   zsomething-elsezdifferent-token)r   r8   r(   r   rb   rS   AttributeError)r"   r   rd   r   r   r   test_token_proxy   s    z"OAuth2SessionTest.test_token_proxyc             C   s   t d}| j|j d S )Nr,   )r   assertFalse
authorized)r"   rd   r   r   r   test_authorized_false   s    z'OAuth2SessionTest.test_authorized_falsec               C   s   t S )N)r/   r   r   r   r   r#      s    c             C   sX   dd }d}xF| j D ]<}t|d}|| j|_| j|j |j| | j|j qW d S )Nc                s    fdd}|S )Nc                s   t j }tj |_|S )N)r   r   r   r   r   )r   r   r   )r   r   r   r      s    zMOAuth2SessionTest.test_authorized_true.<locals>.fake_token.<locals>.fake_sendr   )r   r   r   )r   r   r      s    z:OAuth2SessionTest.test_authorized_true.<locals>.fake_tokenzhttps://example.com/token)r;   )r1   r   r   r<   ri   rj   rZ   r   )r"   r   r@   r;   rd   r   r   r   test_authorized_true   s    

z&OAuth2SessionTest.test_authorized_trueN)__name__
__module____qualname__r3   r>   rE   r   r^   rT   rW   r[   r_   ra   re   rf   rh   rk   rl   r   r   r   r   r      s   5

r   )!
__future__r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr   r   r]   base64r   copyr   unittestr   oauthlib.commonr   oauthlib.oauth2r   r   r	   r
   r   r   r   requests_oauthlibr   r   r/   r   r   r   r   r   r   <module>   s"     