python 3.x - how to test a django decorator? -
i have decorator can verify if user has permission. code working me want write test it. how can test any_permission_required function?
from django.contrib.auth.decorators import user_passes_test def any_permission_required(*perms): return user_passes_test(lambda u: any(u.has_perm(perm) perm in perms)) @any_permission_required('app.ticket_admin', 'app.ticket_read') def ticket_list(request): ...
finally of alasdair , django test code found solution.
class testfoo(testcase): def setup(self): self.user = models.user.objects.create(username='foo', password='bar') self.factory = requestfactory() def test_any_permissions_pass(self): perms = permission.objects.filter(codename__in=('ticket_admin', 'ticket_read')) self.user.user_permissions.add(*perms) @any_permission_required('app.ticket_admin', 'app.ticket_read') def a_view(request): return httpresponse() request = self.factory.get('/foo') request.user = self.user resp = a_view(request) self.assertequal(resp.status_code, 200)
Comments
Post a Comment