Esempio n. 1
0
def test_get_user_command__bad_response(mocker):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains an email of a non-existing user in ServiceNow
    When:
        - Calling function get_user_command
        - A bad response (500) is returned from ServiceNow's API
    Then:
        - Ensure the resulted User Profile object holds information about the bad response.
    """
    import demistomock as demisto

    client = mock_client()
    args = {'user-profile': {'email': '*****@*****.**'}}

    bad_response = Response()
    bad_response.status_code = 500
    bad_response._content = b'{"error": {"detail": "details", "message": "message"}}'

    mocker.patch.object(demisto, 'error')
    mocker.patch.object(Session, 'request', return_value=bad_response)

    user_profile = get_user_command(client, args, 'mapper_in', 'mapper_out')
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.GET_USER
    assert outputs.get('success') is False
    assert outputs.get('errorCode') == 500
    assert outputs.get('errorMessage') == 'message: details'
def test_get_user_command__existing_user(mocker):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains an email of a user
    When:
        - The user exists in ServiceNow
        - Calling function get_user_command
    Then:
        - Ensure the resulted User Profile object holds the correct user details
    """
    client = mock_client()
    args = {'user-profile': {'email': '*****@*****.**'}}

    mocker.patch.object(client,
                        'get_user',
                        return_value=SERVICENOW_USER_OUTPUT)
    mocker.patch.object(IAMUserProfile,
                        'update_with_app_data',
                        return_value={})

    user_profile = get_user_command(client, args, 'mapper_in')
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.GET_USER
    assert outputs.get('success') is True
    assert outputs.get('active') is True
    assert outputs.get('id') == 'mock_id'
    assert outputs.get('username') == 'mock_user_name'
    assert outputs.get('details', {}).get('first_name') == 'mock_first_name'
    assert outputs.get('details', {}).get('last_name') == 'mock_last_name'
Esempio n. 3
0
def test_get_user_command__non_existing_user(mocker):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains an email a user
    When:
        - The user does not exist in ServiceNow
        - Calling function get_user_command
    Then:
        - Ensure the resulted User Profile object holds information about an unsuccessful result.
    """
    client = mock_client()
    args = {'user-profile': {'email': '*****@*****.**'}}

    mocker.patch.object(client, 'get_user', return_value=None)

    user_profile = get_user_command(client, args, 'mapper_in', 'mapper_out')
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.GET_USER
    assert outputs.get('success') is False
    assert outputs.get('errorCode') == IAMErrors.USER_DOES_NOT_EXIST[0]
    assert outputs.get('errorMessage') == IAMErrors.USER_DOES_NOT_EXIST[1]
Esempio n. 4
0
def test_get_user_command__existing_user(mocker, args, mock_url,
                                         requests_mock):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains an email of a user
    When:
        - The user exists in ServiceNow
        - Calling function get_user_command
        Cases:
        Case a: User profile contains username data.
        Case b: User profile contains username and email data.
        Case c: User profile contains username, email and ID data.
    Then:
        - Ensure the resulted User Profile object holds the correct user details
        Cases:
        Case a: Mocked URL querying by username is called.
        Case b: Mocked URL querying by email is called.
        Case c: Mocked URL querying by ID is called.
    """
    client = mock_client()
    requests_mock.get(mock_url, json={'result': [SERVICENOW_USER_OUTPUT]})
    mocker.patch.object(IAMUserProfile,
                        'update_with_app_data',
                        return_value={})

    user_profile = get_user_command(client, args, 'mapper_in', 'mapper_out')
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.GET_USER
    assert outputs.get('success') is True
    assert outputs.get('active') is True
    assert outputs.get('id') == 'mock_id'
    assert outputs.get('username') == 'mock_user_name'
    assert outputs.get('details', {}).get('first_name') == 'mock_first_name'
    assert outputs.get('details', {}).get('last_name') == 'mock_last_name'