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 | """D-07 acceptance: locked-year immutability via DB triggers, reopen, audit chain. |
| 2 | |
| 3 | The key guarantee (booklet D3, P-08): a LOCKED year is immutable even through raw |
| 4 | SQL, bypassing the ORM. These tests issue raw statements to prove the database |
| 5 | itself enforces the rule. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import pytest |
| 11 | from sqlalchemy import text |
| 12 | from sqlalchemy.exc import IntegrityError |
| 13 | |
| 14 | from aram_backend.models import ( |
| 15 | Company, |
| 16 | FiscalYear, |
| 17 | FiscalYearStatus, |
| 18 | ReopenRequest, |
| 19 | ) |
| 20 | from aram_backend.services.audit import append_entry, verify_chain |
| 21 | |
| 22 | |
| 23 | def _locked_year(session): |
| 24 | company = Company(code="IMM", name="شرکت") |
| 25 | session.add(company) |
| 26 | session.flush() |
| 27 | year = FiscalYear(company_id=company.id, year=1404) |
| 28 | session.add(year) |
| 29 | session.flush() |
| 30 | year.status = FiscalYearStatus.APPROVED |
| 31 | session.commit() |
| 32 | year.status = FiscalYearStatus.LOCKED |
| 33 | session.commit() |
| 34 | return year |
| 35 | |
| 36 | |
| 37 | def _draft_year(session): |
| 38 | company = Company(code="IMM", name="شرکت") |
| 39 | session.add(company) |
| 40 | session.flush() |
| 41 | year = FiscalYear(company_id=company.id, year=1404) |
| 42 | session.add(year) |
| 43 | session.commit() |
| 44 | return year |
| 45 | |
| 46 | |
| 47 | def _lock(session, year): |
| 48 | year.status = FiscalYearStatus.APPROVED |
| 49 | session.commit() |
| 50 | year.status = FiscalYearStatus.LOCKED |
| 51 | session.commit() |
| 52 | |
| 53 | |
| 54 | def _seed_balance(session, year): |
| 55 | session.execute( |
| 56 | text( |
| 57 | "INSERT INTO source_documents (year_id, filename, file_hash, document_type, uploaded_at, created_at, updated_at) " |
| 58 | "VALUES (:y, 'l.xlsx', 'h1', 'general_ledger', '2026-01-01', '2026-01-01', '2026-01-01')" |
| 59 | ), |
| 60 | {"y": year.id}, |
| 61 | ) |
| 62 | sd_id = session.execute( |
| 63 | text("SELECT id FROM source_documents WHERE year_id=:y"), {"y": year.id} |
| 64 | ).scalar() |
| 65 | session.execute( |
| 66 | text( |
| 67 | "INSERT INTO ledger_balances (year_id, source_document_id, source_sheet, source_row, import_version, " |
| 68 | "account_full_code, general_code, subsidiary_code, detail1_code, detail2_code, detail3_code, detail4_code, " |
| 69 | "account_name, opening, debit, credit, closing, entry_type, created_at, updated_at) " |
| 70 | "VALUES (:y, :sd, 'Source', 3, 1, '148348', '14', '8348', '', '', '', '', " |
| 71 | "'نمونه', 0, 100, 0, 100, 'O', '2026-01-01', '2026-01-01')" |
| 72 | ), |
| 73 | {"y": year.id, "sd": sd_id}, |
| 74 | ) |
| 75 | session.commit() |
| 76 | |
| 77 | |
| 78 | class TestRawSqlImmutability: |
| 79 | def test_locked_year_blocks_raw_insert(self, session): |
| 80 | year = _locked_year(session) |
| 81 | with pytest.raises(IntegrityError, match="locked"): |
| 82 | session.execute( |
| 83 | text( |
| 84 | "INSERT INTO ledger_balances (year_id, source_document_id, source_sheet, source_row, " |
| 85 | "import_version, account_full_code, account_name, opening, debit, credit, closing, created_at, updated_at) " |
| 86 | "VALUES (:y, 1, 'Source', 4, 1, '148349', 'x', 0, 0, 0, 0, '2026-01-01', '2026-01-01')" |
| 87 | ), |
| 88 | {"y": year.id}, |
| 89 | ) |
| 90 | session.rollback() |
| 91 | |
| 92 | def test_locked_year_blocks_raw_update(self, session): |
| 93 | year = _draft_year(session) |
| 94 | _seed_balance(session, year) |
| 95 | _lock(session, year) |
| 96 | with pytest.raises(IntegrityError, match="locked"): |
| 97 | session.execute( |
| 98 | text("UPDATE ledger_balances SET debit = 999 WHERE year_id = :y"), |
| 99 | {"y": year.id}, |
| 100 | ) |
| 101 | session.rollback() |
| 102 | # Value unchanged. |
| 103 | assert ( |
| 104 | session.execute( |
| 105 | text("SELECT debit FROM ledger_balances WHERE year_id=:y"), {"y": year.id} |
| 106 | ).scalar() |
| 107 | == 100 |
| 108 | ) |
| 109 | |
| 110 | def test_locked_year_blocks_raw_delete(self, session): |
| 111 | year = _draft_year(session) |
| 112 | _seed_balance(session, year) |
| 113 | _lock(session, year) |
| 114 | with pytest.raises(IntegrityError, match="locked"): |
| 115 | session.execute(text("DELETE FROM ledger_balances WHERE year_id = :y"), {"y": year.id}) |
| 116 | session.rollback() |
| 117 | assert ( |
| 118 | session.execute( |
| 119 | text("SELECT COUNT(*) FROM ledger_balances WHERE year_id=:y"), {"y": year.id} |
| 120 | ).scalar() |
| 121 | == 1 |
| 122 | ) |
| 123 | |
| 124 | def test_draft_year_allows_writes(self, session): |
| 125 | company = Company(code="DFT", name="x") |
| 126 | session.add(company) |
| 127 | session.flush() |
| 128 | year = FiscalYear(company_id=company.id, year=1404) |
| 129 | session.add(year) |
| 130 | session.commit() |
| 131 | session.execute( |
| 132 | text( |
| 133 | "INSERT INTO source_documents (year_id, filename, file_hash, document_type, uploaded_at, created_at, updated_at) " |
| 134 | "VALUES (:y, 'l.xlsx', 'h2', 'general_ledger', '2026-01-01', '2026-01-01', '2026-01-01')" |
| 135 | ), |
| 136 | {"y": year.id}, |
| 137 | ) |
| 138 | session.commit() # must not raise |
| 139 | |
| 140 | |
| 141 | class TestReopen: |
| 142 | def test_reopen_without_request_blocked(self, session): |
| 143 | year = _locked_year(session) |
| 144 | year.status = FiscalYearStatus.APPROVED |
| 145 | with pytest.raises(ValueError, match="ReopenRequest"): |
| 146 | session.commit() |
| 147 | session.rollback() |
| 148 | |
| 149 | def test_reopen_with_approved_request_allowed(self, session): |
| 150 | year = _locked_year(session) |
| 151 | req = ReopenRequest(year_id=year.id, reason="اصلاح", status="approved", approved_by="owner") |
| 152 | session.add(req) |
| 153 | session.commit() |
| 154 | year.status = FiscalYearStatus.APPROVED |
| 155 | session.commit() |
| 156 | assert year.status == FiscalYearStatus.APPROVED |
| 157 | |
| 158 | def test_reopen_via_raw_sql_requires_request(self, session): |
| 159 | year = _locked_year(session) |
| 160 | with pytest.raises(IntegrityError, match="transition"): |
| 161 | session.execute( |
| 162 | text("UPDATE fiscal_years SET status='APPROVED' WHERE id=:y"), {"y": year.id} |
| 163 | ) |
| 164 | session.rollback() |
| 165 | |
| 166 | |
| 167 | class TestAuditChain: |
| 168 | def test_chain_builds_and_verifies(self, session): |
| 169 | append_entry(session, action="import", actor="u", payload={"rows": 10}) |
| 170 | append_entry(session, action="approve", actor="u", payload={}) |
| 171 | session.commit() |
| 172 | assert verify_chain(session) is True |
| 173 | |
| 174 | def test_tamper_detected(self, session): |
| 175 | append_entry(session, action="import", actor="u", payload={"rows": 10}) |
| 176 | append_entry(session, action="approve", actor="u", payload={}) |
| 177 | session.commit() |
| 178 | # Simulate an attacker with DB access who disables the append-only trigger |
| 179 | # and corrupts a past payload hash. verify_chain must still detect it — |
| 180 | # an independent tamper-evidence layer beyond the trigger. |
| 181 | session.execute(text("DROP TRIGGER aram_audit_update")) |
| 182 | session.execute(text("UPDATE audit_entries SET payload_hash='deadbeef' WHERE id=1")) |
| 183 | session.commit() |
| 184 | assert verify_chain(session) is False |
| 185 | |
| 186 | def test_audit_append_only_via_raw_sql(self, session): |
| 187 | append_entry(session, action="import", actor="u", payload={}) |
| 188 | session.commit() |
| 189 | with pytest.raises(IntegrityError, match="append-only"): |
| 190 | session.execute(text("UPDATE audit_entries SET actor='hacker' WHERE id=1")) |
| 191 | session.rollback() |
| 192 | with pytest.raises(IntegrityError, match="append-only"): |
| 193 | session.execute(text("DELETE FROM audit_entries WHERE id=1")) |
| 194 | session.rollback() |