
WJ%                 @   s7  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z y d  d l	 m
 Z
 Wn" e k
 r d  d l m
 Z
 Yn Xd  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 Gd 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 X/Users/cory/Documents/Python/requests_org/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                s   t    d  s$   f d d     _ d d d d d d	 d
 d d t d i   _ 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>
token_typeBeareraccess_tokenasdfoiw37850234lkjsdfsdfrefresh_tokenZsldvafkjw34509s8dfsdf
expires_in3600
expires_ati  foocodeZ
asdf345xdf)hasattrr   	fake_timer   	client_idr
   r   r   clientsr   all_clients)r"   r   )r"   r   setUp"   s    	zOAuth2SessionTest.setUpc                sl   d   j  d     f d d   } x?   j D]4 } t d | d   j   } | | _ | j d  q0 Wd  S)NzBearer r&   c                sl   |  j  j d d   } d j d  |  j  k rC |  j  d j d  }   j |   t j   } g  | _ | S)NAuthorizationzutf-8)headersgetencodeassertEqualr   r   Zcookes)r   r   auth_headerr   )r"   r   r   r   verifier9   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_token6   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=barr;   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_urlG   s    z(OAuth2SessionTest.test_authorization_urlz	time.timenewc               C   s   t  S)N)r/   r   r   r   r   r#   X   s    zOAuth2SessionTest.<lambda>c          
      s  t    j    _ d   j d <  j d =  f d d   } x?   j D]4 } t d | d   j  }   j t | j d  qE WxN   j D]C } t d | d   j d	 d
  } | | _   j t	 | j d  q W  f d d   } xK   j D]@ } t d | d   j d	 d
 d |  } | | _ | j d  q W  f d d   } xW   j D]L } t d | d   j d	 d
 d |  } | | _ | j d d d d d qJWd  S)Nz-1r)   r+   c                sG   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_refresh^   s
    zBOAuth2SessionTest.test_refresh_token_request.<locals>.fake_refreshr;   r   zhttps://i.bauto_refresh_urlzhttps://i.b/refreshc                s     j  |    j  d  S)N)r8   r   )r   )r"   r   r   token_updaterr   s    zCOAuth2SessionTest.test_refresh_token_request.<locals>.token_updaterrJ   c                s}   d |  j  k rX   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_auth|   s    zLOAuth2SessionTest.test_refresh_token_request.<locals>.fake_refresh_with_authr0   r,   client_secretbar)
dictr   Zexpired_tokenr1   r   assertRaisesr   r6   r<   r   )r"   rH   r;   r=   rJ   rO   r   )r"   r   test_refresh_token_requestX   s4    
				
		z,OAuth2SessionTest.test_refresh_token_requestc               C   s   t  S)N)r/   r   r   r   r   r#      s    c             C   sW   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 } xW |  j  D]L } t d | d |  j  } t |  j  | _ |  j | j |  |  j  q Wd d i } xN |  j  D]C } t d | d |  j  } t |  | _ |  j t | j |  qv Wd  S)Nzhttps://example.com/tokenr;   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    Y xQ |  j D]F } t d | d	 |  j  } t |  | _ |  j	 | j
 |  |  qu WWd
 QRXd
 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   s2   t  d d d } |  j t | j d d d d  S)Nr,   rC   Z	somestatezhttps://i.b/tokenauthorization_responsezhttps://i.b/no-state?code=abc)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   sq   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   | ` Wd  QRXd  S)Nr&   ztest-accessztest-idr   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 } x_ |  j  D]T } t d |  } | |  j  | _ |  j | j  | j |  |  j | j  q Wd  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/tokenr;   )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   r   r   r]   base64r   copyr   	unittest2r   ImportErrorunittestoauthlib.commonr   oauthlib.oauth2r   r   r	   r
   r   r   r   requests_oauthlibr   r   r/   r   r   r   r   r   r   <module>   s$   