diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.py b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.py index 1ecc46067856..e4e3a1483341 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.py +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.py @@ -103,6 +103,9 @@ class FormType(Enum): # Used for 'send-message', and by the MicrosoftTeamsAsk s TOKEN_EXPIRED_ERROR_CODES = {50173, 700082, } # See: https://login.microsoftonline.com/error?code= REGEX_SEARCH_ERROR_DESC = r"^[^:]*:\s(?P.*?\.)" +# must be synced with ones in TeamsAsk +MS_TEAMS_ASK_MESSAGE_KEYS = {'message_text', 'options', 'entitlement', 'investigation_id', 'task_id', 'form_type'} + class Handler: @staticmethod @@ -502,6 +505,14 @@ def process_mirror_or_unknown_message(message: str) -> dict: return create_adaptive_card(body) +def is_teams_ask_message(msg: str) -> bool: + try: + message: dict = json.loads(msg) + return message.keys() == MS_TEAMS_ASK_MESSAGE_KEYS + except json.decoder.JSONDecodeError: + return False + + def process_ask_user(message: str) -> dict: """ Processes ask user message and creates adaptive card @@ -2062,7 +2073,7 @@ def send_message(): if message: entitlement_match: Match[str] | None = re.search(ENTITLEMENT_REGEX, message) - if entitlement_match: + if entitlement_match and is_teams_ask_message(message): # In TeamsAsk process adaptive_card = process_ask_user(message) conversation = { diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.yml b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.yml index 894b09384c8c..3ed5b29a5b2e 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.yml +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams.yml @@ -712,7 +712,7 @@ script: - description: Generate the login url used for Authorization code flow. name: microsoft-teams-generate-login-url arguments: [] - dockerimage: demisto/teams:1.0.0.85097 + dockerimage: demisto/teams:1.0.0.86933 longRunning: true longRunningPort: true script: '' diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams_test.py b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams_test.py index b9ed964dca2a..21d8c3fc1e65 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams_test.py +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeams/MicrosoftTeams_test.py @@ -464,7 +464,17 @@ def test_send_message_raising_errors(mocker, args, result): assert str(e.value) == result -def test_send_message_with_user(mocker, requests_mock): +@pytest.mark.parametrize('message', ['MESSAGE', '891f1e9d-b8c3-4e24-bfbb-c44bcca4d586', + 'testing 891f1e9d-b8c3-4e24-bfbb-c44bcca4d586 testing']) +def test_send_message_with_user(mocker, requests_mock, message): + """ + Given: + - a message as a basic string and a message that contains GUID. + When: + - running send message function. + Then: + - The message is sent successfully in both cases. + """ # verify message is sent properly given user to send to from MicrosoftTeams import send_message mocker.patch.object(demisto, 'results') @@ -475,7 +485,7 @@ def test_send_message_with_user(mocker, requests_mock): 'args', return_value={ 'team_member': 'Denzel Washington', - 'message': 'MESSAGE' + 'message': message } ) requests_mock.post( @@ -550,7 +560,8 @@ def test_send_message_with_entitlement(mocker, requests_mock): 'options': ['yes', 'no', 'maybe'], 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '72', - 'task_id': '23' + 'task_id': '23', + 'form_type': 'predefined-options' } mocker.patch.object( demisto, @@ -2340,3 +2351,28 @@ def test_handle_teams_proxy_and_ssl(mocker, is_xsoar_8, expected_result): proxies, use_ssl = ms_teams.handle_teams_proxy_and_ssl() assert (proxies, use_ssl) == expected_result + + +DUMMY_ASK_MESSAGE = {"message_text": "message", "options": [ + "option"], "entitlement": "id", "investigation_id": "inv_id", "task_id": "task", "form_type": "form"} + + +@pytest.mark.parametrize('message, result', [ + (json.dumps(DUMMY_ASK_MESSAGE), True), + (json.dumps(DUMMY_ASK_MESSAGE | {"extra_key": "extra"}), False), + ("non json message", False) +]) +def test_is_teams_ask_message(message, result): + """ + Given: + - input message string + When: + - Running is_teams_ask_message. + Then: + - Assert only ask_teams messages return True + If the test fails, please update the first message param in the test to have the same keys as MS_TEAMS_ASK_MESSAGE_KEYS + constant in MicrosoftTeams. + """ + from MicrosoftTeams import is_teams_ask_message + + assert is_teams_ask_message(message) == result diff --git a/Packs/MicrosoftTeams/ReleaseNotes/1_4_50.md b/Packs/MicrosoftTeams/ReleaseNotes/1_4_50.md new file mode 100644 index 000000000000..d6227b11d92f --- /dev/null +++ b/Packs/MicrosoftTeams/ReleaseNotes/1_4_50.md @@ -0,0 +1,7 @@ + +#### Integrations + +##### Microsoft Teams + +- Fixed an issue where ***send-notification*** failed on messages containing a GUID. +- Updated the Docker image to *demisto/teams:1.0.0.86933*. \ No newline at end of file diff --git a/Packs/MicrosoftTeams/Scripts/MicrosoftTeamsAsk/MicrosoftTeamsAsk_test.py b/Packs/MicrosoftTeams/Scripts/MicrosoftTeamsAsk/MicrosoftTeamsAsk_test.py index 87898260f9cf..bba7494029e7 100644 --- a/Packs/MicrosoftTeams/Scripts/MicrosoftTeamsAsk/MicrosoftTeamsAsk_test.py +++ b/Packs/MicrosoftTeams/Scripts/MicrosoftTeamsAsk/MicrosoftTeamsAsk_test.py @@ -4,8 +4,15 @@ import json import pytest +MS_TEAMS_ASK_MESSAGE_KEYS = {'message_text', 'options', 'entitlement', 'investigation_id', + 'task_id', 'form_type'} # must be synced with ones in MicrosoftTeams.py + def execute_command(name, args=None): + """ + if assert MS_TEAMS_ASK_MESSAGE_KEYS == json_message.keys() test fails, update the MS_TEAMS_ASK_MESSAGE_KEYS constant + to have the same keys as the message keys in the MsTeamsAsk script and the test. + """ if name == 'addEntitlement': return [ { @@ -14,14 +21,16 @@ def execute_command(name, args=None): } ] elif name == 'send-notification': - expected_message: str = json.dumps({ + json_message = { 'message_text': 'How are you today?', 'options': ['Great', 'Wonderful', 'SSDD', 'Wooah'], 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '32', 'task_id': '44', - 'form_type': 'predefined-options', - }) + 'form_type': 'predefined-options'} + + expected_message: str = json.dumps(json_message) + assert json_message.keys() == MS_TEAMS_ASK_MESSAGE_KEYS expected_script_arguments: dict = { 'message': expected_message, 'using-brand': 'Microsoft Teams' @@ -33,8 +42,9 @@ def execute_command(name, args=None): if 'team' in args: expected_script_arguments['team'] = 'TestTeam' assert args == expected_script_arguments + return None else: - raise ValueError('Unimplemented command called: {}'.format(name)) + raise ValueError(f'Unimplemented command called: {name}') def test_microsoft_teams_ask(mocker): diff --git a/Packs/MicrosoftTeams/pack_metadata.json b/Packs/MicrosoftTeams/pack_metadata.json index fe87a795e677..f33c6c004c7d 100644 --- a/Packs/MicrosoftTeams/pack_metadata.json +++ b/Packs/MicrosoftTeams/pack_metadata.json @@ -2,7 +2,7 @@ "name": "Microsoft Teams", "description": "Send messages and notifications to your team members.", "support": "xsoar", - "currentVersion": "1.4.49", + "currentVersion": "1.4.50", "author": "Cortex XSOAR", "url": "https://www.paloaltonetworks.com/cortex", "email": "",