Skip to content

Commit 519bb08

Browse files
committed
Add AuthorizationHandler tests
1 parent 4bc0cb8 commit 519bb08

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

msgraphcore/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
REQUEST_TIMEOUT = 100
55
CONNECTION_TIMEOUT = 30
66
BASE_URL = 'https://graph.microsoft.com/v1.0'
7-
SDK_VERSION = '0.0.1-0'
7+
SDK_VERSION = '0.0.3'
88

99
# Used as the key for AuthMiddlewareOption in MiddlewareControl
1010
AUTH_MIDDLEWARE_OPTIONS = 'AUTH_MIDDLEWARE_OPTIONS'
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
from .abc_token_credential import TokenCredential
12
from ..constants import AUTH_MIDDLEWARE_OPTIONS
23
from .middleware import BaseMiddleware
34
from .options.middleware_control import middleware_control
45

56

67
class AuthorizationHandler(BaseMiddleware):
7-
def __init__(self, credential, scopes):
8+
def __init__(self, credential: TokenCredential, scopes: [str]):
89
super().__init__()
910
self.credential = credential
1011
self.scopes = scopes
1112
self.retry_count = 0
1213

1314
def send(self, request, **kwargs):
14-
self._update_scopes_from_middleware_options()
15-
1615
request.headers.update({'Authorization': 'Bearer {}'.format(self._get_access_token())})
1716
response = super().send(request, **kwargs)
1817

@@ -22,12 +21,14 @@ def send(self, request, **kwargs):
2221
return self.send(request, **kwargs)
2322
return response
2423

25-
def _update_scopes_from_middleware_options(self):
24+
def _get_access_token(self):
25+
return self.credential.get_token(*self.get_scopes())[0]
26+
27+
def get_scopes(self):
2628
# Checks if there are any options for this middleware
2729
auth_options_present = middleware_control.get(AUTH_MIDDLEWARE_OPTIONS)
2830
# If there is, get the scopes from the options
2931
if auth_options_present:
30-
self.scopes = auth_options_present.scopes
31-
32-
def _get_access_token(self):
33-
return self.credential.get_token(*self.scopes)[0]
32+
return auth_options_present.scopes
33+
else:
34+
return self.scopes

tests/unit/test_auth_handler.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
3+
from msgraphcore.constants import AUTH_MIDDLEWARE_OPTIONS
4+
from msgraphcore.middleware.authorization import AuthorizationHandler
5+
from msgraphcore.middleware.options.middleware_control import middleware_control
6+
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions
7+
8+
9+
class TestAuthorizationHandler(unittest.TestCase):
10+
def test_uses_scopes_from_auth_options(self):
11+
auth_option = ['email.read']
12+
default_scopes = ['.default']
13+
14+
middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option))
15+
auth_handler = AuthorizationHandler(None, default_scopes)
16+
17+
auth_handler_scopes = auth_handler.get_scopes()
18+
self.assertEqual(auth_option, auth_handler_scopes)
19+
20+
def test_auth_option_does_not_overwrite_default_scopes(self):
21+
auth_option = ['email.read']
22+
default_scopes = ['.default']
23+
24+
middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option))
25+
auth_handler = AuthorizationHandler(None, default_scopes)
26+
27+
self.assertEqual(auth_handler.scopes, default_scopes)
28+
29+

0 commit comments

Comments
 (0)