def test_node_meta(self): node = cio.set('sv-se@page/title', u'', author=u'lundberg') self.assertEqual(node.meta.get('author'), u'lundberg') node = cio.get('page/title') self.assertEqual(len(node.meta.keys()), 0) # Cached node has no meta node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') cio.set('sv-se@page/title#1', u'', comment=u'This works!') node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'comment', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') self.assertEqual(meta.get('comment'), u'This works!') cio.set('sv-se@page/title#1', u'', comment=None) node = cio.load('sv-se@page/title#1') meta = node['meta'] self.assertKeys(meta, 'author', 'modified_at', 'published_at', 'is_published') self.assertEqual(meta.get('author'), u'lundberg') self.assertNotIn('comment', meta)
def get(self, request, uri): """ Load raw node source from storage. JSON Response: {uri: x, data: y} """ uri = self.decode_uri(uri) node = cio.load(uri) return self.render_to_json(node)
def post(self, request, uri): uri = self.decode_uri(uri) data, meta = self.get_post_data(request) meta['author'] = auth.get_username(request) node = cio.set(uri, data, publish=False, **meta) context = cio.load(node.uri) context['content'] = node.content if request.is_ajax(): return self.render_to_json(context) else: return self.render_plugin(request, context)
def test_replace_node_in_render_and_load(self): settings.configure(PLUGINS=[ 'cio.plugins.txt.TextPlugin', 'cio.plugins.md.MarkdownPlugin', 'tests.ReplacerPlugin' ]) node = cio.set('sv-se@page/mine.rpl#1', "My own content") self.assertNotEqual(node.uri.path, 'page/mine.rpl') self.assertEqual(node.uri.path, 'page/rendered.rpl') self.assertEqual(node.content, 'REPLACED') node = cio.load('sv-se@page/loaded.rpl') self.assertEqual(node['uri'].path, 'page/loaded')
def test_upload(self): tests_dir = os.path.dirname(os.path.abspath(__file__)) image_path = os.path.join(tests_dir, 'assets', 'image.png') form = { 'data[width]': u'64', 'data[height]': u'64', 'data[crop]': u'64,64,128,128', 'data[id]': u'vw', 'data[class]': u'year-53', 'data[alt]': u'Zwitter', 'meta[comment]': u'VW' } response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200) with open(image_path, "rb") as image: file = File(image, name=image_path) form['data[file]'] = file response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200) node = json_node(response, simple=False) meta = node.pop('meta') uri, content = node['uri'], node['content'] self.assertEqual(uri, 'i18n://sv-se@header/logo.img#draft') self.assertEqual(meta['comment'], u'VW') html = u'<img ' \ u'alt="Zwitter" ' \ u'class="year-53" ' \ u'height="64" ' \ u'id="vw" ' \ u'src="/media/djedi/img/03/5e/5eba6fc2149822a8dbf76cd6978798f2ddc4ac34.png" ' \ u'width="64" />' self.assertEqual(content, html) # Post new resized version node = cio.load(uri) del form['data[file]'] del form['data[crop]'] form['data[width]'] = form['data[height]'] = u'32' form['data[filename]'] = node['data']['filename'] response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200)
def test_upload(self): tests_dir = os.path.dirname(os.path.abspath(__file__)) image_path = os.path.join(tests_dir, 'assets', 'image.png') form = { 'data[width]': u'64', 'data[height]': u'64', 'data[crop]': u'64,64,128,128', 'data[id]': u'vw', 'data[class]': u'year-53', 'data[alt]': u'Zwitter', 'meta[comment]': u'VW' } response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200) with open(image_path) as image: file = File(image, name=image_path) form['data[file]'] = file response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200) node = json_node(response, simple=False) meta = node.pop('meta') uri, content = node['uri'], node['content'] self.assertEqual(uri, 'i18n://sv-se@header/logo.img#draft') self.assertEqual(meta['comment'], u'VW') html = u'<img ' \ u'alt="Zwitter" ' \ u'class="year-53" ' \ u'height="64" ' \ u'id="vw" ' \ u'src="/media/djedi/img/03/5e/5eba6fc2149822a8dbf76cd6978798f2ddc4ac34.png" ' \ u'width="64" />' self.assertEqual(content, html) # Post new resized version node = cio.load(uri) del form['data[file]'] del form['data[crop]'] form['data[width]'] = form['data[height]'] = u'32' form['data[filename]'] = node['data']['filename'] response = self.post('api', 'i18n://sv-se@header/logo.img', form) self.assertEqual(response.status_code, 200)
def test_revisions(self): def assertRevisions(*revs): revisions = set(cio.revisions('i18n://sv-se@page/title')) assert revisions == set(revs) self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0) node = cio.load('sv-se@page/title') self.assertDictEqual(node, { 'uri': 'i18n://sv-se@page/title.txt', 'data': None, 'content': None, 'meta': {} }) # First draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.txt', u'Content-IO', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft') assertRevisions(('i18n://sv-se@page/title.txt#draft', False)) self.assertIsNone(cio.get('page/title').content) # Publish first draft, version 1 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Second draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up', u'Content-IO - Fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#draft', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Publish second draft, version 2 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Alter published version 2 with self.assertDB(calls=2, selects=1, inserts=0, updates=1): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up#2', u'Content-IO - Lightening fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Not published, still in cache # Re-publish version 2, no change with self.assertDB(selects=1, inserts=0, updates=0): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!') # Rollback version 1 with self.assertDB(calls=3, selects=1, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish('i18n://sv-se@page/title#1') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#2', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Assert get specific version doesn't mess up the cache cache.clear() with self.assertCache(calls=0): self.assertEqual(cio.get('page/title#2').content, u'CONTENT-IO - LIGHTENING FAST!') with self.assertCache(calls=2, misses=1, sets=1): self.assertEqual(cio.get('page/title').content, u'Content-IO') # Load version 1 and 2 data = cio.load('sv-se@page/title#1') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO') data = cio.load('sv-se@page/title#2') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2') self.assertEqual(data['data'], {u'name': u'Content-IO - Lightening fast!'}) # Load without version and expect published version data = cio.load('sv-se@page/title') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO')
def test_revisions(self): def assertRevisions(*revs): revisions = set(cio.revisions('i18n://sv-se@page/title')) assert revisions == set(revs) self.assertEqual(len(set(cio.revisions('i18n://sv-se@page/title'))), 0) node = cio.load('sv-se@page/title') self.assertDictEqual( node, { 'uri': 'i18n://sv-se@page/title.txt', 'data': None, 'content': None, 'meta': {} }) # First draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.txt', u'Content-IO', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#draft') assertRevisions(('i18n://sv-se@page/title.txt#draft', False)) self.assertIsNone(cio.get('page/title').content) # Publish first draft, version 1 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Second draft with self.assertDB(selects=1, inserts=1, updates=0): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up', u'Content-IO - Fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#draft') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#draft', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Publish second draft, version 2 with self.assertDB(calls=4, selects=2, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual(cio.get('page/title').content, u'CONTENT-IO - FAST!') # Alter published version 2 with self.assertDB(calls=2, selects=1, inserts=0, updates=1): with self.assertCache(calls=0): node = cio.set('i18n://sv-se@page/title.up#2', u'Content-IO - Lightening fast!', publish=False) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual( cio.get('page/title').content, u'CONTENT-IO - FAST!') # Not published, still in cache # Re-publish version 2, no change with self.assertDB(selects=1, inserts=0, updates=0): with self.assertCache(calls=1, sets=1): node = cio.publish(node.uri) self.assertEqual(node.uri, 'i18n://sv-se@page/title.up#2') assertRevisions(('i18n://sv-se@page/title.txt#1', False), ('i18n://sv-se@page/title.up#2', True)) self.assertEqual( cio.get('page/title').content, u'CONTENT-IO - LIGHTENING FAST!') # Rollback version 1 with self.assertDB(calls=3, selects=1, updates=2): with self.assertCache(calls=1, sets=1): node = cio.publish('i18n://sv-se@page/title#1') self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt#1') assertRevisions(('i18n://sv-se@page/title.txt#1', True), ('i18n://sv-se@page/title.up#2', False)) self.assertEqual(cio.get('page/title').content, u'Content-IO') # Assert get specific version doesn't mess up the cache cache.clear() with self.assertCache(calls=0): self.assertEqual( cio.get('page/title#2').content, u'CONTENT-IO - LIGHTENING FAST!') with self.assertCache(calls=2, misses=1, sets=1): self.assertEqual(cio.get('page/title').content, u'Content-IO') # Load version 1 and 2 data = cio.load('sv-se@page/title#1') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO') data = cio.load('sv-se@page/title#2') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.up#2') self.assertEqual(data['data'], {u'name': u'Content-IO - Lightening fast!'}) # Load without version and expect published version data = cio.load('sv-se@page/title') self.assertEqual(data['uri'], 'i18n://sv-se@page/title.txt#1') self.assertEqual(data['data'], u'Content-IO')