|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | + |
| 7 | +import unittest |
| 8 | +from collections import namedtuple |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +from ..credential_adaptor import CredentialAdaptor |
| 12 | + |
| 13 | + |
| 14 | +MOCK_ACCESS_TOKEN = "mock_access_token" |
| 15 | +MOCK_DATA = {"test_data": "value1"} |
| 16 | +MOCK_CLAIMS = {"test_claims": "value2"} |
| 17 | + |
| 18 | +class MsalCredentialStub: |
| 19 | + |
| 20 | + def __init__(self, *args, **kwargs): |
| 21 | + self.acquire_token_scopes = None |
| 22 | + self.acquire_token_kwargs = None |
| 23 | + super().__init__() |
| 24 | + |
| 25 | + def acquire_token(self, scopes, **kwargs): |
| 26 | + self.acquire_token_scopes = scopes |
| 27 | + self.acquire_token_kwargs = kwargs |
| 28 | + return { |
| 29 | + 'access_token': MOCK_ACCESS_TOKEN, |
| 30 | + 'token_type': 'Bearer', |
| 31 | + 'expires_in': 1800, |
| 32 | + 'token_source': 'cache' |
| 33 | + } |
| 34 | + |
| 35 | +def _now_timestamp_mock(): |
| 36 | + # 2021-09-06 08:55:23 |
| 37 | + return 1630918523 |
| 38 | + |
| 39 | + |
| 40 | +class TestCredentialAdaptor(unittest.TestCase): |
| 41 | + |
| 42 | + @mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock) |
| 43 | + def test_get_token(self): |
| 44 | + msal_cred = MsalCredentialStub() |
| 45 | + sdk_cred = CredentialAdaptor(msal_cred) |
| 46 | + access_token = sdk_cred.get_token('https://management.core.windows.net//.default') |
| 47 | + assert msal_cred.acquire_token_scopes == ['https://management.core.windows.net//.default'] |
| 48 | + |
| 49 | + from ..util import AccessToken |
| 50 | + assert isinstance(access_token, AccessToken) |
| 51 | + assert access_token.token == MOCK_ACCESS_TOKEN |
| 52 | + assert access_token.expires_on == 1630920323 |
| 53 | + |
| 54 | + sdk_cred.get_token('https://management.core.windows.net//.default', data=MOCK_DATA) |
| 55 | + assert msal_cred.acquire_token_kwargs['data'] == MOCK_DATA |
| 56 | + |
| 57 | + sdk_cred.get_token('https://management.core.windows.net//.default', claims=MOCK_CLAIMS) |
| 58 | + assert msal_cred.acquire_token_kwargs['claims'] == MOCK_CLAIMS |
| 59 | + |
| 60 | + |
| 61 | + @mock.patch('azure.cli.core.auth.util._now_timestamp', new=_now_timestamp_mock) |
| 62 | + def test_get_token_info(self): |
| 63 | + msal_cred = MsalCredentialStub() |
| 64 | + sdk_cred = CredentialAdaptor(msal_cred) |
| 65 | + access_token_info = sdk_cred.get_token_info('https://management.core.windows.net//.default') |
| 66 | + |
| 67 | + from azure.core.credentials import AccessTokenInfo |
| 68 | + assert isinstance(access_token_info, AccessTokenInfo) |
| 69 | + assert access_token_info.token == MOCK_ACCESS_TOKEN |
| 70 | + assert access_token_info.expires_on == 1630920323 |
| 71 | + assert access_token_info.token_type == 'Bearer' |
| 72 | + |
| 73 | + assert msal_cred.acquire_token_scopes == ['https://management.core.windows.net//.default'] |
| 74 | + |
| 75 | + # Actually, TokenRequestOptions doesn't support 'data'. |
| 76 | + sdk_cred.get_token_info('https://management.core.windows.net//.default', options={'data': MOCK_DATA}) |
| 77 | + assert msal_cred.acquire_token_kwargs['data'] == MOCK_DATA |
| 78 | + |
| 79 | + sdk_cred.get_token_info('https://management.core.windows.net//.default', options={'claims': MOCK_CLAIMS}) |
| 80 | + assert msal_cred.acquire_token_kwargs['claims'] == MOCK_CLAIMS |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + unittest.main() |
0 commit comments