Пример #1
0
    def test_action_argument_is_skipped_will_raise_a_TypeError(self):
        """testing if a TypeError will be raised when the action argument is
        skipped
        """
        self.kwargs.pop('action')
        with self.assertRaises(TypeError) as cm:
            Permission(**self.kwargs)

        self.assertEqual(str(cm.exception),
                         '__init__() takes exactly 4 arguments (3 given)')
Пример #2
0
    def test_class_name_argument_skipped(self):
        """testing if a TypeError will be raised when the class_name argument
        is skipped
        """
        self.kwargs.pop('class_name')
        with self.assertRaises(TypeError) as cm:
            Permission(**self.kwargs)

        self.assertEqual(str(cm.exception),
                         '__init__() takes exactly 4 arguments (3 given)')
Пример #3
0
    def test_access_argument_accepts_only_Allow_or_Deny_as_value(self):
        """testing if a ValueError will be raised when the value of access is
        something other than 'Allow' or 'Deny'
        """
        self.kwargs['access'] = 'Allowed'
        with pytest.raises(ValueError) as cm:
            Permission(**self.kwargs)

        assert str(cm.value) == \
            'Permission.access should be "Allow" or "Deny" not Allowed'
Пример #4
0
    def setUp(self):
        """setup the test
        """
        self.kwargs = {
            'access': 'Allow',
            'action': 'Create',
            'class_name': 'Project'
        }

        self.test_permission = Permission(**self.kwargs)
Пример #5
0
    def test_access_argument_is_None(self):
        """testing if a TypeError will be raised when the access argument is
        None
        """
        self.kwargs['access'] = None
        with pytest.raises(TypeError) as cm:
            Permission(**self.kwargs)

        assert str(cm.value) == \
            'Permission.access should be an instance of str not NoneType'
Пример #6
0
    def test_class_name_argument_is_not_a_string(self):
        """testing if a TypeError will be raised when the class_name argument
        is not a string instance
        """
        self.kwargs['class_name'] = 10
        with pytest.raises(TypeError) as cm:
            Permission(**self.kwargs)

        assert str(cm.value) == \
            'Permission.class_name should be an instance of str not int'
Пример #7
0
    def test_action_argument_is_None(self):
        """testing if a TypeError will be raised when the action argument is
        set to None
        """
        self.kwargs['action'] = None
        with self.assertRaises(TypeError) as cm:
            Permission(**self.kwargs)

        self.assertEqual(
            str(cm.exception),
            'Permission.action should be an instance of str not NoneType')
Пример #8
0
    def test_class_name_argument_is_not_a_string(self):
        """testing if a TypeError will be raised when the class_name argument
        is not a string instance
        """
        self.kwargs['class_name'] = 10
        with self.assertRaises(TypeError) as cm:
            Permission(**self.kwargs)

        self.assertEqual(
            str(cm.exception),
            'Permission.class_name should be an instance of str not int')
Пример #9
0
    def test_action_argument_accepts_default_values_only(self):
        """testing if a ValueError will be raised when the action argument is
        not in the list of defaults.DEFAULT_ACTIONS
        """
        self.kwargs['action'] = 'Add'
        with pytest.raises(ValueError) as cm:
            Permission(**self.kwargs)

        assert str(cm.value) == \
            "Permission.action should be one of the values of ['Create', " \
            "'Read', 'Update', 'Delete', 'List'] not 'Add'"
Пример #10
0
    def setUp(self):
        """setup the test
        """
        db.setup()

        # create permissions
        self.test_perm1 = Permission(access='Allow',
                                     action='Create',
                                     class_name='Something')
        self.test_instance = TestClassForACL()
        self.test_instance.name = 'Test'
        self.test_instance.permissions.append(self.test_perm1)
