def test_add_document_triggers_indexing(solr, db_session, request): get_root()['doc'] = Document(title=u'foo', body=u'bar!', description=u'foo!') db_session.flush() results = list(solr.query(title='foo')) assert len(results) == 1 assert results[0]['description'] == 'foo!' assert results[0]['path'] == request.resource_path(get_root()['doc'])
def grid_widget(context, request): show_in_context = get_setting(u'show_in_context') show = False if show_in_context == u'everywhere': show = True elif show_in_context == u'only on root': show = context == get_root() elif show_in_context == u'not on root': show = context != get_root() elif show_in_context == u'nowhere': show = False if show: from js.jquery_colorpicker import jquery_colorpicker from js.gridster import gridster jquery_colorpicker.need() gridster.need() return { 'tiles': grid_settings()['tiles'], 'tile_content': tile_content, 'show': show, 'dimension_x': get_setting(u'dimension_x', 150), 'dimension_y': get_setting(u'dimension_y', 150), 'margin_x': get_setting(u'margin_x', 10), 'margin_y': get_setting(u'margin_y', 10) }
def test_clear(self, db_session): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node child = get_root()['child'] = Node() assert DBSession.query(Node).filter(Node.name == u'child').all() == [ child] get_root().clear() assert DBSession.query(Node).filter(Node.name == u'child').all() == []
def show_in_context(setting, context): """Check if the the item should be shown in the given context.""" show = False if setting == u'everywhere': show = True elif setting == u'only on root': show = context == get_root() elif setting == u'not on root': show = context != get_root() return show
def show_in_context(setting, context): """Check if the the item should be shown in the given context.""" show = False if setting == u'everywhere': show = True elif setting == u'only on root': show = context == get_root() elif setting == u'not on root': show = context != get_root() return show
def test_add_document_triggers_indexing(solr, db_session, request): get_root()['doc'] = Document(title=u'foo', body=u'bar!', description=u'foo!') db_session.flush() results = list(solr.query(title='foo')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'foo!' assert results[0]['path'] == request.resource_path(get_root()['doc'])
def test_clear(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node child = get_root()['child'] = Node() assert DBSession.query(Node).filter(Node.name == u'child').all() == [ child] get_root().clear() assert DBSession.query(Node).filter(Node.name == u'child').all() == []
def test_delete_document_triggers_unindexing(solr, db_session, request): get_root()['doc'] = Document(title=u'delete-me', description=u'bar!') db_session.flush() results = list(solr.query(title='delete-me')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'bar!' del(get_root()['doc']) db_session.flush() results = list(solr.query(title='delete-me')) assert len(results) == 0
def test_delete_document_triggers_unindexing(solr, db_session, request): get_root()['doc'] = Document(title=u'delete-me', description=u'bar!') db_session.flush() results = list(solr.query(title='delete-me')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'bar!' del (get_root()['doc']) db_session.flush() results = list(solr.query(title='delete-me')) assert len(results) == 0
def test_update_document_triggers_reindexing(solr, db_session, request): get_root()['doc'] = Document(title=u'bar', description=u'bar!') db_session.flush() results = list(solr.query(title='bar')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'bar!' assert results[0]['path'] == request.resource_path(get_root()['doc']) get_root()['doc'].description = u'blah!' db_session.flush() results = list(solr.query(title='bar')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'blah!' assert results[0]['path'] == request.resource_path(get_root()['doc'])
def test_update_document_triggers_reindexing(solr, db_session, request): get_root()['doc'] = Document(title=u'bar', description=u'bar!') db_session.flush() results = list(solr.query(title='bar')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'bar!' assert results[0]['path'] == request.resource_path(get_root()['doc']) get_root()['doc'].description = u'blah!' db_session.flush() results = list(solr.query(title='bar')) assert len(results) == 1 assert results[0]['id'] == u'document-2' assert results[0]['description'] == 'blah!' assert results[0]['path'] == request.resource_path(get_root()['doc'])
def test_user_delete(self, events, extra_principals): from kotti.resources import get_root from kotti.security import get_principals from kotti.views.users import user_delete root = get_root() request = DummyRequest() bob = get_principals()[u'bob'] request.params['name'] = u'' user_delete(root, request) assert request.session.pop_flash('error') == [u'No name given.'] assert u'bob' in get_principals().keys() request.params['name'] = u'bob' result = user_delete(root, request) assert u'api' in result api = result[u'api'] assert api.principal == bob assert api.principal_type == u'User' assert u'bob' in get_principals().keys() request.params['name'] = u'john' request.params['delete'] = u'delete' user_delete(root, request) assert request.session.pop_flash('error') == [u"User not found."] assert u'bob' in get_principals().keys() request.params['name'] = u'bob' request.params['delete'] = u'delete' user_delete(root, request) with pytest.raises(KeyError): get_principals()[u'bob']
def content(connection, settings): """sets up some default content using Kotti's testing populator.""" import transaction from kotti import DBSession from kotti import metadata from kotti.resources import get_root if connection.in_transaction(): transaction.abort() DBSession().close() metadata.drop_all(connection.engine) transaction.begin() metadata.create_all(connection.engine) # to create the default content with the correct workflow state # the workflow must be initialized first; please note that these # settings won't persist, though; use the `workflow` fixture if needed from zope.configuration import xmlconfig import kotti xmlconfig.file("workflow.zcml", kotti, execute=True) for populate in settings["kotti.populators"]: populate() # We set the path here since it's required for some integration # tests, and because the 'content' fixture does not depend on # 'event' and therefore the event handlers aren't fired for root # otherwise: get_root().path = "/" transaction.commit()
def test_search(self, extra_principals): from kotti.resources import get_root from kotti.security import get_principals from kotti.views.users import users_manage root = get_root() request = DummyRequest() P = get_principals() request.params['search'] = u'' request.params['query'] = u'Joe' entries = users_manage(root, request)['entries'] assert len(entries) == 0 assert (request.session.pop_flash('info') == [u'No users or groups found.']) request.params['query'] = u'Bob' entries = users_manage(root, request)['entries'] assert entries[0][0] == P['bob'] assert entries[0][1] == ([], []) assert entries[1][0] == P['group:bobsgroup'] assert entries[1][1] == ([], []) P[u'bob'].groups = [u'group:bobsgroup'] P[u'group:bobsgroup'].groups = [u'role:admin'] entries = users_manage(root, request)['entries'] assert (entries[0][1] == (['group:bobsgroup', 'role:admin'], ['role:admin'])) assert entries[1][1] == (['role:admin'], [])
def test_softwarecollection_view_no_project(self): root = get_root() softwarecollection = root['softwarecollection'] = SoftwareCollection() view = SoftwareCollectionView(root['softwarecollection'], DummyRequest()).view() assert view is not None assert ('items' in view) and (len(view['items']) == 0) assert ('settings' in view) \ and ('use_batching' in view['settings']) \ and (view['settings']['use_batching'] is True) assert ('settings' in view) \ and ('pagesize' in view['settings']) \ and (view['settings']['pagesize'] == 5) assert ('settings' in view) \ and ('use_auto_batching' in view['settings']) \ and (view['settings']['use_auto_batching'] is True) assert ('settings' in view) \ and ('link_headline_overview' in view['settings']) \ and (view['settings']['link_headline_overview'] is True) assert (('settings' in view) \ and ('use_batching' in view['settings']) \ and (view['settings']['use_batching'] is True))
def test_association_proxy(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Tag, TagsToContents, Content root = get_root() root[u'content_1'] = Content() root[u'content_1'].tags = [ u'tag 1', u'tag 2', ] assert root[u'content_1'].tags == [ u'tag 1', u'tag 2', ] assert type(root[u'content_1']._tags[0]) == TagsToContents assert type(root[u'content_1']._tags[0].tag) == Tag assert root[u'content_1']._tags[0].tag.title == u'tag 1' assert root[u'content_1']._tags[0].position == 0 assert root[u'content_1']._tags[1].tag.title == u'tag 2' assert root[u'content_1']._tags[1].position == 1 assert len(root[u'content_1']._tags) == 2 root[u'content_2'] = Content() root[u'content_2'].tags = [u'tag 1', u'tag 3'] assert len(root[u'content_2']._tags) == 2 assert root[u'content_2']._tags[0].tag.title == u'tag 1' assert root[u'content_2']._tags[0].position == 0 assert root[u'content_2']._tags[1].tag.title == u'tag 3' assert root[u'content_2']._tags[1].position == 1 assert len(DBSession.query(Tag).all()) == 3
def test_map_principals_with_local_roles(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import map_principals_with_local_roles self.test_principals_with_local_roles() root = get_root() child = root[u'child'] P = get_principals() # No users are defined in P, thus we get the empty list: self.assertEqual(map_principals_with_local_roles(root), []) P['bob'] = {'name': u'bob'} P['group:bobsgroup'] = {'name': u'group:bobsgroup'} value = map_principals_with_local_roles(root) self.assertEqual(len(value), 1) bob, (bob_all, bob_inherited) = value[0] self.assertEqual(bob_all, ['group:bobsgroup']) self.assertEqual(bob_inherited, []) value = map_principals_with_local_roles(child) self.assertEqual(len(value), 2) bob, (bob_all, bob_inherited) = value[0] bobsgroup, (bobsgroup_all, bobsgroup_inherited) = value[1] self.assertEqual(set(bob_all), set(['group:bobsgroup', 'role:editor'])) self.assertEqual(set(bob_inherited), set(['group:bobsgroup', 'role:editor'])) self.assertEqual(bobsgroup_all, ['role:editor']) self.assertEqual(bobsgroup_inherited, [])
def test_inherit(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node from kotti.security import list_groups from kotti.security import list_groups_raw from kotti.security import set_groups root = get_root() child = root[u'child'] = Node() DBSession.flush() self.assertEqual(list_groups('bob', child), []) set_groups('bob', root, ['role:editor']) self.assertEqual(list_groups('bob', child), ['role:editor']) # Groups from the child are added: set_groups('bob', child, ['group:somegroup']) self.assertEqual( set(list_groups('bob', child)), set(['group:somegroup', 'role:editor']) ) # We can ask to list only those groups that are defined locally: self.assertEqual( list_groups_raw(u'bob', child), set(['group:somegroup']))
def test_unique_constraint(self): # Try to add two children with the same name to the root node: session = DBSession() root = get_root() session.add(Node(name=u'child1', parent=root)) session.add(Node(name=u'child1', parent=root)) self.assertRaises(IntegrityError, session.flush)
def add_some_groups(): session = DBSession() root = get_root() child = root[u'child'] = Node() grandchild = child[u'grandchild'] = Node() session.flush() # root: # bob -> group:bobsgroup # frank -> group:franksgroup # group:franksgroup -> role:editor # child: # group:bobsgroup -> group:franksgroup # grandchild: # group:franksgroup -> role:admin # group:franksgroup -> group:bobsgroup # bob and frank are a site-wide members of their respective groups: set_groups('bob', root, ['group:bobsgroup']) set_groups('frank', root, ['group:franksgroup']) # franksgroup has a site-wide editor role: set_groups('group:franksgroup', root, ['role:editor']) # bobsgroup is part of franksgroup on the child level: set_groups('group:bobsgroup', child, ['group:franksgroup']) # franksgroup has the admin role on the grandchild. # and finally, to test recursion, we make franksgroup part of # bobsgroup on the grandchild level: set_groups('group:franksgroup', grandchild, ['role:owner', 'group:bobsgroup'])
def test_map_principals_with_local_roles(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import map_principals_with_local_roles self.test_principals_with_local_roles() root = get_root() child = root[u'child'] P = get_principals() # No users are defined in P, thus we get the empty list: self.assertEqual(map_principals_with_local_roles(root), []) P['bob'] = {'name': u'bob'} P['group:bobsgroup'] = {'name': u'group:bobsgroup'} value = map_principals_with_local_roles(root) self.assertEqual(len(value), 1) bob, (bob_all, bob_inherited) = value[0] self.assertEqual(bob_all, ['group:bobsgroup']) self.assertEqual(bob_inherited, []) value = map_principals_with_local_roles(child) self.assertEqual(len(value), 2) bob, (bob_all, bob_inherited) = value[0] bobsgroup, (bobsgroup_all, bobsgroup_inherited) = value[1] self.assertEqual(set(bob_all), set(['group:bobsgroup', 'role:editor'])) self.assertEqual(set(bob_inherited), set(['group:bobsgroup', 'role:editor'])) self.assertEqual(bobsgroup_all, ['role:editor']) self.assertEqual(bobsgroup_inherited, [])
def test_node_copy_name(self, db_session): from kotti.resources import get_root root = get_root() copy_of_root = root.copy(name=u'copy_of_root') assert copy_of_root.name == u'copy_of_root' assert root.name == u''
def test_copy_local_groups(self): from kotti import DBSession from kotti.resources import get_root from kotti.security import principals_with_local_roles from kotti.security import set_groups self.test_principals_with_local_roles() root = get_root() child = root[u'child'] self.assertEqual(set(principals_with_local_roles(child)), set(['bob', 'group:bobsgroup', 'group:franksgroup'])) # We make a copy of 'child', and we expect the local roles set # on 'child' to not be copied over: child2 = root['child2'] = child.copy() DBSession.flush() self.assertEqual( set(principals_with_local_roles(child2)), set([u'bob', u'group:franksgroup']), ) self.assertEqual(len(principals_with_local_roles(child)), 3) # When we now change the local roles of 'child', the copy is # unaffected: set_groups('group:bobsgroup', child, []) self.assertEqual(len(principals_with_local_roles(child)), 2) self.assertEqual(len(principals_with_local_roles(child2)), 2)
def test_principals_with_local_roles(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node from kotti.security import map_principals_with_local_roles from kotti.security import principals_with_local_roles from kotti.security import set_groups root = get_root() child = root[u'child'] = Node() DBSession.flush() self.assertEqual(principals_with_local_roles(root), []) self.assertEqual(principals_with_local_roles(child), []) self.assertEqual(map_principals_with_local_roles(root), []) self.assertEqual(map_principals_with_local_roles(child), []) set_groups('group:bobsgroup', child, ['role:editor']) set_groups('bob', root, ['group:bobsgroup']) set_groups('group:franksgroup', root, ['role:editor']) self.assertEqual(set(principals_with_local_roles(child)), set(['bob', 'group:bobsgroup', 'group:franksgroup'])) self.assertEqual( set(principals_with_local_roles(child, inherit=False)), set(['group:bobsgroup'])) self.assertEqual(set(principals_with_local_roles(root)), set(['bob', 'group:franksgroup']))
def test_show_dropdown_menus(self): request = NavigationDummyRequest(path='/some-navigation-widget-left') root = get_root() result = navigation_widget_items(root, request) c1 = root[u'content_1'] = Content(title=u'Content_1') c1[u'sub_1'] = Content(title=u'Sub_1') c1[u'sub_1'][u'sub_sub_1'] = Content(title=u'Sub_Sub_1') c2 = root[u'content_2'] = Content(title=u'Content_2') c2[u'sub_2'] = Content(title=u'Sub_2') html = render_view( c1, NavigationDummyRequest(path='/some-navigation-widget-left'), name='navigation-widget-items-left') assert not u'nav-list-careted' in html st = get_current_registry().settings st['kotti_navigation.navigation_widget.left_display_type'] = u'hor_pills_with_dropdowns' html = render_view( c1, NavigationDummyRequest(path='/some-navigation-widget-left'), name='navigation-widget-items-left') assert u'nav-list-careted' in html
def test_apply(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import list_groups from kotti.tests.test_node_views import TestNodeShare from kotti.views.users import users_manage root = get_root() request = DummyRequest() TestNodeShare.add_some_principals() bob = get_principals()[u'bob'] request.params['apply'] = u'' users_manage(root, request) self.assertEqual(request.session.pop_flash('info'), [u'No changes made.']) self.assertEqual(list_groups('bob'), []) bob.groups = [u'role:special'] request.params['role::bob::role:owner'] = u'1' request.params['role::bob::role:editor'] = u'1' request.params['orig-role::bob::role:owner'] = u'' request.params['orig-role::bob::role:editor'] = u'' users_manage(root, request) self.assertEqual(request.session.pop_flash('success'), [u'Your changes have been saved.']) self.assertEqual(set(list_groups('bob')), set(['role:owner', 'role:editor', 'role:special']))
def test_apply(self): from kotti.views.users import share_node root = get_root() request = DummyRequest() self.add_some_principals() request.params['apply'] = u'' share_node(root, request) self.assertEqual( request.session.pop_flash('notice'), [u'No changes made.']) self.assertEqual(list_groups('bob', root), []) set_groups('bob', root, ['role:special']) request.params['role::bob::role:owner'] = u'1' request.params['role::bob::role:editor'] = u'1' request.params['orig-role::bob::role:owner'] = u'' request.params['orig-role::bob::role:editor'] = u'' share_node(root, request) self.assertEqual( request.session.pop_flash('success'), [u'Your changes have been saved.']) self.assertEqual( set(list_groups('bob', root)), set(['role:owner', 'role:editor', 'role:special'])) # We cannot set a role that's not displayed, even if we forged # the request: request.params['role::bob::role:admin'] = u'1' request.params['orig-role::bob::role:admin'] = u'' self.assertRaises(Forbidden, share_node, root, request) self.assertEqual( set(list_groups('bob', root)), set(['role:owner', 'role:editor', 'role:special']))
def test_render_widget(self): root = get_root() html = render_view( root, NavigationDummyRequest(path='/some-navigation-widget-left'), name='navigation-widget-items-left') assert ' class="nav nav-pills"' in html
def test_search(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.tests.test_node_views import TestNodeShare from kotti.views.users import users_manage root = get_root() request = DummyRequest() P = get_principals() TestNodeShare.add_some_principals() request.params['search'] = u'' request.params['query'] = u'Joe' entries = users_manage(root, request)['entries'] self.assertEqual(len(entries), 0) self.assertEqual(request.session.pop_flash('info'), [u'No users or groups found.']) request.params['query'] = u'Bob' entries = users_manage(root, request)['entries'] self.assertEqual(entries[0][0], P['bob']) self.assertEqual(entries[0][1], ([], [])) self.assertEqual(entries[1][0], P['group:bobsgroup']) self.assertEqual(entries[1][1], ([], [])) P[u'bob'].groups = [u'group:bobsgroup'] P[u'group:bobsgroup'].groups = [u'role:admin'] entries = users_manage(root, request)['entries'] self.assertEqual(entries[0][1], (['group:bobsgroup', 'role:admin'], ['role:admin'])) self.assertEqual(entries[1][1], (['role:admin'], []))
def test_groups_from_users(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node from kotti.security import list_groups from kotti.security import set_groups self.make_bob() root = get_root() child = root[u'child'] = Node() DBSession.flush() self.assertEqual(list_groups('bob', root), ['group:bobsgroup']) set_groups('group:bobsgroup', root, ['role:editor']) set_groups('role:editor', child, ['group:foogroup']) self.assertEqual( set(list_groups('bob', root)), set(['group:bobsgroup', 'role:editor']) ) self.assertEqual( set(list_groups('bob', child)), set(['group:bobsgroup', 'role:editor', 'group:foogroup']) )
def test_apply(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import list_groups from kotti.tests.test_node_views import TestNodeShare from kotti.views.users import users_manage root = get_root() request = DummyRequest() TestNodeShare.add_some_principals() bob = get_principals()[u'bob'] request.params['apply'] = u'' users_manage(root, request) self.assertEqual(request.session.pop_flash('info'), [u'No changes made.']) self.assertEqual(list_groups('bob'), []) bob.groups = [u'role:special'] request.params['role::bob::role:owner'] = u'1' request.params['role::bob::role:editor'] = u'1' request.params['orig-role::bob::role:owner'] = u'' request.params['orig-role::bob::role:editor'] = u'' users_manage(root, request) self.assertEqual(request.session.pop_flash('success'), [u'Your changes have been saved.']) self.assertEqual( set(list_groups('bob')), set(['role:owner', 'role:editor', 'role:special']) )
def test_softwarecollection_view_adding_project(self): root = get_root() softwarecollection = root['softwarecollection'] = SoftwareCollection() view = SoftwareCollectionView(root['softwarecollection'], DummyRequest()).view() softwareproject = softwarecollection[ 'softwareproject'] = SoftwareProject() assert softwareproject is not None assert view is not None assert ('items' in view) batch = view['items'] assert type(batch) is plone.batching.batch.BaseBatch assert ('api' in view) \ and (type(view['api']) is kotti.views.util.TemplateAPI) assert ('settings' in view) \ and ('use_batching' in view['settings']) \ and (view['settings']['use_batching'] is True) assert ('settings' in view) \ and ('pagesize' in view['settings']) \ and (view['settings']['pagesize'] == 5) assert ('settings' in view) \ and ('use_auto_batching' in view['settings']) \ and (view['settings']['use_auto_batching'] is True) assert ('settings' in view) \ and ('link_headline_overview' in view['settings']) \ and (view['settings']['link_headline_overview'] is True)
def test_apply(self): from kotti.resources import get_root from kotti.security import list_groups from kotti.security import set_groups from kotti.views.users import share_node root = get_root() request = DummyRequest() self.add_some_principals() request.params["apply"] = u"" share_node(root, request) self.assertEqual(request.session.pop_flash("info"), [u"No changes made."]) self.assertEqual(list_groups("bob", root), []) set_groups("bob", root, ["role:special"]) request.params["role::bob::role:owner"] = u"1" request.params["role::bob::role:editor"] = u"1" request.params["orig-role::bob::role:owner"] = u"" request.params["orig-role::bob::role:editor"] = u"" share_node(root, request) self.assertEqual(request.session.pop_flash("success"), [u"Your changes have been saved."]) self.assertEqual(set(list_groups("bob", root)), set(["role:owner", "role:editor", "role:special"])) # We cannot set a role that's not displayed, even if we forged # the request: request.params["role::bob::role:admin"] = u"1" request.params["orig-role::bob::role:admin"] = u"" self.assertRaises(Forbidden, share_node, root, request) self.assertEqual(set(list_groups("bob", root)), set(["role:owner", "role:editor", "role:special"]))
def test_get_content_items_for_tag_title(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Tag, TagsToContents, Content ses = DBSession root = get_root() root[u'folder_1'] = Content() root[u'folder_1'].tags = [u'first tag', u'second tag'] root[u'folder_1'][u'content_1'] = Content() root[u'folder_1'][u'content_1'].tags = [ u'third tag', ] root[u'folder_1'][u'content_2'] = Content() root[u'folder_1'][u'content_2'].tags = [u'first tag', u'third tag'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'first tag').all() assert [res.name for res in result] == [u'folder_1', u'content_2'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'second tag').all() assert [res.name for res in result] == [u'folder_1'] result = ses.query(Content).join(TagsToContents).join(Tag).filter( Tag.title == u'third tag').all() assert [res.name for res in result] == [u'content_1', u'content_2']
def test_node_lineage_not_loaded_new_parent(self, db_session, root, events): from kotti.resources import Node from kotti.resources import get_root from kotti.events import ObjectEvent from kotti.events import objectevent_listeners # We want to guarantee that an object event handler can call # get_root(), which is only possible if our event handler # avoids flushing: objectevent_listeners[(ObjectEvent, Node)].append( lambda event: get_root()) parent = root['parent'] = Node() child1 = parent['child-1'] = Node() db_session.flush() child1_id = child1.id db_session.expunge_all() child2 = Node(name=u'child-2') child3 = Node(name=u'child-3') child1 = db_session.query(Node).get(child1_id) child3.parent = child2 child2.parent = child1 assert child3.path == u"/parent/child-1/child-2/child-3/"
def test_apply(self): from kotti.resources import get_root from kotti.security import list_groups from kotti.security import set_groups from kotti.views.users import share_node root = get_root() request = DummyRequest() self.add_some_principals() request.params['apply'] = u'' share_node(root, request) self.assertEqual(request.session.pop_flash('info'), [u'No changes made.']) self.assertEqual(list_groups('bob', root), []) set_groups('bob', root, ['role:special']) request.params['role::bob::role:owner'] = u'1' request.params['role::bob::role:editor'] = u'1' request.params['orig-role::bob::role:owner'] = u'' request.params['orig-role::bob::role:editor'] = u'' share_node(root, request) self.assertEqual(request.session.pop_flash('success'), [u'Your changes have been saved.']) self.assertEqual(set(list_groups('bob', root)), set(['role:owner', 'role:editor', 'role:special'])) # We cannot set a role that's not displayed, even if we forged # the request: request.params['role::bob::role:admin'] = u'1' request.params['orig-role::bob::role:admin'] = u'' self.assertRaises(Forbidden, share_node, root, request) self.assertEqual(set(list_groups('bob', root)), set(['role:owner', 'role:editor', 'role:special']))
def test_register_submit_groups_and_roles(self, db_session): from kotti.resources import get_root from kotti.views.login import register from pyramid.httpexceptions import HTTPFound root = get_root() request = DummyRequest() request.POST['title'] = u'Test User' request.POST['name'] = u'test' request.POST['email'] = u'*****@*****.**' request.POST['register'] = u'register', with patch('kotti.views.login.UserAddFormView') as form: with patch('kotti.views.login.get_settings') as get_settings: get_settings.return_value = { 'kotti.register.group': 'mygroup', 'kotti.register.role': 'myrole', } res = register(root, request) form.assert_has_calls([ call().add_user_success({ 'name': u'test', 'roles': set([u'role:myrole']), 'title': u'Test User', 'send_email': True, 'groups': [u'mygroup'], 'email': u'*****@*****.**', }) ]) assert (isinstance(res, HTTPFound))
def test_use_auto_pagination_view(kotti_blog_populate_settings, db_session, dummy_request): from kotti.resources import get_root from kotti_blog.views import use_auto_pagination result = use_auto_pagination(get_root(), dummy_request) assert result['use_auto_pagination'] is False
def test_node_lineage_not_loaded_new_parent(self, db_session, root, events): from kotti.resources import Node from kotti.resources import get_root from kotti.events import ObjectEvent from kotti.events import objectevent_listeners # We want to guarantee that an object event handler can call # get_root(), which is only possible if our event handler # avoids flushing: objectevent_listeners[(ObjectEvent, Node)].append( lambda event: get_root()) parent = root['parent'] = Node() child1 = parent['child-1'] = Node() db_session.flush() child1_id = child1.id db_session.expunge_all() child2 = Node(name=u'child-2') child3 = Node(name=u'child-3') child1 = db_session.query(Node).get(child1_id) child3.parent = child2 child2.parent = child1 assert child3.path == u"/parent/child-1/child-2/child-3/"
def test_apply(self, extra_principals): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import list_groups from kotti.views.users import users_manage root = get_root() request = DummyRequest() bob = get_principals()[u'bob'] request.params['apply'] = u'' users_manage(root, request) assert (request.session.pop_flash('info') == [u'No changes made.']) assert list_groups('bob') == [] bob.groups = [u'role:special'] request.params['role::bob::role:owner'] = u'1' request.params['role::bob::role:editor'] = u'1' request.params['orig-role::bob::role:owner'] = u'' request.params['orig-role::bob::role:editor'] = u'' users_manage(root, request) assert (request.session.pop_flash('success') == [u'Your changes have been saved.']) assert ( set(list_groups('bob')) == set(['role:owner', 'role:editor', 'role:special']) )
def test_node_copy_name(self): from kotti.resources import get_root root = get_root() copy_of_root = root.copy(name=u'copy_of_root') self.assertEqual(copy_of_root.name, u'copy_of_root') self.assertEqual(root.name, u'')
def test_search(self): from kotti.resources import get_root from kotti.security import get_principals from kotti.tests.test_node_views import TestNodeShare from kotti.views.users import users_manage root = get_root() request = DummyRequest() P = get_principals() TestNodeShare.add_some_principals() request.params['search'] = u'' request.params['query'] = u'Joe' entries = users_manage(root, request)['entries'] self.assertEqual(len(entries), 0) self.assertEqual(request.session.pop_flash('info'), [u'No users or groups found.']) request.params['query'] = u'Bob' entries = users_manage(root, request)['entries'] self.assertEqual(entries[0][0], P['bob']) self.assertEqual(entries[0][1], ([], [])) self.assertEqual(entries[1][0], P['group:bobsgroup']) self.assertEqual(entries[1][1], ([], [])) P[u'bob'].groups = [u'group:bobsgroup'] P[u'group:bobsgroup'].groups = [u'role:admin'] entries = users_manage(root, request)['entries'] self.assertEqual(entries[0][1], (['group:bobsgroup', 'role:admin'], ['role:admin'])) self.assertEqual(entries[1][1], (['role:admin'], []))
def test_map_principals_with_local_roles(self, db_session): from kotti.resources import get_root from kotti.security import get_principals from kotti.security import map_principals_with_local_roles self.test_principals_with_local_roles(db_session) root = get_root() child = root[u'child'] P = get_principals() # No users are defined in P, thus we get the empty list: assert map_principals_with_local_roles(root) == [] P['bob'] = {'name': u'bob'} P['group:bobsgroup'] = {'name': u'group:bobsgroup'} value = map_principals_with_local_roles(root) assert len(value) == 1 bob, (bob_all, bob_inherited) = value[0] assert bob_all == ['group:bobsgroup'] assert bob_inherited == [] value = map_principals_with_local_roles(child) assert len(value) == 2 bob, (bob_all, bob_inherited) = value[0] bobsgroup, (bobsgroup_all, bobsgroup_inherited) = value[1] assert (set(bob_all) == set(['group:bobsgroup', 'role:editor'])) assert (set(bob_inherited) == set(['group:bobsgroup', 'role:editor'])) assert bobsgroup_all == ['role:editor'] assert bobsgroup_inherited == []
def test_view(self): from kotti_calendar.widgets import upcoming_events from kotti.resources import get_root from kotti.workflow import get_workflow from kotti_calendar.resources import Calendar from kotti_calendar.resources import Event now = datetime.now() root = get_root() result = upcoming_events(root, DummyRequest()) assert len(result['events']) == 0 root['calendar'] = Calendar() root['calendar']['event1'] = Event(title=u'Event 1', start=now - timedelta(1), end=now + timedelta(2)) root['calendar']['event2'] = Event(title=u'Event 2', start=now + timedelta(1), end=now + timedelta(2)) wf = get_workflow(root) wf.transition_to_state(root['calendar']['event1'], None, u'public') wf.transition_to_state(root['calendar']['event2'], None, u'public') result = upcoming_events(root, DummyRequest()) events = result['events'] assert len(events) == 2 assert events[0].title == u'Event 1' assert events[0].start == now - timedelta(1) assert events[1].title == u'Event 2' assert events[1].start == now + timedelta(1)
def test_render_widget(self): root = get_root() html = render_view( root, NavigationDummyRequest(path='/some-navigation-widget-top'), name='navigation-widget-tree-top') assert '<ul class="nav nav-tabs nav-stacked">' in html
def test_render_widget(self): root = get_root() set_nav_setting('left', 'display_type', 'tree') set_nav_setting('left', 'display_manner', 'tabs') set_nav_setting('left', 'options', ['stacked', 'include_root']) html = render_view(root, self.request, name='navigation-widget-tree') assert '<ul class="nav nav-tabs nav-stacked">' in html
def test_copy_local_groups(self): from kotti import DBSession from kotti.resources import get_root from kotti.security import principals_with_local_roles from kotti.security import set_groups self.test_principals_with_local_roles() root = get_root() child = root[u'child'] self.assertEqual( set(principals_with_local_roles(child)), set(['bob', 'group:bobsgroup', 'group:franksgroup']) ) # We make a copy of 'child', and we expect the local roles set # on 'child' to not be copied over: child2 = root['child2'] = child.copy() DBSession.flush() self.assertEqual( set(principals_with_local_roles(child2)), set([u'bob', u'group:franksgroup']), ) self.assertEqual(len(principals_with_local_roles(child)), 3) # When we now change the local roles of 'child', the copy is # unaffected: set_groups('group:bobsgroup', child, []) self.assertEqual(len(principals_with_local_roles(child)), 2) self.assertEqual(len(principals_with_local_roles(child2)), 2)
def test_tween(self, webtest, filedepot, root, image_asset, db_session): from kotti.resources import Image, get_root # create an image resource img = root['img'] = Image(data=image_asset.read(), title='Image') db_session.flush() root = get_root() img = root['img'] # the image resource itself is served by the full Kotti stack resp = webtest.app.get('/img') assert resp.content_type == 'text/html' assert resp.etag is None assert resp.cache_control.max_age == 0 assert '<img src="http://localhost/img/image" />' in resp.body # the attachments (created by the filters) are served by the # FiledepotServeApp resp = webtest.app.get(img.data['thumb_128x128_url']) assert resp.etag is not None assert resp.cache_control.max_age == 604800 assert resp.body.startswith('\x89PNG') resp = webtest.app.get(img.data['thumb_256x256_url']) assert resp.etag is not None assert resp.cache_control.max_age == 604800 assert resp.body.startswith('\x89PNG')
def test_include_content_types(self): root = get_root() set_nav_setting('left', 'display_type', 'tree') set_nav_setting('left', 'options', ['stacked', 'include_root']) root[u'content_1'] = Content() root[u'content_2'] = Content() root[u'content_2'].in_navigation = False # If we include the content type the nav item is present. set_nav_setting('left', 'include', [Content.type_info.name]) html = render_view(root, self.request, name='navigation-widget-tree') assert u'content_1' in html assert u'content_2' not in html # With an empty include_content_types, the nav item is not present. set_nav_setting('left', 'include', []) html = render_view(root, self.request, name='navigation-widget-tree') assert u'content_1' in html assert u'content_2' not in html # Again, with show_hidden True. set_nav_setting('left', 'options', ['stacked', 'include_root', 'show_hidden_while_logged_in']) with patch('kotti_navigation.util.the_user', return_value='admin'): html = render_view(root, self.request, name='navigation-widget-tree') assert u'content_1' in html assert u'content_2' in html
def test_node_copy_name(self, db_session): from kotti.resources import get_root root = get_root() copy_of_root = root.copy(name=u'copy_of_root') assert copy_of_root.name == u'copy_of_root' assert root.name == u''
def test_render_widget(self): root = get_root() self.request = NavigationDummyRequest(slot='top') set_nav_setting('top', 'options', ['stacked', 'include_root']) html = render_view(root, self.request, name='navigation-widget-tree') assert '<ul class="nav nav-pills nav-stacked">' in html
def test_newsitem(db_session, config): config.include('kotti_newsitem') root = get_root() content = NewsItem() assert content.type_info.addable(root, DummyRequest()) is True root['content'] = content
def test_navigation_widget_no_view(kn_populate, db_session, dummy_request, events): from pyramid.exceptions import PredicateMismatch root = get_root() navigation = Navigation(root, dummy_request) with pytest.raises(PredicateMismatch): navigation.navigation_widget()
def test_association_proxy(self, db_session, events): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Tag, TagsToContents, Content root = get_root() root[u'content_1'] = Content() root[u'content_1'].tags = [u'tag 1', u'tag 2'] assert root[u'content_1'].tags == [u'tag 1', u'tag 2'] assert type(root[u'content_1']._tags[0]) == TagsToContents assert type(root[u'content_1']._tags[0].tag) == Tag assert root[u'content_1']._tags[0].tag.title == u'tag 1' assert root[u'content_1']._tags[0].position == 0 assert root[u'content_1']._tags[1].tag.title == u'tag 2' assert root[u'content_1']._tags[1].position == 1 assert len(root[u'content_1']._tags) == 2 root[u'content_2'] = Content() root[u'content_2'].tags = [u'tag 1', u'tag 3'] assert len(root[u'content_2']._tags) == 2 assert root[u'content_2']._tags[0].tag.title == u'tag 1' assert root[u'content_2']._tags[0].position == 0 assert root[u'content_2']._tags[1].tag.title == u'tag 3' assert root[u'content_2']._tags[1].position == 1 assert len(DBSession.query(Tag).all()) == 3
def test_principals_with_local_roles(self): from kotti import DBSession from kotti.resources import get_root from kotti.resources import Node from kotti.security import map_principals_with_local_roles from kotti.security import principals_with_local_roles from kotti.security import set_groups root = get_root() child = root[u'child'] = Node() DBSession.flush() self.assertEqual(principals_with_local_roles(root), []) self.assertEqual(principals_with_local_roles(child), []) self.assertEqual(map_principals_with_local_roles(root), []) self.assertEqual(map_principals_with_local_roles(child), []) set_groups('group:bobsgroup', child, ['role:editor']) set_groups('bob', root, ['group:bobsgroup']) set_groups('group:franksgroup', root, ['role:editor']) self.assertEqual( set(principals_with_local_roles(child)), set(['bob', 'group:bobsgroup', 'group:franksgroup']) ) self.assertEqual( set(principals_with_local_roles(child, inherit=False)), set(['group:bobsgroup']) ) self.assertEqual( set(principals_with_local_roles(root)), set(['bob', 'group:franksgroup']) )