"""The review photo or video attached to an order in Revisão (Fase 2, front 2). Run against the local stack: SITE_BASE_URL=http://site SITE_HOST_HEADER=localhost python -m tests.review_media_test """ from io import BytesIO from urllib.request import Request, urlopen from uuid import uuid4 from PIL import Image from tests.smoke_test import (Client, approved_quote, final_png_bytes, item_spec, review_photo, upload_bytes, wait_scan) def jpeg(width, height): out = BytesIO() Image.new('RGB', (width, height), (190, 60, 50)).save(out, 'JPEG', quality=80) return out.getvalue() def run(): customer = Client() customer.call('/session') original = upload_bytes(customer, b'LOCAL REVIEW ORIGINAL') item = item_spec('file', '1.01', 0, original) profile = {'cnpj': '11222333000181', 'zap': '11999999999', 'mail': 'review-' + uuid4().hex[:8] + '@example.test'} quote = customer.call('/quotes', {'request_key': str(uuid4()), 'customer': profile, 'items': [item], 'freight': {'service': 'pickup'}}) approved_quote(customer, quote, [item]) order = customer.call('/orders/dev-paid', {'quote_id': quote['id']}) oid, number = order['id'], order['number'] final = upload_bytes(customer, final_png_bytes('review final'), name='LOCAL-REVIEW-FINAL.png', order_id=oid) version = customer.call('/operator/orders/' + oid + '/final-files', {'version': 0, 'note': 'Conferido', 'files': [{'item_index': 0, 'upload_id': final}]}, operator=True)['version'] machine = None def move(state, version): nonlocal machine body = {'state': state, 'version': version, 'reason': ''} if state == 'imp': if machine is None: machine = customer.call('/operator/machines', {'name': 'Review ' + uuid4().hex[:6], 'products': ['textil']}, operator=True)['id'] body['machine_id'] = machine return customer.call('/operator/orders/' + oid + '/move', body, operator=True)['version'] for state in ('tra', 'imp', 'rev'): version = move(state, version) def upload_raw(name, content): start = customer.call('/operator/orders/' + oid + '/reviews/uploads', {'name': name, 'size': len(content)}, operator=True) uid = start['id'] url = customer.call('/operator/uploads/' + uid + '/parts/1', {}, operator=True)['url'] with urlopen(Request(url, data=content, method='PUT'), timeout=30) as response: assert response.status == 200 customer.call('/operator/uploads/' + uid + '/complete', {}, operator=True) wait_scan(customer, uid, operator=True) return uid def add_raw(uid, expected=200): return customer.call('/operator/orders/' + oid + '/reviews', {'upload_id': uid}, operator=True, expected=expected) # Above 1 m without a photo the order does not reach Finalizado. customer.call('/operator/orders/' + oid + '/move', {'state': 'fin', 'version': version}, operator=True, expected=409) # A photo below 1600 px on the longer side is refused. small = upload_raw('LOCAL-SMALL.jpg', jpeg(800, 600)) add_raw(small, expected=422) # The extension must match the content: a PNG named .jpg is refused. fake = upload_raw('LOCAL-FAKE.jpg', final_png_bytes('fake')) add_raw(fake, expected=422) # A real 2000x1500 JPEG becomes DTF--revisao-1.jpg. row = review_photo(customer, oid) assert row['seq'] == 1 and row['kind'] == 'photo', row assert row['name'] == 'DTF-%d-revisao-1.jpg' % number, row board = customer.call('/operator/board', operator=True) saved = next(o for o in board['orders'] if o['id'] == oid) assert [(r['seq'], r['kind'], r['name']) for r in saved['reviews']] == [(1, 'photo', row['name'])], saved['reviews'] folder = customer.call('/operator/queue/folder', {'early': True}, operator=True) entry = next(r for r in folder['reviews'] if r['upload_id'] == row['upload_id']) assert entry['name'] == row['name'] and entry['order'] == oid and entry['number'] == number, entry # The customer sees no photo while the order is still in Revisão. assert customer.call('/customer/orders/' + oid)['reviews'] == [] # A second review can be removed only while the order is in Revisão. second = review_photo(customer, oid) assert second['seq'] == 2, second customer.call('/operator/orders/' + oid + '/reviews/' + str(second['id']) + '/remove', {}, operator=True) # With the photo the order finishes, and the customer then sees it. version = move('fin', version) detail = customer.call('/customer/orders/' + oid) assert detail['state'] == 'fin' assert [(r['id'], r['name']) for r in detail['reviews']] == [(row['id'], row['name'])], detail['reviews'] view = customer.call('/customer/orders/' + oid + '/reviews/' + str(row['id']) + '/view') assert view['kind'] == 'photo' and view['name'] == row['name'] with urlopen(view['url'], timeout=30) as response: assert Image.open(BytesIO(response.read())).size == (2000, 1500) # Once Finalizado, a review can no longer be removed. customer.call('/operator/orders/' + oid + '/reviews/' + str(row['id']) + '/remove', {}, operator=True, expected=409) print('PASS: review photo upload, signature and size checks, the 1 m rule, the folder and the customer view') if __name__ == '__main__': run()