arena/094f314c-aram
add-index-html-10623938925177053894
arena/094f314c-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
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 test_approval_gate import _balanced |
| 27 | |
| 28 | |
| 29 | # --------------------------------------------------------------------- users |
| 30 | class TestUsers: |
| 31 | def test_create_and_authenticate(self, session): |
| 32 | create_user(session, username="alice", password="supersecret", role=UserRole.REVIEWER) |
| 33 | user = authenticate(session, username="alice", password="supersecret") |
| 34 | assert user.role == UserRole.REVIEWER |
| 35 | assert user.password_hash != "supersecret" # never plaintext |
| 36 | assert len(user.salt) == 32 |
| 37 | |
| 38 | def test_wrong_password_rejected(self, session): |
| 39 | create_user(session, username="bob", password="supersecret") |
| 40 | with pytest.raises(UserError): |
| 41 | authenticate(session, username="bob", password="wrong-password") |
| 42 | |
| 43 | def test_duplicate_username_rejected(self, session): |
| 44 | create_user(session, username="carol", password="supersecret") |
| 45 | with pytest.raises(UserError): |
| 46 | create_user(session, username="carol", password="anothersecret") |
| 47 | |
| 48 | def test_short_password_rejected(self, session): |
| 49 | with pytest.raises(UserError): |
| 50 | create_user(session, username="dave", password="short") |
| 51 | |
| 52 | def test_inactive_user_cannot_authenticate(self, session): |
| 53 | create_user(session, username="erin", password="supersecret") |
| 54 | set_active(session, username="erin", active=False) |
| 55 | with pytest.raises(UserError): |
| 56 | authenticate(session, username="erin", password="supersecret") |
| 57 | |
| 58 | def test_require_role_gate(self, session): |
| 59 | create_user(session, username="prep", password="supersecret", role=UserRole.PREPARER) |
| 60 | create_user(session, username="admin", password="supersecret", role=UserRole.ADMIN) |
| 61 | require_role(session, username="admin", allowed={UserRole.ADMIN}) |
| 62 | with pytest.raises(AuthorizationError): |
| 63 | require_role(session, username="prep", allowed={UserRole.ADMIN}) |
| 64 | with pytest.raises(AuthorizationError): |
| 65 | require_role(session, username="ghost", allowed={UserRole.ADMIN}) |
| 66 | |
| 67 | |
| 68 | # --------------------------------------------------------------- reopen/lock |
| 69 | class TestReopenLock: |
| 70 | def _locked(self, session): |
| 71 | year, _ = _balanced(session) |
| 72 | assert approve_year(session, year.id).approved |
| 73 | lock_year(session, year.id, actor="admin") |
| 74 | assert year.status == FiscalYearStatus.LOCKED |
| 75 | return year |
| 76 | |
| 77 | def test_full_reopen_workflow(self, session): |
| 78 | year = self._locked(session) |
| 79 | req = request_reopen(session, year.id, reason="اصلاح طبقهبندی", requested_by="prep") |
| 80 | assert req.status == "pending" |
| 81 | # Reopen before approval is refused. |
| 82 | with pytest.raises(ReopenError): |
| 83 | reopen_year(session, year.id, actor="admin") |
| 84 | approve_reopen(session, req.id, approved_by="admin") |
| 85 | reopen_year(session, year.id, actor="admin") |
| 86 | assert year.status == FiscalYearStatus.APPROVED |
| 87 | assert verify_chain(session) |
| 88 | |
| 89 | def test_request_on_non_locked_fails(self, session): |
| 90 | year, _ = _balanced(session) # Draft |
| 91 | with pytest.raises(ReopenError): |
| 92 | request_reopen(session, year.id, reason="x", requested_by="prep") |
| 93 | |
| 94 | def test_lock_requires_approved(self, session): |
| 95 | year, _ = _balanced(session) # Draft |
| 96 | with pytest.raises(ReopenError): |
| 97 | lock_year(session, year.id, actor="admin") |
| 98 | |
| 99 | def test_reopen_requires_reason(self, session): |
| 100 | year = self._locked(session) |
| 101 | with pytest.raises(ReopenError): |
| 102 | request_reopen(session, year.id, reason=" ", requested_by="prep") |
| 103 | |
| 104 | def test_locked_year_is_immutable(self, session): |
| 105 | year = self._locked(session) |
| 106 | from aram_backend.models import NoteDefinition |
| 107 | |
| 108 | note = NoteDefinition( |
| 109 | year_id=year.id, |
| 110 | note_number="99", |
| 111 | title="x", |
| 112 | configuration_json={"statement": "BS", "section": "Assets"}, |
| 113 | ) |
| 114 | session.add(note) |
| 115 | with pytest.raises(ValueError): |
| 116 | session.flush() |
| 117 | session.rollback() |
| 118 | |
| 119 | |
| 120 | # -------------------------------------------------------------------- backup |
| 121 | class TestBackup: |
| 122 | def test_create_and_verify(self, session, tmp_path): |
| 123 | year, _ = _balanced(session) |
| 124 | approve_year(session, year.id) |
| 125 | session.commit() |
| 126 | store = tmp_path / "store" |
| 127 | store.mkdir() |
| 128 | (store / "ab").mkdir() |
| 129 | (store / "ab" / "cd").write_bytes(b"source-bytes") |
| 130 | dest = tmp_path / "backup" |
| 131 | manifest = create_backup(session, dest, store_root=store) |
| 132 | assert manifest["audit_chain_valid"] is True |
| 133 | assert manifest["source_store_sha256"] |
| 134 | result = verify_backup(dest) |
| 135 | assert result["valid"] is True |
| 136 | assert result["source_files"] == 1 |
| 137 | |
| 138 | def test_tampered_database_detected(self, session, tmp_path): |
| 139 | year, _ = _balanced(session) |
| 140 | approve_year(session, year.id) |
| 141 | session.commit() |
| 142 | store = tmp_path / "store" |
| 143 | store.mkdir() |
| 144 | dest = tmp_path / "backup" |
| 145 | create_backup(session, dest, store_root=store) |
| 146 | # Corrupt the backed-up database bytes. |
| 147 | db = dest / "aram.db" |
| 148 | db.write_bytes(db.read_bytes() + b"tamper") |
| 149 | with pytest.raises(BackupError): |
| 150 | verify_backup(dest) |
| 151 | |
| 152 | def test_non_empty_destination_refused(self, session, tmp_path): |
| 153 | store = tmp_path / "store" |
| 154 | store.mkdir() |
| 155 | dest = tmp_path / "backup" |
| 156 | dest.mkdir() |
| 157 | (dest / "existing").write_text("x") |
| 158 | with pytest.raises(BackupError): |
| 159 | create_backup(session, dest, store_root=store) |
| 160 | |
| 161 | |
| 162 | # ----------------------------------------------------------------- admin API |
| 163 | class TestAdminApi: |
| 164 | def test_bootstrap_first_user_is_admin(self, api_client): |
| 165 | r = api_client.post( |
| 166 | "/api/v1/users", |
| 167 | json={"username": "root", "password": "supersecret", "role": "preparer"}, |
| 168 | ) |
| 169 | assert r.status_code == 200, r.text |
| 170 | assert r.json()["role"] == "admin" # bootstrap forces admin |
| 171 | |
| 172 | def test_login_and_role_gate(self, api_client): |
| 173 | api_client.post("/api/v1/users", json={"username": "root", "password": "supersecret"}) |
| 174 | # login |
| 175 | r = api_client.post( |
| 176 | "/api/v1/auth/login", json={"username": "root", "password": "supersecret"} |
| 177 | ) |
| 178 | assert r.status_code == 200 |
| 179 | assert r.json()["role"] == "admin" |
| 180 | # bad login |
| 181 | r = api_client.post("/api/v1/auth/login", json={"username": "root", "password": "nope"}) |
| 182 | assert r.status_code == 401 |
| 183 | # admin can create a preparer |
| 184 | r = api_client.post( |
| 185 | "/api/v1/users", |
| 186 | json={ |
| 187 | "username": "prep", |
| 188 | "password": "supersecret", |
| 189 | "role": "preparer", |
| 190 | "actor": "root", |
| 191 | }, |
| 192 | ) |
| 193 | assert r.status_code == 200 |
| 194 | # preparer cannot create users |
| 195 | r = api_client.post( |
| 196 | "/api/v1/users", |
| 197 | json={"username": "x", "password": "supersecret", "actor": "prep"}, |
| 198 | ) |
| 199 | assert r.status_code == 403 |
| 200 | |
| 201 | def test_approve_role_gated_once_users_exist(self, api_client): |
| 202 | # Bootstrap admin + a preparer. |
| 203 | api_client.post("/api/v1/users", json={"username": "root", "password": "supersecret"}) |
| 204 | api_client.post( |
| 205 | "/api/v1/users", |
| 206 | json={ |
| 207 | "username": "prep", |
| 208 | "password": "supersecret", |
| 209 | "role": "preparer", |
| 210 | "actor": "root", |
| 211 | }, |
| 212 | ) |
| 213 | year_id = api_client.post( |
| 214 | "/api/v1/companies", json={"code": "RG", "name": "شرکت", "year": 1404} |
| 215 | ).json()["year_id"] |
| 216 | # Preparer is refused by the role gate (403) before controls even run. |
| 217 | r = api_client.post(f"/api/v1/years/{year_id}/approve", json={"actor": "prep"}) |
| 218 | assert r.status_code == 403, r.text |
| 219 | # Admin passes the role gate (controls may still block, but never 403). |
| 220 | r = api_client.post(f"/api/v1/years/{year_id}/approve", json={"actor": "root"}) |
| 221 | assert r.status_code == 200, r.text |
| 222 | |
| 223 | def test_approve_open_when_no_users(self, api_client): |
| 224 | # Bootstrap/dev: with no users, the role gate is inactive (control gate still applies). |
| 225 | year_id = api_client.post( |
| 226 | "/api/v1/companies", json={"code": "NU", "name": "شرکت", "year": 1404} |
| 227 | ).json()["year_id"] |
| 228 | r = api_client.post(f"/api/v1/years/{year_id}/approve", json={"actor": "anyone"}) |
| 229 | assert r.status_code == 200, r.text |
| 230 | |
| 231 | def test_audit_endpoint_chain_valid(self, api_client): |
| 232 | api_client.post("/api/v1/users", json={"username": "root", "password": "supersecret"}) |
| 233 | r = api_client.get("/api/v1/audit/verify") |
| 234 | assert r.status_code == 200 |
| 235 | assert r.json()["chain_valid"] is True |
| 236 | r = api_client.get("/api/v1/audit") |
| 237 | assert r.json()["entries"] # user.create recorded |