예제 #1
0
    def test_to_dict(self):
        change_role_object = story_domain.StoryRightsChange({
            'cmd':
            story_domain.CMD_CHANGE_ROLE,
            'assignee_id':
            'assignee_id',
            'new_role':
            'new_role',
            'old_role':
            'old_role'
        })

        expected_dict = {
            'cmd': story_domain.CMD_CHANGE_ROLE,
            'assignee_id': 'assignee_id',
            'new_role': 'new_role',
            'old_role': 'old_role'
        }

        self.assertDictEqual(expected_dict, change_role_object.to_dict())

        cmd_list = [
            story_domain.CMD_CREATE_NEW, story_domain.CMD_PUBLISH_STORY,
            story_domain.CMD_UNPUBLISH_STORY
        ]

        for cmd in cmd_list:
            cmd_object = story_domain.StoryRightsChange({'cmd': cmd})
            expected_dict = {'cmd': cmd}
            self.assertDictEqual(expected_dict, cmd_object.to_dict())
예제 #2
0
    def test_initializations(self):
        with self.assertRaisesRegexp(
                Exception, 'Invalid change_dict: '
                '{\'invalid_key\': \'invalid_value\'}'):
            story_domain.StoryRightsChange({'invalid_key': 'invalid_value'})

        change_role_object = story_domain.StoryRightsChange({
            'cmd':
            story_domain.CMD_CHANGE_ROLE,
            'assignee_id':
            'assignee_id',
            'new_role':
            'new_role',
            'old_role':
            'old_role'
        })

        self.assertEqual(change_role_object.cmd, story_domain.CMD_CHANGE_ROLE)
        self.assertEqual(change_role_object.assignee_id, 'assignee_id')
        self.assertEqual(change_role_object.new_role, 'new_role')
        self.assertEqual(change_role_object.old_role, 'old_role')

        cmd_list = [
            story_domain.CMD_CREATE_NEW, story_domain.CMD_PUBLISH_STORY,
            story_domain.CMD_UNPUBLISH_STORY
        ]

        for cmd in cmd_list:
            cmd_object = story_domain.StoryRightsChange({'cmd': cmd})
            self.assertEqual(cmd, cmd_object.cmd)

        with self.assertRaisesRegexp(
                Exception, 'Invalid change_dict: '
                '{\'cmd\': \'invalid_command\'}'):
            story_domain.StoryRightsChange({'cmd': 'invalid_command'})
예제 #3
0
def unpublish_story(story_id, committer_id):
    """Marks the given story as unpublished.

    Args:
        story_id: str. The id of the given story.
        committer_id: str. ID of the committer.

    Raises:
        Exception. The given story does not exist.
        Exception. The story is already unpublished.
        Exception. The user does not have enough rights to unpublish the story.
    """
    story_rights = get_story_rights(story_id, strict=False)
    if story_rights is None:
        raise Exception('The given story does not exist')
    user = user_services.UserActionsInfo(committer_id)
    if role_services.ACTION_CHANGE_STORY_STATUS not in user.actions:
        raise Exception(
            'The user does not have enough rights to unpublish the story.')

    if not story_rights.story_is_published:
        raise Exception('The story is already unpublished.')
    story_rights.story_is_published = False
    commit_cmds = [
        story_domain.StoryRightsChange(
            {'cmd': story_domain.CMD_UNPUBLISH_STORY})
    ]
    save_story_rights(story_rights, committer_id, 'Unpublished the story',
                      commit_cmds)
예제 #4
0
 def test_story_rights_change_object_with_extra_attribute_in_cmd(self):
     with self.assertRaisesRegexp(
             utils.ValidationError,
         ('The following extra attributes are present: invalid')):
         story_domain.StoryRightsChange({
             'cmd': 'publish_story',
             'invalid': 'invalid'
         })
예제 #5
0
 def test_story_rights_change_object_with_missing_attribute_in_cmd(self):
     with self.assertRaisesRegexp(
             utils.ValidationError,
         ('The following required attributes are missing: '
          'new_role, old_role')):
         story_domain.StoryRightsChange({
             'cmd': 'change_role',
             'assignee_id': 'assignee_id',
         })
예제 #6
0
def publish_story(story_id, committer_id):
    """Marks the given story as published.

    Args:
        story_id: str. The id of the given story.
        committer_id: str. ID of the committer.

    Raises:
        Exception. The given story does not exist.
        Exception. The story is already published.
        Exception. The user does not have enough rights to publish the story.
    """
    def _are_nodes_valid_for_publishing(story_nodes):
        exploration_id_list = []
        for node in story_nodes:
            if not node.exploration_id:
                raise Exception('Story node with id %s does not contain an '
                                'exploration id.' % node.id)
            exploration_id_list.append(node.exploration_id)
        for exploration in exp_services.get_multiple_explorations_by_id(
                exploration_id_list):
            if exploration is None:
                raise Exception('Exploration id %s doesn\'t exist.' %
                                exploration.id)
        multiple_exploration_rights = (
            rights_manager.get_multiple_exploration_rights_by_ids(
                exploration_id_list))
        for exploration_rights in multiple_exploration_rights:
            if exploration_rights.is_private():
                raise Exception('Exploration with id %s isn\'t published.' %
                                exploration_rights.id)

    story_rights = get_story_rights(story_id, strict=False)
    if story_rights is None:
        raise Exception('The given story does not exist')
    user = user_services.UserActionsInfo(committer_id)
    if role_services.ACTION_CHANGE_STORY_STATUS not in user.actions:
        raise Exception(
            'The user does not have enough rights to publish the story.')

    if story_rights.story_is_published:
        raise Exception('The story is already published.')

    story = get_story_by_id(story_id, strict=False)
    for node in story.story_contents.nodes:
        if node.id == story.story_contents.initial_node_id:
            _are_nodes_valid_for_publishing([node])

    story_rights.story_is_published = True
    commit_cmds = [
        story_domain.StoryRightsChange({'cmd': story_domain.CMD_PUBLISH_STORY})
    ]
    save_story_rights(story_rights, committer_id, 'Published the story',
                      commit_cmds)
