misc: show unlogged only cells to superuser (#45846)

In this case the .shown-because-admin class is present on the cell.
This commit is contained in:
Benjamin Dauvergne 2022-10-14 15:45:47 +02:00
parent 92e68e279a
commit d29dc19ee9
5 changed files with 16 additions and 8 deletions

View File

@ -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

View File

@ -1504,7 +1504,7 @@ def test_page_cell_placeholder_restricted_visibility(app, admin_user):
)
)
assert "<p>Public text</p>" not in resp.text
assert resp.pyquery('.shown-because-admin').text() == 'Public text'
assert "<p>Private text</p>" in resp.text
@ -1757,19 +1757,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 {elt.text() for elt in response.pyquery.items('.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 {elt.text() for elt in response.pyquery.items('.shown-because-admin')} == {
'Visible to unlogged only',
'Visible only to non-members of group',
}

View File

@ -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'

View File

@ -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')

View File

@ -481,6 +481,7 @@ class MockUserWithNameId:
email = 'foo@example.net'
is_authenticated = True
is_anonymous = False
is_superuser = False
def get_name_id(self):
return 'xyz'