142 lines
5.1 KiB
Python
142 lines
5.1 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(app):
|
|
# connect while not being logged in
|
|
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
|
|
|
def test_access(app, admin_user):
|
|
app = login(app)
|
|
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(app, admin_user):
|
|
app = login(app)
|
|
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(app, admin_user):
|
|
Page.objects.all().delete()
|
|
page = Page(title='One', slug='one')
|
|
page.save()
|
|
app = login(app)
|
|
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(app, admin_user):
|
|
Page.objects.all().delete()
|
|
page = Page(title='One', slug='one', template_name='standard')
|
|
page.save()
|
|
app = login(app)
|
|
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(app, admin_user):
|
|
Page.objects.all().delete()
|
|
page = Page(title='One', slug='one', template_name='standard')
|
|
page.save()
|
|
app = login(app)
|
|
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(app, admin_user):
|
|
Page.objects.all().delete()
|
|
page = Page(title='One', slug='one', template_name='standard')
|
|
page.save()
|
|
app = login(app)
|
|
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.status_int == 302
|
|
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'
|
|
|
|
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 = 'World Hello'
|
|
resp = resp.forms[0].submit(xhr=True)
|
|
assert resp.status_int == 200
|
|
assert resp.body.startswith('<p><label')
|
|
|
|
resp = app.get('/manage/pages/%s/' % page.id)
|
|
assert resp.forms[0]['c%s-text' % cells[0].get_reference()].value == 'World Hello'
|
|
|
|
|
|
def test_logout(app, admin_user):
|
|
app = login(app)
|
|
app.get('/logout/')
|
|
assert app.get('/manage/', status=302).location == 'http://localhost:80/login/?next=/manage/'
|
|
|
|
def test_asset_management(app, admin_user):
|
|
app = login(app)
|
|
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)
|