"""The art grade rules: the designer warning and the required treatment (8.5). Run against the local stack: SITE_BASE_URL=http://site SITE_HOST_HEADER=localhost python -m tests.grade_rules_test """ import secrets from uuid import uuid4 from app.core import db from tests.print_file_test import artwork from tests.smoke_test import Client, item_spec, upload_bytes def quote_body(item, treatment, mail): body = {'request_key': str(uuid4()), 'customer': {'cnpj': '11222333000181', 'zap': '11999999999', 'mail': mail}, 'items': [item], 'freight': {'service': 'pickup'}} if treatment: body['treatment'] = True return body def run(): tag = secrets.token_hex(4) mail = f'grade-{tag}@example.test' client = Client() client.call('/session') manager = Client() manager.call('/session') manager.call('/operator/me', operator=True) manager = manager.operator_client price_id = quote_id = upload_id = None rules_set = False treatment_enabled = True try: products = {row['id']: row for row in manager.call('/operator/site/products')['products']} treatment_enabled = products['tratamento']['enabled'] manager.call('/operator/site/products/tratamento', {'enabled': True}) price_id = manager.call('/operator/site/products/tratamento/prices', {'unit_cents': 1000})['id'] manager.call('/operator/site/grade-rules', {'aviso': 100, 'tratamento': 100}) rules_set = True rules = manager.call('/operator/site/grade-rules') assert rules['aviso'] == 100 and rules['tratamento'] == 100, rules assert rules['treatment_offered'] is True, rules upload_id = upload_bytes(client, artwork(), name=f'LOCAL-GRADE-{tag}.png') item = {**item_spec('uv', '1', 50, upload_id, aspect=2), 'quality_status': 'warning', 'quality_acknowledged': True} refused = client.call('/quotes', quote_body(item, False, mail), expected=422) assert refused['detail'] == ('Sua arte tem nota 50: para imprimir com qualidade, ' 'o Ajuste Fino é obrigatório neste pedido.'), refused made = client.call('/quotes', quote_body(item, True, mail)) quote_id = made['id'] assert made['status'] == 'approved', made approved = client.call('/quotes/' + quote_id)['approved'] assert approved['grade_flags'] == {'aviso': 50, 'tratamento': 50}, approved assert approved['treatment'] == {'cents': 1000, 'hours': 24, 'arts': 1, 'unit_cents': 1000}, approved assert approved['total_cents'] == 9990, approved print('PASS: the grade rule requires Ajuste Fino and flags the order with the worst grade') finally: if rules_set: try: manager.call('/operator/site/grade-rules', {'aviso': None, 'tratamento': None}) except Exception: pass with db.connect() as c: if quote_id: c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote_id,)) if price_id: c.execute('DELETE FROM dtf_local.product_prices WHERE id=%s', (price_id,)) if upload_id: c.execute('DELETE FROM dtf_local.uploads WHERE id=%s', (upload_id,)) try: manager.call('/operator/site/products/tratamento', {'enabled': treatment_enabled}) except Exception: pass if __name__ == '__main__': run()