2024-08-12 21:17:08 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
2024-08-21 14:38:34 -05:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
import colander
|
|
|
|
import deform
|
|
|
|
from pyramid import testing
|
|
|
|
|
2024-08-13 21:43:56 -05:00
|
|
|
from wuttaweb.forms import widgets as mod
|
2024-08-25 12:20:28 -05:00
|
|
|
from wuttaweb.forms.schema import FileDownload, PersonRef, RoleRefs, UserRefs, Permissions
|
2024-08-12 21:17:08 -05:00
|
|
|
from tests.util import WebTestCase
|
|
|
|
|
2024-08-13 16:29:34 -05:00
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
class TestObjectRefWidget(WebTestCase):
|
|
|
|
|
2024-08-13 16:29:34 -05:00
|
|
|
def make_field(self, node, **kwargs):
|
|
|
|
# TODO: not sure why default renderer is in use even though
|
|
|
|
# pyramid_deform was included in setup? but this works..
|
|
|
|
kwargs.setdefault('renderer', deform.Form.default_renderer)
|
|
|
|
return deform.Field(node, **kwargs)
|
|
|
|
|
2024-08-12 21:17:08 -05:00
|
|
|
def test_serialize(self):
|
|
|
|
model = self.app.model
|
|
|
|
person = model.Person(full_name="Betty Boop")
|
|
|
|
self.session.add(person)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# standard (editable)
|
|
|
|
node = colander.SchemaNode(PersonRef(self.request, session=self.session))
|
2024-08-13 21:43:56 -05:00
|
|
|
widget = mod.ObjectRefWidget(self.request)
|
2024-08-13 16:29:34 -05:00
|
|
|
field = self.make_field(node)
|
2024-08-12 21:17:08 -05:00
|
|
|
html = widget.serialize(field, person.uuid)
|
2024-08-13 16:29:34 -05:00
|
|
|
self.assertIn('<b-select ', html)
|
2024-08-12 21:17:08 -05:00
|
|
|
|
|
|
|
# readonly
|
|
|
|
node = colander.SchemaNode(PersonRef(self.request, session=self.session))
|
|
|
|
node.model_instance = person
|
2024-08-13 21:43:56 -05:00
|
|
|
widget = mod.ObjectRefWidget(self.request)
|
2024-08-13 16:29:34 -05:00
|
|
|
field = self.make_field(node)
|
2024-08-12 21:17:08 -05:00
|
|
|
html = widget.serialize(field, person.uuid, readonly=True)
|
2024-08-21 14:38:34 -05:00
|
|
|
self.assertIn('Betty Boop', html)
|
|
|
|
self.assertNotIn('<a', html)
|
|
|
|
|
|
|
|
# with hyperlink
|
|
|
|
node = colander.SchemaNode(PersonRef(self.request, session=self.session))
|
|
|
|
node.model_instance = person
|
|
|
|
widget = mod.ObjectRefWidget(self.request, url=lambda p: '/foo')
|
|
|
|
field = self.make_field(node)
|
|
|
|
html = widget.serialize(field, person.uuid, readonly=True)
|
|
|
|
self.assertIn('Betty Boop', html)
|
|
|
|
self.assertIn('<a', html)
|
|
|
|
self.assertIn('href="/foo"', html)
|
2024-08-13 21:43:56 -05:00
|
|
|
|
|
|
|
|
2024-08-25 12:20:28 -05:00
|
|
|
class TestFileDownloadWidget(WebTestCase):
|
|
|
|
|
|
|
|
def make_field(self, node, **kwargs):
|
|
|
|
# TODO: not sure why default renderer is in use even though
|
|
|
|
# pyramid_deform was included in setup? but this works..
|
|
|
|
kwargs.setdefault('renderer', deform.Form.default_renderer)
|
|
|
|
return deform.Field(node, **kwargs)
|
|
|
|
|
|
|
|
def test_serialize(self):
|
|
|
|
|
|
|
|
# nb. we let the field construct the widget via our type
|
|
|
|
# (nb. at first we do not provide a url)
|
|
|
|
node = colander.SchemaNode(FileDownload(self.request))
|
|
|
|
field = self.make_field(node)
|
|
|
|
widget = field.widget
|
|
|
|
|
|
|
|
# null value
|
|
|
|
html = widget.serialize(field, None, readonly=True)
|
|
|
|
self.assertNotIn('<a ', html)
|
|
|
|
self.assertIn('<span>', html)
|
|
|
|
|
|
|
|
# path to nonexistent file
|
|
|
|
html = widget.serialize(field, '/this/path/does/not/exist', readonly=True)
|
|
|
|
self.assertNotIn('<a ', html)
|
|
|
|
self.assertIn('<span>', html)
|
|
|
|
|
|
|
|
# path to actual file
|
|
|
|
datfile = self.write_file('data.txt', "hello\n" * 1000)
|
|
|
|
html = widget.serialize(field, datfile, readonly=True)
|
|
|
|
self.assertNotIn('<a ', html)
|
|
|
|
self.assertIn('<span>', html)
|
|
|
|
self.assertIn('data.txt', html)
|
|
|
|
self.assertIn('kB)', html)
|
|
|
|
|
|
|
|
# path to file, w/ url
|
|
|
|
node = colander.SchemaNode(FileDownload(self.request, url='/download/blarg'))
|
|
|
|
field = self.make_field(node)
|
|
|
|
widget = field.widget
|
|
|
|
html = widget.serialize(field, datfile, readonly=True)
|
|
|
|
self.assertNotIn('<span>', html)
|
|
|
|
self.assertIn('<a href="/download/blarg">', html)
|
|
|
|
self.assertIn('data.txt', html)
|
|
|
|
self.assertIn('kB)', html)
|
|
|
|
|
|
|
|
# nb. same readonly output even if we ask for editable
|
|
|
|
html2 = widget.serialize(field, datfile, readonly=False)
|
|
|
|
self.assertEqual(html2, html)
|
|
|
|
|
|
|
|
|
2024-08-13 21:43:56 -05:00
|
|
|
class TestRoleRefsWidget(WebTestCase):
|
|
|
|
|
|
|
|
def make_field(self, node, **kwargs):
|
|
|
|
# TODO: not sure why default renderer is in use even though
|
|
|
|
# pyramid_deform was included in setup? but this works..
|
|
|
|
kwargs.setdefault('renderer', deform.Form.default_renderer)
|
|
|
|
return deform.Field(node, **kwargs)
|
|
|
|
|
|
|
|
def test_serialize(self):
|
2024-08-21 14:38:34 -05:00
|
|
|
self.pyramid_config.add_route('roles.view', '/roles/{uuid}')
|
2024-08-13 21:43:56 -05:00
|
|
|
model = self.app.model
|
|
|
|
auth = self.app.get_auth_handler()
|
|
|
|
admin = auth.get_role_administrator(self.session)
|
|
|
|
blokes = model.Role(name="Blokes")
|
|
|
|
self.session.add(blokes)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# nb. we let the field construct the widget via our type
|
|
|
|
node = colander.SchemaNode(RoleRefs(self.request, session=self.session))
|
|
|
|
field = self.make_field(node)
|
|
|
|
widget = field.widget
|
|
|
|
|
|
|
|
# readonly values list includes admin
|
|
|
|
html = widget.serialize(field, {admin.uuid, blokes.uuid}, readonly=True)
|
|
|
|
self.assertIn(admin.name, html)
|
|
|
|
self.assertIn(blokes.name, html)
|
|
|
|
|
|
|
|
# editable values list *excludes* admin (by default)
|
|
|
|
html = widget.serialize(field, {admin.uuid, blokes.uuid})
|
|
|
|
self.assertNotIn(admin.uuid, html)
|
|
|
|
self.assertIn(blokes.uuid, html)
|
|
|
|
|
|
|
|
# but admin is included for root user
|
|
|
|
self.request.is_root = True
|
|
|
|
html = widget.serialize(field, {admin.uuid, blokes.uuid})
|
|
|
|
self.assertIn(admin.uuid, html)
|
|
|
|
self.assertIn(blokes.uuid, html)
|
2024-08-14 15:10:54 -05:00
|
|
|
|
|
|
|
|
2024-08-21 14:38:34 -05:00
|
|
|
class TestUserRefsWidget(WebTestCase):
|
|
|
|
|
|
|
|
def make_field(self, node, **kwargs):
|
|
|
|
# TODO: not sure why default renderer is in use even though
|
|
|
|
# pyramid_deform was included in setup? but this works..
|
|
|
|
kwargs.setdefault('renderer', deform.Form.default_renderer)
|
|
|
|
return deform.Field(node, **kwargs)
|
|
|
|
|
|
|
|
def test_serialize(self):
|
|
|
|
model = self.app.model
|
|
|
|
|
|
|
|
# nb. we let the field construct the widget via our type
|
|
|
|
node = colander.SchemaNode(UserRefs(self.request, session=self.session))
|
|
|
|
field = self.make_field(node)
|
|
|
|
widget = field.widget
|
|
|
|
|
|
|
|
# readonly is required
|
|
|
|
self.assertRaises(NotImplementedError, widget.serialize, field, set())
|
|
|
|
self.assertRaises(NotImplementedError, widget.serialize, field, set(), readonly=False)
|
|
|
|
|
|
|
|
# empty
|
|
|
|
html = widget.serialize(field, set(), readonly=True)
|
2024-08-25 12:20:28 -05:00
|
|
|
self.assertEqual(html, '<span></span>')
|
2024-08-21 14:38:34 -05:00
|
|
|
|
|
|
|
# with data, no actions
|
|
|
|
user = model.User(username='barney')
|
|
|
|
self.session.add(user)
|
|
|
|
self.session.commit()
|
|
|
|
html = widget.serialize(field, {user.uuid}, readonly=True)
|
|
|
|
self.assertIn('<b-table ', html)
|
|
|
|
self.assertNotIn('Actions', html)
|
|
|
|
self.assertNotIn('View', html)
|
|
|
|
self.assertNotIn('Edit', html)
|
|
|
|
|
|
|
|
# with view/edit actions
|
|
|
|
with patch.object(self.request, 'is_root', new=True):
|
|
|
|
html = widget.serialize(field, {user.uuid}, readonly=True)
|
|
|
|
self.assertIn('<b-table ', html)
|
|
|
|
self.assertIn('Actions', html)
|
|
|
|
self.assertIn('View', html)
|
|
|
|
self.assertIn('Edit', html)
|
|
|
|
|
|
|
|
|
2024-08-14 15:10:54 -05:00
|
|
|
class TestPermissionsWidget(WebTestCase):
|
|
|
|
|
|
|
|
def make_field(self, node, **kwargs):
|
|
|
|
# TODO: not sure why default renderer is in use even though
|
|
|
|
# pyramid_deform was included in setup? but this works..
|
|
|
|
kwargs.setdefault('renderer', deform.Form.default_renderer)
|
|
|
|
return deform.Field(node, **kwargs)
|
|
|
|
|
|
|
|
def test_serialize(self):
|
|
|
|
permissions = {
|
|
|
|
'widgets': {
|
|
|
|
'label': "Widgets",
|
|
|
|
'perms': {
|
|
|
|
'widgets.polish': {
|
|
|
|
'label': "Polish the widgets",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# nb. we let the field construct the widget via our type
|
|
|
|
node = colander.SchemaNode(Permissions(self.request, permissions, session=self.session))
|
|
|
|
field = self.make_field(node)
|
|
|
|
widget = field.widget
|
|
|
|
|
|
|
|
# readonly output does *not* include the perm by default
|
|
|
|
html = widget.serialize(field, set(), readonly=True)
|
|
|
|
self.assertNotIn("Polish the widgets", html)
|
|
|
|
|
|
|
|
# readonly output includes the perm if set
|
|
|
|
html = widget.serialize(field, {'widgets.polish'}, readonly=True)
|
|
|
|
self.assertIn("Polish the widgets", html)
|
|
|
|
|
|
|
|
# editable output always includes the perm
|
|
|
|
html = widget.serialize(field, set())
|
|
|
|
self.assertIn("Polish the widgets", html)
|