Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 14:15 UTC
dependabot/npm_and_yarn/frontend/vitest-5.1.2
add-index-html-10623938925177053894
arena/094f314c-aram
arena/2d4eda07-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
dependabot/github_actions/actions/checkout-7
dependabot/github_actions/actions/setup-node-6
dependabot/github_actions/actions/setup-python-7
dependabot/github_actions/actions/upload-artifact-7
dependabot/npm_and_yarn/frontend/eslint-plugin-react-hooks-7.1.1
dependabot/npm_and_yarn/frontend/globals-17.15.0
dependabot/npm_and_yarn/frontend/vite-8.4.0
dependabot/npm_and_yarn/frontend/vitest-5.1.2
dependabot/npm_and_yarn/frontend/vitest/coverage-v8-5.1.2
dependabot/pip/backend/mypy-gte-1.13-and-lt-3
dependabot/pip/backend/pytest-cov-gte-6.0-and-lt-8
dependabot/pip/backend/setuptools-gte-75-and-lt-85
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | """Phase 7: users/roles, controlled reopen/lock, backup integrity, audit API.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from aram_backend.models import FiscalYearStatus, UserRole |
| 8 | from aram_backend.services.approval import approve_year |
| 9 | from aram_backend.services.audit import verify_chain |
| 10 | from aram_backend.services.backup import BackupError, create_backup, verify_backup |
| 11 | from aram_backend.services.reopen import ( |
| 12 | ReopenError, |
| 13 | approve_reopen, |
| 14 | lock_year, |
| 15 | reopen_year, |
| 16 | request_reopen, |
| 17 | ) |
| 18 | from aram_backend.services.users import ( |
| 19 | AuthorizationError, |
| 20 | UserError, |
| 21 | authenticate, |
| 22 | create_user, |
| 23 | require_role, |
| 24 | set_active, |
| 25 | ) |
| 26 | from conftest import login, make_user |
| 27 | from test_approval_gate import _balanced |
| 28 | |
| 29 | |
| 30 | # --------------------------------------------------------------------- users |
| 31 | class TestUsers: |
| 32 | def test_create_and_authenticate(self, session): |
| 33 | create_user(session, username="alice", password="supersecret", role=UserRole.REVIEWER) |
| 34 | user = authenticate(session, username="alice", password="supersecret") |
| 35 | assert user.role == UserRole.REVIEWER |
| 36 | assert user.password_hash != "supersecret" # never plaintext |
| 37 | assert len(user.salt) == 32 |
| 38 | |
| 39 | def test_wrong_password_rejected(self, session): |
| 40 | create_user(session, username="bob", password="supersecret") |
| 41 | with pytest.raises(UserError): |
| 42 | authenticate(session, username="bob", password="wrong-password") |
| 43 | |
| 44 | def test_duplicate_username_rejected(self, session): |
| 45 | create_user(session, username="carol", password="supersecret") |
| 46 | with pytest.raises(UserError): |
| 47 | create_user(session, username="carol", password="anothersecret") |
| 48 | |
| 49 | def test_short_password_rejected(self, session): |
| 50 | with pytest.raises(UserError): |
| 51 | create_user(session, username="dave", password="short") |
| 52 | |
| 53 | def test_inactive_user_cannot_authenticate(self, session): |
| 54 | create_user(session, username="erin", password="supersecret") |
| 55 | set_active(session, username="erin", active=False) |
| 56 | with pytest.raises(UserError): |
| 57 | authenticate(session, username="erin", password="supersecret") |
| 58 | |
| 59 | def test_require_role_gate(self, session): |
| 60 | create_user(session, username="prep", password="supersecret", role=UserRole.PREPARER) |
| 61 | create_user(session, username="admin", password="supersecret", role=UserRole.ADMIN) |
| 62 | require_role(session, username="admin", allowed={UserRole.ADMIN}) |
| 63 | with pytest.raises(AuthorizationError): |
| 64 | require_role(session, username="prep", allowed={UserRole.ADMIN}) |
| 65 | with pytest.raises(AuthorizationError): |
| 66 | require_role(session, username="ghost", allowed={UserRole.ADMIN}) |
| 67 | |
| 68 | |
| 69 | # --------------------------------------------------------------- reopen/lock |
| 70 | class TestReopenLock: |
| 71 | def _locked(self, session): |
| 72 | year, _ = _balanced(session) |
| 73 | assert approve_year(session, year.id).approved |
| 74 | lock_year(session, year.id, actor="admin") |
| 75 | assert year.status == FiscalYearStatus.LOCKED |
| 76 | return year |
| 77 | |
| 78 | def test_full_reopen_workflow(self, session): |
| 79 | year = self._locked(session) |
| 80 | req = request_reopen(session, year.id, reason="اصلاح طبقهبندی", requested_by="prep") |
| 81 | assert req.status == "pending" |
| 82 | # Reopen before approval is refused. |
| 83 | with pytest.raises(ReopenError): |
| 84 | reopen_year(session, year.id, actor="admin") |
| 85 | approve_reopen(session, req.id, approved_by="admin") |
| 86 | reopen_year(session, year.id, actor="admin") |
| 87 | assert year.status == FiscalYearStatus.APPROVED |
| 88 | assert verify_chain(session) |
| 89 | |
| 90 | def test_request_on_non_locked_fails(self, session): |
| 91 | year, _ = _balanced(session) # Draft |
| 92 | with pytest.raises(ReopenError): |
| 93 | request_reopen(session, year.id, reason="x", requested_by="prep") |
| 94 | |
| 95 | def test_lock_requires_approved(self, session): |
| 96 | year, _ = _balanced(session) # Draft |
| 97 | with pytest.raises(ReopenError): |
| 98 | lock_year(session, year.id, actor="admin") |
| 99 | |
| 100 | def test_reopen_requires_reason(self, session): |
| 101 | year = self._locked(session) |
| 102 | with pytest.raises(ReopenError): |
| 103 | request_reopen(session, year.id, reason=" ", requested_by="prep") |
| 104 | |
| 105 | def test_locked_year_is_immutable(self, session): |
| 106 | year = self._locked(session) |
| 107 | from aram_backend.models import NoteDefinition |
| 108 | |
| 109 | note = NoteDefinition( |
| 110 | year_id=year.id, |
| 111 | note_number="99", |
| 112 | title="x", |
| 113 | configuration_json={"statement": "BS", "section": "Assets"}, |
| 114 | ) |
| 115 | session.add(note) |
| 116 | with pytest.raises(ValueError): |
| 117 | session.flush() |
| 118 | session.rollback() |
| 119 | |
| 120 | |
| 121 | # -------------------------------------------------------------------- backup |
| 122 | class TestBackup: |
| 123 | def test_create_and_verify(self, session, tmp_path): |
| 124 | year, _ = _balanced(session) |
| 125 | approve_year(session, year.id) |
| 126 | session.commit() |
| 127 | store = tmp_path / "store" |
| 128 | store.mkdir() |
| 129 | (store / "ab").mkdir() |
| 130 | (store / "ab" / "cd").write_bytes(b"source-bytes") |
| 131 | dest = tmp_path / "backup" |
| 132 | manifest = create_backup(session, dest, store_root=store) |
| 133 | assert manifest["audit_chain_valid"] is True |
| 134 | assert manifest["source_store_sha256"] |
| 135 | result = verify_backup(dest) |
| 136 | assert result["valid"] is True |
| 137 | assert result["source_files"] == 1 |
| 138 | |
| 139 | def test_tampered_database_detected(self, session, tmp_path): |
| 140 | year, _ = _balanced(session) |
| 141 | approve_year(session, year.id) |
| 142 | session.commit() |
| 143 | store = tmp_path / "store" |
| 144 | store.mkdir() |
| 145 | dest = tmp_path / "backup" |
| 146 | create_backup(session, dest, store_root=store) |
| 147 | # Corrupt the backed-up database bytes. |
| 148 | db = dest / "aram.db" |
| 149 | db.write_bytes(db.read_bytes() + b"tamper") |
| 150 | with pytest.raises(BackupError): |
| 151 | verify_backup(dest) |
| 152 | |
| 153 | def test_non_empty_destination_refused(self, session, tmp_path): |
| 154 | store = tmp_path / "store" |
| 155 | store.mkdir() |
| 156 | dest = tmp_path / "backup" |
| 157 | dest.mkdir() |
| 158 | (dest / "existing").write_text("x") |
| 159 | with pytest.raises(BackupError): |
| 160 | create_backup(session, dest, store_root=store) |
| 161 | |
| 162 | |
| 163 | # ----------------------------------------------------------------- admin API |
| 164 | class TestAdminApi: |
| 165 | """R1 (booklet 2.1): the api_client fixture is already logged in as the |
| 166 | bootstrap admin ``root``; identity never comes from a request body.""" |
| 167 | |
| 168 | def test_bootstrap_happens_once_and_forces_admin(self, anon_client): |
| 169 | r = anon_client.post( |
| 170 | "/api/v1/users", |
| 171 | json={"username": "root", "password": "supersecret", "role": "preparer"}, |
| 172 | ) |
| 173 | assert r.status_code == 200, r.text |
| 174 | assert r.json()["role"] == "admin" # bootstrap forces admin |
| 175 | # A second attempt is no longer a bootstrap: it needs an ADMIN session. |
| 176 | again = anon_client.post( |
| 177 | "/api/v1/users", json={"username": "second", "password": "supersecret"} |
| 178 | ) |
| 179 | assert again.status_code == 401 |
| 180 | |
| 181 | def test_login_issues_a_token_and_role_gate_uses_the_session(self, api_client): |
| 182 | r = api_client.post( |
| 183 | "/api/v1/auth/login", json={"username": "root", "password": "supersecret"} |
| 184 | ) |
| 185 | assert r.status_code == 200 |
| 186 | assert r.json()["role"] == "admin" |
| 187 | assert len(r.json()["token"]) >= 40 # 256-bit random token |
| 188 | # bad login |
| 189 | r = api_client.post( |
| 190 | "/api/v1/auth/login", json={"username": "root", "password": "nope"} |
| 191 | ) |
| 192 | assert r.status_code == 401 |
| 193 | # admin can create a preparer (no actor field anywhere) |
| 194 | r = api_client.post( |
| 195 | "/api/v1/users", |
| 196 | json={"username": "prep", "password": "supersecret", "role": "preparer"}, |
| 197 | ) |
| 198 | assert r.status_code == 200 |
| 199 | # the preparer's own session cannot create users, even claiming actor=root |
| 200 | prep_token = login(api_client, "prep", "supersecret") |
| 201 | r = api_client.post( |
| 202 | "/api/v1/users", |
| 203 | json={"username": "x", "password": "supersecret", "actor": "root"}, |
| 204 | headers={"Authorization": f"Bearer {prep_token}"}, |
| 205 | ) |
| 206 | assert r.status_code == 403 |
| 207 | |
| 208 | def test_approve_role_gated_by_session(self, api_client): |
| 209 | make_user(api_client, "prep", "preparer") |
| 210 | year_id = api_client.post( |
| 211 | "/api/v1/companies", json={"code": "RG", "name": "شرکت", "year": 1404} |
| 212 | ).json()["year_id"] |
| 213 | # Preparer is refused by the role gate (403) before controls even run. |
| 214 | prep_token = login(api_client, "prep", "supersecret") |
| 215 | r = api_client.post( |
| 216 | f"/api/v1/years/{year_id}/approve", |
| 217 | json={"actor": "root"}, |
| 218 | headers={"Authorization": f"Bearer {prep_token}"}, |
| 219 | ) |
| 220 | assert r.status_code == 403, r.text |
| 221 | # Admin passes the role gate (controls may still block, but never 403). |
| 222 | login(api_client, "root", "supersecret") |
| 223 | r = api_client.post(f"/api/v1/years/{year_id}/approve") |
| 224 | assert r.status_code == 200, r.text |
| 225 | |
| 226 | def test_approve_is_impossible_without_users(self, anon_client): |
| 227 | """The old "no users -> approval allowed" shortcut is gone (R1).""" |
| 228 | r = anon_client.post("/api/v1/years/1/approve", json={"actor": "anyone"}) |
| 229 | assert r.status_code == 401 |
| 230 | |
| 231 | def test_audit_endpoint_chain_valid(self, api_client): |
| 232 | r = api_client.get("/api/v1/audit/verify") |
| 233 | assert r.status_code == 200 |
| 234 | assert r.json()["chain_valid"] is True |
| 235 | r = api_client.get("/api/v1/audit") |
| 236 | actions = {e["action"] for e in r.json()["entries"]} |
| 237 | assert "user.create" in actions |
| 238 | assert "auth.login" in actions |