Пример #11
0
    def test_action_argument_is_skipped_will_raise_a_TypeError(self):
        """testing if a TypeError will be raised when the action argument is
        skipped
        """
        self.kwargs.pop('action')
        with pytest.raises(TypeError) as cm:
            Permission(**self.kwargs)

        if sys.version_info[0] >= 3:
            assert str(cm.value) == \
                "__init__() missing 1 required positional argument: 'action'"
        else:
            if sys.version_info[1] == 7:
                # Python 2.7
                assert str(cm.value) == \
                    '__init__() takes exactly 4 arguments (3 given)'
            else:
                # Python 2.6
                assert str(cm.value) == \
                    '__init__() takes exactly 4 non-keyword arguments ' \
                    '(2 given)'
Пример #12
0
    def test_class_name_argument_skipped(self):
        """testing if a TypeError will be raised when the class_name argument
        is skipped
        """
        self.kwargs.pop('class_name')
        with pytest.raises(TypeError) as cm:
            Permission(**self.kwargs)

        if sys.version_info[0] >= 3:
            assert str(cm.value) == \
                "__init__() missing 1 required positional argument: " \
                "'class_name'"
        else:
            if sys.version_info[1] == 7:
                # Pythone 2.7
                assert str(cm.value) == \
                    '__init__() takes exactly 4 arguments (3 given)'
            else:
                # Pythone 2.6
                assert str(cm.value) == \
                    '__init__() takes exactly 4 non-keyword arguments (3 ' \
                    'given)'
Пример #13
0
def register(class_):
    """Registers the given class to the database.

    It is mainly used to create the :class:`.Action`\ s needed for the
    :class:`.User`\ s and :class:`.Group`\ s to be able to interact with the
    given class. Whatever class you have created needs to be registered.

    Example, lets say that you have a data class which is specific to your
    studio and it is not present in Stalker Object Model (SOM), so you need to
    extend SOM with a new data type. Here is a simple Data class inherited from
    the :class:`.SimpleEntity` class (which is the simplest class you should
    inherit your classes from or use more complex classes down to the
    hierarchy)::

      from sqlalchemy import Column, Integer, ForeignKey
      from stalker.models.entity import SimpleEntity

      class MyDataClass(SimpleEntity):
        '''This is an example class holding a studio specific data which is not
        present in SOM.
        '''

        __tablename__ = 'MyData'
        __mapper_arguments__ = {'polymorphic_identity': 'MyData'}

        my_data_id = Column('id', Integer, ForeignKey('SimpleEntities.c.id'),
                            primary_key=True)

    Now because Stalker is using Pyramid authorization mechanism it needs to be
    able to have an :class:`.Permission` about your new class, so you can
    assign this :class;`.Permission` to your :class:`.User`\ s or
    :class:`.Group`\ s. So you ned to register your new class with
    :func:`stalker.db.register` like shown below::

      from stalker import db
      db.register(MyDataClass)

    This will create the necessary Actions in the 'Actions' table on your
    database, then you can create :class:`.Permission`\ s and assign these to
    your :class:`.User`\ s and :class:`.Group`\ s so they are Allowed or Denied
    to do the specified Action.

    :param class_: The class itself that needs to be registered.
    """
    from stalker.models.auth import Permission

    # create the Permissions
    permissions_db = Permission.query.all()

    if not isinstance(class_, type):
        raise TypeError('To register a class please supply the class itself.')

    # register the class name to entity_types table
    from stalker import (EntityType, StatusMixin, DateRangeMixin,
                         ReferenceMixin, ScheduleMixin)

    class_name = class_.__name__

    if not EntityType.query.filter_by(name=class_name).first():
        new_entity_type = EntityType(class_name)
        # update attributes
        if issubclass(class_, StatusMixin):
            new_entity_type.statusable = True
        if issubclass(class_, DateRangeMixin):
            new_entity_type.dateable = True
        if issubclass(class_, ScheduleMixin):
            new_entity_type.schedulable = True
        if issubclass(class_, ReferenceMixin):
            new_entity_type.accepts_references = True

        DBSession.add(new_entity_type)

    for action in defaults.actions:
        for access in ['Allow', 'Deny']:
            permission_obj = Permission(access, action, class_name)
            if permission_obj not in permissions_db:
                DBSession.add(permission_obj)

    try:
        DBSession.commit()
    except IntegrityError:
        DBSession.rollback()