def _mk_site(self): from karl.models.interfaces import ICommunity from karl.testing import DummyCommunity from karl.testing import DummyModel from karl.testing import DummyRoot from zope.interface import directlyProvides search_results = { 'daniela': [], 'stough': [], 'fred': [], } self.site = site = DummyRoot() communities = site['communities'] = DummyModel() profiles = site['profiles'] = DummyModel() profiles['daniela'] = DummyModel() profiles['stough'] = DummyModel() profiles['fred'] = DummyModel() big_endians = communities['big_endians'] = DummyCommunity() self.big_endians = big_endians directlyProvides(big_endians, ICommunity) content = big_endians['wiki1'] = DummyModel() content.created = datetime.datetime(2010, 5, 12, 3, 42) search_results['daniela'].append(content) content = big_endians['wiki2'] = DummyModel() content.created = datetime.datetime(1975, 7, 7, 7, 23) search_results['fred'].append(content) little_endians = communities['little_endians'] = DummyCommunity() self.little_endians = little_endians directlyProvides(little_endians, ICommunity) content = little_endians['blog1'] = DummyModel() content.created = datetime.datetime(2010, 5, 12, 3, 43) search_results['stough'].append(content) content['comment1'] = DummyModel() content = content['comment1'] content.created = datetime.datetime(2010, 5, 12, 3, 44) search_results['stough'].append(content) content['comment2'] = DummyModel() content = content['comment2'] content.created = datetime.datetime(2010, 5, 12, 3, 45) search_results['daniela'].append(content) self.registerCatalogSearch(DummySearchAdapterFactory(search_results))
def test_user_home_path_w_subpath(self): from zope.interface import Interface from zope.interface import directlyProvides from pyramid.interfaces import ITraverserFactory from karl.testing import DummyCommunity from karl.testing import DummyProfile karl.testing.registerDummySecurityPolicy("userid") c = DummyCommunity() site = c.__parent__.__parent__ directlyProvides(site, Interface) c["foo"] = foo = testing.DummyModel() site["profiles"] = profiles = testing.DummyModel() profiles["userid"] = profile = DummyProfile() profile.home_path = "/communities/community/foo/bar/baz" karl.testing.registerAdapter(dummy_traverser_factory, Interface, ITraverserFactory) from karl.views.utils import get_user_home request = testing.DummyRequest() # Test from site root target, extra_path = get_user_home(site, request) self.failUnless(foo is target) self.assertEqual(['bar', 'baz'], extra_path) # Test from arbitrary point in tree target, extra_path = get_user_home(c, request) self.failUnless(foo is target) self.assertEqual(['bar', 'baz'], extra_path)
def setUp(self): cleanUp() # Set up a dummy community files folder from karl.content.interfaces import ICommunityRootFolder from karl.testing import DummyCommunity from karl.testing import DummyProfile from zope.interface import directlyProvides community = DummyCommunity() files = community["files"] = testing.DummyModel() directlyProvides(files, ICommunityRootFolder) site = community.__parent__.__parent__ profiles = site["profiles"] = testing.DummyModel() chris = profiles["chris"] = DummyProfile() chris.title = "Chris Rossi" self.context = files # Register dummy catalog from zope.interface import Interface from karl.models.interfaces import ICatalogSearch karl.testing.registerAdapter(dummy_catalog_search, Interface, ICatalogSearch) # Register atom entry adapter from karl.views.interfaces import IAtomEntry from karl.content.views.atom import CommunityFileAtomEntry karl.testing.registerAdapter(CommunityFileAtomEntry, (Interface, Interface), IAtomEntry) karl.testing.registerDummyRenderer('karl.views:templates/atomfeed.pt')
def _makeContext(self): from karl.testing import DummyCommunity community = DummyCommunity() community.__parent__.__parent__.sessions = DummySessions() context = community['testing'] = testing.DummyModel() context['profiles'] = testing.DummyModel() return context
def test_weekly_digests(self): from repoze.sendmail.interfaces import IMailDelivery from pyramid.interfaces import IRequest from karl.models.interfaces import IProfile from karl.testing import DummyMailer mailer = DummyMailer() self.config.registry.registerUtility(mailer, IMailDelivery) community = DummyCommunity() community["foo"] = context = testing.DummyModel() directlyProvides(context, IDummy) site = community.__parent__.__parent__ site["profiles"] = profiles = testing.DummyModel() profiles["a"] = DummyProfile() profiles["a"].set_alerts_preference(community.__name__, IProfile.ALERT_DAILY_DIGEST) profiles["b"] = DummyProfile() profiles["b"].set_alerts_preference(community.__name__, IProfile.ALERT_WEEKLY_DIGEST) profiles["c"] = DummyProfile() profiles["c"].set_alerts_preference(community.__name__, IProfile.ALERT_BIWEEKLY_DIGEST) community.member_names = set(("a", "b", "c")) community.moderator_names = set() request = testing.DummyRequest() self.config.registry.registerAdapter(DummyEmailAlertAdapter, (IDummy, IProfile, IRequest), IAlert) tool = self._get_instance() tool.emit(context, request) tool.emit(context, request) self.assertEqual(0, len(mailer)) self.assertEqual(2, len(list(profiles["a"]._pending_alerts))) self.assertEqual(2, len(list(profiles["b"]._pending_alerts))) self.assertEqual(2, len(list(profiles["c"]._pending_alerts))) self.config.testing_add_renderer('karl.utilities:email_digest.pt') tool.send_digests(site, "weekly") self.assertEqual(0, len(list(profiles["a"]._pending_alerts))) self.assertEqual(0, len(list(profiles["b"]._pending_alerts))) self.assertEqual(2, len(list(profiles["c"]._pending_alerts))) self.assertEqual(2, len(mailer)) self.assertEqual(0, len(list(list(profiles["a"]._pending_alerts)))) self.assertEqual(0, len(list(list(profiles["b"]._pending_alerts)))) mtos = [x.mto for x in mailer] self.assertTrue([ '*****@*****.**', ] in mtos) self.assertTrue([ '*****@*****.**', ] in mtos)
def test_otherpage(self): self._register() context = DummyWikiPage(title='Other Page') context.__name__ = 'other_page' from karl.testing import DummyCommunity context.__parent__ = DummyCommunity() from karl.testing import DummyCatalog context.__parent__.catalog = DummyCatalog() request = testing.DummyRequest() from webob.multidict import MultiDict request.params = request.POST = MultiDict() response = self._callFUT(context, request) self.assertEqual(len(response['actions']), 0) self.assertEqual( response['backto'], { 'href': 'http://example.com/communities/community/', 'title': u'Dummy Communit\xe0', }) self.assertEqual( response['head_data'], '<script type="text/javascript">\nwindow._karl_client_data = ' '{"wikitoc": {"items": [{"name": "WIKIPAGE", "author": "", "tags": ' '[], "modified": "2011-08-20T00:00:00", "author_name": "", ' '"created": "2011-08-20T00:00:00", "title": "", "id": ' '"id_WIKIPAGE", "profile_url": "http://example.com/"}]}};\n' '</script>')
def test_emit(self): from repoze.sendmail.interfaces import IMailDelivery from pyramid.interfaces import IRequest from karl.models.interfaces import IProfile from karl.testing import DummyMailer mailer = DummyMailer() self.config.registry.registerUtility(mailer, IMailDelivery) community = DummyCommunity() community["foo"] = context = testing.DummyModel() directlyProvides(context, IDummy) site = community.__parent__.__parent__ site["profiles"] = profiles = testing.DummyModel() profiles["a"] = DummyProfile() profiles["b"] = DummyProfile() profiles["c"] = DummyProfile() community.member_names = set(( "a", "c", )) community.moderator_names = set(("b", )) request = testing.DummyRequest() self.config.registry.registerAdapter(DummyEmailAlertAdapter, (IDummy, IProfile, IRequest), IAlert) self._get_instance().emit(context, request) self.assertEqual(3, len(mailer))
def setUp(self): cleanUp() # Set up a dummy blog from karl.content.interfaces import IBlog from karl.testing import DummyCommunity from karl.testing import DummyProfile from zope.interface import directlyProvides community = DummyCommunity() blog = community["blog"] = testing.DummyModel() directlyProvides(blog, IBlog) blog.title = "Blog Title" site = community.__parent__.__parent__ profiles = site["profiles"] = testing.DummyModel() chris = profiles["chris"] = DummyProfile() chris.title = "Chris Rossi" self.context = blog # Register dummy catalog from zope.interface import Interface from karl.models.interfaces import ICatalogSearch karl.testing.registerAdapter(dummy_catalog_search, Interface, ICatalogSearch) # Register blog entry adapter from karl.views.interfaces import IAtomEntry from karl.content.interfaces import IBlogEntry from karl.views.atom import GenericAtomEntry karl.testing.registerAdapter(GenericAtomEntry, (IBlogEntry, Interface), IAtomEntry) karl.testing.registerDummyRenderer('karl.views:templates/atomfeed.pt')
def test_handle_submit(self): context = self.context # register dummy IPage content factory def factory(title, text, description, creator): page = DummyPage(title=title, text=text, description=description, creator=creator) page['attachments'] = DummyModel() return page registerContentFactory(factory, IPage) # set up attachments from karl.content.interfaces import ICommunityFile from karl.testing import DummyFile registerContentFactory(DummyFile, ICommunityFile) from karl.testing import DummyUpload attachment1 = DummyUpload(filename='test1.txt') attachment2 = DummyUpload(filename=r"C:\My Documents\Ha Ha\test2.txt") # set up tags infrastructure from karl.testing import DummyCommunity from karl.testing import DummyTags community = DummyCommunity() site = community.__parent__.__parent__ site.sessions = DummySessions() site.catalog = DummyCatalog() site.tags = DummyTags() community['pages'] = context # construct converted dict and call handle_submit converted = { 'title': u'Page Title', 'text': u'page text', 'tags': [u'foo', u'bar'], 'attachments': [attachment1, attachment2], } context.ordering = DummyOrdering() controller = self._makeOne(context, self.request) controller.handle_submit(converted) # make sure everything looks good # basic fields self.failUnless(u'page-title' in context) page = context['page-title'] self.assertEqual(page.title, u'Page Title') self.assertEqual(page.text, u'page text') # attachments attachments_folder = page['attachments'] self.failUnless('test1.txt' in attachments_folder) self.failUnless('test2.txt' in attachments_folder) self.assertEqual(attachments_folder['test1.txt'].filename, 'test1.txt') self.assertEqual(attachments_folder['test2.txt'].filename, 'test2.txt') # ordering self.assertEqual(context.ordering.names_added, ['page-title']) # tags self.assertEqual(site.tags._called_with[1]['tags'], [u'foo', u'bar'])
def setUp(self): cleanUp() # Register mail utility from repoze.sendmail.interfaces import IMailDelivery from karl.testing import DummyMailer self.mailer = DummyMailer() karl.testing.registerUtility(self.mailer, IMailDelivery) # Register BlogCommentAlert adapter from karl.models.interfaces import IProfile from karl.models.interfaces import IComment from karl.content.views.adapters import BlogCommentAlert from karl.utilities.interfaces import IAlert from pyramid.interfaces import IRequest karl.testing.registerAdapter(BlogCommentAlert, (IComment, IProfile, IRequest), IAlert) # Register IShowSendAlert adapter self.show_sendalert = True from karl.content.views.interfaces import IShowSendalert class DummyShowSendalert(object): def __init__(myself, context, request): myself.show_sendalert = self.show_sendalert karl.testing.registerAdapter(DummyShowSendalert, (Interface, Interface), IShowSendalert) # Create dummy site skel from karl.testing import DummyCommunity from karl.testing import DummyProfile community = DummyCommunity() site = community.__parent__.__parent__ site.sessions = DummySessions() profiles = testing.DummyModel() site["profiles"] = profiles profiles["a"] = DummyProfile() profiles["b"] = DummyProfile() profiles["c"] = DummyProfile() community.member_names = set([ "b", "c", ]) community.moderator_names = set([ "a", ]) blog = testing.DummyModel() community["blog"] = blog blogentry = blog["foo"] = DummyBlogEntry() self.context = blogentry["comments"] # Create dummy request request = testing.DummyRequest() request.environ['repoze.browserid'] = '1' self.request = request
def test_no_communities(self): from karl.testing import DummyCommunity from karl.testing import DummyUsers karl.testing.registerDummySecurityPolicy("userid") c = DummyCommunity() context = c.__parent__.__parent__ users = context.users = DummyUsers() users.add("userid", "userid", "password", []) request = testing.DummyRequest() response = self._callFUT(context, request) self.assertEqual(response.location, 'http://example.com/communities/')
def test_form_defaults_w_community_sendalert_default(self): from karl.testing import DummyCommunity workflow = self._registerSecurityWorkflow() community = DummyCommunity() community.sendalert_default = False context = community['testing'] = testing.DummyModel() request = testing.DummyRequest() controller = self._makeOne(context, request) defaults = controller.form_defaults() self.assertEqual(defaults['title'], '') self.assertEqual(defaults['tags'], []) self.assertEqual(defaults['text'], '') self.assertEqual(defaults['sendalert'], False) self.assertEqual(defaults['security_state'], workflow.initial_state)
def test_frontpage(self): self._register() context = testing.DummyModel() context.__name__ = 'front_page' context.title = 'Page' from karl.testing import DummyCommunity context.__parent__ = DummyCommunity() request = testing.DummyRequest() response = self._callFUT(context, request) self.assertEqual(len(response['actions']), 2) self.assertEqual( response['actions'][0][1], 'http://example.com/communities/community/front_page/edit.html') # Front page should not have backlink breadcrumb thingy self.assert_(response['backto'] is False)
def setUp(self): # Register mail utility from repoze.sendmail.interfaces import IMailDelivery from karl.testing import DummyMailer self.mailer = DummyMailer() from pyramid.threadlocal import manager from pyramid.registry import Registry manager.stack[0]['registry'] = Registry('testing') karl.testing.registerUtility(self.mailer, IMailDelivery) # Register BlogEntryAlert adapter from karl.models.interfaces import IProfile from karl.content.interfaces import IBlogEntry from karl.content.views.adapters import BlogEntryAlert from karl.utilities.interfaces import IAlert from pyramid.interfaces import IRequest karl.testing.registerAdapter(BlogEntryAlert, (IBlogEntry, IProfile, IRequest), IAlert) karl.testing.registerDummySecurityPolicy("a") # Create dummy site skel from karl.testing import DummyCommunity self.community = DummyCommunity() self.site = self.community.__parent__.__parent__ self.profiles = testing.DummyModel() self.site["profiles"] = self.profiles from karl.testing import DummyProfile self.profiles["a"] = DummyProfile() self.profiles["b"] = DummyProfile() self.profiles["c"] = DummyProfile() for profile in self.profiles.values(): profile["alerts"] = testing.DummyModel() self.community.member_names = set([ "b", "c", ]) self.community.moderator_names = set([ "a", ]) self.blog = self._makeContext() self.community["blog"] = self.blog
def test_respect_alert_prefs(self): from repoze.sendmail.interfaces import IMailDelivery from pyramid.interfaces import IRequest from karl.models.interfaces import IProfile from karl.testing import DummyMailer mailer = DummyMailer() self.config.registry.registerUtility(mailer, IMailDelivery) community = DummyCommunity() community["foo"] = context = testing.DummyModel() directlyProvides(context, IDummy) site = community.__parent__.__parent__ site["profiles"] = profiles = testing.DummyModel() profiles["a"] = DummyProfile() profiles["b"] = DummyProfile() profiles["b"].set_alerts_preference(community.__name__, IProfile.ALERT_DAILY_DIGEST) profiles["c"] = DummyProfile() profiles["c"].set_alerts_preference(community.__name__, IProfile.ALERT_NEVER) profiles["d"] = DummyProfile() profiles["d"].set_alerts_preference(community.__name__, IProfile.ALERT_WEEKLY_DIGEST) profiles["e"] = DummyProfile() profiles["e"].set_alerts_preference(community.__name__, IProfile.ALERT_BIWEEKLY_DIGEST) community.member_names = set(( "a", "c", )) community.moderator_names = set(("b", )) request = testing.DummyRequest() self.config.registry.registerAdapter(DummyEmailAlertAdapter, (IDummy, IProfile, IRequest), IAlert) self._get_instance().emit(context, request) self.assertEqual(1, len(mailer)) self.assertEqual(1, len(list(profiles["b"]._pending_alerts)))
def test_user_home_path(self): from zope.interface.interfaces import IInterface from zope.interface import directlyProvides from pyramid.interfaces import ITraverserFactory from karl.testing import DummyCommunity from karl.testing import DummyProfile karl.testing.registerDummySecurityPolicy("userid") c = DummyCommunity() site = c.__parent__.__parent__ directlyProvides(site, IInterface) c["foo"] = foo = testing.DummyModel() site["profiles"] = profiles = testing.DummyModel() profiles["userid"] = profile = DummyProfile() profile.home_path = "/communities/community/foo" karl.testing.registerAdapter(dummy_traverser_factory(foo), IInterface, ITraverserFactory) request = testing.DummyRequest() response = self._callFUT(site, request) self.assertEqual(response.location, "http://example.com/communities/community/foo/")
def setUp(self): cleanUp() # Create dummy site skel from karl.testing import DummyCommunity community = DummyCommunity() site = community.__parent__.__parent__ site.sessions = DummySessions() blog = testing.DummyModel() community["blog"] = blog blogentry = blog["foo"] = DummyBlogEntry() container = blogentry["comments"] comment = container["99"] = DummyComment('Re: foo', 'text', 'description', 'creator') self.context = comment # Create dummy request request = testing.DummyRequest() request.environ['repoze.browserid'] = '1' self.request = request registerLayoutProvider()
def test_frontpage(self): self._register() context = DummyWikiPage() context.__name__ = 'front_page' context.title = 'Page' from karl.testing import DummyCommunity context.__parent__ = DummyCommunity() from karl.testing import DummyCatalog context.__parent__.catalog = DummyCatalog() request = testing.DummyRequest() response = self._callFUT(context, request) self.assertEqual(len(response['actions']), 0) self.assertEqual(response['backto'], False) self.assertEqual( response['head_data'], '<script type="text/javascript">\nwindow._karl_client_data = ' '{"wikitoc": {"items": [{"name": "WIKIPAGE", "author": "", "tags": ' '[], "modified": "2011-08-20T00:00:00", "author_name": "", ' '"created": "2011-08-20T00:00:00", "title": "", "id": ' '"id_WIKIPAGE", "profile_url": "http://example.com/"}]}};\n' '</script>')
def _mk_dummy_site(self): import datetime from karl.content.interfaces import IBlogEntry from karl.content.interfaces import ICalendarEvent from karl.content.interfaces import ICommunityFile from karl.content.interfaces import IWikiPage from karl.models.interfaces import IComment from karl.testing import DummyCommunity from karl.testing import DummyModel from karl.testing import DummyRoot from repoze.workflow.testing import registerDummyWorkflow from repoze.workflow.testing import DummyWorkflow from zope.interface import directlyProvides site = DummyRoot() site.tags = DummyTags() communities = site['communities'] = DummyModel() big_endians = communities['big_endians'] = DummyCommunity() big_endians.title = 'Big Endians' big_endians.member_names = ['fred', 'martin', 'daniela'] big_endians.moderator_names = ['fred', 'daniela'] big_endians.created = datetime.datetime(2010, 5, 12, 2, 42) big_endians.creator = 'daniela' big_endians.content_modified = datetime.datetime(2010, 6, 12, 2, 42) big_endians.__custom_acl__ = True content = big_endians['wiki1'] = DummyModel() content.created = datetime.datetime(2010, 5, 12, 3, 42) content.creator = 'daniela' directlyProvides(content, IWikiPage) content = big_endians['wiki2'] = DummyModel() content.created = datetime.datetime(1975, 7, 7, 7, 23) content.creator = 'fred' directlyProvides(content, IWikiPage) little_endians = communities['little_endians'] = DummyCommunity() little_endians.title = 'Little Endians' little_endians.member_names = ['dusty', 'roberta', 'nina'] little_endians.moderator_names = ['nina'] little_endians.created = datetime.datetime(2010, 5, 13, 6, 23) little_endians.creator = 'nina' little_endians.content_modified = datetime.datetime(2010, 6, 12, 3, 42) little_endians._p_deactivate = lambda: None little_endians.state = 'public' content = little_endians['blog1'] = DummyModel() content.created = datetime.datetime.now() content.creator = 'nina' directlyProvides(content, IBlogEntry) content['comment1'] = DummyModel() content = content['comment1'] content.created = datetime.datetime.now() content.creator = 'roberta' directlyProvides(content, IComment) content = little_endians['file1'] = DummyModel() content.created = datetime.datetime.now() content.creator = 'dusty' directlyProvides(content, ICommunityFile) content = little_endians['event1'] = DummyModel() content.created = datetime.datetime.now() content.creator = 'dusty' directlyProvides(content, ICalendarEvent) registerDummyWorkflow('security', DummyWorkflow()) return site