# -*- coding: utf-8; -*- from unittest.mock import patch import colander import deform from pyramid import testing from wuttaweb import grids from wuttaweb.forms import widgets as mod from wuttaweb.forms.schema import FileDownload, PersonRef, RoleRefs, UserRefs, Permissions from tests.util import WebTestCase class TestObjectRefWidget(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 make_widget(self, **kwargs): return mod.ObjectRefWidget(self.request, **kwargs) 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)) widget = self.make_widget() field = self.make_field(node) html = widget.serialize(field, person.uuid) self.assertIn('', html) # path to nonexistent file html = widget.serialize(field, '/this/path/does/not/exist', readonly=True) self.assertNotIn('', html) # path to actual file datfile = self.write_file('data.txt', "hello\n" * 1000) html = widget.serialize(field, datfile, readonly=True) self.assertNotIn('', 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('', html) self.assertIn('', 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) class TestGridWidget(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): grid = grids.Grid(self.request, columns=['foo', 'bar'], data=[{'foo': 1, 'bar': 2}, {'foo': 3, 'bar': 4}]) node = colander.SchemaNode(colander.String()) widget = mod.GridWidget(self.request, grid) field = self.make_field(node) # readonly works okay html = widget.serialize(field, None, readonly=True) self.assertIn('') # 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('