99 lines
5.2 KiB
Python
99 lines
5.2 KiB
Python
import io
|
|
import re
|
|
|
|
import openpyxl
|
|
import pytest
|
|
from werkzeug.http import parse_options_header
|
|
|
|
from conftest import post
|
|
from test_workflow import action, new, order, save
|
|
|
|
|
|
def download_name(response):
|
|
return parse_options_header(response.headers['Content-Disposition'])[1]['filename']
|
|
|
|
|
|
@pytest.mark.parametrize('kind,title', [('purchase', '采购单'), ('outsourcing', '委外生产订单')])
|
|
def test_supplier_alias_updates_filename_without_changing_archive(app, clients, kind, title):
|
|
company = '苏州精锐五金制品有限公司'
|
|
assert post(clients['maker'], '/suppliers', {'name': company, 'short_name': '精锐五金'}).status_code == 302
|
|
with app.Session() as db:
|
|
sid = db.query(app.Supplier).one().id
|
|
oid = new(app, clients['maker'], kind)
|
|
assert save(app, clients['maker'], oid, supplier_name=company, intent='submit').status_code == 302
|
|
assert action(app, clients['approver'], oid, 'approve', stamped='1').status_code == 302
|
|
with app.Session() as db:
|
|
number = db.get(app.Order, oid).order_no
|
|
path = '/orders/%s/pdf' % oid
|
|
archive = clients['maker'].get(path).data
|
|
original_version = order(app, oid)['version']
|
|
for suffix, disposition in [('', 'inline'), ('?dl=1', 'attachment')]:
|
|
response = clients['maker'].get(path + suffix)
|
|
assert response.headers['Content-Disposition'].startswith(disposition + ';')
|
|
assert download_name(response) == '%s_%s_精锐五金.pdf' % (title, number)
|
|
assert response.data == archive
|
|
assert post(clients['maker'], '/suppliers', {'id': sid, 'name': company, 'short_name': '精锐'}).status_code == 302
|
|
response = clients['maker'].get(path + '?dl=1')
|
|
assert download_name(response) == '%s_%s_精锐.pdf' % (title, number)
|
|
assert response.data == archive and order(app, oid)['version'] == original_version
|
|
assert clients['other'].get(path + '?dl=1').status_code == 404
|
|
# A cleared alias or removed master-data entry still yields an identifiable name.
|
|
assert post(clients['maker'], '/suppliers', {'id': sid, 'name': company, 'short_name': ''}).status_code == 302
|
|
assert download_name(clients['maker'].get(path)) == '%s_%s_%s.pdf' % (title, number, company)
|
|
assert post(clients['admin'], '/suppliers/%s/delete' % sid).status_code == 302
|
|
response = clients['maker'].get(path)
|
|
assert download_name(response) == '%s_%s_%s.pdf' % (title, number, company)
|
|
assert response.data == archive
|
|
|
|
|
|
def test_pdf_filename_handles_unsafe_characters_and_ambiguous_suppliers(app, clients):
|
|
company = '测试同名公司'
|
|
with app.Session() as db:
|
|
db.add(app.Supplier(name=company, tax_no='ONE', short_name='精锐/五金:分部?\r\n<二>'))
|
|
db.add(app.Supplier(name=company, tax_no='TWO', short_name='另一家'))
|
|
db.commit()
|
|
oid = new(app, clients['maker'])
|
|
assert save(app, clients['maker'], oid, supplier_name=company, supplier_tax_no='ONE').status_code == 302
|
|
response = clients['maker'].get('/orders/%s/pdf?dl=1' % oid)
|
|
filename = download_name(response)
|
|
assert response.status_code == 200
|
|
assert '精锐_五金' in filename and '分部' in filename
|
|
assert not re.search(r'[<>:"/\\|?*\x00-\x1f]', filename)
|
|
assert len(filename.encode('utf-16-le')) // 2 <= 255
|
|
# Without identifying tax information, do not pick an arbitrary company's alias.
|
|
assert save(app, clients['maker'], oid, supplier_name=company, supplier_tax_no='').status_code == 302
|
|
assert download_name(clients['maker'].get('/orders/%s/pdf' % oid)).endswith('_测试同名公司.pdf')
|
|
assert save(app, clients['maker'], oid, supplier_name='公司' * 90).status_code == 302
|
|
assert len(download_name(clients['maker'].get('/orders/%s/pdf' % oid)).encode('utf-16-le')) // 2 <= 255
|
|
|
|
|
|
def test_supplier_import_alias_headers_legacy_preservation_and_blank_clear(app, clients):
|
|
company = '苏州测试有限公司'
|
|
|
|
def upload(heads, row):
|
|
workbook = openpyxl.Workbook()
|
|
workbook.active.append(heads)
|
|
workbook.active.append(row)
|
|
stream = io.BytesIO()
|
|
workbook.save(stream)
|
|
stream.seek(0)
|
|
response = post(clients['maker'], '/suppliers/import_excel', {'file': (stream, '供应商.xlsx')})
|
|
assert response.status_code == 302
|
|
|
|
upload(['公司简称', '供应商名称', '供应商编号'], ['苏测', company, 'S001'])
|
|
with app.Session() as db:
|
|
supplier = db.query(app.Supplier).one()
|
|
assert (supplier.code, supplier.name, supplier.short_name) == ('S001', company, '苏测')
|
|
upload(['编号', '名称', '联系人'], ['S001', company, '新联系人'])
|
|
with app.Session() as db:
|
|
supplier = db.query(app.Supplier).one()
|
|
assert (supplier.short_name, supplier.contact) == ('苏测', '新联系人')
|
|
upload(['编号', '名称', '公司简称'], ['S001', company, ''])
|
|
with app.Session() as db:
|
|
assert db.query(app.Supplier).one().short_name == ''
|
|
template = openpyxl.load_workbook(io.BytesIO(clients['maker'].get('/suppliers/import_template.xlsx').data))
|
|
assert template.active.cell(1, 9).value == '公司简称'
|
|
assert template.active.cell(2, 9).value == '精锐五金'
|
|
template.close()
|
|
assert post(clients['maker'], '/suppliers', {'name': '超长简称测试', 'short_name': '长' * 61}).status_code == 400
|