|
| 1 | +import pytest |
| 2 | +import responses |
| 3 | +from requests import Session |
| 4 | +from requests.adapters import HTTPAdapter |
| 5 | + |
| 6 | +from msgraphcore.constants import CONNECTION_TIMEOUT, REQUEST_TIMEOUT |
| 7 | +from msgraphcore.enums import APIVersion, NationalClouds |
| 8 | +from msgraphcore.graph_client import GraphClient |
| 9 | +from msgraphcore.middleware.authorization import AuthorizationHandler |
| 10 | +from msgraphcore.middleware.middleware import BaseMiddleware, MiddlewarePipeline |
| 11 | + |
| 12 | + |
| 13 | +def test_graph_client_with_default_middleware(): |
| 14 | + """ |
| 15 | + Test creating a graph client with default middleware works as expected |
| 16 | + """ |
| 17 | + credential = _CustomTokenCredential() |
| 18 | + client = GraphClient(credential=credential) |
| 19 | + |
| 20 | + assert isinstance(client.graph_session, Session) |
| 21 | + assert isinstance(client.graph_session.get_adapter('https://'), HTTPAdapter) |
| 22 | + assert client.graph_session.base_url == NationalClouds.Global + '/' + APIVersion.v1 |
| 23 | + |
| 24 | + |
| 25 | +def test_graph_client_with_custom_middleware(): |
| 26 | + """ |
| 27 | + Test creating a graph client with custom middleware works as expected |
| 28 | + """ |
| 29 | + credential = _CustomTokenCredential() |
| 30 | + middleware = [ |
| 31 | + AuthorizationHandler(credential), |
| 32 | + ] |
| 33 | + client = GraphClient(middleware=middleware) |
| 34 | + |
| 35 | + assert isinstance(client.graph_session, Session) |
| 36 | + assert isinstance(client.graph_session.get_adapter('https://'), HTTPAdapter) |
| 37 | + assert client.graph_session.base_url == NationalClouds.Global + '/' + APIVersion.v1 |
| 38 | + |
| 39 | + |
| 40 | +def test_graph_client_with_custom_configuration(): |
| 41 | + """ |
| 42 | + Test creating a graph client with custom middleware works as expected |
| 43 | + """ |
| 44 | + credential = _CustomTokenCredential() |
| 45 | + client = GraphClient( |
| 46 | + credential=credential, api_version=APIVersion.beta, cloud=NationalClouds.China |
| 47 | + ) |
| 48 | + |
| 49 | + assert client.graph_session.base_url == NationalClouds.China + '/' + APIVersion.beta |
| 50 | + |
| 51 | + |
| 52 | +def test_graph_client_uses_same_session(): |
| 53 | + """ |
| 54 | + Test graph client is a singleton class and uses the same session |
| 55 | + """ |
| 56 | + credential = _CustomTokenCredential() |
| 57 | + client = GraphClient(credential=credential) |
| 58 | + |
| 59 | + client2 = GraphClient(credential=credential) |
| 60 | + assert client is client2 |
| 61 | + |
| 62 | + |
| 63 | +@responses.activate |
| 64 | +def test_graph_client_builds_graph_urls(): |
| 65 | + """ |
| 66 | + Test that the graph client builds full urls if supplied with partial |
| 67 | + """ |
| 68 | + credential = _CustomTokenCredential() |
| 69 | + client = GraphClient(credential=credential) |
| 70 | + graph_url = client.graph_session.base_url + '/me' |
| 71 | + |
| 72 | + responses.add(responses.GET, graph_url, status=200) |
| 73 | + |
| 74 | + client.get('/me') |
| 75 | + assert graph_url == responses.calls[0].request.url |
| 76 | + |
| 77 | + |
| 78 | +@responses.activate |
| 79 | +def test_does_not_build_graph_urls_for_full_urls(): |
| 80 | + """ |
| 81 | + Test that the graph client builds full urls if supplied with partial |
| 82 | + """ |
| 83 | + other_url = 'https://microsoft.com/' |
| 84 | + responses.add(responses.GET, other_url, status=200) |
| 85 | + |
| 86 | + credential = _CustomTokenCredential() |
| 87 | + client = GraphClient(credential=credential) |
| 88 | + client.get(other_url) |
| 89 | + request_url = responses.calls[0].request.url |
| 90 | + assert other_url == request_url |
| 91 | + |
| 92 | + |
| 93 | +class _CustomTokenCredential: |
| 94 | + def get_token(self, scopes): |
| 95 | + return ['{token:https://graph.microsoft.com/}'] |
0 commit comments