ó
O'—^c           @  s  d  d l  m 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 d d l m Z d d l m Z m Z e ƒ  Z e ƒ  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ 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   ImproperlyConfigured(   t   reversei   (   t   TestCaseUtilsi   (   t   urlparset   parse_qst   get_user_modelt	   urlencode(   t   get_application_modelt   Grantt   AccessToken(   t   oauth2_settings(   t   ScopedProtectedResourceViewt   ReadWriteScopedResourceViewt   ScopeResourceViewc           B  s   e  Z d  g Z d „  Z RS(   u   scope1c         O  s   d S(   Nu   This is a protected resource(    (   t   selft   requestt   argst   kwargs(    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   get   s    (   t   __name__t
   __module__t   required_scopesR   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR      s   	t   MultiScopeResourceViewc           B  s   e  Z d  d g Z d „  Z RS(   u   scope1u   scope2c         O  s   d S(   Nu   This is a protected resource(    (   R   R   R   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR      s    (   R   R   R   R   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR      s   t   ReadWriteResourceViewc           B  s   e  Z d  „  Z d „  Z RS(   c         O  s   d S(   Nu!   This is a read protected resource(    (   R   R   R   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR   #   s    c         O  s   d S(   Nu"   This is a write protected resource(    (   R   R   R   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   post&   s    (   R   R   R   R   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.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 ƒ |  _ t d d d d	 d
 |  j d t j d t j	 ƒ |  _
 |  j
 j ƒ  d d d d d g t _ d t _ d t _ d  S(   Nu	   test_useru   test@user.comu   123456u   dev_useru   dev@user.comt   nameu   Test Applicationt   redirect_urisu5   http://localhost http://example.com http://example.itt   usert   client_typet   authorization_grant_typeu   readu   writeu   scope1u   scope2u   scope3(   R   t   factoryt	   UserModelt   objectst   create_usert	   test_usert   dev_usert   Applicationt   CLIENT_CONFIDENTIALt   GRANT_AUTHORIZATION_CODEt   applicationt   saveR   t   _SCOPESt
   READ_SCOPEt   WRITE_SCOPE(   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   setUp+   s    			c         C  s+   |  j  j ƒ  |  j j ƒ  |  j j ƒ  d  S(   N(   R+   t   deleteR&   R'   (   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   tearDown=   s    (   R   R   R0   R2   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR   *   s   	t.   TestScopesQueryParameterBackwardsCompatibilityc           B  s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C  s5   t  t |  ƒ j ƒ  d d g t _ d d g t _ d  S(   Nu   readu   write(   t   superR3   R0   R   R-   t   _DEFAULT_SCOPES(   R   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR0   D   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
 ƒ  } t j j d | ƒ } |  j | j d ƒ d S(   uV   
        Tests support for plural `scopes` query parameter on POST requests.

        t   usernameu	   test_usert   passwordu   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopesu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizet   datau   Locationt   codeN(   t   clientt   loginR+   t	   client_idt   TrueR   R   R   R   t   queryt   popR   R$   R   t   assertEqualt   scope(   R   t   authcode_datat   responset
   query_dictt   authorization_codet   grant(    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt0   test_scopes_query_parameter_is_supported_on_postI   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 ƒ d S(   uU   
        Tests support for plural `scopes` query parameter on GET requests.

        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopesu   http://example.itu   redirect_uriu   codeu   response_typeu
   {url}?{qs}t   urlu   oauth2_provider:authorizet   qsiÈ   u   formu   scopeN(   R:   R;   R	   R+   R<   t   formatR   R   R@   t   status_codet   assertInt   contextt   value(   R   t   query_stringRH   RC   t   form(    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt/   test_scopes_query_parameter_is_supported_on_get`   s    (   R   R   R0   RG   RQ   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR3   C   s   		t   TestScopesSavec           B  s   e  Z d  „  Z d „  Z RS(   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
 ƒ  } t j j d | ƒ } |  j | j d ƒ d S(   u9   
        Test scopes are properly saved in grant
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope1 scope2u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   LocationR9   N(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   R   R$   R   R@   RA   (   R   RB   RC   RD   RE   RF   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_scopes_saved_in_grant{   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 | | } t j | j j d ƒ ƒ } | d } t j j d | ƒ }	 |  j |	 j d ƒ d S(   u@   
        Test scopes are properly saved in access token
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope1 scope2u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokent   tokenN(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   t   get_basic_auth_headert   client_secrett   jsont   loadst   contentt   decodeR   R$   R   R@   RA   (
   R   RB   RC   RD   RE   t   token_request_datat   auth_headersRY   t   access_tokent   at(    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt    test_scopes_save_in_access_token‘   s*    

!
(   R   R   RS   R_   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyRR   z   s   	t   TestScopesProtectionc           B  s,   e  Z d  „  Z d „  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(   uX   
        Test access to a scope protected resource with correct scopes provided
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope1 scope2u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu   This is a protected resourceN(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   RU   RV   RW   RX   RY   RZ   R"   R   R&   R   R   t   as_viewR@   (   R   RB   RC   RD   RE   R[   R\   RY   R]   R   t   view(    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_scopes_protection_validµ   s4    

!
c         C  sv  |  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 | j d ƒ d S(   uV   
        Test access to a scope protected resource with wrong scopes provided
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope2u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourcei“  N(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   RU   RV   RW   RX   RY   RZ   R"   R   R&   R   R   Ra   R@   RK   (   R   RB   RC   RD   RE   R[   R\   RY   R]   R   Rb   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_scopes_protection_failß   s4    

!
c         C  sv  |  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 | j d ƒ d S(   u\   
        Test access to a multi-scope protected resource with wrong scopes provided
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope1 scope3u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourcei“  N(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   RU   RV   RW   RX   RY   RZ   R"   R   R&   R   R   Ra   R@   RK   (   R   RB   RC   RD   RE   R[   R\   RY   R]   R   Rb   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_multi_scope_fail	  s4    

!
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(   u^   
        Test access to a multi-scope protected resource with correct scopes provided
        R6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scope1 scope2u   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu   This is a protected resourceN(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   RU   RV   RW   RX   RY   RZ   R"   R   R&   R   R   Ra   R@   (   R   RB   RC   RD   RE   R[   R\   RY   R]   R   Rb   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_multi_scope_valid3  s4    

!
(   R   R   Rc   Rd   Re   Rf   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyR`   ´   s   	*	*	*t   TestReadWriteScopec           B  sG   e  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 ƒ i |  j j d 6d d 6| 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 S(   NR6   u	   test_userR7   u   123456u	   client_idu   random_state_stringu   stateu   scopeu   http://example.itu   redirect_uriu   codeu   response_typeu   allowu   oauth2_provider:authorizeR8   u   Locationu   authorization_codeu
   grant_typeu   oauth2_provider:tokenu   utf-8u   access_token(   R:   R;   R+   R<   R=   R   R   R   R   R>   R?   RU   RV   RW   RX   RY   RZ   (	   R   t   scopesRB   RC   RD   RE   R[   R\   RY   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   get_access_token_  s&    

!c         C  sx   d g t  _ |  j j d ƒ } t j ƒ  } |  j t | | ƒ d d g t  _ d t  _ t j ƒ  } |  j t | | ƒ d  S(   Nu   scope1u   /fakeu   readu   writeu   ciccia(	   R   R-   R"   R   R   Ra   t   assertRaisesR   R.   (   R   R   Rb   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_improperly_configured{  s    	c         C  sx   d g t  _ |  j j d ƒ } t j ƒ  } |  j t | | ƒ d d g t  _ d t  _ t j ƒ  } |  j t | | ƒ d  S(   Nu   scope1u   /fakeu   readu   writeu   ciccia(	   R   R-   R"   R   R   Ra   Rj   R   R.   (   R   R   Rb   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_properly_configuredˆ  s    	c         C  sm   |  j  d ƒ } i d | d 6} |  j j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | d ƒ d  S(   Nu   readu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu!   This is a read protected resource(   Ri   R"   R   R&   R   R   Ra   R@   (   R   R]   R\   R   Rb   RC   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_has_read_scope•  s    c         C  sp   |  j  d ƒ } i d | d 6} |  j j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | j d ƒ d  S(   Nu   scope1u   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourcei“  (	   Ri   R"   R   R&   R   R   Ra   R@   RK   (   R   R]   R\   R   Rb   RC   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_no_read_scope£  s    c         C  sm   |  j  d ƒ } i d | d 6} |  j j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | d ƒ d  S(   Nu   writeu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu"   This is a write protected resource(   Ri   R"   R   R&   R   R   Ra   R@   (   R   R]   R\   R   Rb   RC   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_has_write_scope±  s    c         C  sp   |  j  d ƒ } i d | d 6} |  j j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | j d ƒ d  S(   Nu   scope1u   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourcei“  (	   Ri   R"   R   R&   R   R   Ra   R@   RK   (   R   R]   R\   R   Rb   RC   (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   test_no_write_scope¿  s    (	   R   R   Ri   Rk   Rl   Rm   Rn   Ro   Rp   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyRg   ^  s   						($   t
   __future__R    RW   t   django.testR   R   t   django.core.exceptionsR   t   django.core.urlresolversR   t
   test_utilsR   t   compatR   R   R   R	   t   modelsR
   R   R   t   settingsR   t   viewsR   R   R(   R#   R   R   R   R   R3   RR   R`   Rg   (    (    (    sC   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_scopes.pyt   <module>   s&   "		7:ª