"""Unit tests for the approval state machine — pure logic, no database.""" import pytest from app.approvals.state_machine import TRANSITIONS, assert_editable, assert_transition from app.common.enums import DCRStatus, Role, WorkflowAction from app.common.exceptions import ( ForbiddenError, InvalidStateTransitionError, ReportLockedError, ) class TestHappyPath: def test_mr_submits_draft(self): t = assert_transition(DCRStatus.DRAFT, WorkflowAction.SUBMIT, Role.MR) assert t.to_status == DCRStatus.PENDING_ABM def test_mr_resubmits_returned(self): t = assert_transition(DCRStatus.RETURNED, WorkflowAction.SUBMIT, Role.MR) assert t.to_status == DCRStatus.PENDING_ABM def test_am_approves(self): t = assert_transition(DCRStatus.PENDING_ABM, WorkflowAction.APPROVE, Role.ABM) assert t.to_status == DCRStatus.APPROVED def test_am_returns_requires_reason(self): t = assert_transition(DCRStatus.PENDING_ABM, WorkflowAction.RETURN, Role.ABM) assert t.to_status == DCRStatus.RETURNED assert t.requires_reason is True def test_gm_cannot_approve_or_return(self): for action in (WorkflowAction.APPROVE, WorkflowAction.RETURN): with pytest.raises(ForbiddenError): assert_transition(DCRStatus.PENDING_ABM, action, Role.AGM) class TestAreaManagerIsTheFinalApprover: """There is no AGM stage: the ABM approves or returns.""" def test_no_general_manager_status_or_action_exists(self): assert "PENDING_GM" not in {s.value for s in DCRStatus} assert "FORWARD" not in {a.value for a in WorkflowAction} assert not [t for t in TRANSITIONS if t.allowed_role == Role.AGM] def test_only_the_area_manager_can_decide_a_pending_report(self): for role in (Role.MR, Role.AGM, Role.ADMIN): for action in (WorkflowAction.APPROVE, WorkflowAction.RETURN): with pytest.raises(ForbiddenError): assert_transition(DCRStatus.PENDING_ABM, action, role) class TestAdminCanNeverApprove: """SEC-2 — if ADMIN could approve, the audit trail would be worthless.""" @pytest.mark.parametrize("status", list(DCRStatus)) @pytest.mark.parametrize("action", list(WorkflowAction)) def test_admin_matches_no_transition(self, status, action): with pytest.raises((ForbiddenError, InvalidStateTransitionError)): assert_transition(status, action, Role.ADMIN) class TestApprovedIsTerminal: """A6/Q6 — no reopen path exists in the MVP.""" @pytest.mark.parametrize("action", list(WorkflowAction)) @pytest.mark.parametrize("role", list(Role)) def test_nothing_leaves_approved(self, action, role): with pytest.raises(InvalidStateTransitionError): assert_transition(DCRStatus.APPROVED, action, role) def test_no_transition_originates_from_approved(self): assert not [t for t in TRANSITIONS if t.from_status == DCRStatus.APPROVED] class TestEditability: """V9/V10 — data changes only in DRAFT or RETURNED.""" @pytest.mark.parametrize("status", [DCRStatus.DRAFT, DCRStatus.RETURNED]) def test_editable(self, status): assert_editable(status) # must not raise @pytest.mark.parametrize("status", [DCRStatus.PENDING_ABM, DCRStatus.APPROVED]) def test_not_editable(self, status): with pytest.raises(ReportLockedError): assert_editable(status) class TestWrongRoleAtRightStage: def test_mr_cannot_approve(self): with pytest.raises(ForbiddenError): assert_transition(DCRStatus.PENDING_ABM, WorkflowAction.APPROVE, Role.MR) def test_gm_cannot_submit(self): with pytest.raises(ForbiddenError): assert_transition(DCRStatus.DRAFT, WorkflowAction.SUBMIT, Role.AGM) def test_mr_cannot_return_its_own_report(self): with pytest.raises(InvalidStateTransitionError): assert_transition(DCRStatus.DRAFT, WorkflowAction.RETURN, Role.MR) def test_exactly_four_transitions(): """submit (x2), then the Area Business Manager approves or returns.""" assert len(TRANSITIONS) == 4 assert len(DCRStatus) == 4