Best Python code snippet using avocado_python
permissions.py
Source:permissions.py
...34 list_perms = None35 def __init__(self, request, view):36 self.request = request37 self.view = view38 def check_permissions(self, action:str, obj:object=None):39 permset = getattr(self, "{}_perms".format(action))40 if isinstance(permset, (list, tuple)):41 permset = reduce(lambda acc, v: acc & v, permset)42 elif permset is None:43 # Use empty operator that always return true with44 # empty components.45 permset = And()46 elif isinstance(permset, PermissionComponent):47 # Do nothing48 pass49 elif inspect.isclass(permset) and issubclass(permset, PermissionComponent):50 permset = permset()51 else:52 raise RuntimeError(_("Invalid permission definition."))53 if self.global_perms:54 permset = (self.global_perms & permset)55 if self.enough_perms:56 permset = (self.enough_perms | permset)57 return permset.check_permissions(request=self.request,58 view=self.view,59 obj=obj)60class PermissionComponent(object, metaclass=abc.ABCMeta):61 @abc.abstractmethod62 def check_permissions(self, request, view, obj=None):63 pass64 def __invert__(self):65 return Not(self)66 def __and__(self, component):67 return And(self, component)68 def __or__(self, component):69 return Or(self, component)70class PermissionOperator(PermissionComponent):71 """72 Base class for all logical operators for compose73 components.74 """75 def __init__(self, *components):76 self.components = tuple(components)77class Not(PermissionOperator):78 """79 Negation operator as permission composable component.80 """81 # Overwrites the default constructor for fix82 # to one parameter instead of variable list of them.83 def __init__(self, component):84 super().__init__(component)85 def check_permissions(self, *args, **kwargs):86 component = self.components[0]87 return (not component.check_permissions(*args, **kwargs))88class Or(PermissionOperator):89 """90 Or logical operator as permission component.91 """92 def check_permissions(self, *args, **kwargs):93 valid = False94 for component in self.components:95 if component.check_permissions(*args, **kwargs):96 valid = True97 break98 return valid99class And(PermissionOperator):100 """101 And logical operator as permission component.102 """103 def check_permissions(self, *args, **kwargs):104 valid = True105 for component in self.components:106 if not component.check_permissions(*args, **kwargs):107 valid = False108 break109 return valid110######################################################################111# Generic components.112######################################################################113class AllowAny(PermissionComponent):114 def check_permissions(self, request, view, obj=None):115 return True116class DenyAll(PermissionComponent):117 def check_permissions(self, request, view, obj=None):118 return False119class IsAuthenticated(PermissionComponent):120 def check_permissions(self, request, view, obj=None):121 return request.user and request.user.is_authenticated122class IsSuperUser(PermissionComponent):123 def check_permissions(self, request, view, obj=None):124 return request.user and request.user.is_authenticated and request.user.is_superuser125class HasProjectPerm(PermissionComponent):126 def __init__(self, perm, *components):127 self.project_perm = perm128 super().__init__(*components)129 def check_permissions(self, request, view, obj=None):130 return user_has_perm(request.user, self.project_perm, obj)131class IsProjectAdmin(PermissionComponent):132 def check_permissions(self, request, view, obj=None):133 return is_project_admin(request.user, obj)134class IsObjectOwner(PermissionComponent):135 def check_permissions(self, request, view, obj=None):136 if obj.owner is None:137 return False138 return obj.owner == request.user139######################################################################140# Generic permissions.141######################################################################142class AllowAnyPermission(ResourcePermission):143 enough_perms = AllowAny()144class IsAuthenticatedPermission(ResourcePermission):145 enough_perms = IsAuthenticated()146class TaigaResourcePermission(ResourcePermission):...
acl_test.py
Source:acl_test.py
...3from nose.tools import eq_4class TestACL(unittest.TestCase):5 def test_admin_user(self):6 user = {"type": "admin"}7 result = check_permissions(False,'/something', user)8 eq_(True, result)9 # Page with id10 result = check_permissions('1234','/something', user)11 eq_(True, result)12 # Settings13 result = check_permissions('/settings','/settings', user)14 eq_(True, result)15 def test_readonly_user(self):16 user = {"type": "readonly", "apps":["app1", "app2"], "servers":["server1", "server2"]}17 result = check_permissions(False,'/system', user) # Parent pages - id is False18 eq_(True, result)19 result = check_permissions(False,'/', user) # Parent pages - id is False20 eq_(True, result)21 result = check_permissions(False,'/logs', user) # Parent pages - id is False22 eq_(True, result)23 result = check_permissions(False,'/something', user) # Parent pages - id is False24 eq_(True, result)25 result = check_permissions('server1','/system', user) # Page with server id26 eq_(True, result)27 result = check_permissions('serverdummy','/system', user) # Page with invalid server id28 eq_(False, result)29 30 result = check_permissions('server2','/processes', user) # Page with server id31 eq_(True, result)32 result = check_permissions('serverdummy','/processes', user) # Page with invalid server id33 eq_(False, result)34 result = check_permissions('app1','/logs', user) # Page with app id35 eq_(True, result)36 result = check_permissions('appdummy','/logs', user) # Page with invalid app id37 eq_(False, result)38 39 result = check_permissions('app2','/exceptions', user) # Page with app id40 eq_(True, result)41 result = check_permissions('appdummy','/exceptions', user) # Page with invalid app id42 eq_(False, result)43 result = check_permissions(False,'/settings', user) # Settings module44 eq_(False, result)45 result = check_permissions(False,'/settings/servers', user) # Settings module46 eq_(False, result)47 result = check_permissions(False,'/settings/users', user) # Settings module48 eq_(False, result)49 def test_filtered_apps_for_user(self):50 51 admin_user = {'type': 'admin'}52 result = all_apps_for_user(admin_user)53 eq_('all', result)54 readonly_user = {'type': 'readonly', 'apps': ['all']}55 result = all_apps_for_user(admin_user)56 eq_('all', result)57 readonly_user = {'type': 'readonly', 'apps': [1,2]}58 result = all_apps_for_user(readonly_user)59 eq_(result,[1, 2])60 readonly_user = {'type': 'readonly', 'apps': []}61 result = all_apps_for_user(readonly_user)...
test_base_api_permissions.py
Source:test_base_api_permissions.py
...15# along with this program. If not, see <http://www.gnu.org/licenses/>.16from taiga.base.api.permissions import (AllowAny as TruePermissionComponent,17 DenyAll as FalsePermissionComponent)18def test_permission_component_composition():19 assert (TruePermissionComponent() | TruePermissionComponent()).check_permissions(None, None, None)20 assert (TruePermissionComponent() | FalsePermissionComponent()).check_permissions(None, None, None)21 assert (FalsePermissionComponent() | TruePermissionComponent()).check_permissions(None, None, None)22 assert not (FalsePermissionComponent() | FalsePermissionComponent()).check_permissions(None, None, None)23 assert (TruePermissionComponent() & TruePermissionComponent()).check_permissions(None, None, None)24 assert not (TruePermissionComponent() & FalsePermissionComponent()).check_permissions(None, None, None)25 assert not (FalsePermissionComponent() & TruePermissionComponent()).check_permissions(None, None, None)26 assert not (FalsePermissionComponent() & FalsePermissionComponent()).check_permissions(None, None, None)27 assert (~FalsePermissionComponent()).check_permissions(None, None, None)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!