feat: Minhas folhas on the server and in Minha conta: saved sheets per account with their uploads, 30 days, 50 per account; order again, edit, delete (Rodada 5, B9)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
113
tests/saved_sheets_test.py
Normal file
113
tests/saved_sheets_test.py
Normal file
@@ -0,0 +1,113 @@
|
||||
"""Minhas folhas: an account keeps a built sheet and asks for it again.
|
||||
|
||||
Run against the local stack: python3 -m tests.saved_sheets_test
|
||||
SITE_BASE_URL=http://site SITE_HOST_HEADER=localhost python -m tests.saved_sheets_test
|
||||
"""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import secrets
|
||||
|
||||
from app.core import db
|
||||
from tests.smoke_test import Client, register, upload_bytes
|
||||
|
||||
PASSWORD = 'senha-folhas-12345'
|
||||
CNPJ = '11222333000181'
|
||||
CONTENT = b'LOCAL SAVED SHEET ARTWORK'
|
||||
|
||||
|
||||
def saved_file(uid, name='LOCAL-TEST.cdr'):
|
||||
return {'upload_id': uid, 'name': name, 'size': len(CONTENT), 'lastModified': 0,
|
||||
'type': 'application/octet-stream'}
|
||||
|
||||
|
||||
def body(uid, name='Minha folha', mode='file', metres='1.2', picture=None):
|
||||
item = {'mode': mode, 'metres': metres, 'savedFiles': [saved_file(uid)]}
|
||||
data = {'name': name, 'mode': mode, 'metres': metres, 'item': item}
|
||||
if picture is not None:
|
||||
data['picture'] = picture
|
||||
return data
|
||||
|
||||
|
||||
def run():
|
||||
tag = secrets.token_hex(3)
|
||||
emails = [f'folhas-{tag}@example.test', f'outra-{tag}@example.test']
|
||||
client, other = Client(), Client()
|
||||
try:
|
||||
# A visitor has no account to keep sheets in.
|
||||
client.call('/session')
|
||||
client.call('/account/sheets', expected=401)
|
||||
|
||||
register(client, {'cnpj': CNPJ, 'zap': '16999990000', 'mail': emails[0]}, PASSWORD)
|
||||
register(other, {'cnpj': CNPJ, 'zap': '16999990000', 'mail': emails[1]}, PASSWORD)
|
||||
|
||||
uid = upload_bytes(client, CONTENT)
|
||||
picture = 'data:image/png;base64,AAAA'
|
||||
first = client.call('/account/sheets', body(uid, picture=picture))
|
||||
assert first['id']
|
||||
|
||||
sheets = client.call('/account/sheets')['sheets']
|
||||
assert [s['id'] for s in sheets] == [first['id']], sheets
|
||||
saved = sheets[0]
|
||||
assert saved['available'] is True and saved['picture'] == picture
|
||||
assert saved['total_size'] == len(CONTENT) and saved['item']['savedFiles'][0]['upload_id'] == uid
|
||||
until = datetime.fromisoformat(saved['files_until'])
|
||||
assert abs((until - (datetime.now(timezone.utc) + timedelta(days=30))).total_seconds()) < 300, saved
|
||||
|
||||
# Another account's upload cannot be saved.
|
||||
stranger_uid = upload_bytes(other, CONTENT)
|
||||
refused = client.call('/account/sheets', body(stranger_uid), expected=409)
|
||||
assert 'não está mais disponível' in refused['detail'], refused
|
||||
|
||||
# A sheet of another account is not found.
|
||||
second = other.call('/account/sheets', body(stranger_uid, name='Outra folha'))
|
||||
assert second['id']
|
||||
client.call('/account/sheets/' + second['id'], expected=404, method='DELETE')
|
||||
client.call('/account/sheets/' + second['id'] + '/use', {}, expected=404)
|
||||
client.call('/account/sheets/' + second['id'] + '/files', {}, expected=404)
|
||||
|
||||
# An upload older than 30 days is no longer available.
|
||||
old_uid = upload_bytes(client, CONTENT)
|
||||
old = client.call('/account/sheets', body(old_uid, name='Vencida'))
|
||||
with db.connect() as c:
|
||||
c.execute("UPDATE dtf_local.uploads SET created_at=now()-interval '31 days' WHERE id=%s", (old_uid,))
|
||||
expired = next(s for s in client.call('/account/sheets')['sheets'] if s['id'] == old['id'])
|
||||
assert expired['available'] is False, expired
|
||||
refused = client.call('/account/sheets/' + old['id'] + '/use', {}, expected=409)
|
||||
assert 'venceram' in refused['detail'], refused
|
||||
|
||||
# At most 50 sheets per account.
|
||||
created = 2
|
||||
while created < 50:
|
||||
client.call('/account/sheets', body(uid, name=f'Folha {created + 1}'))
|
||||
created += 1
|
||||
full = client.call('/account/sheets', body(uid, name='A 51a'), expected=409)
|
||||
assert '50 folhas' in full['detail'], full
|
||||
|
||||
# Asking for it again records when and hands back the item.
|
||||
used = client.call('/account/sheets/' + first['id'] + '/use', {})
|
||||
assert used['id'] == first['id'] and used['used_at'] and used['item']['savedFiles'][0]['upload_id'] == uid
|
||||
assert next(s for s in client.call('/account/sheets')['sheets'] if s['id'] == first['id'])['used_at']
|
||||
|
||||
# Editing gets a link to each file, in the item's order.
|
||||
files = client.call('/account/sheets/' + first['id'] + '/files', {})['files']
|
||||
assert len(files) == 1 and files[0]['upload_id'] == uid
|
||||
assert files[0]['name'] == 'LOCAL-TEST.cdr' and files[0]['size'] == len(CONTENT)
|
||||
assert files[0]['url'].startswith('http'), files
|
||||
|
||||
# Deleted, it leaves the list.
|
||||
assert client.call('/account/sheets/' + first['id'], method='DELETE') == {'ok': True}
|
||||
assert first['id'] not in [s['id'] for s in client.call('/account/sheets')['sheets']]
|
||||
client.call('/account/sheets/' + first['id'], method='DELETE', expected=404)
|
||||
|
||||
print('PASS: saved sheets belong to the account, last 30 days, cap at 50, and come back for '
|
||||
'Pedir de novo or Editar')
|
||||
finally:
|
||||
with db.connect() as c:
|
||||
c.execute('DELETE FROM dtf_local.uploads WHERE owner IN '
|
||||
'(SELECT id FROM dtf_local.accounts WHERE email=ANY(%s))', (emails,))
|
||||
c.execute('DELETE FROM dtf_local.accounts WHERE email=ANY(%s)', (emails,))
|
||||
c.execute('DELETE FROM dtf_local.account_signups WHERE email=ANY(%s)', (emails,))
|
||||
c.execute('DELETE FROM dtf_local.mail_queue WHERE to_addr=ANY(%s)', (emails,))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
Reference in New Issue
Block a user