<s> from __future__ import absolute_import , unicode_literals <EOL> from ... unittest import TestCase <EOL> import json <EOL> import jwt <EOL> import mock <EOL> from oauthlib import common <EOL> from oauthlib . oauth2 . rfc6749 . endpoints import Server <EOL> from oauthlib . oauth2 . rfc6749 . endpoints . authorization import AuthorizationEndpoint <EOL> from oauthlib . oauth2 . rfc6749 . endpoints . token import TokenEndpoint <EOL> from oauthlib . oauth2 . rfc6749 . endpoints . resource import ResourceEndpoint <EOL> from oauthlib . oauth2 . rfc6749 . grant_types import AuthorizationCodeGrant <EOL> from oauthlib . oauth2 . rfc6749 . grant_types import ImplicitGrant <EOL> from oauthlib . oauth2 . rfc6749 . grant_types import ResourceOwnerPasswordCredentialsGrant <EOL> from oauthlib . oauth2 . rfc6749 . grant_types import ClientCredentialsGrant <EOL> from oauthlib . oauth2 . rfc6749 import tokens , errors <EOL> class AuthorizationEndpointTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_validator = mock . MagicMock ( ) <EOL> self . addCleanup ( setattr , self , '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> auth_code = AuthorizationCodeGrant ( <EOL> request_validator = self . mock_validator ) <EOL> auth_code . save_authorization_code = mock . MagicMock ( ) <EOL> implicit = ImplicitGrant ( <EOL> request_validator = self . mock_validator ) <EOL> implicit . save_token = mock . MagicMock ( ) <EOL> response_types = { <EOL> '<STR_LIT:code>' : auth_code , <EOL> '<STR_LIT>' : implicit , <EOL> } <EOL> self . expires_in = <NUM_LIT> <EOL> token = tokens . BearerToken ( self . mock_validator , <EOL> expires_in = self . expires_in ) <EOL> self . endpoint = AuthorizationEndpoint ( <EOL> default_response_type = '<STR_LIT:code>' , <EOL> default_token_type = token , <EOL> response_types = response_types ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_authorization_grant ( self ) : <EOL> uri = '<STR_LIT>' <EOL> uri += '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_authorization_response ( <EOL> uri , scopes = [ '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , headers ) <EOL> self . assertURLEqual ( headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_implicit_grant ( self ) : <EOL> uri = '<STR_LIT>' <EOL> uri += '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_authorization_response ( <EOL> uri , scopes = [ '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , headers ) <EOL> self . assertURLEqual ( headers [ '<STR_LIT>' ] , '<STR_LIT>' + str ( self . expires_in ) + '<STR_LIT>' , parse_fragment = True ) <EOL> def test_missing_type ( self ) : <EOL> uri = '<STR_LIT>' <EOL> uri += '<STR_LIT>' <EOL> self . mock_validator . validate_request = mock . MagicMock ( <EOL> side_effect = errors . InvalidRequestError ( ) ) <EOL> headers , body , status_code = self . endpoint . create_authorization_response ( <EOL> uri , scopes = [ '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , headers ) <EOL> self . assertURLEqual ( headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_invalid_type ( self ) : <EOL> uri = '<STR_LIT>' <EOL> uri += '<STR_LIT>' <EOL> self . mock_validator . validate_request = mock . MagicMock ( <EOL> side_effect = errors . UnsupportedResponseTypeError ( ) ) <EOL> headers , body , status_code = self . endpoint . create_authorization_response ( <EOL> uri , scopes = [ '<STR_LIT:all>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , headers ) <EOL> self . assertURLEqual ( headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> class TokenEndpointTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> def set_user ( request ) : <EOL> request . user = mock . MagicMock ( ) <EOL> request . client = mock . MagicMock ( ) <EOL> request . client . client_id = '<STR_LIT>' <EOL> return True <EOL> self . mock_validator = mock . MagicMock ( ) <EOL> self . mock_validator . authenticate_client . side_effect = set_user <EOL> self . addCleanup ( setattr , self , '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> auth_code = AuthorizationCodeGrant ( <EOL> request_validator = self . mock_validator ) <EOL> password = ResourceOwnerPasswordCredentialsGrant ( <EOL> request_validator = self . mock_validator ) <EOL> client = ClientCredentialsGrant ( <EOL> request_validator = self . mock_validator ) <EOL> supported_types = { <EOL> '<STR_LIT>' : auth_code , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT>' : client , <EOL> } <EOL> self . expires_in = <NUM_LIT> <EOL> token = tokens . BearerToken ( self . mock_validator , <EOL> expires_in = self . expires_in ) <EOL> self . endpoint = TokenEndpoint ( '<STR_LIT>' , <EOL> default_token_type = token , grant_types = supported_types ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_authorization_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_password_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_client_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> def test_missing_type ( self ) : <EOL> _ , body , _ = self . endpoint . create_token_response ( '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> token = { '<STR_LIT:error>' : '<STR_LIT>' } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> def test_invalid_type ( self ) : <EOL> body = '<STR_LIT>' <EOL> _ , body , _ = self . endpoint . create_token_response ( '<STR_LIT>' , body = body ) <EOL> token = { '<STR_LIT:error>' : '<STR_LIT>' } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> class SignedTokenEndpointTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . expires_in = <NUM_LIT> <EOL> def set_user ( request ) : <EOL> request . user = mock . MagicMock ( ) <EOL> request . client = mock . MagicMock ( ) <EOL> request . client . client_id = '<STR_LIT>' <EOL> return True <EOL> self . mock_validator = mock . MagicMock ( ) <EOL> self . mock_validator . authenticate_client . side_effect = set_user <EOL> self . addCleanup ( setattr , self , '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> self . private_pem = """<STR_LIT>""" <EOL> self . public_pem = """<STR_LIT>""" <EOL> signed_token = tokens . signed_token_generator ( self . private_pem , <EOL> user_id = <NUM_LIT> ) <EOL> self . endpoint = Server ( <EOL> self . mock_validator , <EOL> token_expires_in = self . expires_in , <EOL> token_generator = signed_token , <EOL> refresh_token_generator = tokens . random_token_generator <EOL> ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_authorization_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> body = json . loads ( body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : body [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( body , token ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_password_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> body = json . loads ( body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : body [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( body , token ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_scopes_and_user_id_stored_in_access_token ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> access_token = json . loads ( body ) [ '<STR_LIT>' ] <EOL> claims = common . verify_signed_token ( self . public_pem , access_token ) <EOL> self . assertEqual ( claims [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( claims [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> @ mock . patch ( '<STR_LIT>' , new = lambda : '<STR_LIT:abc>' ) <EOL> def test_client_grant ( self ) : <EOL> body = '<STR_LIT>' <EOL> headers , body , status_code = self . endpoint . create_token_response ( <EOL> '<STR_LIT>' , body = body ) <EOL> body = json . loads ( body ) <EOL> token = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . expires_in , <EOL> '<STR_LIT>' : body [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( body , token ) <EOL> def test_missing_type ( self ) : <EOL> _ , body , _ = self . endpoint . create_token_response ( '<STR_LIT>' , body = '<STR_LIT>' ) <EOL> token = { '<STR_LIT:error>' : '<STR_LIT>' } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> def test_invalid_type ( self ) : <EOL> body = '<STR_LIT>' <EOL> _ , body , _ = self . endpoint . create_token_response ( '<STR_LIT>' , body = body ) <EOL> token = { '<STR_LIT:error>' : '<STR_LIT>' } <EOL> self . assertEqual ( json . loads ( body ) , token ) <EOL> class ResourceEndpointTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_validator = mock . MagicMock ( ) <EOL> self . addCleanup ( setattr , self , '<STR_LIT>' , mock . MagicMock ( ) ) <EOL> token = tokens . BearerToken ( request_validator = self . mock_validator ) <EOL> self . endpoint = ResourceEndpoint ( default_token = '<STR_LIT>' , <EOL> token_types = { '<STR_LIT>' : token } ) <EOL> def test_defaults ( self ) : <EOL> uri = '<STR_LIT>' <EOL> self . mock_validator . validate_bearer_token . return_value = False <EOL> valid , request = self . endpoint . verify_request ( uri ) <EOL> self . assertFalse ( valid ) <EOL> self . assertEqual ( request . token_type , '<STR_LIT>' ) </s>
