I want the method_decorator to be overwritten whenever my Test is called

  Kiến thức lập trình

This is my test function that i am using to test this is the solution i get to patch the method_decorator but its not working for me

I am not able to get into view because there is a decorator above the class that is wrapping the class and i am not able to patch or override it

This is my TestCase code

class ClientActivityTestCase(APITestCase):
    def mock_decorator(*args, **kwargs):
        """Decorate by doing nothing."""

        def decorator(f):
            @wraps(f)
            def decorated_function(*args, **kwargs):
                return f(*args, **kwargs)

            return decorated_function

        return decorator

    # PATCH THE DECORATOR HERE
    @patch('adminportal.views.super_admin_login_required', mock_decorator)
    def test_client_activity_view_get(self):
        response = self.client.get(reverse("client_activity_report"), HTTP_ORIGIN=settings.HOST_NAME, format='json')
        print(response)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn("Client Activity Report", response.content.decode())

and this is my code in view

@method_decorator(super_admin_login_required, name="dispatch")
class ClientActivityReport(View):
    def get(self, request):
        form = ClientColumnForm(
            initial={
                "columns": [
                    "organization",
                    "location",
                    "client",
                    "leads",
                    "organic",
                    "trials",
                    "zoho",
                    "website_status",
                    # "keyword",
                    # "ranking",
                ]
            }
        )
        return render(
            request,
            "reports/client_activity_report.html",
            {
                "three_months": get_last_three_months(),
                "countries": State.objects.filter(
                    parent_state__isnull=True
                ).values_list("name", flat=True),
                "orders": [
                    "Leads",
                    "Trials",
                    "Organic Leads",
                    "Target Location",
                    "Organization Name",
                ],
                "form": form,
            },
        )

This is the code of my permission class

def super_admin_login_required(
    function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None
):
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log-in page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_superuser
        and (not u.is_anonymous)
        and (u.get_role_display() == "superadmin"),
        login_url=login_url,
        redirect_field_name=redirect_field_name,
    )
    if function:
        return actual_decorator(function)
    return actual_decorator

3

LEAVE A COMMENT