Best Python code snippet using Kiwi_python
missing_permissions.py
Source:missing_permissions.py  
1# Copyright (c) 2019 Alexander Todorov <atodorov@MrSenko.com>2# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html3import astroid4from pylint import interfaces5from pylint import checkers6class MissingPermissionsChecker(checkers.BaseChecker):7    """8        Will inspect functions and classes inside a views.py module for the9        presence of permissions decorator. May generate lots of false negatives10        but we're ok with that. Better inspect than forget to add permission11        decorator!12    """13    allowed_decorators = [14        'permission_required',15        'object_permission_required',16    ]17    inside_views_module = False18    __implements__ = (interfaces.IAstroidChecker,)19    name = 'mising-permissions-checker'20    msgs = {'R4511': ("View is missing @permission_required decorator!",21                      'missing-permission-required',22                      "All views must require permissions!")}23    def visit_module(self, node):24        self.inside_views_module = node.name.endswith('.views')25    def visit_functiondef(self, node):26        if not self.inside_views_module:27            return28        arg0 = None29        if node.args.args:30            arg0 = node.args.args[0]31        if arg0 and arg0.name != 'request':32            return33        # this function is a confirmed view so start checking34        self._check_for_missing_decorator(node)35    def visit_classdef(self, node):36        if not self.inside_views_module:37            return38        # class based views always inherit from something39        # tip: we can be more precise what base classes are allowed in order40        # to identify cbv more correctly! Leaving it like that for now and will41        # revisit later if we start to see many false negatives!42        if not node.bases:43            return44        # for now we check all classes in views.py modules45        # until we learn how to recognize class based views46        self._check_for_missing_decorator(node)47    def _check_for_missing_decorator(self, node):48        if not isinstance(node.parent, astroid.scoped_nodes.Module):49            return50        if not node.decorators:51            self.add_message('missing-permission-required', node=node)52            return53        found_permissions_required = False54        for decorator in node.decorators.nodes:55            if isinstance(decorator, astroid.Call):56                if decorator.func.name in self.allowed_decorators:57                    found_permissions_required = True58                    break59                if decorator.func.name == 'method_decorator' and \60                        isinstance(decorator.args[0], astroid.Call) and \61                        decorator.args[0].func.name in self.allowed_decorators:62                    found_permissions_required = True63                    break64        if not found_permissions_required:65            self.add_message('missing-permission-required', node=node)66class MissingAPIPermissionsChecker(checkers.BaseChecker):67    """68        Will inspect API functions for the presence of permissions decorator!69    """70    __implements__ = (interfaces.IAstroidChecker,)71    name = 'mising-api-permissions-checker'72    msgs = {'R4512': ("API function is missing @permissions_required decorator!",73                      'missing-api-permissions-required',74                      "All API functions must require permissions!")}75    def visit_functiondef(self, node):76        # API functions always have @rpc_method decorator77        if not node.decorators:78            return79        is_api_function = False80        for decorator in node.decorators.nodes:81            if isinstance(decorator, astroid.Call) and \82                    isinstance(decorator.func, astroid.Name) and \83                    decorator.func.name == 'rpc_method':84                is_api_function = True85                break86        if not is_api_function:87            return88        found_permissions_required = False89        for decorator in node.decorators.nodes:90            if isinstance(decorator, astroid.Call) and \91                    isinstance(decorator.func, astroid.Name) and \92                    decorator.func.name == 'permissions_required':93                found_permissions_required = True94                break95            if isinstance(decorator, astroid.Name) and \96                    decorator.name == 'http_basic_auth_login_required':97                found_permissions_required = True98                break99        if not found_permissions_required:...api_distinct.py
Source:api_distinct.py  
...22        self.distinct_found = False23        # for now only check filter() functions24        if node.name != "filter":25            return26        if not is_api_function(node):27            return28        self.current_api_method = node29    def visit_attribute(self, node):30        if node.attrname == "distinct" and node.frame() is self.current_api_method:31            self.distinct_found = True32    def leave_functiondef(self, node):33        # no API method in scope34        if not self.current_api_method:35            return36        if not self.distinct_found:...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!!
