Skip to content

Commit

Permalink
Take on - Microsoft Graph API_Test contribution (#38376)
Browse files Browse the repository at this point in the history
* required yml fields to allow mapping

* added headers argument to msgraph-api-request

* RN

* extras

* yml

* readme

* pre commit

* rn

* changed close reason logic for false positive

* Update Packs/MicrosoftGraphAPI/Integrations/MicrosoftGraphAPI/MicrosoftGraphAPI.yml

Co-authored-by: ShirleyDenkberg <[email protected]>

* Apply suggestions from code review

Co-authored-by: ShirleyDenkberg <[email protected]>

* Update Packs/MicrosoftGraphAPI/Integrations/MicrosoftGraphAPI/README.md

Co-authored-by: ShirleyDenkberg <[email protected]>

* tests

* pre commit

---------

Co-authored-by: ShirleyDenkberg <[email protected]>
Co-authored-by: RotemAmit <[email protected]>
Co-authored-by: michal-dagan <[email protected]>
  • Loading branch information
4 people authored Feb 12, 2025
1 parent 4fff107 commit 4f7ada7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def __init__(self,
self.ms_client = MicrosoftClient(**client_args) # type: ignore[arg-type]

def generic_request(
self,
resource: str,
http_method: str = 'GET',
api_version: str = 'v1.0',
odata: str | None = None,
request_body: dict | None = None,
self,
resource: str,
http_method: str = 'GET',
api_version: str = 'v1.0',
odata: str | None = None,
request_body: dict | None = None,
headers: dict | None = None
):
url_suffix = urljoin(api_version, resource)
if odata:
Expand All @@ -68,6 +69,7 @@ def generic_request(
url_suffix=url_suffix,
json_data=request_body,
resp_type='resp',
headers=headers
)
return res.json() if res.content else None

Expand Down Expand Up @@ -107,6 +109,8 @@ def generic_command(client: MsGraphClient, args: dict[str, Any]) -> CommandResul
request_body = json.loads(request_body)
except json.decoder.JSONDecodeError as e:
raise ValueError(f'Invalid request body - {str(e)}')
headers = args.get('headers')

http_method = args.get('http_method', 'GET')

response = client.generic_request(
Expand All @@ -115,6 +119,7 @@ def generic_command(client: MsGraphClient, args: dict[str, Any]) -> CommandResul
api_version=args.get('api_version', 'v1.0'),
odata=args.get('odata', ''),
request_body=request_body,
headers=dict(subString.split(":") for subString in headers.split(',')) if headers else None
)

if not response:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ configuration:
hidden: true
- display: Use a self-deployed Azure Application
name: self_deployed
section: Connect
required: false
type: 8
additionalinfo: Select this checkbox if you are using a self-deployed Azure application.
Expand Down Expand Up @@ -167,6 +168,9 @@ script:
predefined:
- 'true'
- 'false'
- name: headers
description: 'A comma-separated list of headers to send in the request, for example: ConsistencyLevel:eventual,User-Agent:MyApp/1.0.'
isArray: true
description: Run a Microsoft Graph API query.
name: msgraph-api-request
- description: Run this command to start the authorization process and follow the instructions in the command results.
Expand All @@ -182,7 +186,7 @@ script:
- description: Generate the login URL used for Authorization code flow.
name: msgraph-api-generate-login-url
arguments: []
dockerimage: demisto/crypto:1.0.0.114611
dockerimage: demisto/crypto:1.0.0.2005673
runonce: false
script: '-'
subtype: python3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -187,3 +188,34 @@ def test_test_module_command_with_managed_identities(mocker, requests_mock, clie
qs = get_mock.last_request.qs
assert qs['resource'] == [Resources.graph]
assert client_id and qs['client_id'] == [client_id] or 'client_id' not in qs


@pytest.mark.parametrize("headers, expected_headers", [
("ConsistencyLevel:eventual,User-Agent:MyApp/1.0", {
"ConsistencyLevel": "eventual",
"User-Agent": "MyApp/1.0"
}),
(None, None), # No headers case
])
def test_generic_command_headers(headers, expected_headers):
client = MagicMock(spec=MsGraphClient)
client.generic_request = MagicMock(return_value={"response": "success"})

args = {
"resource": "/test/resource",
"http_method": "GET",
"api_version": "v1.0",
"headers": headers,
"request_body": json.dumps({"key": "value"})
}

generic_command(client, args)

client.generic_request.assert_called_once_with(
resource="/test/resource",
http_method="GET",
api_version="v1.0",
odata="",
request_body={"key": "value"},
headers=expected_headers
)
24 changes: 13 additions & 11 deletions Packs/MicrosoftGraphAPI/Integrations/MicrosoftGraphAPI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,25 @@ Should be used after running the ***msgraph-api-auth-start*** command.
Tests connectivity to Microsoft.

### msgraph-api-request

***
Run a Microsoft Graph API query.


#### Base Command

`msgraph-api`
`msgraph-api-request`

#### Input

| **Argument Name** | **Description** | **Required** |
|-------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| resource | The resource in Microsoft Graph to refer. | Required |
| http_method | The HTTP method used for the request to Microsoft Graph. Possible values are: "GET", "POST", "DELETE", "PUT", or "PATCH". Default is "GET". | Optional |
| api_version | The version of the Microsoft Graph API to use. Possible values are: "v1.0" or "beta". Default is "v1.0". | Optional |
| request_body | The request body (required for POST queries). | Optional |
| odata | OData system query options, e.g. $filter=startswith(givenName, 'J'). For more details see https://docs.microsoft.com/en-us/graph/query-parameters. It is recommended to use the $top query option to limit the result. | Optional |
| populate_context | If "true", will populate the API response to the context data. Default is "true". | Optional |
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| resource | The resource in Microsoft Graph to refer. | Required |
| http_method | The HTTP method used for the request to Microsoft Graph. Possible values are: GET, POST, DELETE, PUT, PATCH. Default is GET. | Optional |
| api_version | The version of the Microsoft Graph API to use. Possible values are: v1.0, beta. Default is v1.0. | Optional |
| request_body | The request body (required for POST queries). | Optional |
| odata | OData system query options, e.g., $filter=startswith(givenName, 'J'). For more details see https://docs.microsoft.com/en-us/graph/query-parameters. It is recommended to use the $top query option to limit the result. | Optional |
| populate_context | If "true" will populate the API response to the context data. Possible values are: true, false. Default is true. | Optional |
| headers | A comma-separated list of headers to send in the GET request, for example: ConsistencyLevel:eventual,User-Agent:MyApp/1.0. | Optional |

#### Context Output

Expand Down Expand Up @@ -175,4 +177,4 @@ We can see that according to the [HTTP request](https://docs.microsoft.com/en-us
- The HTTP method is ***GET***
- The resource is ***/applications***

So in order to list all the applications using the integration, we would run the command: `!msgraph-api resource=/applications http_method=GET`
So in order to list all the applications using the integration, we would run the command: `!msgraph-api resource=/applications http_method=GET`
7 changes: 7 additions & 0 deletions Packs/MicrosoftGraphAPI/ReleaseNotes/1_1_53.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#### Integrations

##### Microsoft Graph API
- Updated the Docker image to: *demisto/crypto:1.0.0.2005673*.

- Added the *headers* argument to the ***msgraph-api-request*** command.
2 changes: 1 addition & 1 deletion Packs/MicrosoftGraphAPI/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Microsoft Graph API",
"description": "Use the Microsoft Graph API integration to interact with Microsoft APIs that do not have dedicated integrations in Cortex XSOAR, for example, Mail Single-User, etc.",
"support": "xsoar",
"currentVersion": "1.1.52",
"currentVersion": "1.1.53",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit 4f7ada7

Please sign in to comment.