def test_creates_when_does_not_exists(self):
        user = self.user

        class Role4(AbstractUserRole):
            available_permissions = {
                'the_permission': True,
            }
        RolesManager.register_role(Role4)

        Role4.assign_role_to_user(user)

        Role4.available_permissions = {
            'the_permission': True,
            'new_permission': True,
        }

        permissions = get_user_permissions(user)

        self.assertIn('the_permission', permissions)
        self.assertIn('new_permission', permissions)
        self.assertTrue(permissions['the_permission'])
        self.assertTrue(permissions['new_permission'])
        self.assertEquals(len(permissions), 2)

        new_permission = UserPermission.objects.get(user=user, permission_name='new_permission')

        self.assertTrue(new_permission.is_granted)
    def setUp(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)

        self.user = mommy.make(get_user_model())

        self.factory = RequestFactory()

        self.request = self.factory.get('/')
        self.request.session = {}
        self.request.user = self.user
def load_roles_and_permissions():
    roles_module = import_module(settings.ROLEPERMISSIONS_MODULE)

    RolesManager._roles = {}
    PermissionsManager._checkers = {}

    for name, cls in roles_module.__dict__.items():
        if inspect.isclass(cls) and issubclass(cls, AbstractUserRole) and cls is not AbstractUserRole:
            RolesManager.register_role(cls)
            # print 'Loading role ' + cls().get_name()

    for app_name in settings.INSTALLED_APPS:
        if app_name is not 'rolepermissions':
            try:
                import_module('.permissions', app_name)
            except ImportError as exc:
                pass
    def test_creates_permission_when_not_existent(self):
        user = self.user

        class Role4(AbstractUserRole):
            available_permissions = {
                'the_permission': True
            }
        RolesManager.register_role(Role4)

        Role4.assign_role_to_user(user)

        Role4.available_permissions = { 'different_one': True }

        self.assertTrue(has_permission(user, 'different_one'))

        permission = UserPermission.objects.get(user=user, permission_name='different_one')

        self.assertTrue(permission.is_granted)
    def setUp(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)
        RolesManager.register_role(Role3)

        self.user = mommy.make(get_user_model())
        self.user_role = Role2.assign_role_to_user(self.user)
    def setUp(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)
        RolesManager.register_role(Role3)

        self.user = mommy.make(get_user_model())

        Role1.assign_role_to_user(self.user)

        @register_object_checker()
        def obj_checker(role, user, obj):
            return obj and True
    def test_retrieve_role(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)

        self.assertEquals(RolesManager.retrieve_role('role1'), Role1)
        self.assertEquals(RolesManager.retrieve_role('role2'), Role2)
    def test_get_roles(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)

        self.assertTrue(isinstance(RolesManager.get_roles(), dict))
        self.assertEquals(len(RolesManager.get_roles()), 2)
    def test_registering_role(self):
        self.assertNotIn('role1', RolesManager._roles)

        RolesManager.register_role(Role1)

        self.assertIn('role1', RolesManager._roles)
    def setUp(self):
        RolesManager.register_role(Role1)
        RolesManager.register_role(Role2)
        RolesManager.register_role(Role3)

        self.user = mommy.make(get_user_model())
 def setUp(self):
     RolesManager.register_role(Role1)
     RolesManager.register_role(Role2)
     RolesManager.register_role(Role3)