def test_add_permissions_to_user(self): user = UserFactory() bootstrap = Mock(users={"bar": user}) component = Permissions(bootstrap) self.assertFalse(user.has_perm("cms.change_page")) component.add_permissions_to_user("bar", [["change_page", "cms", "page"]], {}) user = user._meta.model.objects.get(pk=user.pk) # reload perms self.assertTrue(user.has_perm("cms.change_page"))
def test_add_permissions_to_group(self): group = GroupFactory() user = UserFactory() user.groups.add(group) bootstrap = Mock(groups={"baz": group}) component = Permissions(bootstrap) self.assertFalse(user.has_perm("cms.change_page")) component.add_permissions_to_group("baz", [["change_page", "cms", "page"]], {}) user = user._meta.model.objects.get(pk=user.pk) # reload perms self.assertTrue(user.has_perm("cms.change_page"))
def test_get_permissions(self): component = Permissions(Mock()) perms = [ ["change_page", "cms", "page"], ["delete_page", "cms", "page"], ["use_structure", "cms", "placeholder"], ["change_user", "auth", "user"], ] result = component.get_permissions(perms) self.assertEqual(result.count(), 4) self.assertEqual(set(p.natural_key() for p in result), set(tuple(p) for p in perms))
def test_resolve_aliases(self): component = Permissions(Mock()) perms = [["foo", "bar", "baz"], ["foo", "bar", "test"], "alias"] aliases = {"alias": ["alias", "perm", "change"]} side_effects = [ ["foo", "bar", "baz"], ["foo", "bar", "test"], ["alias", "perm", "change"], ] with patch.object(component, "resolve_alias", side_effect=side_effects) as resolve_alias: result = list(component.resolve_aliases(perms, aliases)) resolve_alias.assert_has_calls( [ call(perms[0], aliases), call(perms[1], aliases), call(perms[2], aliases) ], any_order=True, ) self.assertEqual(result, side_effects)
def test_parse(self): component = Permissions(Mock()) aliases = {"test": ["change_test", "app", "model"]} component.raw_data = { "aliases": aliases, "users": { "user1": ["test"] }, "groups": { "group1": [["change_page", "cms", "page"]] }, } with patch.object( component, "add_permissions_to_user" ) as add_permissions_to_user, patch.object( component, "add_permissions_to_group") as add_permissions_to_group: component.parse() add_permissions_to_user.assert_called_once_with( "user1", ["test"], aliases) add_permissions_to_group.assert_called_once_with( "group1", [["change_page", "cms", "page"]], aliases)
def test_get_permissions_missing_content_types_are_ignored(self): component = Permissions(Mock()) result = component.get_permissions( [["change_page", "cms_like_app", "yet_another_page_model"]]) self.assertFalse(result.exists())
def test_get_permissions_none(self): component = Permissions(Mock()) result = component.get_permissions([]) self.assertFalse(result.exists())
def test_resolve_alias_regular_permission(self): component = Permissions(Mock()) result = component.resolve_alias(["foo", "bar", "baz"], {}) self.assertEqual(result, ["foo", "bar", "baz"])
def test_resolve_alias(self): component = Permissions(Mock()) result = component.resolve_alias("alias", {"alias": ["foo", "bar", "baz"]}) self.assertEqual(result, ["foo", "bar", "baz"])