tests: use context manager to load file contents

This commit is contained in:
Frédéric Péters 2021-04-17 20:27:04 +02:00
parent 5ce9e765f5
commit b10c47bfc9
9 changed files with 75 additions and 62 deletions

View File

@ -561,7 +561,8 @@ def test_settings_idp(pub):
resp2 = resp.click('Identity Providers')
resp2 = resp2.click('New')
idp_metadata_filename = os.path.join(os.path.dirname(__file__), '..', 'idp_metadata.xml')
resp2.form['metadata'] = Upload('idp_metadata.xml', open(idp_metadata_filename, 'rb').read())
with open(idp_metadata_filename, 'rb') as fd:
resp2.form['metadata'] = Upload('idp_metadata.xml', fd.read())
resp2 = resp2.form.submit('submit')
resp = resp.click('Identity Providers')

View File

@ -1147,7 +1147,8 @@ def test_workflows_edit_export_to_model_action(pub):
resp = resp.follow()
resp = resp.click('Document Creation')
model_content = open(os.path.join(os.path.dirname(__file__), '../template.odt'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), '../template.odt'), 'rb') as fd:
model_content = fd.read()
resp.form['model_file$file'] = Upload('test.odt', model_content)
resp = resp.form.submit('submit')
resp = resp.follow()

View File

