<s> from __future__ import unicode_literals <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase , RequestFactory <EOL> from . . compat import urlparse , parse_qs , urlencode <EOL> from . . models import get_application_model <EOL> from . . settings import oauth2_settings <EOL> from . . views import ProtectedResourceView <EOL> Application = get_application_model ( ) <EOL> UserModel = get_user_model ( ) <EOL> class ResourceView ( ProtectedResourceView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return "<STR_LIT>" <EOL> class BaseTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . factory = RequestFactory ( ) <EOL> self . test_user = UserModel . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . dev_user = UserModel . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . application = Application ( <EOL> name = "<STR_LIT>" , <EOL> redirect_uris = "<STR_LIT>" , <EOL> user = self . dev_user , <EOL> client_type = Application . CLIENT_PUBLIC , <EOL> authorization_grant_type = Application . GRANT_IMPLICIT , <EOL> ) <EOL> self . application . save ( ) <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> oauth2_settings . _DEFAULT_SCOPES = [ '<STR_LIT>' ] <EOL> def tearDown ( self ) : <EOL> self . application . delete ( ) <EOL> self . test_user . delete ( ) <EOL> self . dev_user . delete ( ) <EOL> class TestImplicitAuthorizationCodeView ( BaseTest ) : <EOL> def test_pre_auth_valid_client_default_scopes ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> form = response . context [ "<STR_LIT>" ] <EOL> self . assertEqual ( form [ '<STR_LIT>' ] . value ( ) , '<STR_LIT>' ) <EOL> def test_pre_auth_valid_client ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( "<STR_LIT>" , response . context ) <EOL> form = response . context [ "<STR_LIT>" ] <EOL> self . assertEqual ( form [ '<STR_LIT>' ] . value ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( form [ '<STR_LIT:state>' ] . value ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( form [ '<STR_LIT>' ] . value ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( form [ '<STR_LIT>' ] . value ( ) , self . application . client_id ) <EOL> def test_pre_auth_invalid_client ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_pre_auth_default_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> form = response . context [ "<STR_LIT>" ] <EOL> self . assertEqual ( form [ '<STR_LIT>' ] . value ( ) , "<STR_LIT>" ) <EOL> def test_pre_auth_forbibben_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_post_auth_allow ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> form_data = { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = form_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> def test_skip_authorization_completely ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> self . application . skip_authorization = True <EOL> self . application . save ( ) <EOL> query_string = urlencode ( { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> url = "<STR_LIT>" . format ( url = reverse ( '<STR_LIT>' ) , qs = query_string ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , response [ '<STR_LIT>' ] ) <EOL> def test_token_post_auth_deny ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> form_data = { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = form_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( "<STR_LIT>" , response [ '<STR_LIT>' ] ) <EOL> def test_implicit_redirection_uri_with_querystring ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> form_data = { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = form_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( "<STR_LIT>" , response [ '<STR_LIT>' ] ) <EOL> self . assertIn ( "<STR_LIT>" , response [ '<STR_LIT>' ] ) <EOL> def test_implicit_fails_when_redirect_uri_path_is_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> form_data = { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = form_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> class TestImplicitTokenView ( BaseTest ) : <EOL> def test_resource_access_allowed ( self ) : <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> authcode_data = { <EOL> '<STR_LIT>' : self . application . client_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> frag_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . fragment ) <EOL> access_token = frag_dict [ '<STR_LIT>' ] . pop ( ) <EOL> auth_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' + access_token , <EOL> } <EOL> request = self . factory . get ( "<STR_LIT>" , ** auth_headers ) <EOL> request . user = self . test_user <EOL> view = ResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response , "<STR_LIT>" ) </s>
