forked from microsoftgraph/msgraph-sdk-python-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_graphclient.py
More file actions
91 lines (75 loc) · 3.24 KB
/
test_graphclient.py
File metadata and controls
91 lines (75 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import pytest
from azure.identity import EnvironmentCredential
from requests import Session
from msgraph.core import APIVersion, GraphClient
from msgraph.core.middleware.authorization import AuthorizationHandler
def test_graph_client_with_default_middleware():
"""
Test that a graph client uses default middleware if none are provided
"""
# credential = _CustomTokenCredential()
client = GraphClient(credential=EnvironmentCredential())
response = client.get('https://graph.microsoft.com/v1.0/users')
assert response.status_code == 200
def test_graph_client_with_user_provided_session():
"""
Test that the graph client works with a user provided session object
"""
session = Session()
client = GraphClient(session=session, credential=EnvironmentCredential())
response = client.get('https://graph.microsoft.com/v1.0/users', )
assert response.status_code == 200
def test_graph_client_with_custom_settings():
"""
Test that the graph client works with user provided configuration
"""
credential = EnvironmentCredential()
client = GraphClient(api_version=APIVersion.beta, credential=credential)
response = client.get('https://graph.microsoft.com/v1.0/users', )
assert response.status_code == 200
def test_graph_client_with_custom_middleware():
"""
Test client factory works with user provided middleware
"""
credential = EnvironmentCredential()
middleware = [
AuthorizationHandler(credential),
]
client = GraphClient(middleware=middleware)
response = client.get('https://graph.microsoft.com/v1.0/users', )
assert response.status_code == 200
def test_graph_client_adds_context_to_request():
"""
Test the graph client adds a context object to a request
"""
credential = EnvironmentCredential()
scopes = ['https://graph.microsoft.com/.default']
client = GraphClient(credential=credential)
response = client.get('https://graph.microsoft.com/v1.0/users', scopes=scopes)
assert response.status_code == 200
assert hasattr(response.request, 'context')
def test_graph_client_picks_options_from_kwargs():
"""
Test the graph client picks middleware options from kwargs and sets them in the context
"""
credential = EnvironmentCredential()
scopes = ['https://graph.microsoft.com/.default']
client = GraphClient(credential=credential)
response = client.get('https://graph.microsoft.com/v1.0/users', scopes=scopes)
assert response.status_code == 200
assert 'scopes' in response.request.context.middleware_control.keys()
assert response.request.context.middleware_control['scopes'] == scopes
def test_graph_client_allows_passing_optional_kwargs():
"""
Test the graph client allows passing optional kwargs native to the requests library
such as stream, proxy and cert.
"""
credential = EnvironmentCredential()
scopes = ['https://graph.microsoft.com/.default']
client = GraphClient(credential=credential)
response = client.get('https://graph.microsoft.com/v1.0/users', scopes=scopes, stream=True)
assert response.status_code == 200