Example #1
0
def test_incident_name_is_initialized(mocker, requests_mock):
    """
    Given:
     - Integration instance initialized with fetch enabled and without changing incident name

    When:
     - Clicking on Test button (running test-module)

    Then:
     - Verify expected exception is raised as default incident name value is not in response
    """
    url = 'https://test.service-now.com'
    mocker.patch.object(
        demisto,
        'params',
        return_value={
            'isFetch': True,
            'url': url,
            'credentials': {
                'identifier': 'identifier',
                'password': '******',
            },
            'incident_name': None
        }
    )
    mocker.patch.object(demisto, 'command', return_value='test-module')

    def return_error_mock(message, error):
        raise

    mocker.patch('ServiceNowv2.return_error', side_effect=return_error_mock)
    requests_mock.get(
        f'{url}/api/now/table/incident?sysparm_limit=1',
        json={
            'result': [{
                'opened_at': 'sometime'
            }]
        }
    )
    with pytest.raises(ValueError) as e:
        main()
    assert str(e.value) == 'The field [number] does not exist in the ticket.'
Example #2
0
def test_oauth_authentication(mocker, requests_mock):
    """
    Given:
     - Integration instance, initialized with the `Use OAuth Login` checkbox selected.

    When:
     - Clicking on running the !servicenow-oauth-test command.

    Then:
     - Verify that oauth authorization flow is used by checking that the get_access_token is called.
    """
    from unittest.mock import MagicMock
    url = 'https://test.service-now.com'
    mocker.patch.object(demisto, 'command', return_value='servicenow-oauth-test')
    mocker.patch.object(ServiceNowClient, 'get_access_token')
    requests_mock.get(
        f'{url}/api/now/table/incident?sysparm_limit=1',
        json={
            'result': [{
                'opened_at': 'sometime'
            }]
        }
    )

    # Assert that get_access_token is called when `Use OAuth Login` checkbox is selected:
    mocker.patch.object(
        demisto,
        'params',
        return_value={
            'url': url,
            'credentials': {
                'identifier': 'client_id',
                'password': '******'
            },
            'use_oauth': True
        }
    )
    ServiceNowClient.get_access_token = MagicMock()
    main()
    assert ServiceNowClient.get_access_token.called