Skip to content

Commit 508208b

Browse files
committed
Refactor failing tests
1 parent 32cd377 commit 508208b

File tree

7 files changed

+91
-34
lines changed

7 files changed

+91
-34
lines changed

msgraphcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from msgraphcore.client_factory import HTTPClientFactory
12
from msgraphcore.constants import SDK_VERSION
23
from msgraphcore.graph_client import GraphClient
3-
from msgraphcore.client_factory import HTTPClientFactory
44

55
__version__ = SDK_VERSION

msgraphcore/graph_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from msgraphcore.middleware.request_context import RequestContext
21
from typing import List, Optional
32

43
from requests import Request, Session
54

65
from msgraphcore.client_factory import HTTPClientFactory
76
from msgraphcore.middleware.abc_token_credential import TokenCredential
87
from msgraphcore.middleware.middleware import BaseMiddleware
8+
from msgraphcore.middleware.request_context import RequestContext
99

1010
supported_options = ['scopes', 'custom_option']
1111

msgraphcore/middleware/middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def add_middleware(self, middleware):
2525
self._middleware = middleware
2626

2727
def send(self, request, **kwargs):
28-
28+
2929
if not hasattr(request, 'context'):
3030
headers = request.headers
3131
request.context = RequestContext(dict(), headers)
32-
32+
3333
if self._middleware_present():
3434
return self._middleware.send(request, **kwargs)
3535
# No middleware in pipeline, call superclass' send

tests/integration/test_graphclient.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_graph_client_with_custom_settings():
4949
assert response.status_code == 200
5050

5151

52-
def test_client_factory_with_custom_middleware():
52+
def test_graph_client_with_custom_middleware():
5353
"""
5454
Test client factory works with user provided middleware
5555
"""
@@ -62,3 +62,34 @@ def test_client_factory_with_custom_middleware():
6262
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
6363
)
6464
assert response.status_code == 200
65+
66+
67+
def test_graph_client_adds_context_to_request():
68+
"""
69+
Test the graph client adds a context object to a request
70+
"""
71+
credential = _CustomTokenCredential()
72+
scopes = ['User.Read.All']
73+
client = GraphClient(credential=credential)
74+
response = client.get(
75+
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me',
76+
scopes=scopes
77+
)
78+
assert response.status_code == 200
79+
assert hasattr(response.request, 'context')
80+
81+
82+
def test_graph_client_picks_options_from_kwargs():
83+
"""
84+
Test the graph client picks middleware options from kwargs and sets them in the context
85+
"""
86+
credential = _CustomTokenCredential()
87+
scopes = ['User.Read.All']
88+
client = GraphClient(credential=credential)
89+
response = client.get(
90+
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me',
91+
scopes=scopes
92+
)
93+
assert response.status_code == 200
94+
assert 'scopes' in response.request.context.middleware_control.keys()
95+
assert response.request.context.middleware_control['scopes'] == scopes

tests/integration/test_http_client_factory.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,36 @@ def test_client_factory_with_custom_middleware():
6464
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
6565
)
6666
assert response.status_code == 200
67+
68+
69+
def test_context_object_is_attached_to_requests_from_client_factory():
70+
"""
71+
Test that requests from a native HTTP client have a context object attached
72+
"""
73+
credential = _CustomTokenCredential()
74+
middleware = [
75+
AuthorizationHandler(credential),
76+
]
77+
client = HTTPClientFactory().create_with_custom_middleware(middleware)
78+
response = client.get(
79+
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
80+
)
81+
assert response.status_code == 200
82+
assert hasattr(response.request, 'context')
83+
84+
85+
def test_middleware_control_is_empty_for_requests_from_client_factory():
86+
"""
87+
Test that requests from a native HTTP client have no middlware options in the middleware
88+
control
89+
"""
90+
credential = _CustomTokenCredential()
91+
middleware = [
92+
AuthorizationHandler(credential),
93+
]
94+
client = HTTPClientFactory().create_with_custom_middleware(middleware)
95+
response = client.get(
96+
'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
97+
)
98+
assert response.status_code == 200
99+
assert response.request.context.middleware_control == {}

tests/unit/test_auth_handler.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import unittest
1+
import pytest
22

3-
from msgraphcore.constants import AUTH_MIDDLEWARE_OPTIONS
43
from msgraphcore.middleware.authorization import AuthorizationHandler
5-
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions
6-
from msgraphcore.middleware.options.middleware_control import middleware_control
4+
from msgraphcore.middleware.request_context import RequestContext
75

86

9-
class TestAuthorizationHandler(unittest.TestCase):
10-
def test_auth_options_override_default_scopes(self):
11-
auth_option = ['email.read']
12-
default_scopes = ['.default']
7+
def test_context_options_override_default_scopes():
8+
""" Test scopes found in the request context override default scopes"""
9+
default_scopes = ['.default']
10+
middleware_control = {
11+
'scopes': ['email.read'],
12+
}
13+
request_context = RequestContext(middleware_control, {})
1314

14-
middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option))
15-
auth_handler = AuthorizationHandler(None, scopes=default_scopes)
15+
auth_handler = AuthorizationHandler(None, scopes=default_scopes)
1616

17-
auth_handler_scopes = auth_handler.get_scopes()
18-
self.assertEqual(auth_option, auth_handler_scopes)
17+
auth_handler_scopes = auth_handler.get_scopes(request_context)
18+
assert auth_handler_scopes == middleware_control['scopes']
1919

20-
def test_auth_handler_get_scopes_does_not_overwrite_default_scopes(self):
21-
auth_option = ['email.read']
22-
default_scopes = ['.default']
2320

24-
middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option))
25-
auth_handler = AuthorizationHandler(None, scopes=default_scopes)
26-
auth_handler.get_scopes()
21+
def test_auth_handler_get_scopes_does_not_overwrite_default_scopes():
22+
default_scopes = ['.default']
23+
middleware_control = {
24+
'scopes': ['email.read'],
25+
}
26+
request_context = RequestContext(middleware_control, {})
27+
auth_handler = AuthorizationHandler(None, scopes=default_scopes)
2728

28-
self.assertEqual(auth_handler.scopes, default_scopes)
29+
auth_handler_scopes = auth_handler.get_scopes(request_context)
30+
31+
assert auth_handler.scopes == default_scopes

tests/unit/test_middleware_options.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)