Example #1
0
    def test_existing_user(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a user
        When:
            - The user exists in the application
            - 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=USER_APP_DATA)
        mocker.patch.object(IAMUserProfile,
                            'update_with_app_data',
                            return_value={})

        user_profile = IAMCommand().get_user(client, args)
        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'
Example #2
0
    def test_command_is_disabled(self, mocker):
        """
        Given:
            - An app 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=USER_APP_DATA)

        user_profile = IAMCommand(is_update_enabled=False).update_user(
            client, args)
        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.'
Example #3
0
    def test_success(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a non-existing user in the application
        When:
            - Calling function create_user_command
        Then:
            - Ensure a User Profile object with the user data is returned
        """
        client = mock_client()
        args = {'user-profile': {'email': '*****@*****.**'}}

        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=USER_APP_DATA)

        user_profile = IAMCommand().create_user(client, args)
        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'
Example #4
0
    def test_bad_response(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a non-existing user in the application
        When:
            - Calling function get_user_command
            - A bad response (500) is returned from the application 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 = IAMCommand().get_user(client, args)
        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'
Example #5
0
    def test_allow_enable(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains user data
        When:
            - The user is disabled in the application
            - 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=DISABLED_USER_APP_DATA)
        mocker.patch.object(IAMUserProfile, 'map_object', return_value={})
        mocker.patch.object(client, 'update_user', return_value=USER_APP_DATA)

        user_profile = IAMCommand().update_user(client, args)
        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'
Example #6
0
    def test_non_existing_user(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains user data
        When:
            - The user does not exist in the application
            - 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=USER_APP_DATA)

        user_profile = IAMCommand(create_if_not_exists=True).update_user(
            client, args)
        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'
Example #7
0
    def test_user_already_exists(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a user
        When:
            - The user already exists in the application and disabled
            - allow-enable argument is false
            - Calling function create_user_command
        Then:
            - Ensure the command is considered successful and the user is still disabled
        """
        client = mock_client()
        args = {
            'user-profile': {
                'email': '*****@*****.**'
            },
            'allow-enable': 'false'
        }

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

        user_profile = IAMCommand().create_user(client, args)
        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 False
        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'
Example #8
0
    def test_non_existing_user(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email a user
        When:
            - The user does not exist in the application
            - 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 = IAMCommand().get_user(client, args)
        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]
Example #9
0
    def test_non_existing_user(self, mocker):
        """
        Given:
            - An app client object
            - A user-profile argument that contains an email of a user
        When:
            - create-if-not-exists parameter is unchecked
            - The user does not exist in the application
            - Calling function disable_user_command
        Then:
            - Ensure the command is considered successful and skipped
        """
        client = mock_client()
        args = {'user-profile': {'email': '*****@*****.**'}}

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

        user_profile = IAMCommand().disable_user(client, args)
        outputs = get_outputs_from_user_profile(user_profile)

        assert outputs.get('action') == IAMActions.DISABLE_USER
        assert outputs.get('success') is True
        assert outputs.get('skipped') is True
        assert outputs.get('reason') == IAMErrors.USER_DOES_NOT_EXIST[1]