"""The montador's stamp sizes: Expedição reads them and Gestão edits them. Run against the local stack: python -m tests.stamps_test """ import secrets from app.core import db from tests.smoke_test import Client PASSWORD = 'senha-de-teste-123' def signed_in(email, password=PASSWORD): client = Client() client.call('/session') client.call('/operator/login', {'email': email, 'password': password}) return client def run(): tag = secrets.token_hex(3) su = Client() su.call('/session') assert su.call('/operator/me', operator=True)['role'] == 'superadmin' with db.connect() as c: original = c.execute('SELECT letter,width_cm FROM dtf_local.stamp_sizes ORDER BY width_cm, letter').fetchall() emails = {role: f'{role}-{tag}@example.test' for role in ('expedicao', 'designer', 'gestao')} table = [(row['letter'], float(row['width_cm'])) for row in original] try: for role, email in emails.items(): su.call('/operator/users', {'name': role.title(), 'email': email, 'role': role, 'password': PASSWORD}, operator=True) # The new profile creates through the users API and reads the table. shipping = signed_in(emails['expedicao']) me = shipping.call('/operator/me') assert me['role'] == 'expedicao' and me['can'] == ['assemble', 'view'], me read = shipping.call('/operator/stamp-sizes')['sizes'] assert [(row['letter'], float(row['width_cm'])) for row in read] == table, read # The designer does not assemble: no reading the table. designer = signed_in(emails['designer']) designer.call('/operator/stamp-sizes', expected=403) # Gestão replaces the whole table in one go and reads it back, by width. manager = signed_in(emails['gestao']) manager.call('/operator/stamp-sizes', {'sizes': [{'letter': 'D', 'width_cm': 30}, {'letter': 'A', 'width_cm': 20}, {'letter': 'B', 'width_cm': 24.5}]}) read = manager.call('/operator/stamp-sizes')['sizes'] assert [(row['letter'], float(row['width_cm'])) for row in read] == [('A', 20), ('B', 24.5), ('D', 30)], read # A repeated letter and a width out of range are refused. manager.call('/operator/stamp-sizes', {'sizes': [{'letter': 'A', 'width_cm': 20}, {'letter': 'A', 'width_cm': 24}]}, expected=422) manager.call('/operator/stamp-sizes', {'sizes': [{'letter': 'A', 'width_cm': 0}]}, expected=422) manager.call('/operator/stamp-sizes', {'sizes': [{'letter': 'A', 'width_cm': 57}]}, expected=422) # Expedição only reads them; saving stays with Gestão. shipping.call('/operator/stamp-sizes', {'sizes': [{'letter': 'A', 'width_cm': 20}]}, expected=403) read = manager.call('/operator/stamp-sizes')['sizes'] assert [(row['letter'], float(row['width_cm'])) for row in read] == [('A', 20), ('B', 24.5), ('D', 30)], read print('PASS: Expedição reads the montador size table; Gestão replaces it; invalid rows are refused') finally: with db.connect() as c: c.execute('DELETE FROM dtf_local.stamp_sizes') c.cursor().executemany('INSERT INTO dtf_local.stamp_sizes(letter,width_cm) VALUES(%s,%s)', table) c.execute('DELETE FROM dtf_local.operators WHERE email LIKE %s', (f'%-{tag}@example.test',)) if __name__ == '__main__': run()