9 |
9 |
import requests
|
10 |
10 |
from django.apps import apps
|
11 |
11 |
from django.conf import settings
|
12 |
|
from django.contrib.auth.models import User
|
|
12 |
from django.contrib.auth.models import Group, User
|
13 |
13 |
from django.db import connection
|
14 |
14 |
from django.forms.widgets import Media
|
15 |
15 |
from django.template.exceptions import TemplateDoesNotExist
|
... | ... | |
1671 |
1671 |
resp = app.get('/manage/assets/')
|
1672 |
1672 |
assert link_cell.get_slug_for_asset() == 'test_cell_assets'
|
1673 |
1673 |
assert 'Picture — %s (test)' % link_cell.get_label_for_asset() in resp.text
|
|
1674 |
|
|
1675 |
|
|
1676 |
class TestCellVisibility:
|
|
1677 |
@pytest.fixture
|
|
1678 |
def group(self, db):
|
|
1679 |
return Group.objects.create(name='Group')
|
|
1680 |
|
|
1681 |
@pytest.fixture(autouse=True)
|
|
1682 |
def setup(self, db, group):
|
|
1683 |
pg = Page.objects.create(title='Test', slug='test', template_name='standard')
|
|
1684 |
|
|
1685 |
order = 0
|
|
1686 |
|
|
1687 |
def make_cell(**kwargs):
|
|
1688 |
nonlocal order
|
|
1689 |
try:
|
|
1690 |
return TextCell.objects.create(page=pg, placeholder='content', order=order, **kwargs)
|
|
1691 |
finally:
|
|
1692 |
order += 1
|
|
1693 |
|
|
1694 |
make_cell(text='<p>Always visible</p>')
|
|
1695 |
make_cell(text='<p>Visible to unlogged only</p>', restricted_to_unlogged=True)
|
|
1696 |
make_cell(text='<p>Visible to logged only</p>', public=False)
|
|
1697 |
make_cell(text='<p>Visible only to members of group</p>', public=False).groups.add(group)
|
|
1698 |
make_cell(
|
|
1699 |
text='<p id="visible-to-non-members-of-group">Visible only to non-members of group</p>',
|
|
1700 |
public=False,
|
|
1701 |
restricted_to_unlogged=True,
|
|
1702 |
).groups.add(group)
|
|
1703 |
|
|
1704 |
def test_anonymous(self, app):
|
|
1705 |
response = app.get('/test/')
|
|
1706 |
|
|
1707 |
assert 'Always visible' in response
|
|
1708 |
assert 'Visible to unlogged only' in response
|
|
1709 |
assert 'Visible to logged only' not in response
|
|
1710 |
assert 'Visible only to members of group' not in response
|
|
1711 |
assert 'Visible only to non-members of group' not in response
|
|
1712 |
assert response.pyquery('.shown-because-admin').text() == ''
|
|
1713 |
|
|
1714 |
def test_user(self, app):
|
|
1715 |
User.objects.create(username='user')
|
|
1716 |
response = app.get('/test/', user='user')
|
|
1717 |
|
|
1718 |
assert 'Always visible' in response
|
|
1719 |
assert 'Visible to unlogged only' not in response
|
|
1720 |
assert 'Visible to logged only' in response
|
|
1721 |
assert 'Visible only to members of group' not in response
|
|
1722 |
assert 'Visible only to non-members of group' in response
|
|
1723 |
assert response.pyquery('.shown-because-admin').text() == ''
|
|
1724 |
|
|
1725 |
def test_user_with_role(self, app, group):
|
|
1726 |
User.objects.create(username='user').groups.add(group)
|
|
1727 |
response = app.get('/test/', user='user')
|
|
1728 |
|
|
1729 |
assert 'Always visible' in response
|
|
1730 |
assert 'Visible to unlogged only' not in response
|
|
1731 |
assert 'Visible to logged only' in response
|
|
1732 |
assert 'Visible only to members of group' in response
|
|
1733 |
assert 'Visible only to non-members of group' not in response
|
|
1734 |
assert response.pyquery('.shown-because-admin').text() == ''
|
|
1735 |
|
|
1736 |
def test_superuser(self, app):
|
|
1737 |
User.objects.create(username='superuser', is_superuser=True)
|
|
1738 |
response = app.get('/test/', user='superuser')
|
|
1739 |
|
|
1740 |
assert 'Always visible' in response
|
|
1741 |
assert 'Visible to unlogged only' not in response
|
|
1742 |
assert 'Visible to logged only' in response
|
|
1743 |
assert 'Visible only to members of group' in response
|
|
1744 |
assert 'Visible only to non-members of group' in response
|
|
1745 |
assert response.pyquery('.shown-because-admin').text() == 'Visible only to members of group'
|
|
1746 |
|
|
1747 |
def test_superuser_with_role(self, app, group):
|
|
1748 |
User.objects.create(username='superuser', is_superuser=True).groups.add(group)
|
|
1749 |
response = app.get('/test/', user='superuser')
|
|
1750 |
|
|
1751 |
assert 'Always visible' in response
|
|
1752 |
assert 'Visible to unlogged only' not in response
|
|
1753 |
assert 'Visible to logged only' in response
|
|
1754 |
assert 'Visible only to members of group' in response
|
|
1755 |
assert 'Visible only to non-members of group' in response
|
|
1756 |
assert response.pyquery('.shown-because-admin').text() == 'Visible only to non-members of group'
|
1674 |
|
-
|