ó
O'—^c           @  sÊ   d  d l  m 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
 m Z d d l m Z e
 ƒ  Z e ƒ  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   reverse(   t   TestCase(   t   timezonei   (   t   get_application_modelt   AccessToken(   t   get_user_modelt   TestAuthorizedTokenViewsc           B  s    e  Z d  Z d „  Z d „  Z RS(   uD   
    TestCase superclass for Authorized Token Views' Test Cases
    c         C  sz   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  S(   Nu   foo_useru   test@user.comu   123456u   bar_useru   dev@user.comt   nameu   Test Applicationt   redirect_urisu5   http://localhost http://example.com http://example.itt   usert   client_typet   authorization_grant_type(
   t	   UserModelt   objectst   create_usert   foo_usert   bar_usert   Applicationt   CLIENT_CONFIDENTIALt   GRANT_AUTHORIZATION_CODEt   applicationt   save(   t   self(    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   setUp   s    		c         C  s   |  j  j ƒ  |  j j ƒ  d  S(   N(   R   t   deleteR   (   R   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   tearDown!   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyR      s   	t   TestAuthorizedTokenListViewc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u1   
    Tests for the Authorized Token ListView
    c         C  sF   |  j  j t d ƒ ƒ } |  j | j d ƒ |  j d | d k ƒ d S(   uV   
        Test that the view redirects to login page if user is not logged-in.
        u%   oauth2_provider:authorized-token-listi.  u   /accounts/login/?next=u   LocationN(   t   clientt   getR   t   assertEqualt   status_codet
   assertTrue(   R   t   response(    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt%   test_list_view_authorization_required*   s    c         C  s[   |  j  j d d d d ƒ |  j  j t d ƒ ƒ } |  j | j d ƒ |  j d | j ƒ d S(	   uT   
        Test that when you have no tokens, an appropriate message is shown
        t   usernameu   foo_usert   passwordu   123456u%   oauth2_provider:authorized-token-listiÈ   s#   There are no authorized tokens yet.N(   R   t   loginR    R   R!   R"   t   assertInt   content(   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   test_empty_list_view2   s    c         C  sÈ   |  j  j d d d d ƒ t j j d |  j d d d |  j d	 t j ƒ  t	 j
 d
 d ƒ d d ƒ |  j  j t d ƒ ƒ } |  j | j d ƒ |  j d | j ƒ |  j d | j ƒ |  j d | j ƒ d S(   u5   
        Test that the view shows your token
        R&   u   bar_userR'   u   123456R
   t   tokenu
   1234567890R   t   expirest   daysi   t   scopeu
   read writeu%   oauth2_provider:authorized-token-listiÈ   t   readt   writes#   There are no authorized tokens yet.N(   R   R(   R   R   t   createR   R   R   t   nowt   datetimet	   timedeltaR    R   R!   R"   R)   R*   t   assertNotIn(   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   test_list_view_one_token<   s    	c         C  sé   |  j  j d d d d ƒ t j j d |  j d d d |  j d	 t j ƒ  t	 j
 d
 d ƒ d d ƒ t j j d |  j d d d |  j d	 t j ƒ  t	 j
 d
 d ƒ d d ƒ |  j  j t d ƒ ƒ } |  j | j d ƒ |  j d | j ƒ d S(   u6   
        Test that the view shows your tokens
        R&   u   bar_userR'   u   123456R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeu
   0123456789u%   oauth2_provider:authorized-token-listiÈ   s#   There are no authorized tokens yet.N(   R   R(   R   R   R2   R   R   R   R3   R4   R5   R    R   R!   R"   R6   R*   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   test_list_view_two_tokensL   s    		c         C  s¢   |  j  j d d d d ƒ t j j d |  j d d d |  j d	 t j ƒ  t	 j
 d
 d ƒ d d ƒ |  j  j t d ƒ ƒ } |  j | j d ƒ |  j d | j ƒ d S(   uL   
        Test that only currently logged-in user's tokens are shown
        R&   u   bar_userR'   u   123456R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeu%   oauth2_provider:authorized-token-listiÈ   s#   There are no authorized tokens yet.N(   R   R(   R   R   R2   R   R   R   R3   R4   R5   R    R   R!   R"   R)   R*   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt'   test_list_view_shows_correct_user_token^   s    	(   R   R   R   R%   R+   R7   R8   R9   (    (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyR   &   s   		
		t   TestAuthorizedTokenDeleteViewc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u3   
    Tests for the Authorized Token DeleteView
    c         C  s¥   t  j j d |  j d d d |  j d t j ƒ  t j d d ƒ d d	 ƒ |  _	 |  j
 j t d
 d i |  j	 j d 6ƒƒ } |  j | j d ƒ |  j d | d k ƒ d S(   uV   
        Test that the view redirects to login page if user is not logged-in.
        R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeu'   oauth2_provider:authorized-token-deletet   kwargsu   pki.  u   /accounts/login/?next=u   LocationN(   R   R   R2   R   R   R   R3   R4   R5   R,   R   R    R   t   pkR!   R"   R#   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt'   test_delete_view_authorization_requiredq   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 ƒ |  j
 j t d d i |  j	 j d 6ƒƒ } |  j | j d ƒ d S(   uf   
        Test that a GET on this view returns 200 if the token belongs to the logged-in user.
        R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeR&   u   foo_userR'   u   123456u'   oauth2_provider:authorized-token-deleteR;   u   pkiÈ   N(   R   R   R2   R   R   R   R3   R4   R5   R,   R   R(   R    R   R<   R!   R"   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   test_delete_view_works~   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 ƒ |  j
 j t d d i |  j	 j d 6ƒƒ } |  j | j d ƒ d S(   uf   
        Test that a 404 is returned when trying to GET this view with someone else's tokens.
        R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeR&   u   bar_userR'   u   123456u'   oauth2_provider:authorized-token-deleteR;   u   pki”  N(   R   R   R2   R   R   R   R3   R4   R5   R,   R   R(   R    R   R<   R!   R"   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt&   test_delete_view_token_belongs_to_user‹   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 ƒ |  j
 j t d d i |  j	 j d 6ƒƒ } |  j t  j j ƒ  ƒ |  j | t d ƒ ƒ d S(   ua   
        Test that a POST on this view works if the token belongs to the logged-in user.
        R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeR&   u   foo_userR'   u   123456u'   oauth2_provider:authorized-token-deleteR;   u   pku%   oauth2_provider:authorized-token-listN(   R   R   R2   R   R   R   R3   R4   R5   R,   R   R(   t   postR   R<   t   assertFalset   existst   assertRedirects(   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt&   test_delete_view_post_actually_deletes˜   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 ƒ |  j
 j t d d i |  j	 j d 6ƒƒ } |  j t  j j ƒ  ƒ |  j | j d ƒ d S(   uj   
        Test that a 404 is returned when trying to POST on this view with someone else's tokens.
        R
   R,   u
   1234567890R   R-   R.   i   R/   u
   read writeR&   u   bar_userR'   u   123456u'   oauth2_provider:authorized-token-deleteR;   u   pki”  N(   R   R   R2   R   R   R   R3   R4   R5   R,   R   R(   R@   R   R<   R#   RB   R!   R"   (   R   R$   (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt,   test_delete_view_only_deletes_user_own_token¦   s    	+(   R   R   R   R=   R>   R?   RD   RE   (    (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyR:   m   s   				(   t
   __future__R    R4   t   django.core.urlresolversR   t   django.testR   t   django.utilsR   t   modelsR   R   t   compatR   R   R   R   R   R:   (    (    (    sG   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/tests/test_token_view.pyt   <module>   s   		G