예제 #7
0
 def test_to_dict(self):
     story_rights_change_dict = {
         'cmd': 'change_role',
         'assignee_id': 'assignee_id',
         'old_role': story_domain.ROLE_NONE,
         'new_role': story_domain.ROLE_MANAGER
     }
     story_rights_change_object = story_domain.StoryRightsChange(
         story_rights_change_dict)
     self.assertEqual(story_rights_change_object.to_dict(),
                      story_rights_change_dict)
예제 #8
0
 def test_story_rights_change_object_with_invalid_role(self):
     with self.assertRaisesRegexp(utils.ValidationError,
                                  ('Value for old_role in cmd change_role: '
                                   'invalid is not allowed')):
         story_domain.StoryRightsChange({
             'cmd':
             'change_role',
             'assignee_id':
             'assignee_id',
             'old_role':
             'invalid',
             'new_role':
             story_domain.ROLE_MANAGER
         })
예제 #9
0
    def test_story_rights_change_object_with_change_role(self):
        story_rights_change_object = story_domain.StoryRightsChange({
            'cmd':
            'change_role',
            'assignee_id':
            'assignee_id',
            'old_role':
            story_domain.ROLE_NONE,
            'new_role':
            story_domain.ROLE_MANAGER
        })

        self.assertEqual(story_rights_change_object.cmd, 'change_role')
        self.assertEqual(story_rights_change_object.assignee_id, 'assignee_id')
        self.assertEqual(story_rights_change_object.old_role,
                         story_domain.ROLE_NONE)
        self.assertEqual(story_rights_change_object.new_role,
                         story_domain.ROLE_MANAGER)
예제 #10
0
def assign_role(committer, assignee, new_role, story_id):
    """Assigns a new role to the user.

    Args:
        committer: UserActionsInfo. UserActionsInfo object for the user
            who is performing the action.
        assignee: UserActionsInfo. UserActionsInfo object for the user
            whose role is being changed.
        new_role: str. The name of the new role. Possible values are:
            ROLE_MANAGER
        story_id: str. ID of the story.

    Raises:
        Exception. The committer does not have rights to modify a role.
        Exception. The assignee is already a manager for the story.
        Exception. The assignee doesn't have enough rights to become a manager.
        Exception. The role is invalid.
    """
    committer_id = committer.user_id
    story_rights = get_story_rights(story_id)
    if (role_services.ACTION_MODIFY_ROLES_FOR_ANY_ACTIVITY
            not in committer.actions):
        logging.error('User %s tried to allow user %s to be a %s of story %s '
                      'but was refused permission.' %
                      (committer_id, assignee.user_id, new_role, story_id))
        raise Exception(
            'UnauthorizedUserException: Could not assign new role.')

    assignee_username = user_services.get_username(assignee.user_id)
    if role_services.ACTION_EDIT_OWNED_STORY not in assignee.actions:
        raise Exception(
            'The assignee doesn\'t have enough rights to become a manager.')

    old_role = story_domain.ROLE_NONE
    if story_rights.is_manager(assignee.user_id):
        old_role = story_domain.ROLE_MANAGER

    if new_role == story_domain.ROLE_MANAGER:
        if story_rights.is_manager(assignee.user_id):
            raise Exception('This user already is a manager for this story')
        story_rights.manager_ids.append(assignee.user_id)
    elif new_role == story_domain.ROLE_NONE:
        if story_rights.is_manager(assignee.user_id):
            story_rights.manager_ids.remove(assignee.user_id)
        else:
            raise Exception('This user already has no role for this story')
    else:
        raise Exception('Invalid role: %s' % new_role)

    commit_message = 'Changed role of %s from %s to %s' % (assignee_username,
                                                           old_role, new_role)
    commit_cmds = [
        story_domain.StoryRightsChange({
            'cmd': story_domain.CMD_CHANGE_ROLE,
            'assignee_id': assignee.user_id,
            'old_role': old_role,
            'new_role': new_role
        })
    ]

    save_story_rights(story_rights, committer_id, commit_message, commit_cmds)
예제 #11
0
    def test_story_rights_change_object_with_unpublish_story(self):
        story_rights_change_object = story_domain.StoryRightsChange(
            {'cmd': 'unpublish_story'})

        self.assertEqual(story_rights_change_object.cmd, 'unpublish_story')
예제 #12
0
    def test_story_rights_change_object_with_create_new(self):
        story_rights_change_object = story_domain.StoryRightsChange(
            {'cmd': 'create_new'})

        self.assertEqual(story_rights_change_object.cmd, 'create_new')
예제 #13
0
 def test_story_change_rights_object_with_invalid_cmd(self):
     with self.assertRaisesRegexp(utils.ValidationError,
                                  'Command invalid is not allowed'):
         story_domain.StoryRightsChange({'cmd': 'invalid'})
예제 #14
0
 def test_story_rights_change_object_with_missing_cmd(self):
     with self.assertRaisesRegexp(utils.ValidationError,
                                  'Missing cmd key in change dict'):
         story_domain.StoryRightsChange({'invalid': 'data'})