def test_update_user_command__allow_enable(mocker):
    """
    Given:
        - An ServiceNow IAM client object
        - A user-profile argument that contains user data
    When:
        - The user is disabled in ServiceNow
        - allow-enable argument is true
        - Calling function update_user_command
    Then:
        - Ensure the user is enabled at the end of the command execution.
    """
    client = mock_client()
    args = {
        'user-profile': {
            'email': '*****@*****.**',
            'givenname': 'mock_first_name'
        },
        'allow-enable': 'true'
    }

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

    user_profile = update_user_command(client,
                                       args,
                                       'mapper_out',
                                       is_command_enabled=True,
                                       is_enable_enabled=True,
                                       is_create_user_enabled=False,
                                       create_if_not_exists=False)
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.UPDATE_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'
def test_update_user_command__non_existing_user(mocker):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains user data
    When:
        - The user does not exist in ServiceNow
        - create-if-not-exists parameter is checked
        - Create User command is enabled
        - Calling function update_user_command
    Then:
        - Ensure the create action is executed
        - Ensure a User Profile object with the user data is returned
    """
    client = mock_client()
    args = {
        'user-profile': {
            'email': '*****@*****.**',
            'givenname': 'mock_first_name'
        }
    }

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

    user_profile = update_user_command(client,
                                       args,
                                       'mapper_out',
                                       is_command_enabled=True,
                                       is_enable_enabled=False,
                                       is_create_user_enabled=True,
                                       create_if_not_exists=True)
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.CREATE_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_update_user_command__command_is_disabled(mocker):
    """
    Given:
        - A ServiceNow IAM client object
        - A user-profile argument that contains user data
    When:
        - Update User command is disabled
        - Calling function update_user_command
    Then:
        - Ensure the command is considered successful and skipped
    """
    client = mock_client()
    args = {
        'user-profile': {
            'email': '*****@*****.**',
            'givenname': 'mock_first_name'
        }
    }

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

    user_profile = update_user_command(client,
                                       args,
                                       'mapper_out',
                                       is_command_enabled=False,
                                       is_enable_enabled=False,
                                       is_create_user_enabled=False,
                                       create_if_not_exists=False)
    outputs = get_outputs_from_user_profile(user_profile)

    assert outputs.get('action') == IAMActions.UPDATE_USER
    assert outputs.get('success') is True
    assert outputs.get('skipped') is True
    assert outputs.get('reason') == 'Command is disabled.'