ó
O'—^c           @  s—  d  d l  m Z d  d l Z d  d l 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 d d l m Z m Z m Z m Z d d	 l m Z m Z m Z m Z d d
 l m Z d d l m Z d d l m Z e ƒ  Z e ƒ  Z  d e f d „  ƒ  YZ! d e e f d „  ƒ  YZ" d e" f d „  ƒ  YZ# d e" f d „  ƒ  YZ$ d e" f d „  ƒ  YZ% d e" f d „  ƒ  YZ& d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   TestCaset   RequestFactory(   t   reverse(   t   override_settings(   t   timezonei   (   t   urlparset   parse_qst	   urlencodet   get_user_model(   t   get_application_modelt   Grantt   AccessTokent   RefreshToken(   t   oauth2_settings(   t   ProtectedResourceViewi   (   t   TestCaseUtilst   ResourceViewc           B  s   e  Z d  „  Z RS(   c         O  s   d S(   Nu   This is a protected resource(    (   t   selft   requestt   argst   kwargs(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   get   s    (   t   __name__t
   __module__R   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyR      s   t   BaseTestc           B  s   e  Z d  „  Z d „  Z RS(   c         C  s³   t  ƒ  |  _ t j j d d d ƒ |  _ t j j d d d ƒ |  _ d d g t _ t	 d d	 d
 d d |  j d t	 j
 d t	 j ƒ |  _ |  j j ƒ  d d g t _ d d g t _ d  S(   Nu	   test_useru   test@user.comu   123456u   dev_useru   dev@user.comu   httpu   custom-schemet   nameu   Test Applicationt   redirect_urisuQ   http://localhost http://example.com http://example.it custom-scheme://example.comt   usert   client_typet   authorization_grant_typeu   readu   write(   R   t   factoryt	   UserModelt   objectst   create_usert	   test_usert   dev_userR   t   ALLOWED_REDIRECT_URI_SCHEMESt   Applicationt   CLIENT_CONFIDENTIALt   GRANT_AUTHORIZATION_CODEt   applicationt   savet   _SCOPESt   _DEFAULT_SCOPES(   R   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   setUp    s    		c         C  s+   |  j  j ƒ  |  j j ƒ  |  j j ƒ  d  S(   N(   R)   t   deleteR#   R$   (   R   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   tearDown3   s    (   R   R   R-   R/   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyR      s   	t   TestAuthorizationCodeViewc           B  s¼   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s®   |  j  j d d d d ƒ t |  j _ |  j j ƒ  t i |  j j d 6d d 6d d	 6d
 d 6d d 6ƒ } d j d t	 d ƒ d | ƒ } |  j  j
 | ƒ } |  j | j d ƒ d S(   u_   
        If application.skip_authorization = True, should skip the authorization page.
        t   usernameu	   test_usert   passwordu   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}t   urlu   oauth2_provider:authorizet   qsi.  N(   t   clientt   logint   TrueR)   t   skip_authorizationR*   R   t	   client_idt   formatR   R   t   assertEqualt   status_code(   R   t   query_stringR3   t   response(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt"   test_skip_authorization_completely:   s    c         C  sz   |  j  j d d d d ƒ t i d d 6d d 6ƒ } d	 j d
 t d ƒ d | ƒ } |  j  j | ƒ } |  j | j d ƒ d S(   uN   
        Test error for an invalid client_id with response_type: code
        R1   u	   test_userR2   u   123456u   fakeclientidu	   client_idu   codeu   response_typeu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   i  N(   R5   R6   R   R:   R   R   R;   R<   (   R   R=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_pre_auth_invalid_clientN   s    c         C  s#  |  j  j d d d d ƒ t i |  j j d 6d d 6d d	 6d
 d 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j  j | ƒ } |  j | j	 d ƒ |  j
 d | j ƒ | j d } |  j | d j ƒ  d ƒ |  j | d	 j ƒ  d ƒ |  j | d j ƒ  d
 ƒ |  j | d j ƒ  |  j j ƒ d S(   uN   
        Test response for a valid client_id with response_type: code
        R1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   iÈ   u   formN(   R5   R6   R   R)   R9   R:   R   R   R;   R<   t   assertInt   contextt   value(   R   R=   R3   R>   t   form(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_pre_auth_valid_client]   s     c         C  s#  |  j  j d d d d ƒ t i |  j j d 6d d 6d d	 6d
 d 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j  j | ƒ } |  j | j	 d ƒ |  j
 d | j ƒ | j d } |  j | d j ƒ  d ƒ |  j | d	 j ƒ  d ƒ |  j | d j ƒ  d
 ƒ |  j | d j ƒ  |  j j ƒ d S(   uŽ   
        Test response for a valid client_id with response_type: code
        using a non-standard, but allowed, redirect_uri scheme.
        R1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu
   read writeu   scopeu   custom-scheme://example.comu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   iÈ   u   formN(   R5   R6   R   R)   R9   R:   R   R   R;   R<   RA   RB   RC   (   R   R=   R3   R>   RD   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt5   test_pre_auth_valid_client_custom_redirect_uri_schemex   s     c         C  s  t  j j d |  j d d d |  j d t j ƒ  t j d d ƒ d d	 ƒ } |  j	 j
 d
 d d d ƒ t i |  j j d 6d d 6d d 6d	 d 6d d 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j	 j | ƒ } |  j | j d ƒ d | _ | j ƒ  |  j	 j | ƒ } |  j | j d ƒ d S(    u   
        TODO
        R   t   tokenu
   1234567890R)   t   expirest   daysi   t   scopeu
   read writeR1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu   scopeu   http://example.itu   redirect_uriu   autou   approval_promptu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   i.  u   readiÈ   N(   R   R!   t   createR#   R)   R   t   nowt   datetimet	   timedeltaR5   R6   R   R9   R:   R   R   R;   R<   RJ   R*   (   R   t   tokR=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_pre_auth_approval_prompt”   s&    			
