def test_send_message_server_notifications_incident_changed( mocker, requests_mock): """ Given: - Notification from server of an updated incident. When: - Sending notification message of the updated incident. Then: - Ensure message is sent successfully. - Verify the message is sent to the dedicated notifications channel. """ from MicrosoftTeams import send_message mocker.patch.object(demisto, 'results') mocker.patch.object(demisto, 'params', return_value={ 'team': 'The-A-Team', 'min_incident_severity': 'Low', 'incident_notifications_channel': 'General' }) mocker.patch.object( demisto, 'args', return_value={ 'channel': 'incidentNotificationChannel', 'message': 'DBot has updated an incident tadam.\nView it on https://server#/WarRoom/3247', 'messageType': 'incidentChanged', 'severity': 1, 'to': '' }) requests_mock.get( f'https://graph.microsoft.com/v1.0/teams/{team_aad_id}/channels', json={ 'value': [{ 'description': 'general channel', 'displayName': 'General', 'id': '19:[email protected]' }] }) requests_mock.post( f'{service_url}/v3/conversations/19:[email protected]/activities', json={}) send_message() results = demisto.results.call_args[0] assert len(results) == 1 assert results[0] == 'Message was sent successfully.'
def test_send_message(mocker, requests_mock): from MicrosoftTeams import send_message mocker.patch.object(demisto, 'results') # verify that a mirrored message is skipped mocker.patch.object(demisto, 'args', return_value={ 'messageType': 'mirrorEntry', 'originalMessage': 'a mirrored message\n**From Microsoft Teams**' }) assert send_message() is None # verify notification from server with severity below threshold is not sent mocker.patch.object(demisto, 'params', return_value={ 'min_incident_severity': 'Medium', 'team': 'The-A-Team' }) mocker.patch.object(demisto, 'args', return_value={ 'messageType': 'incidentOpened', 'severity': 1 }) assert send_message() is None # verify error is raised if no user/channel were provided mocker.patch.object(demisto, 'args', return_value={}) with pytest.raises(ValueError) as e: send_message() assert str( e.value) == 'No channel or team member to send message were provided.' # verify error is raised if both user and channel were provided mocker.patch.object(demisto, 'args', return_value={ 'channel': 'somechannel', 'team_member': 'someuser' }) with pytest.raises(ValueError) as e: send_message() assert str( e.value ) == 'Provide either channel or team member to send message to, not both.' # verify message is sent properly given user to send to mocker.patch.object(demisto, 'params', return_value={'bot_id': bot_id}) mocker.patch.object(demisto, 'args', return_value={ 'team_member': 'Denzel Washington', 'message': 'MESSAGE' }) requests_mock.post(f'{service_url}/v3/conversations', json={'id': 'conversation-id'}) requests_mock.post( f'{service_url}/v3/conversations/conversation-id/activities', json={}) expected_create_personal_conversation_data: dict = { 'bot': { 'id': f'28:{bot_id}', 'name': 'DemistoBot' }, 'members': [{ 'id': '29:1pBMMC85IyjM3tr_MCZi7KW4pw4EULxLN4C7R_xoi3Wva_lOn3VTf7xJlCLK-r-pMumrmoz9agZxsSrCf7__u9R' }], 'channelData': { 'tenant': { 'id': tenant_id } } } send_message() assert requests_mock.request_history[0].json( ) == expected_create_personal_conversation_data results = demisto.results.call_args[0] assert len(results) == 1 assert results[0] == 'Message was sent successfully.' # verify message is sent properly given channel mocker.patch.object(demisto, 'params', return_value={'team': 'The-A-Team'}) mocker.patch.object(demisto, 'args', return_value={ 'channel': 'incident-1', 'message': 'MESSAGE' }) requests_mock.post( f"{service_url}/v3/conversations/{mirrored_channels[0]['channel_id']}/activities", json={}) send_message() results = demisto.results.call_args[0] assert len(results) == 1 assert results[0] == 'Message was sent successfully.' # verify message is sent properly given entitlement message: dict = { 'message_text': 'is this really working?', 'options': ['yes', 'no', 'maybe'], 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '72', 'task_id': '23' } mocker.patch.object(demisto, 'args', return_value={ 'team_member': '*****@*****.**', 'message': json.dumps(message) }) expected_ask_user_message: dict = { 'attachments': [{ 'content': { '$schema': 'http://adaptivecards.io/schemas/adaptive-card.json', 'actions': [{ 'data': { 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '72', 'response': 'yes', 'task_id': '23' }, 'title': 'yes', 'type': 'Action.Submit' }, { 'data': { 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '72', 'response': 'no', 'task_id': '23' }, 'title': 'no', 'type': 'Action.Submit' }, { 'data': { 'entitlement': '4404dae8-2d45-46bd-85fa-64779c12abe8', 'investigation_id': '72', 'response': 'maybe', 'task_id': '23' }, 'title': 'maybe', 'type': 'Action.Submit' }], 'body': [{ 'text': 'is this really working?', 'type': 'TextBlock' }], 'type': 'AdaptiveCard', 'version': '1.0' }, 'contentType': 'application/vnd.microsoft.card.adaptive' }], 'type': 'message' } send_message() assert requests_mock.request_history[4].json() == expected_ask_user_message results = demisto.results.call_args[0] assert len(results) == 1 assert results[0] == 'Message was sent successfully.' # verify proper error is raised if invalid JSON provided as adaptive card mocker.patch.object(demisto, 'args', return_value={ 'channel': 'channel', 'adaptive_card': 'THISisSTRINGnotJSON' }) with pytest.raises(ValueError) as e: send_message() assert str(e.value) == 'Given adaptive card is not in valid JSON format.' # verify proper error is raised if both message and adaptive card were provided mocker.patch.object(demisto, 'args', return_value={ 'channel': 'channel', 'message': 'message', 'adaptive_card': '{"a":"b"}' }) with pytest.raises(ValueError) as e: send_message() assert str( e.value) == 'Provide either message or adaptive to send, not both.' # verify proper error is raised if neither message or adaptive card were provided mocker.patch.object(demisto, 'args', return_value={'channel': 'channel'}) with pytest.raises(ValueError) as e: send_message() assert str(e.value) == 'No message or adaptive card to send were provided.' # verify adaptive card sent successfully adaptive_card: dict = { "contentType": "application/vnd.microsoft.card.adaptive", "content": { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.0", "body": [{ "type": "Container", "items": [{ "type": "TextBlock", "text": "What a pretty adaptive card" }] }] } } mocker.patch.object(demisto, 'args', return_value={ 'team_member': '*****@*****.**', 'adaptive_card': json.dumps(adaptive_card) }) expected_conversation: dict = { 'type': 'message', 'attachments': [adaptive_card] } send_message() assert requests_mock.request_history[6].json() == expected_conversation results = demisto.results.call_args[0] assert len(results) == 1 assert results[0] == 'Message was sent successfully.'