ó
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	 m
 Z
 m Z m Z d d l 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 f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   TestCaset   RequestFactory(   t   reversei   (   t   urlparset   parse_qst	   urlencodet   get_user_model(   t   get_application_model(   t   oauth2_settings(   t   ProtectedResourceViewt   AuthorizationViewt   ResourceViewc           B  s   e  Z d  „  Z RS(   c         O  s   d S(   Nu   This is a protected resource(    (   t   selft   requestt   argst   kwargs(    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   get   s    (   t   __name__t
   __module__R   (    (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.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 g t _ d g t _ d  S(   Nu	   test_useru   test@user.comu   123456u   dev_useru   dev@user.comt   nameu   Test Implicit Applicationt   redirect_urisu5   http://localhost http://example.com http://example.itt   usert   client_typet   authorization_grant_typeu   readu   write(   R   t   factoryt	   UserModelt   objectst   create_usert	   test_usert   dev_usert   Applicationt   CLIENT_PUBLICt   GRANT_IMPLICITt   applicationt   saveR	   t   _SCOPESt   _DEFAULT_SCOPES(   R   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   setUp   s    		c         C  s+   |  j  j ƒ  |  j j ƒ  |  j j ƒ  d  S(   N(   R#   t   deleteR   R   (   R   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   tearDown*   s    (   R   R   R'   R)   (    (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyR      s   	t!   TestImplicitAuthorizationCodeViewc           B  sb   e  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 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 ƒ d S(   ub   
        Test response for a valid client_id with response_type: token and default_scopes
        t   usernameu	   test_usert   passwordu   123456u	   client_idu   tokenu   response_typeu   random_state_stringu   stateu   http://example.itu   redirect_uriu
   {url}?{qs}t   urlu   oauth2_provider:authorizet   qsiÈ   u   formu   scopeu   readN(   t   clientt   loginR   R#   t	   client_idt   formatR   R   t   assertEqualt   status_codet   assertInt   contextt   value(   R   t   query_stringR-   t   responset   form(    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt)   test_pre_auth_valid_client_default_scopes1   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(   uO   
        Test response for a valid client_id with response_type: token
        R+   u	   test_userR,   u   123456u	   client_idu   tokenu   response_typeu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}R-   u   oauth2_provider:authorizeR.   iÈ   u   formN(   R/   R0   R   R#   R1   R2   R   R   R3   R4   R5   R6   R7   (   R   R8   R-   R9   R:   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_pre_auth_valid_clientE   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(   uO   
        Test error for an invalid client_id with response_type: token
        R+   u	   test_userR,   u   123456u   fakeclientidu	   client_idu   tokenu   response_typeu
   {url}?{qs}R-   u   oauth2_provider:authorizeR.   i  N(   R/   R0   R   R2   R   R   R3   R4   (   R   R8   R-   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_pre_auth_invalid_client`   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(   uf   
        Test for default redirect uri if omitted from query string with response_type: token
        R+   u	   test_userR,   u   123456u	   client_idu   tokenu   response_typeu
   {url}?{qs}R-   u   oauth2_provider:authorizeR.   iÈ   u   formu   redirect_uriu   http://localhostN(   R/   R0   R   R#   R1   R2   R   R   R3   R4   R6   R7   (   R   R8   R-   R9   R:   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_pre_auth_default_redirecto   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(   ul   
        Test error when passing a forbidden redirect_uri in query string with response_type: token
        R+   u	   test_userR,   u   123456u	   client_idu   tokenu   response_typeu   http://forbidden.itu   redirect_uriu
   {url}?{qs}R-   u   oauth2_provider:authorizeR.   i  N(
   R/   R0   R   R#   R1   R2   R   R   R3   R4   (   R   R8   R-   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt    test_pre_auth_forbibben_redirect   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(   uc   
        Test authorization code is given for an allowed request with response_type: token
        R+   u	   test_userR,   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   tokenu   response_typeu   allowu   oauth2_provider:authorizet   datai.  u   http://example.it#u   Locationu   access_token=u   state=random_state_stringN(
   R/   R0   R#   R1   t   Truet   postR   R3   R4   R5   (   R   t	   form_dataR9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_post_auth_allow‘   s    
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 ƒ |  j d | d ƒ |  j d | d ƒ |  j d | d ƒ d S(   u_   
        If application.skip_authorization = True, should skip the authorization page.
        R+   u	   test_userR,   u   123456u	   client_idu   tokenu   response_typeu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu
   {url}?{qs}R-   u   oauth2_provider:authorizeR.   i.  u   http://example.it#u   Locationu   access_token=u   state=random_state_stringN(   R/   R0   RA   R#   t   skip_authorizationR$   R   R1   R2   R   R   R3   R4   R5   (   R   R8   R-   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt"   test_skip_authorization_completely¦   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
        R+   u	   test_userR,   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   tokenu   response_typeu   allowu   oauth2_provider:authorizeR@   i.  u   error=access_deniedu   LocationN(
   R/   R0   R#   R1   t   FalseRB   R   R3   R4   R5   (   R   RC   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_token_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 ƒ |  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
        R+   u	   test_userR,   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.com?foo=baru   redirect_uriu   tokenu   response_typeu   allowu   oauth2_provider:authorizeR@   i.  u   Locationu   access_token=N(
   R/   R0   R#   R1   RA   RB   R   R3   R4   R5   (   R   RC   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt.   test_implicit_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
        R+   u	   test_userR,   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:authorizeR@   i  N(	   R/   R0   R#   R1   RA   RB   R   R3   R4   (   R   RC   R9   (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt5   test_implicit_fails_when_redirect_uri_path_is_invalidç   s    
(   R   R   R;   R<   R=   R>   R?   RD   RF   RH   RI   RJ   (    (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyR*   0   s   									t   TestImplicitTokenViewc           B  s   e  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
 ƒ  } i d | d 6} |  j j d |  } |  j | _ t j ƒ  } | | ƒ } |  j | d ƒ d  S(   NR+   u	   test_userR,   u   123456u	   client_idu   random_state_stringu   stateu
   read writeu   scopeu   http://example.itu   redirect_uriu   tokenu   response_typeu   allowu   oauth2_provider:authorizeR@   u   Locationu   access_tokenu   Bearer u   HTTP_AUTHORIZATIONu   /fake-resourceu   This is a protected resource(   R/   R0   R#   R1   RA   RB   R   R   R   t   fragmentt   popR   R   R   R   R   t   as_viewR3   (   R   t   authcode_dataR9   t	   frag_dictt   access_tokent   auth_headersR   t   view(    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   test_resource_access_allowedû   s$    
(   R   R   RT   (    (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyRK   ú   s   (   t
   __future__R    t   mockt   django.testR   R   t   django.core.urlresolversR   t   compatR   R   R   R   t   modelsR   t   settingsR	   t   viewsR
   R   R    R   R   R   R*   RK   (    (    (    sE   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_implicit.pyt   <module>   s   "		Ê