diff options
author | Maxime Turcotte <maxime.turcotte@savoirfairelinux.com> | 2014-05-28 12:07:27 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-06-13 09:31:40 -0400 |
commit | 504c89e8008c557a1e83c45535b549f77a3503b2 (patch) | |
tree | 3e4345d28dbbbdfa18bb7abfb68950c67655c9ea /tests/modeladmin | |
parent | bf743a4d571bbb7da276bc21c61f6ada5d26942c (diff) | |
download | django-504c89e8008c557a1e83c45535b549f77a3503b2.tar.gz |
Fixed #6327 -- Added has_module_permission method to BaseModelAdmin
Thanks chrj for the suggestion.
Diffstat (limited to 'tests/modeladmin')
-rw-r--r-- | tests/modeladmin/tests.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py index 3f5f378818..93cb58a42c 100644 --- a/tests/modeladmin/tests.py +++ b/tests/modeladmin/tests.py @@ -1542,3 +1542,93 @@ class ListDisplayEditableTests(CheckTestCase): list_editable = ['name', 'slug'] list_display_links = ['pub_date'] self.assertIsValid(ProductAdmin, ValidationTestModel) + + +class ModelAdminPermissionTests(TestCase): + + class MockUser(object): + def has_module_perms(self, app_label): + if app_label == "modeladmin": + return True + return False + + class MockAddUser(MockUser): + def has_perm(self, perm): + if perm == "modeladmin.add_band": + return True + return False + + class MockChangeUser(MockUser): + def has_perm(self, perm): + if perm == "modeladmin.change_band": + return True + return False + + class MockDeleteUser(MockUser): + def has_perm(self, perm): + if perm == "modeladmin.delete_band": + return True + return False + + def test_has_add_permission(self): + """ + Ensure that has_add_permission returns True for users who can add + objects and False for users who can't. + """ + ma = ModelAdmin(Band, AdminSite()) + request = MockRequest() + request.user = self.MockAddUser() + self.assertTrue(ma.has_add_permission(request)) + request.user = self.MockChangeUser() + self.assertFalse(ma.has_add_permission(request)) + request.user = self.MockDeleteUser() + self.assertFalse(ma.has_add_permission(request)) + + def test_has_change_permission(self): + """ + Ensure that has_change_permission returns True for users who can edit + objects and False for users who can't. + """ + ma = ModelAdmin(Band, AdminSite()) + request = MockRequest() + request.user = self.MockAddUser() + self.assertFalse(ma.has_change_permission(request)) + request.user = self.MockChangeUser() + self.assertTrue(ma.has_change_permission(request)) + request.user = self.MockDeleteUser() + self.assertFalse(ma.has_change_permission(request)) + + def test_has_delete_permission(self): + """ + Ensure that has_delete_permission returns True for users who can delete + objects and False for users who can't. + """ + ma = ModelAdmin(Band, AdminSite()) + request = MockRequest() + request.user = self.MockAddUser() + self.assertFalse(ma.has_delete_permission(request)) + request.user = self.MockChangeUser() + self.assertFalse(ma.has_delete_permission(request)) + request.user = self.MockDeleteUser() + self.assertTrue(ma.has_delete_permission(request)) + + def test_has_module_permission(self): + """ + Ensure that has_module_permission returns True for users who have any + permission for the module and False for users who don't. + """ + ma = ModelAdmin(Band, AdminSite()) + request = MockRequest() + request.user = self.MockAddUser() + self.assertTrue(ma.has_module_permission(request)) + request.user = self.MockChangeUser() + self.assertTrue(ma.has_module_permission(request)) + request.user = self.MockDeleteUser() + self.assertTrue(ma.has_module_permission(request)) + ma.opts.app_label = "anotherapp" + request.user = self.MockAddUser() + self.assertFalse(ma.has_module_permission(request)) + request.user = self.MockChangeUser() + self.assertFalse(ma.has_module_permission(request)) + request.user = self.MockDeleteUser() + self.assertFalse(ma.has_module_permission(request)) |