예제 #1
0
    def setUp(self):
        super(InquiryPermissionsResolverTestCase, self).setUp()

        # Create some mock users
        user_1_db = UserDB(name='custom_role_inquiry_list_grant')
        user_1_db = User.add_or_update(user_1_db)
        self.users['custom_role_inquiry_list_grant'] = user_1_db

        user_2_db = UserDB(name='custom_role_inquiry_view_grant')
        user_2_db = User.add_or_update(user_2_db)
        self.users['custom_role_inquiry_view_grant'] = user_2_db

        user_3_db = UserDB(name='custom_role_inquiry_respond_grant')
        user_3_db = User.add_or_update(user_3_db)
        self.users['custom_role_inquiry_respond_grant'] = user_3_db

        user_4_db = UserDB(name='custom_role_inquiry_all_grant')
        user_4_db = User.add_or_update(user_4_db)
        self.users['custom_role_inquiry_all_grant'] = user_4_db

        user_5_db = UserDB(name='custom_role_inquiry_inherit')
        user_5_db = User.add_or_update(user_5_db)
        self.users['custom_role_inquiry_inherit'] = user_5_db

        # Create a workflow for testing inheritance of action_execute permission
        # to inquiry_respond permission
        wf_db = ActionDB(pack='examples',
                         name='mistral-ask-basic',
                         entry_point='',
                         runner_type={'name': 'mistral-v2'})
        wf_db = Action.add_or_update(wf_db)
        self.resources['wf'] = wf_db
        runner = {'name': 'mistral-v2'}
        liveaction = {'action': 'examples.mistral-ask-basic'}
        status = action_constants.LIVEACTION_STATUS_PAUSED

        # Spawn workflow
        action = {'uid': wf_db.get_uid(), 'pack': 'examples'}
        wf_exc_db = ActionExecutionDB(action=action,
                                      runner=runner,
                                      liveaction=liveaction,
                                      status=status)
        wf_exc_db = ActionExecution.add_or_update(wf_exc_db)

        # Create an Inquiry on which permissions can be granted
        action_1_db = ActionDB(pack='core',
                               name='ask',
                               entry_point='',
                               runner_type={'name': 'inquirer'})
        action_1_db = Action.add_or_update(action_1_db)
        self.resources['action_1'] = action_1_db
        runner = {'name': 'inquirer'}
        liveaction = {'action': 'core.ask'}
        status = action_constants.LIVEACTION_STATUS_PENDING

        # For now, Inquiries are "borrowing" the ActionExecutionDB model,
        # so we have to test with that model
        action = {'uid': action_1_db.get_uid(), 'pack': 'core'}
        inquiry_1_db = ActionExecutionDB(action=action,
                                         runner=runner,
                                         liveaction=liveaction,
                                         status=status)

        # A separate inquiry that has a parent (so we can test workflow permission inheritance)
        inquiry_2_db = ActionExecutionDB(action=action,
                                         runner=runner,
                                         liveaction=liveaction,
                                         status=status,
                                         parent=str(wf_exc_db.id))

        # A bit gross, but it's what we have to do since Inquiries
        # don't yet have their own data model
        def get_uid():
            return "inquiry"

        inquiry_1_db.get_uid = get_uid
        inquiry_2_db.get_uid = get_uid

        inquiry_1_db = ActionExecution.add_or_update(inquiry_1_db)
        inquiry_2_db = ActionExecution.add_or_update(inquiry_2_db)
        self.resources['inquiry_1'] = inquiry_1_db
        self.resources['inquiry_2'] = inquiry_2_db

        ############################################################
        # Create some mock roles with associated permission grants #
        ############################################################

        # Custom role - "inquiry_list" grant
        grant_db = PermissionGrantDB(
            resource_uid=self.resources['inquiry_1'].get_uid(),
            resource_type=ResourceType.INQUIRY,
            permission_types=[PermissionType.INQUIRY_LIST])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_list_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_list_grant'] = role_db

        # Custom role - "inquiry_view" grant
        grant_db = PermissionGrantDB(
            resource_uid=self.resources['inquiry_1'].get_uid(),
            resource_type=ResourceType.INQUIRY,
            permission_types=[PermissionType.INQUIRY_VIEW])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_view_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_view_grant'] = role_db

        # Custom role - "inquiry_respond" grant
        grant_db = PermissionGrantDB(
            resource_uid=self.resources['inquiry_1'].get_uid(),
            resource_type=ResourceType.INQUIRY,
            permission_types=[PermissionType.INQUIRY_RESPOND])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_respond_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_respond_grant'] = role_db

        # Custom role - "inquiry_all" grant
        grant_db = PermissionGrantDB(
            resource_uid=self.resources['inquiry_1'].get_uid(),
            resource_type=ResourceType.INQUIRY,
            permission_types=[PermissionType.INQUIRY_ALL])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_all_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_all_grant'] = role_db

        # Custom role - inheritance grant
        grant_db = PermissionGrantDB(
            resource_uid=self.resources['wf'].get_uid(),
            resource_type=ResourceType.ACTION,
            permission_types=[PermissionType.ACTION_EXECUTE])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_inherit',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_inherit'] = role_db

        #####################################
        # Create some mock role assignments #
        #####################################

        user_db = self.users['custom_role_inquiry_list_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_list_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_view_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_view_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_respond_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_respond_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_all_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_all_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_inherit']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_inherit'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)
