Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 3, 22:00 UTC
test(security): reproduce same-flush approval ownership bypass
davoodmehraban89 committed
Oct 30, 2026 at 16:27 UTC
8e637e96a8c09b5f2fc826922dd4b53a7ec512a3
1 file changed
+105
backend/tests/test_models.py
+105
@@ -559,3 +559,108 @@ def test_mapping_direct_fk_resolves_pending_note(session, same_year):
559
session.commit()
560
session.rollback()
561
assert session.query(MappingRule).count() == 0
562
+
563
+
564
+# Same-flush approval must atomically freeze the entire fiscal-year aggregate.
565
+# Prior to this regression, checking only persisted FiscalYear.status returned DRAFT
566
+# while a pending APPROVED transition was in the same SQLAlchemy flush.
567
+@pytest.mark.parametrize("record_kind", [
568
+ "account", "source", "note", "statement", "mapping", "note_row", "statement_line",
569
+])
570
+def test_approval_and_existing_year_record_edit_in_one_flush_are_rejected(session, record_kind):
571
+ year = make_year(session, 1403)
572
+ note = NoteDefinition(year_id=year.id, note_number="10", title="Existing note")
573
+ statement = FinancialStatement(
574
+ year_id=year.id, statement_type=StatementType.BALANCE_SHEET
575
+ )
576
+ account = AccountVersion(
577
+ year_id=year.id, account_code="1000", account_name="Existing account"
578
+ )
579
+ source = SourceDocument(
580
+ year_id=year.id, filename="synthetic.xlsx",
581
+ file_hash="synthetic-sha", document_type="general_ledger",
582
+ )
583
+ mapping = MappingRule(
584
+ year_id=year.id, rule_key="existing", account_code="1000",
585
+ target_note=note, mapping_type="O",
586
+ )
587
+ note_row = NoteRow(note=note, row_order=0, label="Existing row")
588
+ statement_line = StatementLine(
589
+ statement=statement, target_note=note,
590
+ line_order=0, line_label="Existing statement row",
591
+ )
592
+ session.add_all([
593
+ note, statement, account, source, mapping, note_row, statement_line
594
+ ])
595
+ session.commit()
596
+ records = {
597
+ "account": (account, "account_name"),
598
+ "source": (source, "filename"),
599
+ "note": (note, "title"),
600
+ "statement": (statement, "version"),
601
+ "mapping": (mapping, "rule_key"),
602
+ "note_row": (note_row, "label"),
603
+ "statement_line": (statement_line, "line_label"),
604
+ }
605
+ record, field = records[record_kind]
606
+ original = getattr(record, field)
607
+ year_id = year.id
608
+ record_id = record.id
609
+ year.status = FiscalYearStatus.APPROVED
610
+ setattr(record, field, original + "-edited" if isinstance(original, str) else original + 1)
611
+
612
+ with pytest.raises(ValueError, match="cannot be mutated"):
613
+ session.commit()
614
+ session.rollback()
615
+ session.expire_all()
616
+ assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT
617
+ assert getattr(session.get(type(record), record_id), field) == original
618
+
619
+
620
+def test_approval_and_new_source_registration_in_one_flush_are_rejected(session):
621
+ year = make_year(session, 1403)
622
+ year_id = year.id
623
+ year.status = FiscalYearStatus.APPROVED
624
+ session.add(SourceDocument(
625
+ year_id=year_id, filename="new.xlsx", file_hash="synthetic-new",
626
+ document_type="general_ledger",
627
+ ))
628
+
629
+ with pytest.raises(ValueError, match="cannot be mutated"):
630
+ session.commit()
631
+ session.rollback()
632
+ session.expire_all()
633
+ assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT
634
+ assert session.query(SourceDocument).filter_by(year_id=year_id).count() == 0
635
+
636
+
637
+def test_approval_and_company_edit_in_one_flush_are_rejected(session):
638
+ year = make_year(session, 1403)
639
+ company_id = year.company_id
640
+ original = year.company.name
641
+ year_id = year.id
642
+ year.status = FiscalYearStatus.APPROVED
643
+ year.company.name = "Company changed during approval"
644
+
645
+ with pytest.raises(ValueError, match="cannot be mutated"):
646
+ session.commit()
647
+ session.rollback()
648
+ session.expire_all()
649
+ assert session.get(FiscalYear, year_id).status is FiscalYearStatus.DRAFT
650
+ assert session.get(Company, company_id).name == original
651
+
652
+
653
+def test_draft_edit_committed_before_approval_is_still_allowed(session):
654
+ year = make_year(session, 1403)
655
+ account = AccountVersion(
656
+ year_id=year.id, account_code="1000", account_name="Initial"
657
+ )
658
+ session.add(account)
659
+ session.commit()
660
+ account.account_name = "Validated"
661
+ session.commit()
662
+ year.status = FiscalYearStatus.APPROVED
663
+ session.commit()
664
+ session.expire_all()
665
+ assert session.get(AccountVersion, account.id).account_name == "Validated"
666
+ assert session.get(FiscalYear, year.id).status is FiscalYearStatus.APPROVED