combo/tests/test_manager.py

131 lines
4.7 KiB
Python

import base64
import os
import StringIO
from django.core.files.storage import default_storage
from django.conf import settings
from django.contrib.auth.models import User
import pytest
from webtest import TestApp
from combo.wsgi import application
from combo.data.models import Page, CellBase, TextCell
pytestmark = pytest.mark.django_db
@pytest.fixture
def admin_user():
try:
user = User.objects.get(username='admin')
except User.DoesNotExist:
user = User.objects.create_superuser('admin', email=None, password='admin')
return user
def login(app, username='admin', password='admin'):
login_page = app.get('/login/')
login_form = login_page.forms[0]
login_form['username'] = username
login_form['password'] = password
resp = login_form.submit()
assert resp.status_int == 302
return app
def test_unlogged_access():
# connect while not being logged in
app = TestApp(application)
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
def test_access(admin_user):
app = login(TestApp(application))
resp = app.get('/manage/', status=200)
assert 'Pages' in resp.body
assert "This site doesn't have any page yet." in resp.body
def test_add_page(admin_user):
app = login(TestApp(application))
resp = app.get('/manage/', status=200)
resp = resp.click('New')
assert resp.forms[0]['title'].value == 'Home' # default title for first page
assert resp.forms[0]['slug'].value == 'index' # default slug for first page
resp = resp.forms[0].submit()
assert resp.location == 'http://localhost:80/manage/pages/1/'
def test_add_second_page(admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one')
page.save()
app = login(TestApp(application))
resp = app.get('/manage/', status=200)
resp = resp.click('New')
# assert there's no defaul title or slug for the second page
assert resp.forms[0]['title'].value == ''
assert resp.forms[0]['slug'].value == ''
def test_delete_page(admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')
page.save()
app = login(TestApp(application))
resp = app.get('/manage/pages/%s/' % page.id)
resp = resp.click('delete')
assert 'Confirm Deletion' in resp.body
resp = resp.forms[0].submit()
assert resp.location == 'http://localhost:80/manage/'
assert Page.objects.count() == 0
def test_export_page(admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')
page.save()
app = login(TestApp(application))
resp = app.get('/manage/pages/%s/' % page.id)
resp = resp.click('export')
assert resp.headers['content-type'] == 'application/json'
assert resp.json.get('fields').get('slug') == 'one'
def test_add_edit_cell(admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')
page.save()
app = login(TestApp(application))
resp = app.get('/manage/pages/%s/' % page.id)
# click on first button link, this should add a text cell
resp = app.get(resp.html.find('button').get('data-add-url'))
assert resp.location == 'http://localhost:80/manage/pages/1/'
cells = CellBase.get_cells(page_id=page.id)
assert len(cells) == 1
assert isinstance(cells[0], TextCell)
resp = app.get('/manage/pages/%s/' % page.id)
assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.body
resp.forms[0]['c%s-text' % cells[0].get_reference()].value = 'Hello world'
resp = resp.forms[0].submit()
assert resp.location == 'http://localhost:80/manage/pages/1/'
resp = app.get('/manage/pages/%s/' % page.id)
assert resp.forms[0]['c%s-text' % cells[0].get_reference()].value == 'Hello world'
def test_logout(admin_user):
app = login(TestApp(application))
app.get('/logout/')
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
def test_asset_management(admin_user):
app = login(TestApp(application))
resp = app.get('/manage/assets/')
assert 'have any asset yet.' in resp.body
filepath = os.path.join(settings.CKEDITOR_UPLOAD_PATH, 'test.png')
pix = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVQI12NoAAAAggCB3UNq9AAAAABJRU5ErkJggg=='
default_storage.save(filepath, StringIO.StringIO(base64.decodestring(pix)))
assert os.path.exists(default_storage.path(filepath))
resp = app.get('/manage/assets/')
assert 'have any asset yet.' not in resp.body
app.get('/manage/assets/delete?img_orig=%s' % filepath, status=204)
assert not os.path.exists(default_storage.path(filepath))
app.get('/manage/assets/delete?img_orig=/foo.png', status=403)