c         C  sï   |  j  t j d ƒ t j j d |  j d d d |  j d t j	 ƒ  t
 j d d ƒ d	 d
 ƒ |  j j d d d d ƒ t i |  j j d 6d d 6d d 6d
 d 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j j | ƒ } |  j  | j d ƒ d S(   u   
        TODO
        u   forceR   RG   u
   1234567890R)   RH   RI   i   RJ   u
   read writeR1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   iÈ   N(   R;   R   t   REQUEST_APPROVAL_PROMPTR   R!   RK   R#   R)   R   RL   RM   RN   R5   R6   R   R9   R:   R   R   R<   (   R   R=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt%   test_pre_auth_approval_prompt_default®   s    	c         C  så   d t  _ t j j d |  j d d d |  j d t j ƒ  t	 j
 d d ƒ d	 d
 ƒ |  j j d d d d ƒ t i |  j j d 6d d 6d d 6d
 d 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j j | ƒ } |  j | j d ƒ d S(   u   
        TODO
        u   autoR   RG   u
   1234567890R)   RH   RI   i   RJ   u
   read writeR1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   random_state_stringu   stateu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   i.  N(   R   RQ   R   R!   RK   R#   R)   R   RL   RM   RN   R5   R6   R   R9   R:   R   R   R;   R<   (   R   R=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt.   test_pre_auth_approval_prompt_default_overrideÄ   s    		c         C  s§   |  j  j d d d d ƒ t i |  j j d 6d d 6ƒ } d j d	 t d
 ƒ d | ƒ } |  j  j | ƒ } |  j | j	 d ƒ | j
 d } |  j | d j ƒ  d ƒ d S(   ue   
        Test for default redirect uri if omitted from query string with response_type: code
        R1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   iÈ   u   formu   redirect_uriu   http://localhostN(   R5   R6   R   R)   R9   R:   R   R   R;   R<   RB   RC   (   R   R=   R3   R>   RD   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_pre_auth_default_redirectÚ   s    c         C  s‡   |  j  j d d d d ƒ t i |  j j d 6d d 6d d	 6ƒ } d
 j d t d ƒ d | ƒ } |  j  j | ƒ } |  j | j	 d ƒ d S(   uk   
        Test error when passing a forbidden redirect_uri in query string with response_type: code
        R1   u	   test_userR2   u   123456u	   client_idu   codeu   response_typeu   http://forbidden.itu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   i  N(
   R5   R6   R   R)   R9   R:   R   R   R;   R<   (   R   R=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt    test_pre_auth_forbibben_redirectì   s    c         C  s”   |  j  j d d d d ƒ t i |  j j d 6d d 6ƒ } d j d	 t d
 ƒ d | ƒ } |  j  j | ƒ } |  j | j	 d ƒ |  j
 d | d ƒ d S(   uO   
        Test error when passing a wrong response_type in query string
        R1   u	   test_userR2   u   123456u	   client_idu   WRONGu   response_typeu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   i.  u   error=unsupported_response_typeu   LocationN(   R5   R6   R   R)   R9   R:   R   R   R;   R<   RA   (   R   R=   R3   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt!   test_pre_auth_wrong_response_typeü   s    c         C  sÀ   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d | d ƒ |  j	 d | d ƒ |  j	 d | d ƒ d S(   ub   
        Test authorization code is given for an allowed request with response_type: code
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizet   datai.  u   http://example.it?u   Locationu   state=random_state_stringu   code=N(
   R5   R6   R)   R9   R7   t   postR   R;   R<   RA   (   R   t	   form_dataR>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_code_post_auth_allow  s    
c         C  s˜   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d | d ƒ d S(   u<   
        Test error when resource owner deny access
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u   error=access_deniedu   LocationN(
   R5   R6   R)   R9   t   FalseRX   R   R;   R<   RA   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_code_post_auth_deny!  s    
c         C  s˜   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d | d ƒ d S(   ul   
        Test authorization code is given for an allowed request with a response_type not supported
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   UNKNOWNu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u   http://example.it?erroru   LocationN(
   R5   R6   R)   R9   R7   RX   R   R;   R<   RA   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt$   test_code_post_auth_bad_responsetype4  s    
c         C  s„   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ d S(   ug   
        Test authorization code is given for an allowed request with a forbidden redirect_uri
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://forbidden.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i  N(	   R5   R6   R)   R9   R7   RX   R   R;   R<   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt*   test_code_post_auth_forbidden_redirect_uriG  s    
c         C  s„   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ d S(   u=   
        Test validation of a malicious redirect_uri
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   /../u   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i  N(	   R5   R6   R)   R9   R7   RX   R   R;   R<   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt*   test_code_post_auth_malicious_redirect_uriY  s    
c         C  sÀ   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d | d ƒ |  j	 d | d ƒ |  j	 d | d ƒ d S(   u¢   
        Test authorization code is given for an allowed request with response_type: code
        using a non-standard, but allowed, redirect_uri scheme.
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   custom-scheme://example.comu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u   custom-scheme://example.com?u   Locationu   state=random_state_stringu   code=N(
   R5   R6   R)   R9   R7   RX   R   R;   R<   RA   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt4   test_code_post_auth_allow_custom_redirect_uri_schemek  s    
c         C  s¬   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d | d ƒ |  j	 d | d ƒ d S(   u|   
        Test error when resource owner deny access
        using a non-standard, but allowed, redirect_uri scheme.
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   custom-scheme://example.comu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u   custom-scheme://example.com?u   Locationu   error=access_deniedN(
   R5   R6   R)   R9   R[   RX   R   R;   R<   RA   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt3   test_code_post_auth_deny_custom_redirect_uri_scheme  s    
c         C  s¬   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j	 d
 | d ƒ |  j	 d | d ƒ d S(   u½   
        Tests that a redirection uri with query string is allowed
        and query string is retained on redirection.
        See http://tools.ietf.org/html/rfc6749#section-3.1.2
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.com?foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u   Locationu   code=N(
   R5   R6   R)   R9   R7   RX   R   R;   R<   RA   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt4   test_code_post_auth_redirection_uri_with_querystring–  s    
c         C  s˜   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ |  j d | d ƒ d S(   u¨   
        Test that in case of error the querystring of the redirection uri is preserved

        See https://github.com/evonove/django-oauth-toolkit/issues/238
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.com?foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i.  u.   http://example.com?foo=bar&error=access_deniedu   LocationN(	   R5   R6   R)   R9   R[   RX   R   R;   R<   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt<   test_code_post_auth_failing_redirection_uri_with_querystring¬  s    
c         C  s„   |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} |  j | j d ƒ d S(   uV   
        Tests that a redirection uri is matched using scheme + netloc + path
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.com/a?foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   i  N(	   R5   R6   R)   R9   R7   RX   R   R;   R<   (   R   RY   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt;   test_code_post_auth_fails_when_redirect_uri_path_is_invalidÁ  s    
(   R   R   R?   R@   RE   RF   RP   RR   RS   RT   RU   RV   RZ   R\   R]   R^   R_   R`   Ra   Rb   Rc   Rd   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyR0   9   s(   																			t   TestAuthorizationCodeTokenViewc           B  s³   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s{   i |  j  j d 6d d 6d d 6d d 6d d	 6t d
 6} |  j j t d ƒ d | ƒ} t t | d ƒ j ƒ } | d j	 ƒ  S(   uF   
        Helper method to retrieve a valid authorization code
        u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   u   Location(
   R)   R9   R7   R5   RX   R   R   R   t   queryt   pop(   R   t   authcode_dataR>   t
   query_dict(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   get_authÕ  s    
c         C  sð   |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } |  j	 | j
 d ƒ t j | j j d ƒ ƒ } |  j	 | d d ƒ |  j	 | d d ƒ |  j	 | d t j ƒ d S(   u^   
        Request an access token using basic authentication for client authentication
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   iÈ   u   utf-8u
   token_typeu   Beareru   scopeu
   read writeu
   expires_inN(   R5   R6   Rj   t   get_basic_auth_headerR)   R9   t   client_secretRX   R   R;   R<   t   jsont   loadst   contentt   decodeR   t   ACCESS_TOKEN_EXPIRE_SECONDS(   R   t   authorization_codet   token_request_datat   auth_headersR>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_authæ  s    
!c         C  sæ  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } |  j d | k ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j  j t d
 ƒ d | | } i d d 6| d d 6| d d 6} |  j  j t d
 ƒ d | | } |  j | j d ƒ t	 j
 | j j d ƒ ƒ } |  j d | k ƒ |  j  j t d
 ƒ d | | } |  j | j d ƒ t	 j
 | j j d ƒ ƒ } |  j d | j ƒ  k ƒ d S(   u?   
        Request an access token using a refresh token
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokenu   scopeiÈ   u   access_tokeni‘  u   invalid_grantN(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   t
   assertTrueR;   R<   t   values(   R   Rr   Rs   Rt   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_refreshü  s8    
!
!!!c         C  sI  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } | d } | d } i d d 6| d 6| d d 6} |  j  j t d
 ƒ d | | } |  j | j d ƒ |  j t j j d | ƒ j ƒ  ƒ |  j t j j d | ƒ j ƒ  ƒ d S(   uU   
        Ensure existing refresh tokens are cleaned up when issuing new ones
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokenu   access_tokenu   scopeiÈ   RG   N(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   R;   R<   t   assertFalseR   R!   t   filtert   existsR   (   R   Rr   Rs   Rt   R>   Ro   t   rtt   at(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt#   test_refresh_invalidates_old_tokens(  s&    
!

!"c         C  s+  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } |  j d | k ƒ i d d 6| d d 6} |  j  j t d
 ƒ d | | } |  j | j d ƒ t	 j
 | j j d ƒ ƒ } |  j d | k ƒ d S(   uY   
        Request an access token using a refresh token without passing any scope
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokeniÈ   u   access_tokenN(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   Rv   R;   R<   (   R   Rr   Rs   Rt   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_refresh_no_scopesG  s"    
!!c         C  s  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } |  j d | k ƒ i d d 6| d d 6d d 6} |  j  j t d
 ƒ d | | } |  j | j d ƒ d S(   uP   
        Request an access token using a refresh token and wrong scopes
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokenu   read write nukeu   scopei‘  N(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   Rv   R;   R<   (   R   Rr   Rs   Rt   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_refresh_bad_scopesc  s     
!
!c         C  s<  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } |  j d | k ƒ i d d 6| d d 6| d d 6} |  j  j t d
 ƒ d | | } |  j | j d ƒ |  j  j t d
 ƒ d | | } |  j | j d ƒ d S(   u[   
        Try refreshing an access token with the same refresh token more than once
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokenu   scopeiÈ   i‘  N(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   Rv   R;   R<   (   R   Rr   Rs   Rt   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt$   test_refresh_fail_repeating_requests}  s$    
!!!c      	   C  sX  |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j |  j j ƒ } |  j  j t d
 ƒ d | | } t	 j
 | j j d ƒ ƒ } |  j d | k ƒ i d d 6| d d 6| d d 6} t j d d t ƒm |  j  j t d
 ƒ d | | } |  j | j d ƒ |  j  j t d
 ƒ d | | } |  j | j d ƒ Wd QXd S(   uu   
        Try refreshing an access token with the same refresh token more than once when not rotating tokens.
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   u   utf-8u   refresh_tokenu   scopeuO   oauthlib.oauth2.rfc6749.request_validator.RequestValidator.rotate_refresh_tokent   return_valueiÈ   N(   R5   R6   Rj   Rk   R)   R9   Rl   RX   R   Rm   Rn   Ro   Rp   Rv   t   mockt   patchR[   R;   R<   (   R   Rr   Rs   Rt   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt3   test_refresh_repeating_requests_non_rotating_tokens™  s(    
!
!!c         C  sŠ   |  j  j d d d d ƒ i d d 6d d 6d	 d
 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } |  j | j	 d ƒ d S(   uH   
        Request an access token using a bad authorization code
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   BLAHu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   i‘  N(
   R5   R6   Rk   R)   R9   Rl   RX   R   R;   R<   (   R   Rs   Rt   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_auth_bad_authcode¸  s    
!c         C  sŠ   |  j  j d d d d ƒ i d d 6d d 6d	 d
 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } |  j | j	 d ƒ d S(   uG   
        Request an access token using a bad grant_type string
        R1   u	   test_userR2   u   123456u   UNKNOWNu
   grant_typeu   BLAHu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   i  N(
   R5   R6   Rk   R)   R9   Rl   RX   R   R;   R<   (   R   Rs   Rt   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_auth_bad_granttypeÈ  s    
!c         C  sÍ   |  j  j d d d d ƒ t d |  j d |  j d d d	 t j ƒ  d
 d d d ƒ } | j ƒ  i d d 6d d 6d d 6} |  j |  j j	 |  j j
 ƒ } |  j  j t d ƒ d | | } |  j | j d ƒ d S(   uF   
        Request an access token using an expired grant token
        R1   u	   test_userR2   u   123456R)   R   t   codeu   BLAHRH   t   redirect_uriu    RJ   u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   oauth2_provider:tokenRW   i‘  N(   R5   R6   R   R)   R#   R   RL   R*   Rk   R9   Rl   RX   R   R;   R<   (   R   t   gRs   Rt   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_auth_grant_expiredØ  s    *

!c         C  s   |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} |  j |  j j d
 ƒ } |  j  j t d ƒ d | | } |  j | j	 d ƒ d S(   u^   
        Request an access token using basic authentication for client authentication
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   BOOM!u   oauth2_provider:tokenRW   i‘  N(
   R5   R6   Rj   Rk   R)   R9   RX   R   R;   R<   (   R   Rr   Rs   Rt   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_auth_bad_secretë  s    
!c         C  sÈ   |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6} d
 j |  j j |  j j ƒ } t j | j	 d ƒ ƒ } i d | j
 d ƒ d 6} |  j  j t d ƒ d | | } |  j | j d ƒ d S(   u^   
        Request an access token using basic authentication for client authentication
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu   {0}:{1}u   utf-8u   Wrong u   HTTP_AUTHORIZATIONu   oauth2_provider:tokenRW   i‘  N(   R5   R6   Rj   R:   R)   R9   Rl   t   base64t	   b64encodet   encodeRp   RX   R   R;   R<   (   R   Rr   Rs   t	   user_passt   auth_stringRt   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_basic_auth_wrong_auth_typeü  s    
!c         C  sé   |  j  j d d d d ƒ |  j ƒ  } i d d 6| d 6d d	 6|  j j d
 6|  j j d 6} |  j  j t d ƒ d | ƒ} |  j | j	 d ƒ t
 j | j j d ƒ ƒ } |  j | d d ƒ |  j | d d ƒ |  j | d t j ƒ d S(   uC   
        Request an access token using client_type: public
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu	   client_idu   client_secretu   oauth2_provider:tokenRW   iÈ   u   utf-8u
   token_typeu   Beareru   scopeu
   read writeu
   expires_inN(   R5   R6   Rj   R)   R9   Rl   RX   R   R;   R<   Rm   Rn   Ro   Rp   R   Rq   (   R   Rr   Rs   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_request_body_params  s    c         C  sø   |  j  j d d d d ƒ t j |  j _ |  j j ƒ  |  j ƒ  } i d d 6| d 6d d	 6|  j j d
 6} |  j  j	 t
 d ƒ d | ƒ} |  j | j d ƒ t j | j j d ƒ ƒ } |  j | d d ƒ |  j | d d ƒ |  j | d t j ƒ d S(   uC   
        Request an access token using client_type: public
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   http://example.itu   redirect_uriu	   client_idu   oauth2_provider:tokenRW   iÈ   u   utf-8u
   token_typeu   Beareru   scopeu
   read writeu
   expires_inN(   R5   R6   R&   t   CLIENT_PUBLICR)   R   R*   Rj   R9   RX   R   R;   R<   Rm   Rn   Ro   Rp   R   Rq   (   R   Rr   Rs   R>   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_public)  s    c         C  sž   |  j  j d d d d ƒ t j |  j _ |  j j ƒ  |  j ƒ  } i d d 6| d 6d d	 6|  j j d
 6} |  j  j	 t
 d ƒ d | ƒ} |  j | j d ƒ d S(   uz   
        Request an access token using client_type: public and ensure redirect_uri is
        properly validated.
        R1   u	   test_userR2   u   123456u   authorization_codeu
   grant_typeu   codeu   /../u   redirect_uriu	   client_idu   oauth2_provider:tokenRW   i‘  N(   R5   R6   R&   R”   R)   R   R*   Rj   R9   RX   R   R;   R<   (   R   Rr   Rs   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_malicious_redirect_uriB  s    c         C  sa  |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} t t | d ƒ j	 ƒ } | d j
 ƒ  } i d d 6| d 6d
 d 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } |  j | j d ƒ t j | j j d ƒ ƒ } |  j | d d ƒ |  j | d	 d ƒ |  j | d t j ƒ d S(   ue   
        Tests code exchange succeed when redirect uri matches the one used for code request
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.it?foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokeniÈ   u   utf-8u
   token_typeu   Beareru
   expires_inN(   R5   R6   R)   R9   R7   RX   R   R   R   Rf   Rg   Rk   Rl   R;   R<   Rm   Rn   Ro   Rp   R   Rq   (   R   Rh   R>   Ri   Rr   Rs   Rt   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt2   test_code_exchange_succeed_when_redirect_uri_matchW  s,    

!c         C  s  |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} t t | d ƒ j	 ƒ } | d j
 ƒ  } i d d 6| d 6d d 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } |  j | j d ƒ d S(   uj   
        Tests code exchange fails when redirect uri does not match the one used for code request
        R1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.it?foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   u   Locationu   authorization_codeu
   grant_typeu   http://example.it?foo=baraau   oauth2_provider:tokeni‘  N(   R5   R6   R)   R9   R7   RX   R   R   R   Rf   Rg   Rk   Rl   R;   R<   (   R   Rh   R>   Ri   Rr   Rs   Rt   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt9   test_code_exchange_fails_when_redirect_uri_does_not_matchz  s$    

!c         C  sz  |  j  j d d d d ƒ d |  j _ |  j j ƒ  i |  j j d 6d d 6d	 d
 6d d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} t	 t
 | d ƒ j ƒ } | d j ƒ  } i d d 6| d 6d d 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } |  j | j d ƒ t j | j j d ƒ ƒ } |  j | d d ƒ |  j | d
 d	 ƒ |  j | d t j ƒ d S(   ue   
        Tests code exchange succeed when redirect uri matches the one used for code request
        R1   u	   test_userR2   u   123456u+   http://localhost http://example.com?foo=baru	   client_idu   random_state_stringu   stateu
   read writeu   scopeu"   http://example.com?bar=baz&foo=baru   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokeniÈ   u   utf-8u
   token_typeu   Beareru
   expires_inN(   R5   R6   R)   R   R*   R9   R7   RX   R   R   R   Rf   Rg   Rk   Rl   R;   R<   Rm   Rn   Ro   Rp   R   Rq   (   R   Rh   R>   Ri   Rr   Rs   Rt   Ro   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pytM   test_code_exchange_succeed_when_redirect_uri_match_with_multiple_query_params˜  s0    

!(   R   R   Rj   Ru   Rx   R~   R   R€   R   R…   R†   R‡   R‹   RŒ   R’   R“   R•   R–   R—   R˜   R™   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyRe   Ô  s&   			,														#	t&   TestAuthorizationCodeProtectedResourcec           B  s   e  Z d  „  Z d „  Z RS(   c         C  ss  |  j  j d d d d ƒ i |  j j d 6d d 6d d	 6d
 d 6d d 6t d 6} |  j  j t d ƒ d | ƒ} t t | d ƒ j	 ƒ } | d j
 ƒ  } i d d 6| d 6d
 d 6} |  j |  j j |  j j ƒ } |  j  j t d ƒ d | | } t j | j j d ƒ ƒ } | d } i d | d 6} |  j j d |  }	 |  j |	 _ t j ƒ  }
 |
 |	 ƒ } |  j | d ƒ d  S(   NR1   u	   test_userR2   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeRW   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu   This is a protected resource(   R5   R6   R)   R9   R7   RX   R   R   R   Rf   Rg   Rk   Rl   Rm   Rn   Ro   Rp   R   R   R#   R   R   t   as_viewR;   (   R   Rh   R>   Ri   Rr   Rs   Rt   Ro   t   access_tokenR   t   view(    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_resource_access_allowed¿  s4    

!
c         C  s]   i d d 6} |  j  j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | j d ƒ d  S(   Nu   Bearer u	   faketokenu   HTTP_AUTHORIZATIONu   /fake-resourcei“  u   Bearer faketoken(   R   R   R#   R   R   R›   R;   R<   (   R   Rt   R   R   R>   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_resource_access_denyæ  s    
(   R   R   Rž   RŸ   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyRš   ¾  s   	't   TestDefaultScopesc           B  s   e  Z d  „  Z RS(   c         C  s(  |  j  j d d d d ƒ d g t _ t i |  j j d 6d d 6d	 d
 6d d 6ƒ } d j d t d ƒ d | ƒ } |  j  j	 | ƒ } |  j
 | j d ƒ |  j d | j ƒ | j d } |  j
 | d j ƒ  d ƒ |  j
 | d
 j ƒ  d	 ƒ |  j
 | d j ƒ  d ƒ |  j
 | d j ƒ  |  j j ƒ d S(   uc   
        Test response for a valid client_id with response_type: code using default scopes
        R1   u	   test_userR2   u   123456u   readu	   client_idu   codeu   response_typeu   random_state_stringu   stateu   http://example.itu   redirect_uriu
   {url}?{qs}R3   u   oauth2_provider:authorizeR4   iÈ   u   formu   scopeN(   R5   R6   R   R,   R   R)   R9   R:   R   R   R;   R<   RA   RB   RC   (   R   R=   R3   R>   RD   (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   test_pre_auth_default_scopesô  s     (   R   R   R¡   (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyR    ò  s   ('   t
   __future__R    R   Rm   RM   Rƒ   t   django.testR   R   t   django.core.urlresolversR   t   django.test.utilsR   t   django.utilsR   t   compatR   R   R   R	   t   modelsR
   R   R   R   t   settingsR   t   viewsR   t
   test_utilsR   R&   R    R   R   R0   Re   Rš   R    (    (    (    sO   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_authorization_code.pyt   <module>   s.   ""		ÿ œÿ ë4