def test_w_groups(self): self._register() karltesting.registerDummySecurityPolicy('admin', ['group.community:yum:bar']) context = testing.DummyModel() profiles = context['profiles'] = testing.DummyModel() profiles['admin'] = testing.DummyModel() yum = testing.DummyModel() context['yum'] = yum yum.title = 'Yum!' context.catalog = karltesting.DummyCatalog({1:'/foo'}) foo = testing.DummyModel() karltesting.registerModels({'/foo':foo}) request = testing.DummyRequest( params={'titlestartswith':'A'}, layout_manager=mock.Mock()) info = self._callFUT(context, request) communities = info['communities'] self.assertEqual(len(communities), 1) self.assertEqual(communities[0].context, foo) self.failUnless(communities) self.failUnless(info['actions']) _checkCookie(request, 'all') add_portlet = request.layout_manager.layout.add_portlet self.assertEqual(add_portlet.call_count, 1) self.assertEqual(add_portlet.call_args[0][0], 'my_communities')
def test_with_tag_multiple_users(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory context = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/foo': context}) directlyProvides(context, IDummyBlogEntry) registerContentFactory(testing.DummyModel, IDummyBlogEntry) tags = context.tags = DummyTags(users=('dummy', 'dummy2')) def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community is None assert user is None return [] tags.getRelatedTags = _getRelated context.catalog = karltesting.DummyCatalog({1: '/foo'}) request = testing.DummyRequest() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/') self.assertEqual(entry['tagusers_count'], '2 people') self.assertEqual(entry['type'], 'Blog Entry') self.assertEqual(entry['tagusers_href'], 'http://example.com/tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 0)
def test_with_tag_multiple_users(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory context = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/foo': context}) directlyProvides(context, IDummyBlogEntry) registerContentFactory(testing.DummyModel, IDummyBlogEntry) tags = context.tags = DummyTags(users=('dummy', 'dummy2')) def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community is None assert user is None return [] tags.getRelatedTags = _getRelated context.catalog = karltesting.DummyCatalog({1:'/foo'}) request = testing.DummyRequest() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/') self.assertEqual(entry['tagusers_count'], '2 people') self.assertEqual(entry['type'], 'Blog Entry') self.assertEqual(entry['tagusers_href'], 'http://example.com/tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 0)
def test_excludes_inactive(self): from datetime import datetime from datetime import timedelta from karl.utils import coarse_datetime_repr now = datetime.now() today = now.today() six_months_ago = today - timedelta(days=180) self._set_TODAY(today) self._register() context = testing.DummyModel() profiles = context['profiles'] = testing.DummyModel() profiles[None] = testing.DummyModel() catalog = context.catalog = karltesting.DummyCatalog( {1: '/foo', 2: '/bar'}) foo = testing.DummyModel(content_modified=now - timedelta(1)) bar = testing.DummyModel(content_modified=now - timedelta(32)) karltesting.registerModels({'/foo': foo, '/bar': bar, }) request = testing.DummyRequest() info = self._callFUT(context, request) self.assertEqual(len(catalog.queries), 1) query = catalog.queries[0] self.assertEqual(query['content_modified'], (coarse_datetime_repr(six_months_ago), None)) communities = info['communities'] self.assertEqual(len(communities), 2) self.assertEqual(communities[0].context, foo) self.assertEqual(communities[1].context, bar) self.failUnless(info['actions']) _checkCookie(request, 'active')
def test_with_both_params_w_tags_tool(self): root = testing.DummyModel() root.catalog = karltesting.DummyCatalog({1:'/community/target'}) context = root['community'] = testing.DummyModel() target = context['target'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/community/target': target}) tags = root.tags = DummyTags() def _getUsers(tags=None, items=None, community=None): assert list(tags) == ['tag1'] assert list(items) == [1] assert community == 'community' return ['phred'] tags.getUsers = _getUsers def _getTags(items=None, users=None, community=None): assert list(items) == [1] assert list(users) == ['phred'] assert community == 'community' return ['tag2'] tags.getTags = _getTags profiles = root['profiles'] = testing.DummyModel() p1 = profiles['phred'] = testing.DummyModel() p1.firstname, p1.lastname = 'J. Phred', 'Bloggs' request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} result = self._callFUT(context, request) self.assertEqual(len(result['users']), 1) self.assertEqual(result['users'][0]['login'], 'phred') self.assertEqual(result['users'][0]['fullname'], 'J. Phred Bloggs') self.assertEqual(result['users'][0]['also'], ['tag2'])
def _makeCommunity(self): from zope.interface import directlyProvides from karl.models.interfaces import ICommunity community = testing.DummyModel(title='thetitle') directlyProvides(community, ICommunity) foo = testing.DummyModel(__name__='foo') catalog = karltesting.DummyCatalog({1:'/foo'}) karltesting.registerModels({'/foo':foo}) community.catalog = catalog return community
def _makeCommunity(self): from zope.interface import directlyProvides from karl.models.interfaces import ICommunity community = testing.DummyModel(title="thetitle") community.member_names = community.moderator_names = set() directlyProvides(community, ICommunity) foo = testing.DummyModel(__name__="foo") catalog = karltesting.DummyCatalog({1: "/foo"}) karltesting.registerModels({"/foo": foo}) community.catalog = catalog return community
def _makeCommunity(self): from zope.interface import directlyProvides from karl.models.interfaces import ICommunity community = testing.DummyModel(title="thetitle") catalog = karltesting.DummyCatalog({1: "/profiles/phred", 2: "/profiles/bharney", 3: "/profiles/wylma"}) phred = testing.DummyModel(__name__="phred") bharney = testing.DummyModel(__name__="bharney") wylma = testing.DummyModel(__name__="wylma") karltesting.registerModels({"/profiles/phred": phred, "/profiles/bharney": bharney, "/profiles/wylma": wylma}) community.catalog = catalog community.member_names = set(["phred", "bharney", "wylma"]) community.moderator_names = set(["bharney"]) directlyProvides(community, ICommunity) return community
def test_with_both_params_no_tags_tool(self): root = testing.DummyModel() root.catalog = karltesting.DummyCatalog({1: '/community/target'}) context = root['community'] = testing.DummyModel() target = context['target'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/community/target': target}) request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} result = self._callFUT(context, request) self.assertEqual(result['tag'], 'tag1') self.assertEqual(result['url'], 'http://example.com/community/target/') self.assertEqual(result['title'], 'Target') self.assertEqual(len(result['users']), 0)
def test_with_both_params_no_tags_tool(self): root = testing.DummyModel() root.catalog = karltesting.DummyCatalog({1:'/community/target'}) context = root['community'] = testing.DummyModel() target = context['target'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/community/target': target}) request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} result = self._callFUT(context, request) self.assertEqual(result['tag'], 'tag1') self.assertEqual(result['url'], 'http://example.com/community/target/') self.assertEqual(result['title'], 'Target') self.assertEqual(len(result['users']), 0)
def test_wo_groups(self): self._register() context = testing.DummyModel() profiles = context['profiles'] = testing.DummyModel() profiles[None] = testing.DummyModel() context.catalog = karltesting.DummyCatalog({1: '/foo'}) foo = testing.DummyModel() karltesting.registerModels({'/foo': foo}) request = testing.DummyRequest(params={'titlestartswith': 'A'}) info = self._callFUT(context, request) communities = info['communities'] self.assertEqual(len(communities), 1) self.assertEqual(communities[0].context, foo) self.failUnless(communities) self.failUnless(info['actions']) _checkCookie(request, 'all')
def test_with_tag_multiple_users(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory from karl.models.interfaces import IProfile root = testing.DummyModel() tags = root.tags = DummyTags() def _getUsers(tags=None, items=None, community=None): assert list(tags) == ['tag1'] assert list(items) == [1] assert community is None return ('dummy', 'dummy2') tags.getUsers = _getUsers def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community is None assert user == 'phred' return [] tags.getRelatedTags = _getRelated root.catalog = karltesting.DummyCatalog({1: '/profiles/phred'}) profiles = root['profiles'] = testing.DummyModel() context = profiles['phred'] = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/profiles/phred': context}) directlyProvides(context, IProfile) registerContentFactory(testing.DummyModel, IProfile) request = testing.DummyRequest() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/profiles/phred/') self.assertEqual(entry['tagusers_count'], '2 people') self.assertEqual(entry['type'], 'Person') self.assertEqual( entry['tagusers_href'], 'http://example.com/profiles/phred/' 'tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 0)
def test_wo_groups(self): self._register() context = testing.DummyModel() profiles = context['profiles'] = testing.DummyModel() profiles[None] = testing.DummyModel() context.catalog = karltesting.DummyCatalog({1:'/foo'}) foo = testing.DummyModel() karltesting.registerModels({'/foo':foo}) request = testing.DummyRequest( params={'titlestartswith':'A'}) info = self._callFUT(context, request) communities = info['communities'] self.assertEqual(len(communities), 1) self.assertEqual(communities[0].context, foo) self.failUnless(communities) self.failUnless(info['actions']) _checkCookie(request, 'all')
def test_with_both_params_no_tags_tool(self): root = testing.DummyModel() path = root['path'] = testing.DummyModel() to = path['to'] = testing.DummyModel() target = to['item'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/path/to/item': target}) request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} context = testing.DummyModel() context.catalog = DummyCatalog() result = self._callFUT(context, request) self.assertEqual(result['tag'], 'tag1') self.assertEqual(result['url'], 'http://example.com/path/to/item/') self.assertEqual(result['title'], 'Target') self.assertEqual(len(result['users']), 0)
def test_with_tag_multiple_users(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory from karl.models.interfaces import IProfile root = testing.DummyModel() tags = root.tags = DummyTags() def _getUsers(tags=None, items=None, community=None): assert list(tags) == ['tag1'] assert list(items) == [1] assert community is None return ('dummy', 'dummy2') tags.getUsers = _getUsers def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community is None assert user == 'phred' return [] tags.getRelatedTags = _getRelated root.catalog = karltesting.DummyCatalog({1:'/profiles/phred'}) profiles = root['profiles'] = testing.DummyModel() context = profiles['phred'] = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/profiles/phred': context}) directlyProvides(context, IProfile) registerContentFactory(testing.DummyModel, IProfile) request = testing.DummyRequest() request.layout_manager = mock.Mock() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/profiles/phred/') self.assertEqual(entry['tagusers_count'], '2 people') self.assertEqual(entry['type'], 'Person') self.assertEqual(entry['tagusers_href'], 'http://example.com/profiles/phred/' 'tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 0)
def test_unfound_model(self): from pyramid.interfaces import IDebugLogger class DummyLogger: def warn(self, msg): self.msg = msg logger = DummyLogger() karltesting.registerUtility(logger, IDebugLogger) a = testing.DummyModel() b = testing.DummyModel() karltesting.registerModels({'/a':a}) context = testing.DummyModel() context.catalog = karltesting.DummyCatalog({1:'/a', 2:'/b'}) adapter = self._makeOne(context) num, docids, resolver = adapter() self.assertEqual(num, 2) self.assertEqual(list(docids), [1, 2]) results = filter(None, map(resolver, docids)) self.assertEqual(results, [a]) self.assertEqual(logger.msg, 'Model missing: /b')
def _makeCommunity(self): from zope.interface import directlyProvides from karl.models.interfaces import ICommunity community = testing.DummyModel(title='thetitle') catalog = karltesting.DummyCatalog({1: '/profiles/phred', 2: '/profiles/bharney', 3: '/profiles/wylma', }) phred = testing.DummyModel(__name__='phred') bharney = testing.DummyModel(__name__='bharney') wylma = testing.DummyModel(__name__='wylma') karltesting.registerModels({'/profiles/phred':phred, '/profiles/bharney':bharney, '/profiles/wylma':wylma, }) community.catalog = catalog community.member_names = set(['phred', 'bharney', 'wylma']) community.moderator_names = set(['bharney']) directlyProvides(community, ICommunity) return community
def test_recent_items(self): from zope.interface import Interface from zope.interface import directlyProvides from karl.models.interfaces import ICommunity from karl.models.interfaces import ICatalogSearch from karl.models.interfaces import IGridEntryInfo karltesting.registerAdapter(DummySearchAdapter, Interface, ICatalogSearch) karltesting.registerAdapter(DummyAdapter, (Interface, Interface), IGridEntryInfo) f = testing.DummyModel() karltesting.registerModels({'/communities/1/file':f}) context = testing.DummyModel() directlyProvides(context, ICommunity) context.searchresults = [f] context.catalog = karltesting.DummyCatalog({1:'/communities/1/file'}) request = testing.DummyRequest() api = self._makeOne(context, request) recent_items = api.recent_items self.assertEqual(len(recent_items), 1) self.assertEqual(recent_items[0].context, f)
def test_it(self): from zope.interface import Interface from karl.models.interfaces import ICommunity import datetime context = testing.DummyModel(title='thetitle') directlyProvides(context, ICommunity) context.member_names = context.moderator_names = set() context.title = "Community" foo = testing.DummyModel() foo.modified = datetime.datetime(2009, 9, 2, 10, 28, 0) request = testing.DummyRequest() karltesting.registerDummyRenderer( 'karl.views:templates/atomfeed.pt') from karl.models.interfaces import ICatalogSearch from karl.models.adapters import CatalogSearch catalog = karltesting.DummyCatalog({1:'/foo'}) karltesting.registerModels({'/foo':foo}) context.catalog = catalog karltesting.registerAdapter(CatalogSearch, (Interface), ICatalogSearch) self._callFUT(context, request)
def test_recent_items(self): from zope.interface import Interface from zope.interface import directlyProvides from karl.models.interfaces import ICommunity from karl.models.interfaces import ICatalogSearch from karl.models.interfaces import IGridEntryInfo karltesting.registerAdapter(DummySearchAdapter, Interface, ICatalogSearch) karltesting.registerAdapter(DummyAdapter, (Interface, Interface), IGridEntryInfo) f = testing.DummyModel() karltesting.registerModels({'/communities/1/file': f}) context = testing.DummyModel() directlyProvides(context, ICommunity) context.searchresults = [f] context.catalog = karltesting.DummyCatalog({1: '/communities/1/file'}) request = testing.DummyRequest() api = self._makeOne(context, request) recent_items = api.recent_items self.assertEqual(len(recent_items), 1) self.assertEqual(recent_items[0].context, f)
def test_w_groups(self): self._register() karltesting.registerDummySecurityPolicy('admin', ['group.community:yum:bar']) context = testing.DummyModel() profiles = context['profiles'] = testing.DummyModel() profiles['admin'] = testing.DummyModel() yum = testing.DummyModel() context['yum'] = yum yum.title = 'Yum!' context.catalog = karltesting.DummyCatalog({1: '/foo'}) foo = testing.DummyModel() karltesting.registerModels({'/foo': foo}) request = testing.DummyRequest(params={'titlestartswith': 'A'}) info = self._callFUT(context, request) communities = info['communities'] self.assertEqual(len(communities), 1) self.assertEqual(communities[0].context, foo) self.failUnless(communities) self.failUnless(info['actions']) _checkCookie(request, 'all')
def test_with_tag(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory from karl.models.interfaces import ICommunity root = testing.DummyModel() tags = root.tags = DummyTags() def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community == 'community' assert user is None return ['tag2', 'tag3'] tags.getRelatedTags = _getRelated root.catalog = karltesting.DummyCatalog({1:'/community'}) context = root['community'] = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/community': context}) directlyProvides(context, ICommunity) registerContentFactory(testing.DummyModel, ICommunity) request = testing.DummyRequest() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/community/') self.assertEqual(entry['tagusers_count'], '1 person') self.assertEqual(entry['type'], 'Community') self.assertEqual(entry['tagusers_href'], 'http://example.com/community/' 'tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 2) self.assertEqual(result['related'][0], 'tag2') self.assertEqual(result['related'][1], 'tag3')
def test_with_tag(self): from zope.interface import directlyProvides from repoze.lemonade.testing import registerContentFactory from karl.models.interfaces import ICommunity root = testing.DummyModel() tags = root.tags = DummyTags() def _getRelated(tag, degree=1, community=None, user=None): assert tag == 'tag1' assert degree == 1 assert community == 'community' assert user is None return ['tag2', 'tag3'] tags.getRelatedTags = _getRelated root.catalog = karltesting.DummyCatalog({1: '/community'}) context = root['community'] = testing.DummyModel() context.title = 'title' karltesting.registerModels({'/community': context}) directlyProvides(context, ICommunity) registerContentFactory(testing.DummyModel, ICommunity) request = testing.DummyRequest() request.subpath = [u'tag1'] result = self._callFUT(context, request) self.assertEqual(result['tag'], u'tag1') self.assertEqual(len(result['entries']), 1) entry = result['entries'][0] self.assertEqual(entry['description'], '') self.assertEqual(entry['title'], 'title') self.assertEqual(entry['href'], 'http://example.com/community/') self.assertEqual(entry['tagusers_count'], '1 person') self.assertEqual(entry['type'], 'Community') self.assertEqual( entry['tagusers_href'], 'http://example.com/community/' 'tagusers.html?tag=tag1&docid=1') self.assertEqual(len(result['related']), 0)
def test_with_both_params_w_tags_tool(self): context = testing.DummyModel() context.catalog = DummyCatalog() tags = context.tags = DummyTags() tags.getUsers = lambda tags=None, items=None, community=None: ['phred'] tags.getTags = lambda items=None, users=None, community=None: ['tag2'] path = context['path'] = testing.DummyModel() to = path['to'] = testing.DummyModel() target = to['item'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/path/to/item': target}) profiles = context['profiles'] = testing.DummyModel() p1 = profiles['phred'] = testing.DummyModel() p1.firstname, p1.lastname = 'J. Phred', 'Bloggs' request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} result = self._callFUT(context, request) self.assertEqual(len(result['users']), 1) self.assertEqual(result['users'][0]['login'], 'phred') self.assertEqual(result['users'][0]['fullname'], 'J. Phred Bloggs') self.assertEqual(result['users'][0]['also'], ['tag2'])
def test_with_both_params_w_tags_tool(self): root = testing.DummyModel() root.catalog = karltesting.DummyCatalog({1: '/community/target'}) context = root['community'] = testing.DummyModel() target = context['target'] = testing.DummyModel() target.title = 'Target' karltesting.registerModels({'/community/target': target}) tags = root.tags = DummyTags() def _getUsers(tags=None, items=None, community=None): assert list(tags) == ['tag1'] assert list(items) == [1] assert community == 'community' return ['phred'] tags.getUsers = _getUsers def _getTags(items=None, users=None, community=None): assert list(items) == [1] assert list(users) == ['phred'] assert community == 'community' return ['tag2'] tags.getTags = _getTags profiles = root['profiles'] = testing.DummyModel() p1 = profiles['phred'] = testing.DummyModel() p1.firstname, p1.lastname = 'J. Phred', 'Bloggs' request = testing.DummyRequest() request.params = {'tag': 'tag1', 'docid': '1'} result = self._callFUT(context, request) self.assertEqual(len(result['users']), 1) self.assertEqual(result['users'][0]['login'], 'phred') self.assertEqual(result['users'][0]['fullname'], 'J. Phred Bloggs') self.assertEqual(result['users'][0]['also'], ['tag2'])