def test_admin_traversal_by_name(self): post_add_permission = Permission.objects.get( content_type__app_label='blog', content_type__model='post', codename='add_post') self.user.user_permissions.add(post_add_permission) model_admin = ModelAdmin2(Post, djadmin2.default) request = self.factory.get(reverse('admin2:blog_post_index')) request.user = self.user permissions = TemplatePermissionChecker(request, model_admin) context = { 'permissions': permissions, } result = self.render('{{ permissions.has_add_permission }}', context) self.assertEqual(result, 'True') result = self.render('{{ permissions.blog_post.has_add_permission }}', context) self.assertEqual(result, 'True') result = self.render( '{{ permissions.blog_post.has_change_permission }}', context) self.assertEqual(result, 'False') result = self.render( '{{ permissions.auth_user.has_delete_permission }}', context) self.assertEqual(result, 'False') result = self.render( '{{ permissions.unknown_app.has_add_permission }}', context) self.assertEqual(result, '') result = self.render( '{{ permissions.blog_post.has_unvalid_permission }}', context) self.assertEqual(result, '')
def test_permission_wrapper(self): model_admin = ModelAdmin2(Post, djadmin2.default) request = self.factory.get(reverse('admin2:blog_post_index')) request.user = self.user permissions = TemplatePermissionChecker(request, model_admin) context = { 'permissions': permissions, } result = self.render('{{ permissions.has_unvalid_permission }}', context) self.assertEqual(result, '') result = self.render('{{ permissions.has_add_permission }}', context) self.assertEqual(result, 'False') post_add_permission = Permission.objects.get( content_type__app_label='blog', content_type__model='post', codename='add_post') self.user.user_permissions.add(post_add_permission) # invalidate the users permission cache if hasattr(self.user, '_perm_cache'): del self.user._perm_cache result = self.render('{{ permissions.has_add_permission }}', context) self.assertEqual(result, 'True')
def test_object_level_permission(self): model_admin = ModelAdmin2(Post, djadmin2.default) request = self.factory.get(reverse('admin2:blog_post_index')) request.user = self.user permissions = TemplatePermissionChecker(request, model_admin) post = Post.objects.create(title='Hello', body='world') context = { 'post': post, 'permissions': permissions, } result = self.render( '{% load admin2_tags %}' '{{ permissions.has_unvalid_permission|for_object:post }}', context) self.assertEqual(result, '') result = self.render( '{% load admin2_tags %}' '{{ permissions.has_add_permission|for_object:post }}', context) self.assertEqual(result, 'False') post_add_permission = Permission.objects.get( content_type__app_label='blog', content_type__model='post', codename='add_post') self.user.user_permissions.add(post_add_permission) # invalidate the users permission cache if hasattr(self.user, '_perm_cache'): del self.user._perm_cache # object level permission are not supported by default. So this will # return ``False``. result = self.render( '{% load admin2_tags %}' '{{ permissions.has_add_permission }}' '{{ permissions.has_add_permission|for_object:post }}', context) self.assertEqual(result, 'TrueFalse') # binding an object and then checking for a specific view also works result = self.render( '{% load admin2_tags %}' '{{ permissions.has_add_permission }}' '{% with permissions|for_object:post as permissions %}' '{{ permissions.has_add_permission }}' '{% endwith %}', context) self.assertEqual(result, 'TrueFalse')
def get_model_admin(self, model): return ModelAdmin2(model, default)