202 lines
5.4 KiB
Python
202 lines
5.4 KiB
Python
import os
|
|
import shutil
|
|
from django.utils.six import StringIO
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from quixote import cleanup, get_publisher
|
|
from wcs.qommon import errors, sessions
|
|
from wcs.qommon.ident.password_accounts import PasswordAccount
|
|
from wcs.qommon.http_request import HTTPRequest
|
|
from wcs.qommon.template import get_current_theme
|
|
from wcs.categories import Category
|
|
from wcs.roles import Role
|
|
from wcs.workflows import Workflow
|
|
from wcs.formdef import FormDef
|
|
from wcs import fields
|
|
|
|
from utilities import get_app, login, create_temporary_pub
|
|
|
|
def setup_module(module):
|
|
cleanup()
|
|
|
|
global pub
|
|
|
|
pub = create_temporary_pub()
|
|
|
|
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
|
pub.set_app_dir(req)
|
|
pub.cfg['identification'] = {'methods': ['password']}
|
|
pub.write_cfg()
|
|
|
|
def create_user():
|
|
if pub.user_class.has_key('user'):
|
|
return
|
|
user1 = pub.user_class(name='user')
|
|
user1.id = 'admin'
|
|
user1.is_admin = False
|
|
user1.store()
|
|
|
|
account1 = PasswordAccount(id='user')
|
|
account1.set_password('user')
|
|
account1.user_id = user1.id
|
|
account1.store()
|
|
|
|
pub.cfg['identification'] = {'methods': ['password']}
|
|
pub.write_cfg()
|
|
|
|
return user1
|
|
|
|
def create_formdef():
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.fields = []
|
|
formdef.store()
|
|
return formdef
|
|
|
|
def teardown_module(module):
|
|
shutil.rmtree(pub.APP_DIR)
|
|
|
|
def test_with_user():
|
|
user = create_user()
|
|
app = login(get_app(pub), username='user', password='user')
|
|
resp = app.get('/', status=200)
|
|
resp = app.get('/myspace/', status=200)
|
|
|
|
def test_myspace_with_user_forms():
|
|
user = create_user()
|
|
formdef = create_formdef()
|
|
|
|
cat = Category(name='cat')
|
|
cat.store()
|
|
formdef.category_id = cat.id
|
|
formdef.enable_tracking_codes = True
|
|
|
|
wf = Workflow(name='status')
|
|
st1 = wf.add_status('Status1', 'st1')
|
|
wf.store()
|
|
formdef.workflow_id = wf.id
|
|
formdef.store()
|
|
|
|
formdef.data_class().wipe()
|
|
formdata = formdef.data_class()()
|
|
formdata.user_id = user.id
|
|
formdata.status = 'wf-st1'
|
|
formdata.receipt_time = time.localtime()
|
|
formdata.data = {}
|
|
formdata.store()
|
|
draft = formdef.data_class()()
|
|
draft.user_id = user.id
|
|
draft.status = 'draft'
|
|
draft.receipt_time = time.localtime()
|
|
draft.data = {}
|
|
draft.store()
|
|
|
|
app = login(get_app(pub), username='user', password='user')
|
|
resp = app.get('/myspace/')
|
|
assert formdata.id != draft.id
|
|
assert '<a href="/test/%s/"' % formdata.id in resp
|
|
assert '<a href="/test/%s"' % draft.id in resp
|
|
resp = app.get('/test/%s/' % formdata.id, status=302)
|
|
assert resp.location == 'http://example.net/cat/test/%s/' % formdata.id
|
|
resp = app.get('/test/%s/' % draft.id, status=302)
|
|
resp = resp.follow(status=302)
|
|
assert resp.location.startswith('http://example.net/cat/test/?mt=')
|
|
resp = resp.follow(status=200)
|
|
|
|
# disable formdef: formdatas are still visible and accessible, drafts are not
|
|
formdef.disabled = True
|
|
formdef.store()
|
|
resp = app.get('/myspace/')
|
|
assert formdata.id != draft.id
|
|
assert '<a href="/test/%s/"' % formdata.id in resp
|
|
assert not '<a href="/test/%s"' % draft.id in resp
|
|
resp = app.get('/cat/test/%s' % formdata.id)
|
|
resp.status_int = 200
|
|
resp = app.get('/cat/test/%s/' % draft.id, status=302)
|
|
resp = resp.follow(status=403)
|
|
|
|
|
|
def test_form_category_redirection():
|
|
Category.wipe()
|
|
cat = Category(name='baz')
|
|
cat.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'foobar'
|
|
formdef.category_id = cat.id
|
|
formdef.fields = []
|
|
formdef.store()
|
|
|
|
# check we get a redirection to /category/formdef/
|
|
resp = get_app(pub).get('/foobar/')
|
|
assert resp.location.endswith('/baz/foobar/')
|
|
|
|
# check missing trailing slashs are ok
|
|
resp = get_app(pub).get('/foobar')
|
|
assert resp.location.endswith('/baz/foobar')
|
|
resp = resp.follow()
|
|
assert resp.location.endswith('/baz/foobar/')
|
|
|
|
# even if there's a query string
|
|
resp = get_app(pub).get('/foobar?test=1')
|
|
assert resp.location.endswith('/baz/foobar?test=1')
|
|
resp = resp.follow()
|
|
assert resp.location.endswith('/baz/foobar/')
|
|
|
|
# check direct category access without trailing slash
|
|
resp = get_app(pub).get('/baz')
|
|
assert resp.location.endswith('/baz/')
|
|
resp = get_app(pub).get('/baz?test=1')
|
|
assert resp.location.endswith('/baz/')
|
|
|
|
|
|
def test_form_and_category_same_slug():
|
|
Category.wipe()
|
|
cat = Category(name='foobar')
|
|
cat.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'foobar'
|
|
formdef.category_id = cat.id
|
|
formdef.fields = []
|
|
formdef.store()
|
|
|
|
# check we get to the form, not the category
|
|
resp = get_app(pub).get('/foobar/')
|
|
assert resp.form
|
|
|
|
|
|
def test_form_and_category_same_slug2():
|
|
Category.wipe()
|
|
cat = Category(name='test')
|
|
cat.store()
|
|
|
|
FormDef.wipe()
|
|
formdef = FormDef()
|
|
formdef.name = 'foobar'
|
|
formdef.category_id = cat.id
|
|
formdef.fields = []
|
|
formdef.store()
|
|
|
|
formdef = FormDef()
|
|
formdef.name = 'test'
|
|
formdef.fields = []
|
|
formdef.store()
|
|
|
|
# check we get to the foobar form
|
|
resp = get_app(pub).get('/foobar/')
|
|
assert resp.location == 'http://example.net/test/foobar/'
|
|
resp = resp.follow()
|
|
assert resp.form
|
|
assert '<h3>foobar</h3>' in resp
|
|
|
|
# check we get to the test form
|
|
resp = get_app(pub).get('/test/')
|
|
assert resp.form
|
|
assert '<h3>test</h3>' in resp
|