def test_notification_two_attaches(): d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test comment notification') fs1 = FieldStorage() fs1.name = 'file_info' fs1.filename = 'fake.txt' fs1.type = 'text/plain' fs1.file = StringIO('this is the content of the fake file\n') fs2 = FieldStorage() fs2.name = 'file_info' fs2.filename = 'fake2.txt' fs2.type = 'text/plain' fs2.file = StringIO('this is the content of the fake file\n') p = t.post(text=u'test message', forum=None, subject='', file_info=[fs1, fs2]) ThreadLocalORMSession.flush_all() n = M.Notification.query.get( subject=u'[test:wiki] Test comment notification') base_url = h.absurl('{}attachment/'.format(p.url())) assert_in( '\nAttachments:\n\n' '- [fake.txt]({0}fake.txt) (37 Bytes; text/plain)\n' '- [fake2.txt]({0}fake2.txt) (37 Bytes; text/plain)'.format(base_url), n.text)
def test_attachment_methods(): d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test Thread') p = t.post('This is a post') p_att = p.attach('foo.text', StringIO('Hello, world!'), discussion_id=d._id, thread_id=t._id, post_id=p._id) t_att = p.attach('foo2.text', StringIO('Hello, thread!'), discussion_id=d._id, thread_id=t._id) d_att = p.attach('foo3.text', StringIO('Hello, discussion!'), discussion_id=d._id) ThreadLocalORMSession.flush_all() assert p_att.post == p assert p_att.thread == t assert p_att.discussion == d for att in (p_att, t_att, d_att): assert 'wiki/_discuss' in att.url() assert 'attachment/' in att.url() # Test notification in mail t = M.Thread.new(discussion_id=d._id, subject='Test comment notification') fs = FieldStorage() fs.name='file_info' fs.filename='fake.txt' fs.type = 'text/plain' fs.file=StringIO('this is the content of the fake file\n') p = t.post(text=u'test message', forum= None, subject= '', file_info=fs) ThreadLocalORMSession.flush_all() n = M.Notification.query.get(subject=u'[test:wiki] Test comment notification') assert '\nAttachment: fake.txt (37 Bytes; text/plain)' in n.text
def test_export_with_attachments(self): project = M.Project.query.get(shortname='test') discussion = project.app_instance('discussion') thread = sorted(Forum.query.get(shortname='general').threads, key=attrgetter('last_post_date'))[-1] post = thread.first_post test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = BytesIO(b'test file1\n') post.add_attachment(test_file1) ThreadLocalORMSession.flush_all() f = tempfile.TemporaryFile('w+') temp_dir = tempfile.mkdtemp() discussion.bulk_export(f, temp_dir, True) f.seek(0) discussion = json.loads(f.read()) forums = sorted(discussion['forums'], key=lambda x: x['name']) threads = sorted(forums[0]['threads'], key=lambda x: x['subject']) file_path = os.path.join('discussion', str(post.discussion_id), str(post.thread_id), post.slug, 'test_file') assert_equal(threads[0]['posts'][0]['attachments'][0]['path'], file_path) os.path.exists(file_path)
def test_export_with_attachments(self): project = M.Project.query.get(shortname='test') blog = project.app_instance('blog') with h.push_context('test', 'blog', neighborhood='Projects'): post = BM.BlogPost.new( title='Test title', text='test post', labels=['the firstlabel', 'the second label'], delete=None ) ThreadLocalORMSession.flush_all() test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = StringIO('test file1\n') p = post.discussion_thread.add_post(text='test comment') p.add_multiple_attachments(test_file1) ThreadLocalORMSession.flush_all() f = tempfile.TemporaryFile() temp_dir = tempfile.mkdtemp() blog.bulk_export(f, temp_dir, True) f.seek(0) blog = json.loads(f.read()) blog['posts'] = sorted( blog['posts'], key=lambda x: x['title'], reverse=True) file_path = 'blog/{}/{}/{}/test_file'.format( post._id, post.discussion_thread._id, list(post.discussion_thread.post_class().query.find())[0].slug ) assert_equal(blog['posts'][0]['discussion_thread']['posts'][0] ['attachments'][0]['path'], file_path) assert os.path.exists(os.path.join(temp_dir, file_path))
def test_export_with_attachments(self): project = M.Project.query.get(shortname='test') discussion = project.app_instance('discussion') post = Forum.query.get(shortname='general').sorted_threads[0].first_post test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = StringIO('test file1\n') post.add_attachment(test_file1) ThreadLocalORMSession.flush_all() f = tempfile.TemporaryFile() temp_dir = tempfile.mkdtemp() discussion.bulk_export(f, temp_dir, True) f.seek(0) discussion = json.loads(f.read()) forums = sorted(discussion['forums'], key=lambda x: x['name']) threads = sorted(forums[0]['threads'], key=lambda x: x['subject']) file_path = os.path.join( 'discussion', str(post.discussion_id), str(post.thread_id), post.slug, 'test_file' ) assert_equal(threads[0]['posts'][0]['attachments'][0]['path'], file_path) os.path.exists(file_path)
def test_attachment_methods(): d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test Thread') p = t.post('This is a post') p_att = p.attach('foo.text', StringIO('Hello, world!'), discussion_id=d._id, thread_id=t._id, post_id=p._id) t_att = p.attach('foo2.text', StringIO('Hello, thread!'), discussion_id=d._id, thread_id=t._id) d_att = p.attach('foo3.text', StringIO('Hello, discussion!'), discussion_id=d._id) ThreadLocalORMSession.flush_all() assert p_att.post == p assert p_att.thread == t assert p_att.discussion == d for att in (p_att, t_att, d_att): assert 'wiki/_discuss' in att.url() assert 'attachment/' in att.url() # Test notification in mail t = M.Thread.new(discussion_id=d._id, subject='Test comment notification') fs = FieldStorage() fs.name = 'file_info' fs.filename = 'fake.txt' fs.type = 'text/plain' fs.file = StringIO('this is the content of the fake file\n') p = t.post(text=u'test message', forum=None, subject='', file_info=fs) ThreadLocalORMSession.flush_all() n = M.Notification.query.get( subject=u'[test:wiki] Test comment notification') assert '\nAttachment: fake.txt (37 Bytes; text/plain)' in n.text
def test_attachment_methods(): d = M.Discussion(shortname="test", name="test") t = M.Thread.new(discussion_id=d._id, subject="Test Thread") p = t.post("This is a post") p_att = p.attach("foo.text", StringIO("Hello, world!"), discussion_id=d._id, thread_id=t._id, post_id=p._id) t_att = p.attach("foo2.text", StringIO("Hello, thread!"), discussion_id=d._id, thread_id=t._id) d_att = p.attach("foo3.text", StringIO("Hello, discussion!"), discussion_id=d._id) ThreadLocalORMSession.flush_all() assert p_att.post == p assert p_att.thread == t assert p_att.discussion == d for att in (p_att, t_att, d_att): assert "wiki/_discuss" in att.url() assert "attachment/" in att.url() # Test notification in mail t = M.Thread.new(discussion_id=d._id, subject="Test comment notification") fs = FieldStorage() fs.name = "file_info" fs.filename = "fake.txt" fs.type = "text/plain" fs.file = StringIO("this is the content of the fake file\n") p = t.post(text=u"test message", forum=None, subject="", file_info=fs) ThreadLocalORMSession.flush_all() n = M.Notification.query.get(subject=u"[test:wiki] Test comment notification") assert "\nAttachment: fake.txt (37 Bytes; text/plain)" in n.text
def test_export_with_attachments(self): project = M.Project.query.get(shortname='test') blog = project.app_instance('blog') with h.push_context('test', 'blog', neighborhood='Projects'): post = BM.BlogPost.new( title='Test title', text='test post', labels=['the firstlabel', 'the second label'], delete=None) ThreadLocalORMSession.flush_all() test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = BytesIO(b'test file1\n') p = post.discussion_thread.add_post(text='test comment') p.add_multiple_attachments(test_file1) ThreadLocalORMSession.flush_all() f = tempfile.TemporaryFile() temp_dir = tempfile.mkdtemp() blog.bulk_export(f, temp_dir, True) f.seek(0) blog = json.loads(f.read()) blog['posts'] = sorted(blog['posts'], key=lambda x: x['title'], reverse=True) file_path = 'blog/{}/{}/{}/test_file'.format( post._id, post.discussion_thread._id, list(post.discussion_thread.post_class().query.find())[0].slug) assert_equal( blog['posts'][0]['discussion_thread']['posts'][0]['attachments'][0] ['path'], file_path) assert os.path.exists(os.path.join(temp_dir, file_path))
def test_notification_two_attaches(): d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test comment notification') fs1 = FieldStorage() fs1.name = 'file_info' fs1.filename = 'fake.txt' fs1.type = 'text/plain' fs1.file = StringIO('this is the content of the fake file\n') fs2 = FieldStorage() fs2.name = 'file_info' fs2.filename = 'fake2.txt' fs2.type = 'text/plain' fs2.file = StringIO('this is the content of the fake file\n') t.post(text=u'test message', forum=None, subject='', file_info=[fs1, fs2]) ThreadLocalORMSession.flush_all() n = M.Notification.query.get(subject=u'[test:wiki] Test comment notification') assert '\nAttachment: fake.txt (37 Bytes; text/plain) fake2.txt (37 Bytes; text/plain)' in n.text
def test_valid_imagefile_upload(self): from adhocracy.forms.common import ValidImageFileUpload from formencode import Invalid from cgi import FieldStorage from io import BytesIO value = FieldStorage() value.file = BytesIO(b"binarydata") value.filename = u"test.png" value.name = u"thumbs" self.assertRaises(Invalid, ValidImageFileUpload.to_python, value)
def test_multiple_attachments(): test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test1.txt' test_file1.type = 'text/plain' test_file1.file = StringIO('test file1\n') test_file2 = FieldStorage() test_file2.name = 'file_info' test_file2.filename = 'test2.txt' test_file2.type = 'text/plain' test_file2.file = StringIO('test file2\n') d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test Thread') test_post = t.post('test post') test_post.add_multiple_attachments([test_file1, test_file2]) ThreadLocalORMSession.flush_all() assert_equals(len(test_post.attachments), 2) attaches = test_post.attachments assert 'test1.txt' in [attaches[0].filename, attaches[1].filename] assert 'test2.txt' in [attaches[0].filename, attaches[1].filename]
def setup_with_tools(self): super(TestBulkExport, self).setup_with_tools() self.project = M.Project.query.get(shortname='test') self.tracker = self.project.app_instance('bugs') self.new_ticket(summary='foo', _milestone='1.0') self.new_ticket(summary='bar', _milestone='2.0') self.ticket = TM.Ticket.query.find(dict(summary='foo')).first() self.post = self.ticket.discussion_thread.add_post(text='silly comment') ThreadLocalORMSession.flush_all() test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = StringIO('test file1\n') self.post.add_attachment(test_file1) ThreadLocalORMSession.flush_all()
def test_add_attachment(): test_file = FieldStorage() test_file.name = 'file_info' test_file.filename = 'test.txt' test_file.type = 'text/plain' test_file.file = StringIO('test file\n') d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test Thread') test_post = t.post('test post') test_post.add_attachment(test_file) ThreadLocalORMSession.flush_all() assert_equals(len(test_post.attachments), 1) attach = test_post.attachments[0] assert attach.filename == 'test.txt', attach.filename assert attach.content_type == 'text/plain', attach.content_type
def test_add_attachment(): test_file = FieldStorage() test_file.name = "file_info" test_file.filename = "test.txt" test_file.type = "text/plain" test_file.file = StringIO("test file\n") d = M.Discussion(shortname="test", name="test") t = M.Thread.new(discussion_id=d._id, subject="Test Thread") test_post = t.post("test post") test_post.add_attachment(test_file) ThreadLocalORMSession.flush_all() assert test_post.attachments.count() == 1, test_post.attachments.count() attach = test_post.attachments.first() assert attach.filename == "test.txt", attach.filename assert attach.content_type == "text/plain", attach.content_type
def test_add_attachment(): test_file = FieldStorage() test_file.name = 'file_info' test_file.filename = 'test.txt' test_file.type = 'text/plain' test_file.file = StringIO('test file\n') d = M.Discussion(shortname='test', name='test') t = M.Thread.new(discussion_id=d._id, subject='Test Thread') test_post = t.post('test post') test_post.add_attachment(test_file) ThreadLocalORMSession.flush_all() assert test_post.attachments.count() == 1, test_post.attachments.count() attach = test_post.attachments.first() assert attach.filename == 'test.txt', attach.filename assert attach.content_type == 'text/plain', attach.content_type
def setup_with_tools(self): super(TestBulkExport, self).setup_with_tools() self.project = M.Project.query.get(shortname='test') self.tracker = self.project.app_instance('bugs') self.new_ticket(summary='foo', _milestone='1.0') self.new_ticket(summary='bar', _milestone='2.0') self.ticket = TM.Ticket.query.find(dict(summary='foo')).first() self.post = self.ticket.discussion_thread.add_post( text='silly comment') ThreadLocalORMSession.flush_all() test_file1 = FieldStorage() test_file1.name = 'file_info' test_file1.filename = 'test_file' test_file1.file = BytesIO(b'test file1\n') self.post.add_attachment(test_file1) ThreadLocalORMSession.flush_all()