class TestAllowReadOnlyIfPublic(TestCase): def setUp(self): self.permission = AllowReadOnlyIfPublic() self.request_factory = RequestFactory() self.unsafe_methods = ('patch', 'post', 'put', 'delete') self.safe_methods = ('get', 'options', 'head') def request(self, verb): request = getattr(self.request_factory, verb)('/') request.user = AnonymousUser() return request def test_has_permission(self): for verb in self.safe_methods: assert self.permission.has_permission(self.request(verb), myview) for verb in self.unsafe_methods: assert not self.permission.has_permission(self.request(verb), myview) def test_has_object_permission_public(self): obj = Mock(spec=['is_public']) obj.is_public.return_value = True for verb in self.safe_methods: assert self.permission.has_object_permission( self.request(verb), myview, obj) for verb in self.unsafe_methods: assert not self.permission.has_object_permission( self.request(verb), myview, obj) def test_has_object_permission_not_public(self): obj = Mock(spec=['is_public']) obj.is_public.return_value = False for verb in self.unsafe_methods + self.safe_methods: assert not self.permission.has_object_permission( self.request(verb), myview, obj)
class TestAllowReadOnlyIfPublic(TestCase): def setUp(self): self.permission = AllowReadOnlyIfPublic() self.request_factory = RequestFactory() self.unsafe_methods = ('patch', 'post', 'put', 'delete') self.safe_methods = ('get', 'options', 'head') def request(self, verb): request = getattr(self.request_factory, verb)('/') request.user = AnonymousUser() return request def test_has_permission(self): for verb in self.safe_methods: assert self.permission.has_permission(self.request(verb), myview) for verb in self.unsafe_methods: assert not self.permission.has_permission( self.request(verb), myview) def test_has_object_permission_public(self): obj = Mock(spec=['is_public']) obj.is_public.return_value = True for verb in self.safe_methods: assert self.permission.has_object_permission( self.request(verb), myview, obj) for verb in self.unsafe_methods: assert not self.permission.has_object_permission( self.request(verb), myview, obj) def test_has_object_permission_not_public(self): obj = Mock(spec=['is_public']) obj.is_public.return_value = False for verb in self.unsafe_methods + self.safe_methods: assert not self.permission.has_object_permission( self.request(verb), myview, obj)
def setUp(self): self.permission = AllowReadOnlyIfPublic() self.request_factory = RequestFactory() self.unsafe_methods = ('patch', 'post', 'put', 'delete') self.safe_methods = ('get', 'options', 'head')