def test_validjson(): app.cfg.namespace_mapping = [(u'', 'default_backend'), (u'ns1/', 'default_backend'), (u'ns1/ns2/', 'other_backend')] item = Item.create(u'ns1/ns2/existingname') meta = {NAMESPACE: u'ns1/ns2', CONTENTTYPE: u'text/plain;charset=utf-8'} become_trusted() item._save(meta, data='This is a valid Item.') valid_itemid = 'a1924e3d0a34497eab18563299d32178' # ('names', 'namespace', 'field', 'value', 'result') tests = [([u'somename', u'@revid'], '', '', 'somename', False), ([u'bar', u'ns1'], '', '', 'bar', False), ([u'foo', u'foo', u'bar'], '', '', 'foo', False), ([u'ns1ns2ns3', u'ns1/subitem'], '', '', 'valid', False), ([u'foobar', u'validname'], '', ITEMID, valid_itemid + '8080', False), ([u'barfoo', u'validname'], '', ITEMID, valid_itemid.replace('a', 'y'), False), ([], '', 'itemid', valid_itemid, True), ([u'existingname'], 'ns1/ns2', '', 'existingname', False), ] for name, namespace, field, value, result in tests: meta = {NAME: name, NAMESPACE: namespace} x = JSON(json.dumps(meta)) y = Names(name) state = dict(fqname=CompositeName(namespace, field, value), itemid=None, meta=meta) value = x.validate(state) and y.validate(state) assert value == result
def test_validjson(): """ Tests for changes to metadata when modifying an item. Does not apply to usersettings form. """ app.cfg.namespace_mapping = [(u'', 'default_backend'), (u'ns1/', 'default_backend'), (u'ns1/ns2/', 'other_backend')] item = Item.create(u'ns1/ns2/existingname') meta = {NAMESPACE: u'ns1/ns2', CONTENTTYPE: u'text/plain;charset=utf-8'} become_trusted() item._save(meta, data='This is a valid Item.') valid_itemid = 'a1924e3d0a34497eab18563299d32178' # ('names', 'namespace', 'field', 'value', 'result') tests = [([u'somename', u'@revid'], '', '', 'somename', False), # item names cannot begin with @ # TODO for above? - create item @x, get error message, change name in meta to xx, get an item with names @40x and alias of xx ([u'bar', u'ns1'], '', '', 'bar', False), # item names cannot match namespace names ([u'foo', u'foo', u'bar'], '', '', 'foo', False), # names in the name list must be unique. ([u'ns1ns2ns3', u'ns1/subitem'], '', '', 'valid', False), # Item names must not match with existing namespaces; items cannot be in 2 namespaces ([u'foobar', u'validname'], '', ITEMID, valid_itemid + '8080', False), # attempts to change itemid in meta result in "Item(s) named foobar, validname already exist." ([u'barfoo', u'validname'], '', ITEMID, valid_itemid.replace('a', 'y'), False), # similar to above ([], '', 'itemid', valid_itemid, True), # deleting all names from the metadata of an existing item will make it nameless, succeeds ([u'existingname'], 'ns1/ns2', '', 'existingname', False), # item already exists ] for name, namespace, field, value, result in tests: meta = {NAME: name, NAMESPACE: namespace} x = JSON(json.dumps(meta)) y = Names(name) state = dict(fqname=CompositeName(namespace, field, value), itemid=None, meta=meta) value = x.validate(state) and y.validate(state) assert value == result
def test_validjson(): """ Tests for changes to metadata when modifying an item. Does not apply to usersettings form. """ app.cfg.namespace_mapping = [(u'', 'default_backend'), (u'ns1/', 'default_backend'), (u'ns1/ns2/', 'other_backend')] item = Item.create(u'ns1/ns2/existingname') meta = {NAMESPACE: u'ns1/ns2', CONTENTTYPE: u'text/plain;charset=utf-8'} become_trusted() item._save(meta, data='This is a valid Item.') valid_itemid = 'a1924e3d0a34497eab18563299d32178' # ('names', 'namespace', 'field', 'value', 'result') tests = [ ([u'somename', u'@revid'], '', '', 'somename', False), # item names cannot begin with @ # TODO for above? - create item @x, get error message, change name in meta to xx, get an item with names @40x and alias of xx ([u'bar', u'ns1'], '', '', 'bar', False ), # item names cannot match namespace names ([u'foo', u'foo', u'bar'], '', '', 'foo', False), # names in the name list must be unique. ( [u'ns1ns2ns3', u'ns1/subitem'], '', '', 'valid', False ), # Item names must not match with existing namespaces; items cannot be in 2 namespaces ( [u'foobar', u'validname'], '', ITEMID, valid_itemid + '8080', False ), # attempts to change itemid in meta result in "Item(s) named foobar, validname already exist." ([u'barfoo', u'validname'], '', ITEMID, valid_itemid.replace('a', 'y'), False), # similar to above ( [], '', 'itemid', valid_itemid, True ), # deleting all names from the metadata of an existing item will make it nameless, succeeds ([u'existingname'], 'ns1/ns2', '', 'existingname', False), # item already exists ] for name, namespace, field, value, result in tests: meta = {NAME: name, NAMESPACE: namespace} x = JSON(json.dumps(meta)) y = Names(name) state = dict(fqname=CompositeName(namespace, field, value), itemid=None, meta=meta) value = x.validate(state) and y.validate(state) assert value == result
class _ModifyForm(BaseModifyForm): """ ModifyForm (the form used on +modify view), sans the content part. Combined dynamically with the ModifyForm of the Content subclass in Contentful.ModifyForm. Subclasses of Contentful should generally override this instead of ModifyForm. """ meta_form = BaseMetaForm extra_meta_text = JSON.using( label=L_("Extra MetaData (JSON)")).with_properties(rows=ROWS_META, cols=COLS) meta_template = 'modify_meta.html' def _load(self, item): """ Load metadata and data from :item into :self. Used by BaseModifyForm.from_item. """ meta = item.prepare_meta_for_modify(item.meta) # Default value of `policy` argument of Flatland.Dict.set's is # 'strict', which causes KeyError to be thrown when meta contains # meta keys that are not present in self['meta_form']. Setting # policy to 'duck' suppresses this behavior. if 'acl' not in meta: meta['acl'] = "None" self['meta_form'].set(meta, policy='duck') for k in self['meta_form'].field_schema_mapping.keys( ) + IMMUTABLE_KEYS: meta.pop(k, None) self['extra_meta_text'].set(item.meta_dict_to_text(meta)) self['content_form']._load(item.content) def _dump(self, item): """ Dump useful data out of :self. :item contains the old item and should not be the primary data source; but it can be useful in case the data in :self is not sufficient. :returns: a tuple (meta, data, contenttype_guessed, comment), suitable as arguments of the same names to pass to item.modify """ # Since the metadata form for tickets is an incomplete one, we load the # original meta and update it with those from the metadata editor # e.g. we get PARENTID in here meta = item.meta_filter(item.prepare_meta_for_modify(item.meta)) meta.update(self['meta_form'].value) meta.update(item.meta_text_to_dict(self['extra_meta_text'].value)) data, contenttype_guessed = self['content_form']._dump( item.content) comment = self['comment'].value return meta, data, contenttype_guessed, comment