def test_incorrect_implementations(self): """ test for exceptions """ # Trying to instantiate. with self.assertRaises(ImproperlyConfigured): Role() # Trying to use the methods on the abstract class. with self.assertRaises(ImproperlyConfigured): Role.get_class_name() # Trying to register another class but using # the same name. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(Advisor) # Trying to access the inherit mode of a # role class using inherit=False. with self.assertRaises(NotAllowed): roles.Advisor.get_inherit_mode() # Check if the role class using ALL_MODELS # actually get all models by your method. models_list = roles.Coordenator.get_models() self.assertEqual(models_list, apps.get_models())
def setUp(self): RoleManager.cleanup() RoleManager.register_role(Advisor) RoleManager.register_role(Coordenator) self.john = MyUser.objects.create(username='******') self.bob = MyUser.objects.create(username='******')
def autodiscover(): """ Find for Role class implementations on all apps in order to auto-register. """ from importlib import import_module from django.conf import settings from improved_permissions.roles import RoleManager try: # Check if the Permission model # is ready to be used. from django.contrib.auth.models import Permission Permission.objects.count() except: # pragma: no cover return # Remove all references about previous # role classes and erase all cache. RoleManager.cleanup() dip_cache().clear() # Looking for Role classes in "roles.py". module = get_config('MODULE', 'roles') for app in settings.INSTALLED_APPS: try: mod = import_module('%s.%s' % (app, module)) rc_list = [obj[1] for obj in inspect.getmembers(mod, is_role)] for roleclass in rc_list: RoleManager.register_role(roleclass) except ImportError: pass # Clear the cache again after # all registrations. dip_cache().clear()
def get_roleclass(role_class): """ Get the role class signature by string or by itself. """ from improved_permissions.roles import RoleManager roles_list = RoleManager.get_roles() if isinstance(role_class, str): # Trying to get the role class # via string representation. for role in roles_list: if role.get_class_name() == role_class: return role elif role_class in roles_list: # Already a Role class. return role_class raise RoleNotFound("'%s' is not a registered role class." % role_class)
def test_another_module(self): """ test if the config dictionary works fine """ # Testing module name. new_ipc = {'MODULE': 'other_roles'} with self.settings(IMPROVED_PERMISSIONS_SETTINGS=new_ipc): autodiscover() roles_list = RoleManager.get_roles() self.assertEqual(roles_list, [AnotherRole]) # Testing the case if the dictionary does not exists. with self.settings(IMPROVED_PERMISSIONS_SETTINGS=None): self.assertEqual(get_config('CACHE', 'new_default'), 'new_default') self.assertEqual(get_config('MODULE', 'new_roles'), 'new_roles') # Test the default cache prefix key self.assertEqual(get_config('CACHE_PREFIX_KEY', 'prefix'), 'dip') # Test the new cache prefix key via settings new_ipc = {'CACHE_PREFIX_KEY': 'django-improved-permissions-'} with self.settings(IMPROVED_PERMISSIONS_SETTINGS=new_ipc): self.assertEqual(get_config('CACHE_PREFIX_KEY', 'prefix'), 'django-improved-permissions-')
def setUp(self): RoleManager.cleanup() RoleManager.register_role(Advisor) RoleManager.register_role(Teacher) RoleManager.register_role(Secretary) RoleManager.register_role(SubCoordenator) RoleManager.register_role(Coordenator) RoleManager.register_role(UniqueOwner) self.john = MyUser.objects.create(username="******") self.bob = MyUser.objects.create(username="******") self.mike = MyUser.objects.create(username="******") self.julie = MyUser.objects.create(username="******") self.unique = UniqueTogether.objects.create(content='Some data')
def setUp(self): RoleManager.cleanup() RoleManager.register_role(roles.Advisor) RoleManager.register_role(roles.Coordenator)
def test_incorrect_implementations(self): """ test all exceptions on validate method """ # Trying to instantiate RoleManager. with self.assertRaises(ImproperlyConfigured): RoleManager() # Trying to register a random object. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(object) # Role class without "verbose_name". with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(NoVerboseNameRole) # Role class without "models". with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(NoModelRole1) # Role class with "models" as no list. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(NoModelRole2) # Role class with "models" as list of random things. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(NoModelRole3) # Role class without any deny or allow. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(NoAllowDenyRole) # Role class with "deny" defined incorrectly. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(WrongDenyRole) # Registering the role classes correctly. RoleManager.register_role(Advisor) # Trying to register Advisor again. with self.assertRaises(ImproperlyConfigured): RoleManager.register_role(Advisor) # Checking list. self.assertEqual(RoleManager.get_roles(), [Advisor])
def setUp(self): RoleManager.cleanup()
def setUp(self): self.john = MyUser.objects.create(username='******') self.bob = MyUser.objects.create(username='******') self.mike = MyUser.objects.create(username='******') self.library = Library.objects.create(title='Cool Library') self.book = Book.objects.create(title='Very Nice Book 1', library=self.library) self.chapter = Chapter.objects.create(title='Very Nice Chapter 1', book=self.book) self.paragraph = Paragraph.objects.create(content='Such Text 1', chapter=self.chapter) self.another_book = Book.objects.create(title='Very Nice Book 2', library=self.library) RoleManager.cleanup() RoleManager.register_role(Author) RoleManager.register_role(Advisor) RoleManager.register_role(Coordenator) RoleManager.register_role(Reviewer) RoleManager.register_role(LibraryOwner) RoleManager.register_role(LibraryWorker) dip_cache().clear()