Files
dtf-system/tests/test_pricing.py
Cauê Faleiros 3664fe827e feat: password recovery by e-mail, team alerts, and a price for files opened by hand
Pilot customers had no way to reset a forgotten password, and the 7 October
meeting asked for two things that only needed the client's inputs to go live.

Password recovery: "Esqueci minha senha" in Minha conta sends a link that
works once, for an hour (/conta/redefinir#token, in the fragment so it never
reaches a server or a Referer). Only the token's hash is stored, the e-mail's
text is erased once sent, the answer never says whether an account exists,
and a reset signs out every other session. Limited per address and source.

Team alerts: Kanban users who tick "Receber alertas por e-mail" in Usuários
get one when an order is paid, a cart waits for review, or a payment needs
attention. Messages are queued in the transaction that caused them and sent
by the worker with retries (app/mail.py). Integrações shows an E-mail row
with "Enviar teste".

E-mail is off in production (MAIL_ADAPTER=off; recovery keeps offering
WhatsApp) until the client's SMTP account exists; then it is only settings
(docs/PORTAINER.md). The local stack keeps messages in the database and log.

Files opened by hand: a finished sheet in CDR, AI, PSD or TIFF (or with a
length the customer typed) pays the full price plus MANUAL_FILE_EXTRA_CENTS
per metre, to steer customers to PNG as the meeting asked. It is 0 until
Marcus gives the amount. The Site shows it before checkout, the server
charges it, Tiny gets it in the unit price, and the Kanban says it.

Tested: new tests/mail_test (reset flow, SMTP delivery against a local SMTP
server, retry, who gets alerts), pricing unit tests, and the browser test now
checks the Site total equals the server's, run with the charge at 0 and at
R$ 5,00. The full CI suite passes in a CI-style stack.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 11:48:24 -03:00

63 lines
3.5 KiB
Python

"""Run from repository root: python3 -m unittest tests.test_pricing -v."""
import json
from pathlib import Path
import subprocess
import unittest
from app.core.pricing import is_manual, price, TIERS
class PricingTests(unittest.TestCase):
def test_every_grade_against_actual_site_javascript(self):
# The ladders live with the rest of the Site's behaviour; this test exists
# to prove the server agrees with whatever the customer is shown.
source = Path('web/site-config.js').read_text()
tiers = source.split('const FAIXAS=')[1].split('\n};',1)[0]+'\n}'
calculator = source.split('const cobrar=')[1].split(';',1)[0]
js = f'const FAIXAS={tiers};const MINIMO_M=1;const cobrar={calculator};'
js += '''const results=[];for(const mode of Object.keys(FAIXAS))for(let grade=0;grade<=100;grade++)
for(const metres of [0.01,0.99,1,1.01,1.1,2.75,2.8,2.8000000000000003,19.99,60,12000]){
const unit=FAIXAS[mode].find(x=>grade>=x[0])[1];
results.push([mode,String(metres),grade,cobrar(metres),Math.round(unit*100),Math.round(cobrar(metres)*unit*100)]);
}process.stdout.write(JSON.stringify(results));'''
cases = json.loads(subprocess.check_output(['node','-e',js],text=True))
for mode,metres,grade,billed,unit,total in cases:
with self.subTest(mode=mode,metres=metres,grade=grade):
result=price(mode,metres,grade)
self.assertEqual(float(result['billed_metres']),billed)
self.assertEqual(result['unit_cents'],unit)
self.assertEqual(result['total_cents'],total)
def test_assembly_is_included_at_every_tier(self):
for grade in range(101):
self.assertEqual(price('avulsa','1',grade)['total_cents']-price('file','1',grade)['total_cents'],1000)
self.assertEqual(price('uv','1',grade)['total_cents']-price('uvfile','1',grade)['total_cents'],1400)
def test_manual_file_extra_is_per_metre_and_not_a_discount(self):
plain, extra = price('file', '1.01', 0), price('file', '1.01', 0, 500)
self.assertEqual(extra['unit_cents'], plain['unit_cents'] + 500)
self.assertEqual(extra['total_cents'], plain['total_cents'] + 550)
self.assertEqual(extra['discount_cents'], 0)
self.assertEqual(extra['manual_extra_cents'], 500)
self.assertNotIn('manual_extra_cents', plain)
for bad in (-1, True, 1.5):
with self.assertRaises(ValueError):price('file', '1', 0, bad)
def test_which_items_are_opened_by_hand(self):
def item(kind, measurement, uid='u1'):
return {'production': {'sources': [{'kind': kind, 'measurement': measurement, 'upload_id': uid}]}}
self.assertTrue(is_manual(item('sheet', 'customer'), {'u1': 'folha.png'}))
self.assertTrue(is_manual(item('sheet', 'file'), {'u1': 'FOLHA.CDR'}))
self.assertTrue(is_manual(item('sheet', 'file'), {'u1': 'folha.tif'}))
self.assertFalse(is_manual(item('sheet', 'file'), {'u1': 'folha.pdf'}))
# Loose artwork is assembled by the Site; it never pays this.
self.assertFalse(is_manual(item('artwork', 'customer'), {'u1': 'arte.cdr'}))
def test_invalid_inputs(self):
for value in ('0','-1','NaN','Infinity','12001'):
with self.assertRaises(ValueError):price('file',value,90)
for grade in (-1,101,True,89.5):
with self.assertRaises(ValueError):price('file','1',grade)
with self.assertRaises(ValueError):price('other','1',90)
if __name__ == '__main__':
unittest.main()