예제 #2
0
    def setUp(self):
        super(InquiryPermissionsResolverTestCase, self).setUp()

        # Create some mock users
        user_1_db = UserDB(name='custom_role_inquiry_list_grant')
        user_1_db = User.add_or_update(user_1_db)
        self.users['custom_role_inquiry_list_grant'] = user_1_db

        user_2_db = UserDB(name='custom_role_inquiry_view_grant')
        user_2_db = User.add_or_update(user_2_db)
        self.users['custom_role_inquiry_view_grant'] = user_2_db

        user_3_db = UserDB(name='custom_role_inquiry_respond_grant')
        user_3_db = User.add_or_update(user_3_db)
        self.users['custom_role_inquiry_respond_grant'] = user_3_db

        user_4_db = UserDB(name='custom_role_inquiry_all_grant')
        user_4_db = User.add_or_update(user_4_db)
        self.users['custom_role_inquiry_all_grant'] = user_4_db

        user_5_db = UserDB(name='custom_role_inquiry_inherit')
        user_5_db = User.add_or_update(user_5_db)
        self.users['custom_role_inquiry_inherit'] = user_5_db

        # Create a workflow for testing inheritance of action_execute permission
        # to inquiry_respond permission
        wf_db = ActionDB(pack='examples', name='mistral-ask-basic', entry_point='',
                         runner_type={'name': 'mistral-v2'})
        wf_db = Action.add_or_update(wf_db)
        self.resources['wf'] = wf_db
        runner = {'name': 'mistral-v2'}
        liveaction = {'action': 'examples.mistral-ask-basic'}
        status = action_constants.LIVEACTION_STATUS_PAUSED

        # Spawn workflow
        action = {'uid': wf_db.get_uid(), 'pack': 'examples'}
        wf_exc_db = ActionExecutionDB(action=action, runner=runner, liveaction=liveaction,
                                      status=status)
        wf_exc_db = ActionExecution.add_or_update(wf_exc_db)

        # Create an Inquiry on which permissions can be granted
        action_1_db = ActionDB(pack='core', name='ask', entry_point='',
                               runner_type={'name': 'inquirer'})
        action_1_db = Action.add_or_update(action_1_db)
        self.resources['action_1'] = action_1_db
        runner = {'name': 'inquirer'}
        liveaction = {'action': 'core.ask'}
        status = action_constants.LIVEACTION_STATUS_PENDING

        # For now, Inquiries are "borrowing" the ActionExecutionDB model,
        # so we have to test with that model
        action = {'uid': action_1_db.get_uid(), 'pack': 'core'}
        inquiry_1_db = ActionExecutionDB(action=action, runner=runner, liveaction=liveaction,
                                         status=status)

        # A separate inquiry that has a parent (so we can test workflow permission inheritance)
        inquiry_2_db = ActionExecutionDB(action=action, runner=runner, liveaction=liveaction,
                                         status=status, parent=str(wf_exc_db.id))

        # A bit gross, but it's what we have to do since Inquiries
        # don't yet have their own data model
        def get_uid():
            return "inquiry"

        inquiry_1_db.get_uid = get_uid
        inquiry_2_db.get_uid = get_uid

        inquiry_1_db = ActionExecution.add_or_update(inquiry_1_db)
        inquiry_2_db = ActionExecution.add_or_update(inquiry_2_db)
        self.resources['inquiry_1'] = inquiry_1_db
        self.resources['inquiry_2'] = inquiry_2_db

        ############################################################
        # Create some mock roles with associated permission grants #
        ############################################################

        # Custom role - "inquiry_list" grant
        grant_db = PermissionGrantDB(resource_uid=self.resources['inquiry_1'].get_uid(),
                                     resource_type=ResourceType.INQUIRY,
                                     permission_types=[PermissionType.INQUIRY_LIST])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_list_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_list_grant'] = role_db

        # Custom role - "inquiry_view" grant
        grant_db = PermissionGrantDB(resource_uid=self.resources['inquiry_1'].get_uid(),
                                     resource_type=ResourceType.INQUIRY,
                                     permission_types=[PermissionType.INQUIRY_VIEW])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_view_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_view_grant'] = role_db

        # Custom role - "inquiry_respond" grant
        grant_db = PermissionGrantDB(resource_uid=self.resources['inquiry_1'].get_uid(),
                                     resource_type=ResourceType.INQUIRY,
                                     permission_types=[PermissionType.INQUIRY_RESPOND])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_respond_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_respond_grant'] = role_db

        # Custom role - "inquiry_all" grant
        grant_db = PermissionGrantDB(resource_uid=self.resources['inquiry_1'].get_uid(),
                                     resource_type=ResourceType.INQUIRY,
                                     permission_types=[PermissionType.INQUIRY_ALL])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_all_grant',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_all_grant'] = role_db

        # Custom role - inheritance grant
        grant_db = PermissionGrantDB(resource_uid=self.resources['wf'].get_uid(),
                                     resource_type=ResourceType.ACTION,
                                     permission_types=[PermissionType.ACTION_EXECUTE])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_db = RoleDB(name='custom_role_inquiry_inherit',
                         permission_grants=permission_grants)
        role_db = Role.add_or_update(role_db)
        self.roles['custom_role_inquiry_inherit'] = role_db

        #####################################
        # Create some mock role assignments #
        #####################################

        user_db = self.users['custom_role_inquiry_list_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_list_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_view_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_view_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_respond_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_respond_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_all_grant']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_all_grant'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        user_db = self.users['custom_role_inquiry_inherit']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role=self.roles['custom_role_inquiry_inherit'].name,
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)