wcs/tests/admin_pages/test_category.py

244 lines
7.5 KiB
Python

import pytest
from wcs.categories import Category
from wcs.formdef import FormDef
from wcs.qommon.http_request import HTTPRequest
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
from .test_all import create_superuser
@pytest.fixture
def pub(request):
pub = create_temporary_pub()
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
pub.set_app_dir(req)
pub.cfg['identification'] = {'methods': ['password']}
pub.cfg['language'] = {'language': 'en'}
pub.write_cfg()
create_superuser(pub)
return pub
def teardown_module(module):
clean_temporary_pub()
def test_categories(pub):
app = login(get_app(pub))
app.get('/backoffice/forms/categories/')
def test_categories_legacy_urls(pub):
app = login(get_app(pub))
resp = app.get('/backoffice/categories/')
assert resp.location.endswith('/backoffice/forms/categories/')
resp = app.get('/backoffice/categories/1')
assert resp.location.endswith('/backoffice/forms/categories/1')
resp = app.get('/backoffice/categories/1/')
assert resp.location.endswith('/backoffice/forms/categories/1/')
def test_categories_new(pub):
Category.wipe()
app = login(get_app(pub))
# go to the page and cancel
resp = app.get('/backoffice/forms/categories/')
resp = resp.click('New Category')
resp = resp.forms[0].submit('cancel')
assert resp.location == 'http://example.net/backoffice/forms/categories/'
# go to the page and add a category
resp = app.get('/backoffice/forms/categories/')
resp = resp.click('New Category')
resp.forms[0]['name'] = 'a new category'
resp.forms[0]['description'] = 'description of the category'
resp = resp.forms[0].submit('submit')
assert resp.location == 'http://example.net/backoffice/forms/categories/'
resp = resp.follow()
assert 'a new category' in resp.text
resp = resp.click('a new category')
assert '<h2>a new category' in resp.text
assert Category.get(1).name == 'a new category'
assert Category.get(1).description == 'description of the category'
def test_categories_edit(pub):
Category.wipe()
category = Category(name='foobar')
category.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/1/')
assert 'No form associated to this category' in resp.text
resp = resp.click(href='edit')
assert resp.forms[0]['name'].value == 'foobar'
resp.forms[0]['description'] = 'category description'
resp = resp.forms[0].submit('submit')
assert resp.location == 'http://example.net/backoffice/forms/categories/'
resp = resp.follow()
resp = resp.click('foobar')
assert '<h2>foobar' in resp.text
assert Category.get(1).description == 'category description'
app.get('/backoffice/forms/categories/foo-bar/', status=404)
def test_categories_edit_duplicate_name(pub):
Category.wipe()
category = Category(name='foobar')
category.store()
category = Category(name='foobar2')
category.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/1/')
resp = resp.click(href='edit')
assert resp.forms[0]['name'].value == 'foobar'
resp.forms[0]['name'] = 'foobar2'
resp = resp.forms[0].submit('submit')
assert 'This name is already used' in resp.text
resp = resp.forms[0].submit('cancel')
assert resp.location == 'http://example.net/backoffice/forms/categories/'
def test_categories_with_formdefs(pub):
Category.wipe()
category = Category(name='foobar')
category.store()
FormDef.wipe()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/1/')
assert 'form bar' not in resp.text
formdef = FormDef()
formdef.name = 'form bar'
formdef.fields = []
formdef.category_id = category.id
formdef.store()
resp = app.get('/backoffice/forms/categories/1/')
assert 'form bar' in resp.text
assert 'No form associated to this category' not in resp.text
def test_categories_delete(pub):
Category.wipe()
category = Category(name='foobar')
category.store()
FormDef.wipe()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/1/')
assert 'popup.js' in resp.text
resp = resp.click(href='delete')
resp = resp.forms[0].submit('cancel')
assert resp.location == 'http://example.net/backoffice/forms/categories/1/'
assert Category.count() == 1
resp = app.get('/backoffice/forms/categories/1/')
resp = resp.click(href='delete')
resp = resp.forms[0].submit()
assert resp.location == 'http://example.net/backoffice/forms/categories/'
resp = resp.follow()
assert Category.count() == 0
def test_categories_edit_description(pub):
Category.wipe()
category = Category(name='foobar')
category.description = 'category description'
category.store()
app = login(get_app(pub))
# this URL is used for editing from the frontoffice, there's no link
# pointing to it in the admin.
resp = app.get('/backoffice/forms/categories/1/description')
assert resp.forms[0]['description'].value == 'category description'
resp.forms[0]['description'] = 'updated description'
# check cancel doesn't save the change
resp2 = resp.forms[0].submit('cancel')
assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
assert Category.get(1).description == 'category description'
# check submit does it properly
resp2 = resp.forms[0].submit('submit')
assert resp2.location == 'http://example.net/backoffice/forms/categories/1/'
resp2 = resp2.follow()
assert Category.get(1).description == 'updated description'
def test_categories_new_duplicate_name(pub):
Category.wipe()
category = Category(name='foobar')
category.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/')
resp = resp.click('New Category')
resp.forms[0]['name'] = 'foobar'
resp = resp.forms[0].submit('submit')
assert 'This name is already used' in resp.text
def test_categories_reorder(pub):
Category.wipe()
category = Category(name='foo')
category.store()
category = Category(name='bar')
category.store()
category = Category(name='baz')
category.store()
app = login(get_app(pub))
app.get('/backoffice/forms/categories/update_order?order=1;2;3;')
categories = Category.select()
Category.sort_by_position(categories)
assert [x.id for x in categories] == ['1', '2', '3']
app.get('/backoffice/forms/categories/update_order?order=3;1;2;')
categories = Category.select()
Category.sort_by_position(categories)
assert [x.id for x in categories] == ['3', '1', '2']
def test_categories_edit_roles(pub):
pub.role_class.wipe()
role_a = pub.role_class(name='a')
role_a.store()
role_b = pub.role_class(name='b')
role_b.store()
Category.wipe()
category = Category(name='foobar')
category.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/categories/1/edit')
resp.form['export_roles$element0'] = role_a.id
resp = resp.form.submit('export_roles$add_element')
resp.form['export_roles$element1'] = role_b.id
resp.form['statistics_roles$element0'] = role_a.id
resp = resp.form.submit('submit')
category = Category.get(category.id)
assert {x.id for x in category.export_roles} == {role_a.id, role_b.id}
assert {x.id for x in category.statistics_roles} == {role_a.id}
resp = app.get('/backoffice/forms/categories/1/edit')
assert resp.form['export_roles$element0'].value == role_a.id