def test_auth_handler_get_scopes_does_not_overwrite_default_scopes(): default_scopes = ['.default'] middleware_control = { 'scopes': ['email.read'], } request_context = RequestContext(middleware_control, {}) auth_handler = AuthorizationHandler(None, scopes=default_scopes) auth_handler_scopes = auth_handler.get_scopes(request_context) assert auth_handler.scopes == default_scopes
def test_context_options_override_default_scopes(): """ Test scopes found in the request context override default scopes""" default_scopes = ['.default'] middleware_control = { 'scopes': ['email.read'], } request_context = RequestContext(middleware_control, {}) auth_handler = AuthorizationHandler(None, scopes=default_scopes) auth_handler_scopes = auth_handler.get_scopes(request_context) assert auth_handler_scopes == middleware_control['scopes']
def test_register_middleware(): credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = HTTPClientFactory() client._register(middleware) assert isinstance(client.session.get_adapter('https://'), HTTPAdapter)
def test_create_with_custom_middleware(): """Test creation of HTTP Clients with custom middleware""" credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = HTTPClientFactory().create_with_custom_middleware( middleware=middleware) custom_middleware = client.get_adapter('https://') assert isinstance(custom_middleware, HTTPAdapter)
def test_graph_client_with_custom_middleware(): """ Test client factory works with user provided middleware """ credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = GraphClient(middleware=middleware) response = client.get( 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me' ) assert response.status_code == 200
def __init__(self, credential: TokenCredential, scopes: [str] = ['.default'], middleware: list = []): super().__init__() self._append_sdk_version() self._base_url = BASE_URL auth_handler = AuthorizationHandler(credential, scopes) # The authorization handler should be the first middleware in the pipeline. middleware.insert(0, auth_handler) self._register(middleware)
def test_graph_client_with_custom_middleware(): """ Test creating a graph client with custom middleware works as expected """ credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = GraphClient(middleware=middleware) assert isinstance(client.graph_session, Session) assert isinstance(client.graph_session.get_adapter('https://'), HTTPAdapter) assert client.graph_session.base_url == NationalClouds.Global + '/' + APIVersion.v1
def test_context_object_is_attached_to_requests_from_client_factory(): """ Test that requests from a native HTTP client have a context object attached """ credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = HTTPClientFactory().create_with_custom_middleware(middleware) response = client.get( 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me' ) assert response.status_code == 200 assert hasattr(response.request, 'context')
def test_middleware_control_is_empty_for_requests_from_client_factory(): """ Test that requests from a native HTTP client have no middlware options in the middleware control """ credential = _CustomTokenCredential() middleware = [ AuthorizationHandler(credential), ] client = HTTPClientFactory().create_with_custom_middleware(middleware) response = client.get( 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me' ) assert response.status_code == 200 assert response.request.context.middleware_control == {}