<s> from __future__ import unicode_literals <EOL> import json <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase , RequestFactory <EOL> from . test_utils import TestCaseUtils <EOL> from . . compat import urlparse , parse_qs , urlencode <EOL> from . . models import get_application_model , Grant , AccessToken <EOL> from . . settings import oauth2_settings <EOL> from . . views import ScopedProtectedResourceView , ReadWriteScopedResourceView <EOL> Application = get_application_model ( ) <EOL> UserModel = get_user_model ( ) <EOL> class ScopeResourceView ( ScopedProtectedResourceView ) : <EOL> required_scopes = [ '<STR_LIT>' ] <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return "<STR_LIT>" <EOL> class MultiScopeResourceView ( ScopedProtectedResourceView ) : <EOL> required_scopes = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return "<STR_LIT>" <EOL> class ReadWriteResourceView ( ReadWriteScopedResourceView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return "<STR_LIT>" <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return "<STR_LIT>" <EOL> class BaseTest ( TestCaseUtils , 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_CONFIDENTIAL , <EOL> authorization_grant_type = Application . GRANT_AUTHORIZATION_CODE , <EOL> ) <EOL> self . application . save ( ) <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> oauth2_settings . READ_SCOPE = '<STR_LIT>' <EOL> oauth2_settings . WRITE_SCOPE = '<STR_LIT>' <EOL> def tearDown ( self ) : <EOL> self . application . delete ( ) <EOL> self . test_user . delete ( ) <EOL> self . dev_user . delete ( ) <EOL> class TestScopesQueryParameterBackwardsCompatibility ( BaseTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestScopesQueryParameterBackwardsCompatibility , self ) . setUp ( ) <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> oauth2_settings . _DEFAULT_SCOPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_scopes_query_parameter_is_supported_on_post ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> grant = Grant . objects . get ( code = authorization_code ) <EOL> self . assertEqual ( grant . scope , "<STR_LIT>" ) <EOL> def test_scopes_query_parameter_is_supported_on_get ( 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:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <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> class TestScopesSave ( BaseTest ) : <EOL> def test_scopes_saved_in_grant ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> grant = Grant . objects . get ( code = authorization_code ) <EOL> self . assertEqual ( grant . scope , "<STR_LIT>" ) <EOL> def test_scopes_save_in_access_token ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> access_token = content [ '<STR_LIT>' ] <EOL> at = AccessToken . objects . get ( token = access_token ) <EOL> self . assertEqual ( at . scope , "<STR_LIT>" ) <EOL> class TestScopesProtection ( BaseTest ) : <EOL> def test_scopes_protection_valid ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> access_token = content [ '<STR_LIT>' ] <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 = ScopeResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response , "<STR_LIT>" ) <EOL> def test_scopes_protection_fail ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> access_token = content [ '<STR_LIT>' ] <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 = ScopeResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_multi_scope_fail ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> access_token = content [ '<STR_LIT>' ] <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 = MultiScopeResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_multi_scope_valid ( self ) : <EOL> """<STR_LIT>""" <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:code>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> access_token = content [ '<STR_LIT>' ] <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 = MultiScopeResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response , "<STR_LIT>" ) <EOL> class TestReadWriteScope ( BaseTest ) : <EOL> def get_access_token ( self , scopes ) : <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>' : scopes , <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 = authcode_data ) <EOL> query_dict = parse_qs ( urlparse ( response [ '<STR_LIT>' ] ) . query ) <EOL> authorization_code = query_dict [ '<STR_LIT:code>' ] . pop ( ) <EOL> token_request_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:code>' : authorization_code , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> auth_headers = self . get_basic_auth_header ( self . application . client_id , self . application . client_secret ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , data = token_request_data , ** auth_headers ) <EOL> content = json . loads ( response . content . decode ( "<STR_LIT:utf-8>" ) ) <EOL> return content [ '<STR_LIT>' ] <EOL> def test_improperly_configured ( self ) : <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' ] <EOL> request = self . factory . get ( "<STR_LIT>" ) <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> self . assertRaises ( ImproperlyConfigured , view , request ) <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> oauth2_settings . READ_SCOPE = '<STR_LIT>' <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> self . assertRaises ( ImproperlyConfigured , view , request ) <EOL> def test_properly_configured ( self ) : <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' ] <EOL> request = self . factory . get ( "<STR_LIT>" ) <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> self . assertRaises ( ImproperlyConfigured , view , request ) <EOL> oauth2_settings . _SCOPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> oauth2_settings . READ_SCOPE = '<STR_LIT>' <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> self . assertRaises ( ImproperlyConfigured , view , request ) <EOL> def test_has_read_scope ( self ) : <EOL> access_token = self . get_access_token ( '<STR_LIT>' ) <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 = ReadWriteResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response , "<STR_LIT>" ) <EOL> def test_no_read_scope ( self ) : <EOL> access_token = self . get_access_token ( '<STR_LIT>' ) <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 = ReadWriteResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_has_write_scope ( self ) : <EOL> access_token = self . get_access_token ( '<STR_LIT>' ) <EOL> auth_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' + access_token , <EOL> } <EOL> request = self . factory . post ( "<STR_LIT>" , ** auth_headers ) <EOL> request . user = self . test_user <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response , "<STR_LIT>" ) <EOL> def test_no_write_scope ( self ) : <EOL> access_token = self . get_access_token ( '<STR_LIT>' ) <EOL> auth_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' + access_token , <EOL> } <EOL> request = self . factory . post ( "<STR_LIT>" , ** auth_headers ) <EOL> request . user = self . test_user <EOL> view = ReadWriteResourceView . as_view ( ) <EOL> response = view ( request ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) </s>
