combo/tests/test_public.py

181 lines
6.4 KiB
Python

from webtest import TestApp
import datetime
import pytest
import urllib
from combo.wsgi import application
from combo.data.models import Page, CellBase, TextCell
pytestmark = pytest.mark.django_db
from test_manager import admin_user, login
def test_missing_index():
Page.objects.all().delete()
app = TestApp(application)
resp = app.get('/', status=200)
assert 'Welcome' in resp.body
def test_index():
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard')
page.save()
app = TestApp(application)
resp = app.get('/index', status=301)
assert resp.location == 'http://localhost:80/'
resp = app.get('/', status=200)
# check {% now %} inside a skeleton_extra_placeholder is interpreted
assert str(datetime.datetime.now().year) in resp.body
def test_page_contents():
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard')
page.save()
cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
cell.save()
app = TestApp(application)
resp = app.get('/', status=200)
assert 'Foobar' in resp.body
def test_page_contents_unlogged_only(admin_user):
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard')
page.save()
cell = TextCell(page=page, placeholder='content', text='Foobar', order=0,
restricted_to_unlogged=True)
cell.save()
app = TestApp(application)
resp = app.get('/', status=200)
assert 'Foobar' in resp.body
app = login(TestApp(application))
resp = app.get('/', status=200)
assert not 'Foobar' in resp.body
def test_page_footer_acquisition():
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard')
page.save()
cell = TextCell(page=page, placeholder='footer', text='BARFOO', order=0)
cell.save()
app = TestApp(application)
resp = app.get('/', status=200)
assert 'BARFOO' in resp.body
page = Page(title='Second', slug='second', template_name='standard')
page.save()
resp = app.get('/second', status=301)
assert resp.location == 'http://localhost:80/second/'
resp = app.get('/second/', status=200)
assert 'BARFOO' in resp.body
def test_page_redirect():
Page.objects.all().delete()
page = Page(title='Elsewhere', slug='elsewhere', template_name='standard',
redirect_url='http://example.net')
page.save()
app = TestApp(application)
resp = app.get('/elsewhere/', status=302)
assert resp.location == 'http://example.net'
def test_page_private_unlogged():
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard', public=False)
page.save()
app = TestApp(application)
resp = app.get('/', status=302)
assert resp.location == 'http://localhost:80/login/?next=/'
def test_page_private_logged_in(admin_user):
Page.objects.all().delete()
page = Page(title='Home', slug='index', template_name='standard', public=False)
page.save()
app = login(TestApp(application))
resp = app.get('/', status=200)
def test_page_skeleton():
Page.objects.all().delete()
page = Page(title='Elsewhere', slug='elsewhere', template_name='standard',
redirect_url='http://example.net/foo/')
page.save()
app = TestApp(application)
# url prefix match
resp = app.get('/__skeleton__/?source=%s' % urllib.quote('http://example.net/foo/bar'))
assert '{% block placeholder-content %}{% block content %}{% endblock %}{% endblock %}' in resp.body
assert '{% block placeholder-footer %}{% block footer %}{% endblock %}{% endblock %}' in resp.body
# url netloc match
resp = app.get('/__skeleton__/?source=%s' % urllib.quote('http://example.net'))
assert '{% block placeholder-content %}{% block content %}{% endblock %}{% endblock %}' in resp.body
assert '{% block placeholder-footer %}{% block footer %}{% endblock %}{% endblock %}' in resp.body
# no match
resp = app.get('/__skeleton__/?source=%s' % urllib.quote('http://example.com/foo/bar'), status=403)
# check with a footer cell
cell = TextCell(page=page, placeholder='footer', text='Foobar', order=0)
cell.save()
resp = app.get('/__skeleton__/?source=%s' % urllib.quote('http://example.net'))
assert '{% block placeholder-content %}{% block content %}{% endblock %}{% endblock %}' in resp.body
assert not '{% block placeholder-footer %}{% block footer %}{% endblock %}{% endblock %}' in resp.body
assert 'Foobar' in resp.body
# check {% now %} inside a skeleton_extra_placeholder is not interpreted
assert '{%now' in resp.body
def test_subpage_location():
Page.objects.all().delete()
page_index = Page(title='Home Page', slug='index', template_name='standard')
page_index.save()
page_sibling = Page(title='Second top level page', slug='second', template_name='standard')
page_sibling.save()
page = Page(title='Child of Home', slug='child-home',
template_name='standard', parent=page_index)
page.save()
page = Page(title='Grand child of home', slug='grand-child-home',
template_name='standard', parent=page)
page.save()
page = Page(title='Child of second', slug='child-second',
template_name='standard', parent=page_sibling)
page.save()
page = Page(title='Grand child of second', slug='grand-child-second',
template_name='standard', parent=page)
page.save()
app = TestApp(application)
resp = app.get('/', status=200)
assert 'Home Page' in resp.body
resp = app.get('/child-home/', status=200)
assert 'Child of Home' in resp.body
resp = app.get('/child-home/grand-child-home/', status=200)
assert 'Grand child of home' in resp.body
assert app.get('/child-home/grand-child-home', status=301).location == \
'http://localhost:80/child-home/grand-child-home/'
app.get('/grand-child-home/', status=404)
resp = app.get('/second/', status=200)
assert 'Second top level page' in resp.body
resp = app.get('/second/child-second/', status=200)
assert 'Child of second' in resp.body
resp = app.get('/second/child-second/grand-child-second/', status=200)
assert 'Grand child of second' in resp.body
def test_404():
Page.objects.all().delete()
app = TestApp(application)
resp = app.get('/foobar/', status=404)
assert "This page doesn't exist" in resp.body