5.12 (livrat): editare in modal a randurilor de preview, cont obligatoriu inainte de import, formular editare extras (_form_editare, _editare_preview_modal), plus suita de teste aferenta (preview edit/compact, mapare op, form editare, signup, admin panel). Design + planificare: - docs/design.md: sistem de design (tokeni, breakpoints, scara control, componente, a11y). - docs/prd/prd-5.12-* si prd-5.13-* (5.13 cu raport /autoplan: CEO+Design+Eng, audit trail). Curatare: sterse PNG-urile de test/mockup temporare din radacina. Nota: implementarea CSS 5.13 (responsive compact + sistem butoane) NU e inca facuta — planul revizuit cere refactorul testelor fragile din test_web_responsive.py INAINTE de CSS. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
173 lines
6.5 KiB
Python
173 lines
6.5 KiB
Python
"""Teste US-001 (PRD 3.1): coloana accounts.active + helper-e cont in app/accounts.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture()
|
|
def conn(monkeypatch):
|
|
tmp = tempfile.mkdtemp()
|
|
monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "test_accounts.db"))
|
|
from app.config import get_settings
|
|
get_settings.cache_clear()
|
|
from app.db import get_connection, init_db
|
|
init_db()
|
|
c = get_connection()
|
|
yield c
|
|
c.close()
|
|
get_settings.cache_clear()
|
|
|
|
|
|
def test_create_account_returneaza_id(conn):
|
|
from app.accounts import create_account
|
|
acct_id = create_account(conn, "Service X")
|
|
assert isinstance(acct_id, int)
|
|
# AUTOINCREMENT peste default id=1 -> primul cont creat are id>=2 (nu atinge default).
|
|
assert acct_id >= 2
|
|
|
|
|
|
def test_create_account_activ_implicit(conn):
|
|
from app.accounts import create_account
|
|
acct_id = create_account(conn, "Service X")
|
|
row = conn.execute("SELECT active FROM accounts WHERE id=?", (acct_id,)).fetchone()
|
|
assert row["active"] == 1
|
|
|
|
|
|
def test_create_account_inactiv(conn):
|
|
from app.accounts import create_account
|
|
acct_id = create_account(conn, "Service X", active=False)
|
|
row = conn.execute("SELECT active FROM accounts WHERE id=?", (acct_id,)).fetchone()
|
|
assert row["active"] == 0
|
|
|
|
|
|
def test_create_account_name_gol_ridica_eroare(conn):
|
|
from app.accounts import create_account
|
|
with pytest.raises(ValueError):
|
|
create_account(conn, " ")
|
|
# nu a inserat nimic peste default
|
|
n = conn.execute("SELECT COUNT(*) AS n FROM accounts").fetchone()["n"]
|
|
assert n == 1
|
|
|
|
|
|
def test_create_account_cui_duplicat_respins(conn):
|
|
from app.accounts import create_account
|
|
first = create_account(conn, "Service A", cui="RO123")
|
|
with pytest.raises(ValueError) as exc:
|
|
create_account(conn, "Service B", cui="RO123")
|
|
# mesaj cu cauza + fix care numeste contul existent (A4)
|
|
msg = str(exc.value)
|
|
assert "RO123" in msg
|
|
assert str(first) in msg
|
|
|
|
|
|
def test_create_cui_null_multiplu_permis(conn):
|
|
from app.accounts import create_account
|
|
a = create_account(conn, "Fara CUI 1")
|
|
b = create_account(conn, "Fara CUI 2")
|
|
assert a != b
|
|
|
|
|
|
def test_create_cui_normalizat(conn):
|
|
from app.accounts import create_account
|
|
create_account(conn, "Service A", cui=" ro123 ")
|
|
# normalizat la RO123 -> duplicat respins indiferent de spatii/caz
|
|
with pytest.raises(ValueError):
|
|
create_account(conn, "Service B", cui="RO123")
|
|
row = conn.execute("SELECT cui FROM accounts WHERE name='Service A'").fetchone()
|
|
assert row["cui"] == "RO123"
|
|
|
|
|
|
def test_set_active_comuta(conn):
|
|
from app.accounts import create_account, set_active
|
|
acct_id = create_account(conn, "Service X")
|
|
set_active(conn, acct_id, False)
|
|
assert conn.execute("SELECT active FROM accounts WHERE id=?", (acct_id,)).fetchone()["active"] == 0
|
|
set_active(conn, acct_id, True)
|
|
assert conn.execute("SELECT active FROM accounts WHERE id=?", (acct_id,)).fetchone()["active"] == 1
|
|
|
|
|
|
def test_set_active_idempotent(conn):
|
|
from app.accounts import create_account, set_active
|
|
acct_id = create_account(conn, "Service X") # deja activ
|
|
set_active(conn, acct_id, True) # nu trebuie sa arunce
|
|
assert conn.execute("SELECT active FROM accounts WHERE id=?", (acct_id,)).fetchone()["active"] == 1
|
|
|
|
|
|
def test_set_active_inexistent_ridica(conn):
|
|
from app.accounts import set_active
|
|
with pytest.raises(ValueError):
|
|
set_active(conn, 9999, True)
|
|
|
|
|
|
def test_list_accounts_ordonat_fara_creds(conn):
|
|
from app.accounts import create_account, list_accounts
|
|
create_account(conn, "Service B")
|
|
create_account(conn, "Service A")
|
|
rows = list_accounts(conn)
|
|
ids = [r["id"] for r in rows]
|
|
assert ids == sorted(ids)
|
|
for r in rows:
|
|
assert "rar_creds_enc" not in r
|
|
assert set(r.keys()) == {"id", "name", "cui", "email", "active", "status", "created_at"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# US-001 (PRD 5.12): accounts.email + validari companie/email/CUI
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_create_account_fara_email_ridica(conn):
|
|
"""create_account cu email="" ridica ValueError (email gol nu e acceptat)."""
|
|
from app.accounts import create_account
|
|
with pytest.raises(ValueError, match="email"):
|
|
create_account(conn, "Service X", cui="RO100", email="")
|
|
|
|
|
|
def test_create_account_fara_cui_ridica(conn):
|
|
"""create_account cu cui="" ridica ValueError (CUI gol nu e acceptat)."""
|
|
from app.accounts import create_account
|
|
with pytest.raises(ValueError, match="[Cc][Uu][Ii]|cod unic"):
|
|
create_account(conn, "Service X", cui="", email="test@test.com")
|
|
|
|
|
|
def test_email_normalizat_lowercase_trim(conn):
|
|
"""email e normalizat: trim + lower."""
|
|
from app.accounts import create_account
|
|
acct_id = create_account(conn, "Service X", cui="RO200", email=" Test@EXAMPLE.Com ")
|
|
row = conn.execute("SELECT email FROM accounts WHERE id=?", (acct_id,)).fetchone()
|
|
assert row["email"] == "test@example.com"
|
|
|
|
|
|
def test_migrare_adauga_coloana_email_idempotent(conn):
|
|
"""_migrate e idempotent: ruleaza de doua ori fara eroare si coloana email exista."""
|
|
from app.db import _migrate
|
|
_migrate(conn) # a doua rulare (prima e in init_db)
|
|
cols = {r["name"] for r in conn.execute("PRAGMA table_info(accounts)").fetchall()}
|
|
assert "email" in cols
|
|
|
|
|
|
def test_account_is_complete_false_pe_legacy_incomplet(conn):
|
|
"""account_is_complete() returneaza False pe cont fara email sau fara CUI."""
|
|
from app.accounts import create_account, account_is_complete
|
|
# cont fara email si fara CUI
|
|
acct_id = create_account(conn, "Service Legacy")
|
|
row = conn.execute("SELECT * FROM accounts WHERE id=?", (acct_id,)).fetchone()
|
|
assert account_is_complete(row) is False
|
|
|
|
# cont fara email, cu CUI
|
|
acct_id2 = create_account(conn, "Service Cu CUI", cui="RO300")
|
|
row2 = conn.execute("SELECT * FROM accounts WHERE id=?", (acct_id2,)).fetchone()
|
|
assert account_is_complete(row2) is False
|
|
|
|
# cont complet (cu email si CUI si name)
|
|
acct_id3 = create_account(conn, "Service Complet", cui="RO301", email="x@y.com")
|
|
row3 = conn.execute("SELECT * FROM accounts WHERE id=?", (acct_id3,)).fetchone()
|
|
assert account_is_complete(row3) is True
|
|
|
|
# contul sistem id=1 e EXCEPTAT (returneaza True indiferent)
|
|
row_sys = conn.execute("SELECT * FROM accounts WHERE id=1").fetchone()
|
|
assert account_is_complete(row_sys) is True
|