@ -672,8 +672,8 @@ def test_backoffice_image_column(pub):
formdef.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
jpg = open(os.path.join(os.path.dirname(__file__), '..', 'image-with-gps-data.jpeg'), 'rb').read()
upload.receive([jpg])
with open(os.path.join(os.path.dirname(__file__), '..', 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
for formdata in formdef.data_class().select(lambda x: x.status == 'wf-new'):
formdata.data['4'] = upload
formdata.store()

View File

@ -425,7 +425,8 @@ def test_formdata_generated_document_odt_download(pub, odt_template):
export_to.convert_to_pdf = False
export_to.label = 'create doc'
template_filename = os.path.join(os.path.dirname(__file__), '..', odt_template)
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/' + odt_template, content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -517,7 +518,8 @@ def test_formdata_generated_document_odt_download_with_substitution_variable(pub
export_to.label = 'create doc'
export_to.varname = 'created_doc'
template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -638,7 +640,8 @@ def test_formdata_generated_document_odt_to_pdf_download(pub):
export_to.label = 'create doc'
export_to.varname = 'created_doc'
template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -709,7 +712,8 @@ def test_formdata_generated_document_odt_to_pdf_download_push_to_portfolio(
export_to.label = 'create doc'
export_to.varname = 'created_doc'
template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -802,7 +806,8 @@ def test_formdata_generated_document_non_interactive(pub):
export_to.convert_to_pdf = False
export_to.method = 'non-interactive'
template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -864,7 +869,8 @@ def test_formdata_generated_document_to_backoffice_field(pub):
export_to.convert_to_pdf = False
export_to.method = 'non-interactive'
template_filename = os.path.join(os.path.dirname(__file__), '..', 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)

View File

@ -292,8 +292,8 @@ def test_workflow_send_mail_template_attachments(pub, superuser, mail_templates_
formdef.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
jpg = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
upload.receive([jpg])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
formdata = formdef.data_class()()
formdata.data = {'1': upload}
formdata.just_created()

View File

@ -477,12 +477,14 @@ def test_email_plain_with_attachments(emails):
create_temporary_pub()
jpg = PicklableUpload('test.jpeg', 'image/jpeg')
jpg_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
jpg_content = fd.read()
jpg.receive([jpg_content])
txt = PicklableUpload('test.txt', 'text/plain')
txt.receive([b'foo-text-bar'])
odt = PicklableUpload('test.odt', 'application/vnd.oasis.opendocument.text')
odt_content = open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb') as fd:
odt_content = fd.read()
odt.receive([odt_content])
send_email('jpg', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[jpg])
@ -542,8 +544,8 @@ def test_email_plain_and_html_with_attachments(emails):
pub = create_temporary_pub()
pub.cfg['emails'] = {'footer': 'Footer\nText'}
jpg = PicklableUpload('test.jpeg', 'image/jpeg')
jpg_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
jpg.receive([jpg_content])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
jpg.receive([fd.read()])
send_email('test', mail_body='Hello', email_rcpt='test@localhost', attachments=[jpg])
assert emails.count() == 1

View File

@ -115,7 +115,8 @@ def test_form_file_field_upload_storage(wscall, pub):
json.dumps({"err": 0, "data": {"redirect_url": "https://crypto.example.net/"}}),
)
image_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
image_content = fd.read()
upload_0 = Upload('file.jpg', image_content, 'image/jpeg')
upload_1 = Upload('remote.jpg', image_content, 'image/jpeg')
@ -255,7 +256,8 @@ def test_thumbnail_caching(pub):
assert formdef.fields[0].storage == 'default'
image_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
image_content = fd.read()
upload = Upload('file.jpg', image_content, 'image/jpeg')
resp = get_app(pub).get('/test/')
@ -292,7 +294,8 @@ def test_remoteopaque_in_attachmentevolutionpart(wscall, pub):
json.dumps({"err": 0, "data": {"redirect_url": "https://crypto.example.net/"}}),
)
image_content = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
image_content = fd.read()
upload_0 = Upload('local-file.jpg', image_content, 'image/jpeg')
upload_1 = Upload('remote-file.jpg', image_content, 'image/jpeg')

View File

@ -1116,8 +1116,8 @@ def test_register_comment_with_attachment_file(pub):
wf.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
jpg = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
upload.receive([jpg])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
formdef = FormDef()
formdef.name = 'baz'
@ -1549,8 +1549,8 @@ def test_email_attachments(pub, emails):
formdef.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
jpg = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
upload.receive([jpg])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
formdata = formdef.data_class()()
formdata.data = {'3': upload}
formdata.just_created()
@ -3345,7 +3345,8 @@ def test_geolocate_image(pub):
formdef.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
upload.receive([open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
formdata = formdef.data_class()()
formdata.data = {'3': upload}
@ -3375,7 +3376,8 @@ def test_geolocate_image(pub):
# invalid photo
upload = PicklableUpload('test.jpeg', 'image/jpeg')
upload.receive([open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb').read()])
with open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb') as fd:
upload.receive([fd.read()])
formdata.data = {'3': upload}
formdata.geolocations = None
item.perform(formdata)
@ -3478,7 +3480,8 @@ def test_export_to_model_image(pub):
formdef.store()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
image_data = open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
image_data = fd.read()
upload.receive([image_data])
formdata = formdef.data_class()()
@ -3491,7 +3494,8 @@ def test_export_to_model_image(pub):
item.convert_to_pdf = False
item.method = 'non-interactive'
template_filename = os.path.join(os.path.dirname(__file__), 'template-with-image.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -3555,7 +3559,8 @@ def test_export_to_model_backoffice_field(pub):
item.method = 'non-interactive'
item.convert_to_pdf = False
template_filename = os.path.join(os.path.dirname(__file__), 'template.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -3613,7 +3618,8 @@ def test_export_to_model_django_template(pub):
item.method = 'non-interactive'
item.attach_to_history = True
template_filename = os.path.join(os.path.dirname(__file__), 'template-django.odt')
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload('/foo/template-django.odt', content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -3622,21 +3628,24 @@ def test_export_to_model_django_template(pub):
item.convert_to_pdf = False
item.perform(formdata)
new_content = zipfile.ZipFile(open(formdata.evolution[0].parts[0].filename, 'rb')).read('content.xml')
with open(formdata.evolution[0].parts[0].filename, 'rb') as fd:
new_content = zipfile.ZipFile(fd).read('content.xml')
assert b'>foo-export-to-template-with-django<' in new_content
formdef.name = 'Name with a \' simple quote'
formdef.store()
item.perform(formdata)
new_content = zipfile.ZipFile(open(formdata.evolution[0].parts[1].filename, 'rb')).read('content.xml')
with open(formdata.evolution[0].parts[1].filename, 'rb') as fd:
new_content = zipfile.ZipFile(fd).read('content.xml')
assert b'>Name with a \' simple quote<' in new_content
formdef.name = 'A <> name'
formdef.store()
item.perform(formdata)
new_content = zipfile.ZipFile(open(formdata.evolution[0].parts[2].filename, 'rb')).read('content.xml')
with open(formdata.evolution[0].parts[2].filename, 'rb') as fd:
new_content = zipfile.ZipFile(fd).read('content.xml')
assert b'>A &lt;&gt; name<' in new_content
@ -3677,7 +3686,8 @@ def test_export_to_model_form_details_section(pub, filename):
formdef.store()
formdef.data_class().wipe()
upload = PicklableUpload('test.jpeg', 'image/jpeg')
upload.receive([open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
upload.receive([fd.read()])
formdata = formdef.data_class()()
formdata.data = {
'4': 'string',
@ -3703,7 +3713,8 @@ def test_export_to_model_form_details_section(pub, filename):
item.method = 'non-interactive'
item.attach_to_history = True
template_filename = os.path.join(os.path.dirname(__file__), filename)
template = open(template_filename, 'rb').read()
with open(template_filename, 'rb') as fd:
template = fd.read()
upload = QuixoteUpload(filename, content_type='application/octet-stream')
upload.fp = io.BytesIO()
upload.fp.write(template)
@ -3712,9 +3723,8 @@ def test_export_to_model_form_details_section(pub, filename):
item.convert_to_pdf = False
item.perform(formdata)
new_content = force_text(
zipfile.ZipFile(open(formdata.evolution[0].parts[0].filename, 'rb')).read('content.xml')
)
with open(formdata.evolution[0].parts[0].filename, 'rb') as fd:
new_content = force_text(zipfile.ZipFile(fd).read('content.xml'))
# section content has been removed
assert 'Titre de page' not in new_content
assert 'Titre' not in new_content
@ -3736,9 +3746,8 @@ def test_export_to_model_form_details_section(pub, filename):
assert 'XfooY, Xfoo2Y' in new_content
if filename == 'template-form-details-no-styles.odt':
new_styles = force_text(
zipfile.ZipFile(open(formdata.evolution[0].parts[0].filename, 'rb')).read('styles.xml')
)
with open(formdata.evolution[0].parts[0].filename, 'rb') as fd:
new_styles = force_text(zipfile.ZipFile(fd).read('styles.xml'))
assert 'Field_20_Label' in new_styles
@ -4405,7 +4414,9 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
# store a PiclableUpload
upload = PicklableUpload('test.jpeg', 'image/jpeg')
upload.receive([open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()])
with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
image_with_gps_data = fd.read()
upload.receive([image_with_gps_data])
formdata = formdef.data_class()()
formdata.data = {'00': upload}
@ -4418,13 +4429,8 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
formdata = formdef.data_class().get(formdata.id)
assert formdata.data['bo1'].base_filename == 'test.jpeg'
assert formdata.data['bo1'].content_type == 'image/jpeg'
assert (
formdata.data['bo1'].get_content()
== open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
)
assert formdata.data['bo1'].get_base64_content() == base64.encodebytes(
open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
)
assert formdata.data['bo1'].get_content() == image_with_gps_data
assert formdata.data['bo1'].get_base64_content() == base64.encodebytes(image_with_gps_data)
# check with template string
formdata = formdef.data_class()()
@ -4438,10 +4444,7 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
assert formdata.data['bo1'].base_filename == 'test.jpeg'
assert formdata.data['bo1'].content_type == 'image/jpeg'
assert (
formdata.data['bo1'].get_content()
== open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
)
assert formdata.data['bo1'].get_content() == image_with_gps_data
# check with template string, without _raw
formdata = formdef.data_class()()
@ -4455,10 +4458,7 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
assert formdata.data['bo1'].base_filename == 'test.jpeg'
assert formdata.data['bo1'].content_type == 'image/jpeg'
assert (
formdata.data['bo1'].get_content()
== open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
)
assert formdata.data['bo1'].get_content() == image_with_gps_data
# check |strip_metadata filter
formdata = formdef.data_class()()
@ -4515,7 +4515,9 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
assert not Image or formdata.data['bo1'].get_content().find(b'<exif:XResolution>') == -1
upload2 = PicklableUpload('test2.odt', 'application/vnd.oasis.opendocument.text')
upload2.receive([open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb').read()])
with open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb') as fd:
template_odt = fd.read()
upload2.receive([template_odt])
formdata = formdef.data_class()()
formdata.data = {'00': upload2}
formdata.just_created()
@ -4525,10 +4527,7 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
formdata = formdef.data_class().get(formdata.id)
assert formdata.data['bo1'].base_filename == 'new_test2.odt'
assert formdata.data['bo1'].content_type == 'my content type'
assert (
formdata.data['bo1'].get_content()
== open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb').read()
)
assert formdata.data['bo1'].get_content() == template_odt
assert formdata.data['bo1'].qfilename == formdata.data['00'].qfilename
# check storing response as attachment

View File

@ -404,7 +404,8 @@ class HttpRequestsMocking:
if url.startswith('file://'):
try:
status, data = 200, open(url[7:]).read()
with open(url[7:]) as fd:
status, data = 200, fd.read()
except IOError:
status = 404