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_user_manual_form(wtfs_app, pdf_1, pdf_2): with open(pdf_1, 'rb') as file: pdf_1 = BytesIO(file.read()) with open(pdf_2, 'rb') as file: pdf_2 = BytesIO(file.read()) user_manual = UserManual(wtfs_app) form = UserManualForm() form.apply_model(user_manual) assert form.pdf.data is None # Add field_storage = FieldStorage() field_storage.file = pdf_1 field_storage.type = 'application/pdf' field_storage.filename = 'example_1.pdf' form.pdf.process(PostData({'pdf': field_storage})) form.update_model(user_manual) pdf_1.seek(0) assert user_manual.pdf == pdf_1.read() form.apply_model(user_manual) assert form.pdf.data == { 'filename': 'user_manual.pdf', 'size': 8130, 'mimetype': 'application/pdf' } # Replace field_storage = FieldStorage() field_storage.file = pdf_2 field_storage.type = 'application/pdf' field_storage.filename = 'example_2.pdf' form.pdf.process(PostData({'pdf': field_storage})) form.update_model(user_manual) pdf_2.seek(0) assert user_manual.pdf == pdf_2.read() form.apply_model(user_manual) assert form.pdf.data == { 'filename': 'user_manual.pdf', 'size': 9115, 'mimetype': 'application/pdf' } # Delete form.pdf.action = 'delete' form.update_model(user_manual) assert not user_manual.exists
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_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 create_file(mimetype, filename, content): fs = FieldStorage() fs.file = TemporaryFile("wb+") fs.type = mimetype fs.filename = filename fs.file.write(content) fs.file.seek(0) return fs
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 to_fieldstorage(f): fs = FieldStorage() fs.file = f fs.type = f.mimetype opt = f.mimetype_params opt['filename'] = f.filename fs.disposition_options = opt fs.type_options = opt return fs
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 process(content, **kwargs): field = MunicipalityDataUploadField(**kwargs) field = field.bind(form, 'upload') field_storage = FieldStorage() field_storage.file = BytesIO(content) field_storage.type = 'text/plain' field_storage.filename = 'test.csv' field.process(PostData({'upload': field_storage})) return field
def apply_model(self, model): self.title.data = model.title self.portrait.data = model.portrait self.export_fields.data = model.export_fields if model.organigram_file: fs = FieldStorage() fs.file = BytesIO(model.organigram_file.read()) fs.type = model.organigram_file.content_type fs.filename = model.organigram_file.filename self.organigram.data = self.organigram.process_fieldstorage(fs) if hasattr(self, 'is_hidden_from_public'): self.is_hidden_from_public.data = model.is_hidden_from_public self.reorder_export_fields()
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 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_update_dataset_form(session): request = DummyRequest(session, DummyPrincipal()) # Validate form = UpdateDatasetForm() form.request = request assert not form.validate() file = BytesIO() workbook = Workbook(file) worksheet = workbook.add_worksheet('DATA') workbook.add_worksheet('CITATION') worksheet.write_row(0, 0, ColumnMapper().columns.values()) worksheet.write_row( 1, 0, [ 100.1, # anr / NUMERIC '1.2.2008', # datum / DATE 1, # legislatur / INTEGER '2004-2008', # legisjahr / INT4RANGE 'kurztitel de', # titel_kurz_d 'kurztitel fr', # titel_kurz_f 'titel de', # titel_off_d 'titel fr', # titel_off_f 'stichwort', # stichwort / TEXT 2, # anzahl / INTEGER 3, # rechtsform ]) workbook.close() file.seek(0) field_storage = FieldStorage() field_storage.file = file field_storage.type = 'application/excel' field_storage.filename = 'test.xlsx' form.dataset.process(DummyPostData({'dataset': field_storage})) assert form.validate()
def test_import_municipality_data_form(session): municipalities = MunicipalityCollection(session) municipalities.add(name="Boppelsen", bfs_number=82) municipalities.add(name="Adlikon", bfs_number=21) # Test apply form = ImportMunicipalityDataForm() form.request = Request(session) form.file.data = { 21: { 'dates': [date(2019, 1, 1), date(2019, 1, 7)] }, 241: { 'dates': [date(2019, 1, 3), date(2019, 1, 9)] }, 82: { 'dates': [date(2019, 1, 4), date(2019, 1, 10)] } } form.update_model(municipalities) assert [(m.bfs_number, [d.date for d in m.pickup_dates]) for m in municipalities.query() ] == [(21, [date(2019, 1, 1), date(2019, 1, 7)]), (82, [date(2019, 1, 4), date(2019, 1, 10)])] # Test validation form = ImportMunicipalityDataForm() form.request = Request(session) assert not form.validate() field_storage = FieldStorage() field_storage.file = BytesIO( "Adlikon;21;-1;Normal;12.2.2015".encode('cp1252')) field_storage.type = 'text/csv' field_storage.filename = 'test.csv' form.file.process(PostData({'file': field_storage})) assert form.validate()
def test_fileupload_handle(self): # Abstract file upload handle container = ContainerNode(name='container') request = self.layer.new_request() abstract_upload_handle = FileUploadHandle(container, request) # If request method is GET, existing files are read. Abstract # implementation returns empty result self.assertEqual(abstract_upload_handle(), {'files': []}) # If request method is POST, a file upload is assumed filedata = FieldStorage() filedata.type = 'text/plain' filedata.filename = 'test.txt' filedata.file = StringIO('I am the payload') request.method = 'POST' request.params['file'] = filedata del request.params['_LOCALE_'] res = abstract_upload_handle() self.assertEqual(res['files'][0]['name'], 'test.txt') self.assertEqual(res['files'][0]['size'], 0) self.assertEqual( res['files'][0]['error'], 'Abstract ``FileUploadHandle`` does not implement ``create_file``' ) # Concrete implementation of file upload handle upload_handle = ContainerFileUploadHandle(container, request) # Upload file res = upload_handle() self.assertEqual(res['files'], [{ 'name': 'test.txt', 'size': 16, 'view_url': '/test.txt', 'download_url': '/test.txt/download', 'delete_url': '/test.txt/filedelete_handle', 'delete_type': 'GET' }]) self.checkOutput(""" <class 'cone.fileupload.tests.ContainerNode'>: container <class 'cone.fileupload.tests.File'>: test.txt body: 'I am the payload' """, container.treerepr()) # Read existing files request = self.layer.new_request() upload_handle = ContainerFileUploadHandle(container, request) self.assertEqual(upload_handle()['files'], [{ 'name': 'test.txt', 'size': 16, 'view_url': '/test.txt', 'download_url': '/test.txt/download', 'delete_url': '/test.txt/filedelete_handle', 'delete_type': 'GET' }]) # Test file delete handle file = container['test.txt'] request = self.layer.new_request() self.assertEqual( filedelete_handle(file, request), {'files': [{'test.txt': True}]} ) self.checkOutput(""" <class 'cone.fileupload.tests.ContainerNode'>: container """, container.treerepr())
def test_attachments_form(swissvotes_app, attachments): session = swissvotes_app.session() names = list(attachments.keys()) # Test apply / update votes = SwissVoteCollection(swissvotes_app) vote = votes.add( id=1, bfs_number=Decimal('1'), date=date(1990, 6, 2), legislation_number=4, legislation_decade=NumericRange(1990, 1994), title_de="Vote DE", title_fr="Vote FR", short_title_de="V D", short_title_fr="V F", votes_on_same_day=2, _legal_form=1, ) form = AttachmentsForm() form.request = DummyRequest(session, DummyPrincipal()) # ... empty form.apply_model(vote) assert all([getattr(form, name).data is None for name in names]) form.update_model(vote) assert all([getattr(vote, name) is None for name in names]) # ... add attachments (de_CH) for name, attachment in attachments.items(): setattr(vote, name, attachment) session.flush() # ... not present with fr_CH vote.session_manager.current_locale = 'fr_CH' form.apply_model(vote) assert all([getattr(form, name).data is None for name in names]) # ... present with de_CH vote.session_manager.current_locale = 'de_CH' form.apply_model(vote) for name in names: data = getattr(form, name).data assert data['size'] assert data['filename'] == name assert data['mimetype'] in ( 'application/pdf', 'application/zip', 'application/vnd.ms-office', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) form.update_model(vote) for name in names: file = getattr(vote, name) assert file == attachments[name] assert file.reference.filename == name assert file.reference.content_type in ( 'application/pdf', 'application/zip', 'application/vnd.ms-office', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) # ... replace all de_CH for name in names: field_storage = FieldStorage() field_storage.file = BytesIO(f'{name}-1'.encode()) field_storage.type = 'image/png' # ignored field_storage.filename = f'{name}.png' # extension removed getattr(form, name).process(DummyPostData({name: field_storage})) form.update_model(vote) assert all([ getattr(vote, name).reference.file.read() == f'{name}-1'.encode() for name in names ]) # ... add all fr_CH vote.session_manager.current_locale = 'fr_CH' for name in names: field_storage = FieldStorage() field_storage.file = BytesIO(f'{name}-fr'.encode()) field_storage.type = 'image/png' # ignored field_storage.filename = f'{name}.png' # extension removed getattr(form, name).process(DummyPostData({name: field_storage})) form.update_model(vote) assert all([ getattr(vote, name).reference.file.read() == f'{name}-fr'.encode() for name in names ]) # ... delete all fr_CH for name in names: getattr(form, name).action = 'delete' form.update_model(vote) assert all([getattr(vote, name) is None for name in names]) vote.session_manager.current_locale = 'de_CH' assert all([getattr(vote, name) is not None for name in names]) # ... delete all de_CH for name in names: getattr(form, name).action = 'delete' form.update_model(vote) assert vote.files == [] # Test validation form = AttachmentsForm() assert form.validate()
from sqlalchemy.orm.exc import NoResultFound from webob.multidict import MultiDict from warehouse.admin.interfaces import ISponsorLogoStorage from warehouse.admin.views import sponsors as views from warehouse.sponsors.models import Sponsor from ....common.db.sponsors import SponsorFactory COLOR_LOGO_FILE = FieldStorage() COLOR_LOGO_FILE.filename = "colorlogo.png" COLOR_LOGO_FILE.file = io.BytesIO(( b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06" b"\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\xdac\xfc\xcf\xc0P\x0f\x00" b"\x04\x85\x01\x80\x84\xa9\x8c!\x00\x00\x00\x00IEND\xaeB`\x82")) COLOR_LOGO_FILE.type = "image/png" WHITE_LOGO_FILE = FieldStorage() WHITE_LOGO_FILE.filename = "whitelogo.png" WHITE_LOGO_FILE.file = io.BytesIO(( b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06" b"\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\xdac\xfc\xcf\xc0P\x0f\x00" b"\x04\x85\x01\x80\x84\xa9\x8c!\x00\x00\x00\x00IEND\xaeB`\x82")) WHITE_LOGO_FILE.type = "image/png" class TestSponsorList: def test_list_all_sponsors(self, db_request): [SponsorFactory.create() for _ in range(5)] sponsors = db_request.db.query(Sponsor).order_by(Sponsor.name).all()
def test_fileupload_handle(self): # Abstract file upload handle container = ContainerNode(name='container') request = self.layer.new_request() abstract_upload_handle = FileUploadHandle(container, request) # If request method is GET, existing files are read. Abstract # implementation returns empty result self.assertEqual(abstract_upload_handle(), {'files': []}) # If request method is POST, a file upload is assumed filedata = FieldStorage() filedata.type = 'text/plain' filedata.filename = 'test.txt' filedata.file = StringIO('I am the payload') request.method = 'POST' request.params['file'] = filedata del request.params['_LOCALE_'] res = abstract_upload_handle() self.assertEqual(res['files'][0]['name'], 'test.txt') self.assertEqual(res['files'][0]['size'], 0) self.assertEqual( res['files'][0]['error'], 'Abstract ``FileUploadHandle`` does not implement ``create_file``' ) # Concrete implementation of file upload handle upload_handle = ContainerFileUploadHandle(container, request) # Upload file res = upload_handle() self.assertEqual(res['files'], [{ 'url': '/test.txt', 'deleteType': 'GET', 'deleteUrl': '/test.txt/filedelete_handle', 'name': 'test.txt', 'size': 16 }]) self.checkOutput(""" <class 'cone.fileupload.tests.ContainerNode'>: container <class 'cone.fileupload.tests.File'>: test.txt body: 'I am the payload' """, container.treerepr()) # Read existing files request = self.layer.new_request() upload_handle = ContainerFileUploadHandle(container, request) self.assertEqual(upload_handle()['files'], [{ 'url': '/test.txt', 'deleteType': 'GET', 'deleteUrl': '/test.txt/filedelete_handle', 'name': 'test.txt', 'size': 16 }]) # Test file delete handle file = container['test.txt'] request = self.layer.new_request() self.assertEqual( filedelete_handle(file, request), {'files': [{'test.txt': True}]} ) self.checkOutput(""" <class 'cone.fileupload.tests.ContainerNode'>: container """, container.treerepr())