|
18 | 18 | CLIENT_ID = 'dummy_client'
|
19 | 19 | CLIENT_SECRET = 'dummy_secret'
|
20 | 20 | APP_URL = 'https://login.microsoftonline.com/dummy_tenant/oauth2/v2.0/token'
|
| 21 | +AUTH_CODE = 'dummy_auth_code' |
| 22 | +REDIRECT_URI = 'https://localhost/myapp' |
21 | 23 | SCOPE = 'https://graph.microsoft.com/.default'
|
22 | 24 | RESOURCE = 'https://defender.windows.com/shtak'
|
23 | 25 | RESOURCES = ['https://resource1.com', 'https://resource2.com']
|
@@ -62,15 +64,17 @@ def oproxy_client_refresh():
|
62 | 64 | )
|
63 | 65 |
|
64 | 66 |
|
65 |
| -def self_deployed_client(): |
| 67 | +def self_deployed_client(grant_type=CLIENT_CREDENTIALS): |
66 | 68 | tenant_id = TENANT
|
67 | 69 | client_id = CLIENT_ID
|
68 | 70 | client_secret = CLIENT_SECRET
|
69 | 71 | base_url = BASE_URL
|
| 72 | + auth_code = AUTH_CODE if grant_type == AUTHORIZATION_CODE else '' |
70 | 73 | resource = RESOURCE
|
71 | 74 | ok_codes = OK_CODES
|
72 | 75 |
|
73 | 76 | return MicrosoftClient(self_deployed=True, tenant_id=tenant_id, auth_id=client_id, enc_key=client_secret,
|
| 77 | + grant_type=grant_type, auth_code=auth_code, |
74 | 78 | resource=resource, base_url=base_url, verify=True, proxy=False, ok_codes=ok_codes)
|
75 | 79 |
|
76 | 80 |
|
@@ -717,11 +721,57 @@ def test_generate_login_url():
|
717 | 721 | """
|
718 | 722 | from MicrosoftApiModule import generate_login_url
|
719 | 723 |
|
720 |
| - client = self_deployed_client() |
| 724 | + client = self_deployed_client(grant_type=AUTHORIZATION_CODE) |
721 | 725 |
|
722 | 726 | result = generate_login_url(client)
|
723 | 727 |
|
724 | 728 | expected_url = f'[login URL](https://login.microsoftonline.com/{TENANT}/oauth2/v2.0/authorize?' \
|
725 | 729 | f'response_type=code&scope=offline_access%20https://graph.microsoft.com/.default' \
|
726 | 730 | f'&client_id={CLIENT_ID}&redirect_uri=https://localhost/myapp)'
|
727 | 731 | assert expected_url in result.readable_output, "Login URL is incorrect"
|
| 732 | + |
| 733 | + |
| 734 | +def test_get_access_token_auth_code_reconfigured(mocker, requests_mock): |
| 735 | + """ |
| 736 | + Given: |
| 737 | + - The auth code was reconfigured |
| 738 | + When: |
| 739 | + - Calling function get_access_token |
| 740 | + Then: |
| 741 | + - Ensure the access token is as expected in the body of the request and in the integration context |
| 742 | + """ |
| 743 | + context = {'auth_code': AUTH_CODE, 'access_token': TOKEN, |
| 744 | + 'valid_until': 3605, 'current_refresh_token': REFRESH_TOKEN} |
| 745 | + |
| 746 | + mocker.patch.object(demisto, 'getIntegrationContext', return_value=context) |
| 747 | + mocker.patch.object(demisto, 'setIntegrationContext') |
| 748 | + |
| 749 | + tenant_id = TENANT |
| 750 | + client_id = CLIENT_ID |
| 751 | + client_secret = CLIENT_SECRET |
| 752 | + base_url = BASE_URL |
| 753 | + new_auth_code = 'reconfigured_auth_code' |
| 754 | + resource = None |
| 755 | + ok_codes = OK_CODES |
| 756 | + grant_type = AUTHORIZATION_CODE |
| 757 | + |
| 758 | + client = MicrosoftClient(self_deployed=True, tenant_id=tenant_id, auth_id=client_id, enc_key=client_secret, |
| 759 | + grant_type=grant_type, auth_code=new_auth_code, |
| 760 | + resource=resource, base_url=base_url, verify=True, proxy=False, ok_codes=ok_codes) |
| 761 | + |
| 762 | + requests_mock.post( |
| 763 | + APP_URL, |
| 764 | + json={'access_token': TOKEN, 'expires_in': '3600'}) |
| 765 | + |
| 766 | + body = { |
| 767 | + 'client_id': CLIENT_ID, |
| 768 | + 'client_secret': CLIENT_SECRET, |
| 769 | + 'redirect_uri': REDIRECT_URI, |
| 770 | + 'grant_type': AUTHORIZATION_CODE, |
| 771 | + 'code': new_auth_code, |
| 772 | + } |
| 773 | + |
| 774 | + assert client.get_access_token() |
| 775 | + req_body = requests_mock._adapter.last_request._request.body |
| 776 | + assert urllib.parse.urlencode(body) == req_body |
| 777 | + assert demisto.getIntegrationContext().get('auth_code') == new_auth_code |
0 commit comments