def test_mirror_investigation(mocker, requests_mock):
    from MicrosoftTeams import mirror_investigation

    mocker.patch.object(demisto, 'results')
    mocker.patch.object(demisto, 'setIntegrationContext')
    mocker.patch.object(demisto, 'params', return_value={'team': 'The-A-Team'})

    # verify command cannot be executed in the war room
    mocker.patch.object(demisto, 'investigation', return_value={'type': 9})
    with pytest.raises(ValueError) as e:
        mirror_investigation()
    assert str(e.value) == 'Can not perform this action in playground.'

    # verify channel is mirrored successfully and a message is sent to it
    mocker.patch.object(demisto, 'investigation', return_value={'id': '2'})
    channel_id: str = 'channel-id'
    # create channel mock request
    requests_mock.post(
        f'https://graph.microsoft.com/v1.0/teams/{team_aad_id}/channels',
        json={'id': channel_id})
    # send message mock request
    requests_mock.post(
        f'{service_url}/v3/conversations/{channel_id}/activities', json={})
    mirror_investigation()
    updated_mirrored_channels: list = mirrored_channels[:]
    updated_mirrored_channels.append({
        'channel_id': 'channel-id',
        'investigation_id': '2',
        'mirror_type': 'all',
        'mirror_direction': 'both',
        'auto_close': 'true',
        'mirrored': False,
        'channel_name': 'incident-2'
    })
    expected_integration_context: dict = {
        'bot_name':
        'DemistoBot',
        'tenant_id':
        tenant_id,
        'service_url':
        service_url,
        'teams':
        json.dumps([{
            'mirrored_channels': updated_mirrored_channels,
            'team_id': team_id,
            'team_aad_id': team_aad_id,
            'team_members': team_members,
            'team_name': 'The-A-Team'
        }])
    }
    assert requests_mock.request_history[1].json() == {
        'displayName': 'incident-2',
        'description': 'Channel to mirror incident 2'
    }
    assert requests_mock.request_history[3].json() == {
        'text':
        'This channel was created to mirror [incident 2](https://test-address:8443#/WarRoom/2) between '
        'Teams and Demisto. In order for your Teams messages to be mirrored in Demisto, you need to'
        ' mention the Demisto Bot in the message.',
        'type':
        'message'
    }

    assert demisto.setIntegrationContext.call_count == 3
    set_integration_context = demisto.setIntegrationContext.call_args[0]
    assert len(set_integration_context) == 1
    set_integration_context[0].pop('graph_access_token')
    set_integration_context[0].pop('graph_valid_until')
    set_integration_context[0].pop('bot_access_token')
    set_integration_context[0].pop('bot_valid_until')
    assert set_integration_context[0] == expected_integration_context
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[
        0] == 'Investigation mirrored successfully in channel incident-2.'

    # verify channel mirror is updated successfully
    mocker.patch.object(demisto, 'setIntegrationContext')
    mocker.patch.object(demisto,
                        'args',
                        return_value={
                            'mirror_type': 'chat',
                            'direction': 'FromDemisto',
                            'autoclose': 'false'
                        })
    mocker.patch.object(demisto, 'investigation', return_value={'id': '1'})
    mirror_investigation()
    assert demisto.setIntegrationContext.call_count == 1
    set_integration_context = demisto.setIntegrationContext.call_args[0]
    assert len(set_integration_context) == 1
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[0] == 'Investigation mirror was updated successfully.'

    # verify channel with custom channel name is mirrored successfully
    mocker.patch.object(demisto, 'investigation', return_value={'id': '14'})
    mocker.patch.object(demisto,
                        'args',
                        return_value={'channel_name': 'booya'})

    mirror_investigation()
    assert requests_mock.request_history[5].json() == {
        'displayName': 'booya',
        'description': 'Channel to mirror incident 14'
    }
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[
        0] == 'Investigation mirrored successfully in channel booya.'
Beispiel #2
0
def test_mirror_investigation(mocker, requests_mock):
    from MicrosoftTeams import mirror_investigation

    mocker.patch.object(demisto, 'results')
    mocker.patch.object(demisto, 'setIntegrationContext')
    mocker.patch.object(demisto, 'params', return_value={'team': 'The-A-Team'})

    # verify command cannot be executed in the war room
    mocker.patch.object(demisto, 'investigation', return_value={'type': 9})
    with pytest.raises(ValueError) as e:
        mirror_investigation()
    assert str(e.value) == 'Can not perform this action in playground.'

    # verify channel is mirrored successfully
    mocker.patch.object(demisto, 'investigation', return_value={'id': '2'})
    requests_mock.post(
        f'https://graph.microsoft.com/v1.0/teams/{team_aad_id}/channels',
        json={'id': 'channel-id'})
    mirror_investigation()
    updated_mirrored_channels: list = mirrored_channels[:]
    updated_mirrored_channels.append({
        'channel_id': 'channel-id',
        'investigation_id': '2',
        'mirror_type': 'all',
        'mirror_direction': 'both',
        'auto_close': 'true',
        'mirrored': False,
        'channel_name': 'incident-2'
    })
    expected_integration_context: dict = {
        'bot_name':
        'DemistoBot',
        'tenant_id':
        tenant_id,
        'service_url':
        service_url,
        'teams':
        json.dumps([{
            'mirrored_channels': updated_mirrored_channels,
            'team_id': team_id,
            'team_aad_id': team_aad_id,
            'team_members': team_members,
            'team_name': 'The-A-Team'
        }])
    }
    assert requests_mock.request_history[1].json() == {
        'displayName': 'incident-2',
        'description': 'Channel to mirror incident 2'
    }
    assert demisto.setIntegrationContext.call_count == 2
    set_integration_context = demisto.setIntegrationContext.call_args[0]
    assert len(set_integration_context) == 1
    set_integration_context[0].pop('graph_access_token')
    set_integration_context[0].pop('graph_valid_until')
    assert set_integration_context[0] == expected_integration_context
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[
        0] == 'Investigation mirrored successfully in channel incident-2.'

    # verify channel mirror is updated successfully
    mocker.patch.object(demisto, 'setIntegrationContext')
    mocker.patch.object(demisto,
                        'args',
                        return_value={
                            'mirror_type': 'chat',
                            'direction': 'FromDemisto',
                            'autoclose': 'false'
                        })
    mocker.patch.object(demisto, 'investigation', return_value={'id': '1'})
    mirror_investigation()
    assert demisto.setIntegrationContext.call_count == 1
    set_integration_context = demisto.setIntegrationContext.call_args[0]
    assert len(set_integration_context) == 1
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[0] == 'Investigation mirror was updated successfully.'