def test_communities_nonviewable_filtered(self): self._registerTagbox() self._registerCatalogSearch() from repoze.bfg.testing import DummyModel from repoze.bfg.testing import registerDummySecurityPolicy registerDummySecurityPolicy(permissive=False) from karl.testing import DummyCommunity from karl.testing import DummyUsers renderer = testing.registerDummyRenderer('templates/profile.pt') request = testing.DummyRequest() context = DummyProfile() users = DummyUsers() community1 = DummyCommunity() community1.title = "Community 1" communities = community1.__parent__ communities["community2"] = community2 = DummyCommunity() community2.title = "Community 2" users.add("userid", "userid", "password", ["group.community:community:members", "group.community:community2:moderators"]) site = communities.__parent__ site.users = users site["profiles"] = profiles = DummyModel() profiles["userid"] = context self._callFUT(context, request) self.assertEqual(renderer.communities, [])
def test_crackHeaders_with_date_from_date_header(self): import datetime from email.utils import formatdate from karl.testing import DummyUsers import time context = self._makeRoot() context.users = DummyUsers() profile = self._makeContext() profile.__name__ = 'extant' by_email = {'*****@*****.**': profile} pf = context['profiles'] = self._makeContext() pf.getProfileByEmail = lambda email: by_email.get(email) cf = context['communities'] = self._makeContext() community = cf['testing'] = self._makeContext() community['default'] = self._makeContext() mailin = self._makeOne(context) mailin.default_tool = 'default' message = DummyMessage() message.to = ('*****@*****.**', ) message.from_ = ('*****@*****.**', ) message.subject = 'subject' date = time.mktime((2010, 5, 12, 2, 42, 0, 0, 0, -1)) message.date = formatdate(date) info = mailin.crackHeaders(message) self.assertEqual(info['date'], datetime.datetime(2010, 5, 12, 2, 42))
def test_checkPermission_community_miss(self): from karl.testing import DummyUsers from karl.testing import registerDummySecurityPolicy registerDummySecurityPolicy('phred', permissive=False) context = self._makeRoot() communities = context['communities'] = self._makeContext() community = communities['testcommunity'] = self._makeContext() community['testtool'] = self._makeContext() users = context.users = DummyUsers() userinfo = users._by_id['phred'] = {} mailin = self._makeOne(context) info = { 'author': 'phred', 'targets': [{ 'community': 'testcommunity', 'tool': 'testtool', 'report': None, }, { 'error': 'Some other previous error', }], } mailin.checkPermission(info) self.assertEqual(info['targets'][0]['error'], 'Permission Denied') self.assertEqual(info['targets'][1]['error'], 'Some other previous error')
def test_crackHeaders_default_community_in_CC(self): from karl.testing import DummyUsers context = self._makeRoot() context.users = DummyUsers() profile = self._makeContext() profile.__name__ = 'extant' by_email = {'*****@*****.**': profile} pf = context['profiles'] = self._makeContext() pf.getProfileByEmail = lambda email: by_email.get(email) cf = context['communities'] = self._makeContext() community = cf['testing'] = self._makeContext() community['default'] = self._makeContext() mailin = self._makeOne(context) mailin.default_tool = 'default' message = DummyMessage() message.to = ('*****@*****.**', ) message.cc = ('*****@*****.**', ) message.from_ = ('*****@*****.**', ) message.subject = 'subject' info = mailin.crackHeaders(message) self.failIf(info.get('error')) self.assertEqual(info['author'], 'extant') self.assertEqual(info['subject'], 'subject') targets = info['targets'] self.assertEqual(len(targets), 1) info = targets[0] self.assertEqual(info['community'], 'testing') self.assertEqual(info['tool'], 'default') self.assertEqual(info['in_reply_to'], None)
def test_checkPermission_report_hit(self): from zope.interface import directlyProvides from karl.models.interfaces import IPeopleDirectory from karl.testing import DummyUsers from karl.testing import registerDummySecurityPolicy registerDummySecurityPolicy('phred', permissive=True) context = self._makeRoot() pd = context['people'] = self._makeContext() directlyProvides(pd, IPeopleDirectory) section = pd['section'] = self._makeContext() report = section['report'] = self._makeContext() users = context.users = DummyUsers() userinfo = users._by_id['phred'] = {} mailin = self._makeOne(context) info = { 'author': 'phred', 'targets': [{ 'community': None, 'tool': None, 'report': 'section+report', }] } mailin.checkPermission(info) self.failIf('error' in info['targets'][0])
def test_is_staff_for_nonexistent_user(self): from karl.models.peopledirectory import is_staff from karl.testing import DummyUsers obj = _makeProfile() site = testing.DummyModel() site['testuser'] = obj site.users = DummyUsers() self.assertEqual(is_staff(obj, ()), ())
def test_get_groups_for_nonexistent_user(self): from karl.models.peopledirectory import get_groups from karl.testing import DummyUsers obj = _makeProfile() site = testing.DummyModel() site['testuser'] = obj site.users = DummyUsers() self.assertEqual(get_groups(obj, 0), 0)
def test_is_staff_for_non_profile(self): from karl.models.peopledirectory import is_staff from karl.testing import DummyUsers obj = testing.DummyModel() site = testing.DummyModel() site['testuser'] = obj site.users = DummyUsers() site.users.add('testuser', 'testuser', '', []) self.assertEqual(is_staff(obj, ()), ())
def test_is_staff_for_staff(self): from karl.models.peopledirectory import is_staff from karl.testing import DummyUsers obj = _makeProfile() site = testing.DummyModel() site['testuser'] = obj site.users = DummyUsers() site.users.add('testuser', 'testuser', '', ['group.KarlStaff']) self.assertEqual(is_staff(obj, ()), True)
def test_get_groups_for_non_profile(self): from karl.models.peopledirectory import get_groups from karl.testing import DummyUsers obj = testing.DummyModel() site = testing.DummyModel() site['testuser'] = obj site.users = DummyUsers() site.users.add('testuser', 'testuser', '', ['group1']) self.assertEqual(get_groups(obj, 0), 0)
def _makeOne(self, context, request, active=True): # create the site and the users infrastructure first site = self.site = testing.DummyModel(sessions=self.sessions) site['profile'] = context from karl.testing import DummyUsers users = self.users = site.users = DummyUsers() if active: users.add('profile', 'profile', 'password', ['group.KarlLovers']) from karl.views.people import AdminEditProfileFormController return AdminEditProfileFormController(context, 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 setUp(self): cleanUp() self.parent = DummyModel(title='dummyparent') self.context = DummyModel(title='dummytitle', text='dummytext') self.context['attachments'] = DummyModel() self.parent['child'] = self.context self.parent.catalog = DummyCatalog() self.parent["profiles"] = DummyModel() users = self.parent.users = DummyUsers() users.add("userid", "userid", "password", [])
def test_no_profile(self): from karl.views.utils import get_user_date_format from karl.testing import DummyUsers karl.testing.registerDummySecurityPolicy("userid") context = testing.DummyModel() profiles = context["profiles"] = testing.DummyModel() users = context.users = DummyUsers() users.add("userid", "userid", "password", []) request = testing.DummyRequest() date_format = get_user_date_format(context, request) self.assertEqual(date_format, 'en-US')
def test_no_communities(self): from karl.views.utils import get_user_home from karl.testing import DummyUsers from karl.testing import DummyProfile karl.testing.registerDummySecurityPolicy("userid") context = testing.DummyModel() communities = context["communities"] = testing.DummyModel() profiles = context["profiles"] = testing.DummyModel() profile = profiles["userid"] = DummyProfile() users = context.users = DummyUsers() users.add("userid", "userid", "password", []) request = testing.DummyRequest() target, extra_path = get_user_home(context, request) self.failUnless(target is communities) self.assertEqual(extra_path, [])
def test_communities(self): self._registerTagbox() self._registerCatalogSearch() from repoze.bfg.testing import DummyModel from karl.testing import DummyCommunity from karl.testing import DummyUsers renderer = testing.registerDummyRenderer('templates/profile.pt') request = testing.DummyRequest() context = DummyProfile() users = DummyUsers() community1 = DummyCommunity() community1.title = "Community 1" communities = community1.__parent__ communities["community2"] = community2 = DummyCommunity() community2.title = "Community 2" users.add("userid", "userid", "password", ["group.community:community:members", "group.community:community2:moderators"]) site = communities.__parent__ site.users = users site["profiles"] = profiles = DummyModel() profiles["userid"] = context self._callFUT(context, request) self.assertEqual(renderer.communities, [ {"title": "Community 1", "moderator": False, "url": "http://example.com/communities/community/",}, {"title": "Community 2", "moderator": True, "url": "http://example.com/communities/community2/",}, ])
def test_checkPermission_community_hit(self): from karl.testing import DummyUsers from karl.testing import registerDummySecurityPolicy registerDummySecurityPolicy('phred', permissive=True) context = self._makeRoot() communities = context['communities'] = self._makeContext() community = communities['testcommunity'] = self._makeContext() community['testtool'] = self._makeContext() users = context.users = DummyUsers() userinfo = users._by_id['phred'] = {} mailin = self._makeOne(context) info = { 'author': 'phred', 'targets': [{ 'community': 'testcommunity', 'tool': 'testtool', 'report': None, }] } mailin.checkPermission(info) self.failIf('error' in info['targets'][0])
def test_crackHeaders_permission_denied(self): from karl.testing import registerDummySecurityPolicy registerDummySecurityPolicy('someuser', permissive=False) from karl.testing import DummyUsers context = self._makeRoot() context.users = DummyUsers() profile = self._makeContext() profile.__name__ = 'extant' by_email = {'*****@*****.**': profile} pf = context['profiles'] = self._makeContext() pf.getProfileByEmail = lambda email: by_email.get(email) cf = context['communities'] = self._makeContext() community = cf['testing'] = self._makeContext() tool = community['default'] = self._makeContext() mailin = self._makeOne(context) mailin.default_tool = 'default' message = DummyMessage() message.to = ('*****@*****.**', ) message.from_ = ('*****@*****.**', ) message.subject = 'subject' info = mailin.crackHeaders(message) self.assertEqual(info['targets'][0]['error'], 'Permission Denied')
def test_space_as_home_path(self): from zope.interface import Interface from pyramid.interfaces import ITraverserFactory from karl.views.utils import get_user_home from karl.testing import DummyUsers from karl.testing import DummyProfile karl.testing.registerDummySecurityPolicy("userid") context = testing.DummyModel() communities = context["communities"] = testing.DummyModel() community = communities["community"] = testing.DummyModel() profiles = context["profiles"] = testing.DummyModel() profile = profiles["userid"] = DummyProfile() profile.home_path = ' ' karl.testing.registerAdapter(dummy_traverser_factory, Interface, ITraverserFactory) users = context.users = DummyUsers() users.add("userid", "userid", "password", [ "group.community:community:members", ]) request = testing.DummyRequest() target, extra_path = get_user_home(context, request) self.failUnless(target is community) self.assertEqual(extra_path, [])
def setUp(self): cleanUp() self.root = root = testing.DummyModel() self.profiles = profiles = testing.DummyModel() root['profiles'] = profiles root['people'] = testing.DummyModel() root['people']['categories'] = categories = testing.DummyModel() categories['entities'] = entities = testing.DummyModel() entities['open-society-institute'] = testing.DummyModel(sync_id='123') entities['hardees'] = testing.DummyModel(sync_id='456') categories['departments'] = departments = testing.DummyModel() departments['information-retrieval'] = testing.DummyModel( sync_id='321') departments['paper-chase'] = testing.DummyModel(sync_id='654') categories['boards'] = boards = testing.DummyModel() boards['1-x-3'] = testing.DummyModel(sync_id='987') categories['offices'] = offices = testing.DummyModel() offices['fondation-connaissance-et-liberte'] = testing.DummyModel( sync_id='213') offices['hand-creme'] = testing.DummyModel(sync_id='643') from karl.testing import DummyUsers root.users = DummyUsers()
def test_handle_submit(self): context = self.context request = self.request # fake the mailer from repoze.sendmail.interfaces import IMailDelivery from karl.testing import DummyMailer mailer = DummyMailer() karl.testing.registerUtility(mailer, IMailDelivery) # fake catalog search from karl.models.interfaces import ICatalogSearch from zope.interface import Interface karl.testing.registerAdapter(DummyProfileSearch, (Interface, ), ICatalogSearch) # fake a staff user from karl.testing import DummyUsers context.users = DummyUsers() context.users.add('me', 'me', 'password', ['group.KarlStaff']) # register dummy renderer for email template reg = get_current_registry() config = Configurator(reg, autocommit=True) renderer = config.testing_add_template( 'templates/email_reset_password.pt') # test w/ staff user controller = self._makeOne(context, request) converted = {'email': '*****@*****.**'} response = controller.handle_submit(converted) self.failIf(len(mailer)) self.assertEqual( response.location, 'http://login.example.com/resetpassword?email=me%40example.com') self.assertEqual(request.session['came_from'], 'http://example.com/login.html') # register dummy profile search profile_search = DummyProfileSearch(context) def search_adapter(context): return profile_search karl.testing.registerAdapter(search_adapter, (Interface, ), ICatalogSearch) # convert to non-staff user and test again, email should # go out this time context.users._by_id['me']['groups'] = [] response = controller.handle_submit(converted) self.assertEqual( response.location, 'http://example.com/reset_sent.html?email=me%40example.com') profile = profile_search.profile self.failUnless(profile.password_reset_key) self.failUnless(profile.password_reset_time) self.assertEqual(len(mailer), 1) msg = mailer.pop() self.assertEqual(len(msg.mto), 1) self.assertEqual(msg.mto[0], '*****@*****.**') self.assertEqual( dict(msg.msg._headers)['Subject'], 'karl3test Password Reset Request') renderer.assert_(login='******', system_name='karl3test') self.failUnless(hasattr(renderer, 'reset_url')) self.failUnless( renderer.reset_url.startswith( 'http://example.com/reset_confirm.html?key='), renderer.reset_url)
def _setupUsers(self): from karl.testing import DummyUsers self.context.users = DummyUsers() self.context.users.add('me', 'me', 'password', [])
def setUp(self): self.context = context = testing.DummyResource() context.users = DummyUsers() context['profiles'] = testing.DummyResource()
def test_handle_submit(self): context = self.context from zope.interface import directlyProvides from karl.models.interfaces import ICommunity directlyProvides(context, ICommunity) from repoze.lemonade.testing import registerContentFactory registerContentFactory(DummyCommunity, ICommunity) context.users = DummyUsers({}) controller = self._makeOne(context, self.request) # first time fill out all the fields converted = { 'title': 'Intranet Title', 'name': 'url_name', 'address': 'address', 'city': 'city', 'state': 'state', 'country': 'country', 'zipcode': 'zipcode', 'telephone': 'telephone', 'navigation': '<ul><li>something</li>', 'middle_portlets': 'one\ntwo\nthree', 'right_portlets': 'four\nfive\nsix', 'css': 'html font-size: 10px;', } response = controller.handle_submit(converted) self.failUnless('url_name' in context) intranet = context['url_name'] self.assertEqual(intranet.title, 'Intranet Title') self.assertEqual(intranet.address, 'address') self.assertEqual(intranet.city, 'city') self.assertEqual(intranet.state, 'state') self.assertEqual(intranet.country, 'country') self.assertEqual(intranet.zipcode, 'zipcode') self.assertEqual(intranet.telephone, 'telephone') self.assertEqual(intranet.navigation, '<ul><li>something</li></ul>') self.assertEqual(intranet.middle_portlets, ['one', 'two', 'three']) self.assertEqual(intranet.right_portlets, ['four', 'five', 'six']) self.assertEqual(intranet.css, 'html font-size: 10px;') self.failUnless( '?status_message=Intranet%20added' in response.location) # now try again, same values, make sure it fails on name check from pyramid_formish import ValidationError self.assertRaises(ValidationError, controller.handle_submit, converted) # this time no name, nav, or portlet values to make sure the # auto-fill stuff works correctly converted['title'] = 'Another Title' converted['name'] = None converted['navigation'] = None converted['middle_portlets'] = None converted['right_portlets'] = None response = controller.handle_submit(converted) self.failUnless('another-title' in context) intranet = context['another-title'] self.assertEqual(intranet.title, 'Another Title') from karl.content.views.intranets import sample_navigation from karl.content.views.intranets import sample_middle_portlets from karl.content.views.intranets import sample_right_portlets from lxml.html.clean import clean_html self.assertEqual(clean_html(intranet.navigation.strip()), clean_html(sample_navigation.strip())) self.assertEqual(intranet.middle_portlets, sample_middle_portlets) self.assertEqual(intranet.right_portlets, sample_right_portlets)