def test_not_logged_in(self): from karl.views.utils import get_user_date_format karl.testing.registerDummySecurityPolicy() context = makeRoot() request = testing.DummyRequest() date_format = get_user_date_format(context, request) self.assertEqual(date_format, 'en-US')
def test_upload_has_mimetype_instead_of_type(self): from cStringIO import StringIO from karl.content.interfaces import ICommunityFile from repoze.lemonade.testing import registerContentFactory karl.testing.registerDummySecurityPolicy("userid") def make_image(title, stream, mimetype, filename, creator): res = testing.DummyModel() res.title = title res.mimetype = mimetype res.data = stream.read() res.filename = filename res.creator = creator res.is_image = True return res registerContentFactory(make_image, ICommunityFile) context = makeRoot() context.title = 'Howdy Doody' context.__name__ = 'howdydoody' context.size = 1000 dummy_upload = DummyUpload(StringIO(one_pixel_jpeg), 'image/jpeg') dummy_upload.mimetype = dummy_upload.type del dummy_upload.type form = {'photo': dummy_upload} self._callFUT(context, form) self.assertTrue('photo' in context) photo = context['photo'] self.assertEqual(photo.title, 'Photo of Howdy Doody') self.assertEqual(photo.mimetype, 'image/jpeg') self.assertEqual(photo.data, one_pixel_jpeg) self.assertEqual(photo.filename, 'test.dat') self.assertEqual(photo.creator, 'userid')
def test_handle_no_email_attachments(self): import datetime from pyramid.testing import DummyModel from karl.content.interfaces import IBlogEntry self._set_NOW(datetime.datetime(2009, 01, 28, 10, 00, 00)) self._registerFactory(IBlogEntry, _makeBlogEntryClass()) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = DummyModel() root = makeRoot() root["foo"] = context context.__parent__ = root adapter = self._makeOne(context) message = object() # ignored info = {"subject": "SUBJECT", "author": "phreddy", "date": datetime.datetime(2010, 5, 12, 2, 42)} adapter.handle(message, info, "TEXT", []) self.assertEqual(len(context), 1) entry_id, entry = context.items()[0] self.assertEqual(entry_id, "subject") self.assertEqual(entry.title, "SUBJECT") self.assertEqual(entry.creator, "phreddy") self.assertEqual(entry.text, "TEXT") self.assertEqual(entry.description, "TEXT") self.assertEqual(entry.created, datetime.datetime(2010, 5, 12, 2, 42)) self.failIf(len(entry["attachments"])) self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
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 = makeRoot() 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() context.blah = 'foo' 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_GET_came_from_endswith_logout_html_relative(self): request = testing.DummyRequest(session={'came_from': '/logout.html'}) context = makeRoot() renderer = karl.testing.registerDummyRenderer('templates/login.pt') self._callFUT(context, request) self.assertEqual(request.session['came_from'], 'http://example.com/') self.assertEqual(renderer.app_url, 'http://example.com')
def test_handle_w_alert(self): import datetime from pyramid.testing import DummyModel from karl.models.interfaces import IComment self._registerFactory(IComment, DummyModel) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = _makeBlogEntryClass()('foo', 'foo', 'foo', 'foo') comments = context['comments'] = DummyModel() comments.next_id = '1' root = makeRoot() root['foo'] = context context.__parent__ = root adapter = self._makeOne(context) message = object() # ignored info = { 'subject': 'SUBJECT', 'author': 'phreddy', 'date': datetime.datetime(2010, 5, 12, 2, 42) } adapter.handle(message, info, 'TEXT', []) self.assertEqual(len(comments), 1) comment_id, comment = comments.items()[0] self.assertEqual(comment_id, '1') self.assertEqual(comment.title, 'SUBJECT') self.assertEqual(comment.creator, 'phreddy') self.assertEqual(comment.text, 'TEXT') self.failIf('attachments' in comment) self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def test_handle_no_email_attachments(self): from pyramid.testing import DummyModel from karl.models.interfaces import IComment import datetime self._registerFactory(IComment, DummyModel) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = _makeBlogEntryClass()("foo", "foo", "foo", "foo") root = makeRoot() root["entry"] = context context.__parent__ = root comments = context["comments"] = DummyModel() comments.next_id = "1" adapter = self._makeOne(context) message = object() # ignored info = {"subject": "SUBJECT", "author": "phreddy", "date": datetime.datetime(2010, 5, 12, 2, 42)} adapter.handle(message, info, "TEXT", []) self.assertEqual(len(comments), 1) comment_id, comment = comments.items()[0] self.assertEqual(comment_id, "1") self.assertEqual(comment.title, "SUBJECT") self.assertEqual(comment.creator, "phreddy") self.assertEqual(comment.text, "TEXT") self.assertEqual(comment.description, "TEXT") self.assertEqual(comment.created, datetime.datetime(2010, 5, 12, 2, 42)) self.failIf("attachments" in comment) self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
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 = makeRoot() 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() context.blah = 'foo' 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_POST_no_password_in_form(self): from pyramid.httpexceptions import HTTPFound request = testing.DummyRequest() request.POST['form.submitted'] = 1 request.POST['login'] = '******' context = makeRoot() response = self._callFUT(context, request) self.failUnless(isinstance(response, HTTPFound)) self.assertEqual(response.location, 'http://example.com/login.html')
def test_GET_came_from_other_absolute(self): request = testing.DummyRequest( session={'came_from': 'http://example.com/somewhere.html'}) context = makeRoot() renderer = karl.testing.registerDummyRenderer('templates/login.pt') self._callFUT(context, request) self.assertEqual(request.session['came_from'], 'http://example.com/somewhere.html') self.assertEqual(renderer.app_url, 'http://example.com')
def test_GET_forget_headers_when_auth_tkt_not_None(self, forget): request = testing.DummyRequest(session={'came_from': '/somewhere.html'}) context = makeRoot() karl.testing.registerDummyRenderer('templates/login.pt') forget.return_value = [('a', '1')] response = self._callFUT(context, request) self.assertEqual(dict(response.headers), dict([('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '0'), ('a', '1')])) forget.assert_called_once_with(request)
def setUp(self): testing.cleanUp() from karl.testing import registerSettings registerSettings() site = makeRoot() self.context = site request = testing.DummyRequest() request.environ['repoze.browserid'] = '1' self.request = request karl.testing.registerDummyRenderer( 'karl.views:forms/templates/snippets.pt')
def test_no_profile(self): from karl.views.utils import get_user_date_format from karl.testing import DummyUsers karl.testing.registerDummySecurityPolicy("userid") context = makeRoot() 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_handle_w_email_attachments(self): import datetime from pyramid.testing import DummyModel from karl.models.interfaces import IComment from karl.content.interfaces import ICommunityFile self._registerFactory(IComment, DummyModel) self._registerFactory(ICommunityFile) self._registerSettings() self._registerContextURL() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = _makeBlogEntryClass()('foo', 'foo', 'foo', 'foo') root = makeRoot() root['foo'] = context context.__parent__ = root comments = context['comments'] = DummyModel() comments.next_id = '1' adapter = self._makeOne(context) message = object() # ignored info = { 'subject': 'SUBJECT', 'author': 'phreddy', 'date': datetime.datetime(2010, 5, 12, 2, 42) } attachments = [ ('file1.png', 'image/png', 'IMAGE1'), ('file1.png', 'image/png', 'IMAGE2'), ] adapter.handle(message, info, 'TEXT', attachments) self.assertEqual(len(comments), 1) comment_id, comment = comments.items()[0] self.assertEqual(comment_id, '1') self.assertEqual(comment.title, 'SUBJECT') self.assertEqual(comment.creator, 'phreddy') self.assertEqual(comment.text, 'TEXT') attachments = comment self.assertEqual(len(attachments), 2) file1 = attachments['file1.png'] self.assertEqual(file1.title, 'file1.png') self.assertEqual(file1.filename, 'file1.png') self.assertEqual(file1.mimetype, 'image/png') self.assertEqual(file1.stream.read(), 'IMAGE1') file2 = attachments['file1-1.png'] self.assertEqual(file2.title, 'file1.png') self.assertEqual(file2.filename, 'file1.png') self.assertEqual(file2.mimetype, 'image/png') self.assertEqual(file2.stream.read(), 'IMAGE2') self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def test_handle_with_email_attachments_w_entry_attachments(self): import datetime from pyramid.testing import DummyModel from karl.content.interfaces import IBlogEntry from karl.content.interfaces import ICommunityFile self._set_NOW(datetime.datetime(2009, 01, 28, 10, 00, 00)) self._registerFactory(IBlogEntry, _makeBlogEntryClass()) self._registerFactory(ICommunityFile) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = DummyModel() root = makeRoot() root['foo'] = context context.__parent__ = root adapter = self._makeOne(context) message = object() # ignored info = { 'subject': 'SUBJECT', 'author': 'phreddy', 'date': datetime.datetime(2010, 5, 12, 2, 42) } attachments = [ ('file1.bin', 'application/octet-stream', 'DATA'), ('file2.png', 'image/png', 'IMAGE'), ] adapter.handle(message, info, 'TEXT', attachments) self.assertEqual(len(context), 1) entry_id, entry = context.items()[0] self.assertEqual(entry_id, 'subject') self.assertEqual(entry.title, 'SUBJECT') self.assertEqual(entry.creator, 'phreddy') self.assertEqual(entry.text, 'TEXT') attachments = entry['attachments'] self.assertEqual(len(attachments), 2) file1 = attachments['file1.bin'] self.assertEqual(file1.title, 'file1.bin') self.assertEqual(file1.filename, 'file1.bin') self.assertEqual(file1.mimetype, 'application/octet-stream') self.assertEqual(file1.stream.read(), 'DATA') file2 = attachments['file2.png'] self.assertEqual(file2.title, 'file2.png') self.assertEqual(file2.filename, 'file2.png') self.assertEqual(file2.mimetype, 'image/png') self.assertEqual(file2.stream.read(), 'IMAGE') self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def test_POST_impersonate_no_admin_user(self, remember): from pyramid.httpexceptions import HTTPFound request = testing.DummyRequest() request.POST['form.submitted'] = 1 request.POST['login'] = '******' request.POST['password'] = '******' context = makeRoot() context.users = DummyUsers() context.settings = {} del context.users.data['admin'] remember.return_value = [('Faux-Header', 'Faux-Value')] response = self._callFUT(context, request) self.failUnless(isinstance(response, HTTPFound)) self.assertTrue('Bad+username' in response.location) self.assertTrue('Faux-Header' not in response.headers)
def test_POST_impersonate(self, remember): from pyramid.httpexceptions import HTTPFound request = testing.DummyRequest() request.POST['form.submitted'] = 1 request.POST['login'] = '******' request.POST['password'] = '******' context = makeRoot() context.users = DummyUsers() context.settings = {} remember.return_value = [('Faux-Header', 'Faux-Value')] response = self._callFUT(context, request) self.failUnless(isinstance(response, HTTPFound)) self.assertEqual(response.location, 'http://example.com') remember.assert_called_once_with(request, 'userid', max_age=None) headers = dict(response.headers) self.assertEqual(headers['Faux-Header'], 'Faux-Value')
def test_handle_w_email_attachments(self): import datetime from pyramid.testing import DummyModel from karl.models.interfaces import IComment from karl.content.interfaces import ICommunityFile self._registerFactory(IComment, DummyModel) self._registerFactory(ICommunityFile) self._registerSettings() self._registerContextURL() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = _makeBlogEntryClass()("foo", "foo", "foo", "foo") root = makeRoot() root["foo"] = context context.__parent__ = root comments = context["comments"] = DummyModel() comments.next_id = "1" adapter = self._makeOne(context) message = object() # ignored info = {"subject": "SUBJECT", "author": "phreddy", "date": datetime.datetime(2010, 5, 12, 2, 42)} attachments = [("file1.png", "image/png", "IMAGE1"), ("file1.png", "image/png", "IMAGE2")] adapter.handle(message, info, "TEXT", attachments) self.assertEqual(len(comments), 1) comment_id, comment = comments.items()[0] self.assertEqual(comment_id, "1") self.assertEqual(comment.title, "SUBJECT") self.assertEqual(comment.creator, "phreddy") self.assertEqual(comment.text, "TEXT") attachments = comment self.assertEqual(len(attachments), 2) file1 = attachments["file1.png"] self.assertEqual(file1.title, "file1.png") self.assertEqual(file1.filename, "file1.png") self.assertEqual(file1.mimetype, "image/png") self.assertEqual(file1.stream.read(), "IMAGE1") file2 = attachments["file1-1.png"] self.assertEqual(file2.title, "file1.png") self.assertEqual(file2.filename, "file1.png") self.assertEqual(file2.mimetype, "image/png") self.assertEqual(file2.stream.read(), "IMAGE2") self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def test_POST_w_plugins_miss(self, remember): from pyramid.httpexceptions import HTTPFound from urlparse import urlsplit from urlparse import parse_qsl request = testing.DummyRequest() request.POST['form.submitted'] = 1 request.POST['login'] = '******' request.POST['password'] = '******' context = makeRoot() context.users = DummyUsers() response = self._callFUT(context, request) self.failUnless(isinstance(response, HTTPFound)) (_, _, path, query, _) = urlsplit(response.location) self.assertEqual(path, '/login.html') self.assertEqual(request.session['came_from'], 'http://example.com') query = dict(parse_qsl(query, 1, 1)) self.assertEqual(query['reason'], 'Bad username or password')
def test_handle_with_email_attachments_w_entry_attachments(self): import datetime from pyramid.testing import DummyModel from karl.content.interfaces import IBlogEntry from karl.content.interfaces import ICommunityFile self._set_NOW(datetime.datetime(2009, 01, 28, 10, 00, 00)) self._registerFactory(IBlogEntry, _makeBlogEntryClass()) self._registerFactory(ICommunityFile) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = DummyModel() root = makeRoot() root["foo"] = context context.__parent__ = root adapter = self._makeOne(context) message = object() # ignored info = {"subject": "SUBJECT", "author": "phreddy", "date": datetime.datetime(2010, 5, 12, 2, 42)} attachments = [("file1.bin", "application/octet-stream", "DATA"), ("file2.png", "image/png", "IMAGE")] adapter.handle(message, info, "TEXT", attachments) self.assertEqual(len(context), 1) entry_id, entry = context.items()[0] self.assertEqual(entry_id, "subject") self.assertEqual(entry.title, "SUBJECT") self.assertEqual(entry.creator, "phreddy") self.assertEqual(entry.text, "TEXT") attachments = entry["attachments"] self.assertEqual(len(attachments), 2) file1 = attachments["file1.bin"] self.assertEqual(file1.title, "file1.bin") self.assertEqual(file1.filename, "file1.bin") self.assertEqual(file1.mimetype, "application/octet-stream") self.assertEqual(file1.stream.read(), "DATA") file2 = attachments["file2.png"] self.assertEqual(file2.title, "file2.png") self.assertEqual(file2.filename, "file2.png") self.assertEqual(file2.mimetype, "image/png") self.assertEqual(file2.stream.read(), "IMAGE") self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def test_POST_w_profile(self, remember): from datetime import datetime from pyramid.httpexceptions import HTTPFound request = testing.DummyRequest() request.POST['form.submitted'] = 1 request.POST['login'] = '******' request.POST['password'] = '******' context = makeRoot() context.users = DummyUsers() context.settings = {} context['profiles'] = testing.DummyModel() profile = context['profiles']['userid'] = testing.DummyModel() before = datetime.utcnow() remember.return_value = [('Faux-Header', 'Faux-Value')] response = self._callFUT(context, request) after = datetime.utcnow() self.failUnless(isinstance(response, HTTPFound)) self.assertEqual(response.location, 'http://example.com') headers = dict(response.headers) self.assertEqual(headers['Faux-Header'], 'Faux-Value') self.failUnless(before <= profile.last_login_time <= after)
def test_replace_photo(self): from cStringIO import StringIO from karl.content.interfaces import ICommunityFile from repoze.lemonade.testing import registerContentFactory karl.testing.registerDummySecurityPolicy("userid") def make_image(title, stream, mimetype, filename, creator): res = testing.DummyModel() res.title = title res.mimetype = mimetype res.data = stream.read() res.filename = filename res.creator = creator res.is_image = True return res registerContentFactory(make_image, ICommunityFile) class DummyModel(testing.DummyModel): # Simulate repoze.folder behavior of refusing to overwrite # existing keys without an explicit removal. def __setitem__(self, name, value): if name in self: raise KeyError(u'An object named %s already exists' % name) return testing.DummyModel.__setitem__(self, name, value) context = makeRoot() context.title = 'Howdy Doody' context.__name__ = 'howdydoody' context.size = 1000 context['photo'] = testing.DummyModel() form = {'photo': DummyUpload(StringIO(one_pixel_jpeg), 'image/jpeg')} self._callFUT(context, form) self.assertTrue('photo' in context) photo = context['photo'] self.assertEqual(photo.title, 'Photo of Howdy Doody') self.assertEqual(photo.mimetype, 'image/jpeg') self.assertEqual(photo.data, one_pixel_jpeg) self.assertEqual(photo.filename, 'test.dat') self.assertEqual(photo.creator, 'userid')
def test_handle_w_mailinglist_wo_subdomain(self): md = self._registerMailDelivery() _called_with = self._registerCatalogSearch([0, 1]) message = self._makeMessage() root = makeRoot() people = root['people'] = testing.DummyModel() people.__parent__ = root section = people['section'] = testing.DummyModel() report = section['report'] = testing.DummyModel() report['mailinglist'] = testing.DummyModel(short_address='alias') report.getQuery = lambda: {'testing': True} adapter = self._makeOne(report) adapter.handle(message, {'report': 'section+report'}, 'text', ()) self.assertEqual(len(md._sent), 2) for index, (to, sentmessage) in enumerate(md._sent): self.assertEqual(to, ['*****@*****.**' % index]) self.failIf('Message-Id' in sentmessage) self.assertEqual(sentmessage['Reply-To'], '*****@*****.**') self.assertEqual(_called_with, [{'testing': True}])
def test_handle_no_email_attachments(self): import datetime from pyramid.testing import DummyModel from karl.content.interfaces import IBlogEntry self._set_NOW(datetime.datetime(2009, 01, 28, 10, 00, 00)) self._registerFactory(IBlogEntry, _makeBlogEntryClass()) self._registerContextURL() self._registerSettings() alerts = self._registerAlerts() workflow = self._registerSecurityWorkflow() context = DummyModel() root = makeRoot() root['foo'] = context context.__parent__ = root adapter = self._makeOne(context) message = object() # ignored info = { 'subject': 'SUBJECT', 'author': 'phreddy', 'date': datetime.datetime(2010, 5, 12, 2, 42) } adapter.handle(message, info, 'TEXT', []) self.assertEqual(len(context), 1) entry_id, entry = context.items()[0] self.assertEqual(entry_id, 'subject') self.assertEqual(entry.title, 'SUBJECT') self.assertEqual(entry.creator, 'phreddy') self.assertEqual(entry.text, 'TEXT') self.assertEqual(entry.description, 'TEXT') self.assertEqual(entry.created, datetime.datetime(2010, 5, 12, 2, 42)) self.failIf(len(entry['attachments'])) self.assertEqual(len(alerts.emissions), 1) self.failUnless(workflow.initialized)
def _makeContext(self): return makeRoot()