From 2c434d87a8309f7e7819dae976511b1cbccf990d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 14 Oct 2022 15:45:47 +0200 Subject: [PATCH 2/2] misc: show unlogged only cells to superuser (#45846) In this case the .shown-because-admin class is present on the cell. --- combo/data/models.py | 4 ++-- tests/test_cells.py | 16 +++++++++++----- tests/test_family.py | 1 + tests/test_public.py | 2 +- tests/wcs/utils.py | 1 + 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index a6608439..3bdb9628 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -76,14 +76,14 @@ class PostException(Exception): def element_is_visible(element, user=None, ignore_superuser=False): + if user is not None and user.is_superuser and not ignore_superuser: + return True if element.public: if getattr(element, 'restricted_to_unlogged', None) is True: return user is None or user.is_anonymous return True if user is None or user.is_anonymous: return False - if user.is_superuser and not ignore_superuser: - return True page_groups = element.groups.all() if not page_groups: groups_ok = True diff --git a/tests/test_cells.py b/tests/test_cells.py index 76c0b20e..e3199058 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -1485,7 +1485,7 @@ def test_page_cell_placeholder_restricted_visibility(app, admin_user): ) ) - assert "

Public text

" not in resp.text + assert resp.pyquery('.shown-because-admin').text() == 'Public text' assert "

Private text

" in resp.text @@ -1738,19 +1738,25 @@ class TestCellVisibility: response = app.get('/test/', user='superuser') assert 'Always visible' in response - assert 'Visible to unlogged only' not in response + assert 'Visible to unlogged only' in response assert 'Visible to logged only' in response assert 'Visible only to members of group' in response assert 'Visible only to non-members of group' in response - assert response.pyquery('.shown-because-admin').text() == 'Visible only to members of group' + assert {response.pyquery(elt).text() for elt in response.pyquery('.shown-because-admin')} == { + 'Visible to unlogged only', + 'Visible only to members of group', + } def test_superuser_with_role(self, app, group): User.objects.create(username='superuser', is_superuser=True).groups.add(group) response = app.get('/test/', user='superuser') assert 'Always visible' in response - assert 'Visible to unlogged only' not in response + assert 'Visible to unlogged only' in response assert 'Visible to logged only' in response assert 'Visible only to members of group' in response assert 'Visible only to non-members of group' in response - assert response.pyquery('.shown-because-admin').text() == 'Visible only to non-members of group' + assert {response.pyquery(elt).text() for elt in response.pyquery('.shown-because-admin')} == { + 'Visible to unlogged only', + 'Visible only to non-members of group', + } diff --git a/tests/test_family.py b/tests/test_family.py index b08759fe..774f9e68 100644 --- a/tests/test_family.py +++ b/tests/test_family.py @@ -35,6 +35,7 @@ class MockUserWithNameId: email = 'foo@example.net' is_authenticated = True is_anonymous = False + is_superuser = False def get_name_id(self): return 'xyz' diff --git a/tests/test_public.py b/tests/test_public.py index 02b20ce7..d809aace 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -81,7 +81,7 @@ def test_page_contents_unlogged_only(app, admin_user): app = login(app) resp = app.get('/', status=200) - assert not 'Foobar' in resp.text + assert resp.pyquery('.shown-because-admin').text() == 'Foobar' @pytest.mark.skipif('mellon is None') diff --git a/tests/wcs/utils.py b/tests/wcs/utils.py index 4d7d64ef..0fbfcc68 100644 --- a/tests/wcs/utils.py +++ b/tests/wcs/utils.py @@ -475,6 +475,7 @@ class MockUserWithNameId: email = 'foo@example.net' is_authenticated = True is_anonymous = False + is_superuser = False def get_name_id(self): return 'xyz' -- 2.37.2