Skip to content

Commit

Permalink
test: beginning tests for delete can funding received and subscribing…
Browse files Browse the repository at this point in the history
… to those events
  • Loading branch information
rajohnson90 committed Jan 17, 2025
1 parent 635a201 commit 6fd93eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions backend/ops_api/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,5 @@ def before_request_function(app: Flask, request: request):

request.message_bus = MessageBus()
request.message_bus.subscribe(OpsEventType.CREATE_NEW_CAN, can_history_trigger)
request.message_bus.subscribe(OpsEventType.CREATE_CAN_FUNDING_BUDGET, can_history_trigger)
request.message_bus.subscribe(OpsEventType.DELETE_CAN_FUNDING_RECEIVED, can_history_trigger)
12 changes: 12 additions & 0 deletions backend/ops_api/ops/services/can_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ def can_history_trigger(
history_type=CANHistoryType.CAN_FUNDING_CREATED,
)
session.add(history_event)
case OpsEventType.DELETE_CAN_FUNDING_RECEIVED:
# budget = locale.currency(event.event_details["new_can_funding_budget"]["budget"], grouping=True)
creator_name = event.event_details["new_can_funding_budget"]["created_by_user"]["full_name"]
history_event = CANHistory(
can_id=event.event_details["new_can_funding_budget"]["can"]["id"],
ops_event_id=event.id,
history_title=f"**FY {current_fiscal_year} Budget Entered**",
history_message=f"{creator_name} entered a FY {current_fiscal_year} budget of {budget}",
timestamp=event.created_on,
history_type=CANHistoryType.CAN_FUNDING_CREATED,
)
session.add(history_event)
session.commit()
19 changes: 15 additions & 4 deletions backend/ops_api/tests/ops/can/test_can_funding_received.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from models import CANFundingReceived
from ops.services.can_funding_received import CANFundingReceivedService
from ops_api.tests.utils import DummyContextManager


@pytest.mark.usefixtures("app_ctx")
Expand Down Expand Up @@ -242,6 +243,10 @@ def test_funding_received_delete(budget_team_auth_client, mocker):
mocker_delete_funding_received = mocker.patch(
"ops_api.ops.services.can_funding_received.CANFundingReceivedService.delete"
)
context_manager = DummyContextManager()
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__enter__")
mocker_ops_event_ctxt_mgr.return_value = context_manager
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__exit__")
response = budget_team_auth_client.delete(f"/api/v1/can-funding-received/{test_funding_received_id}")

assert response.status_code == 200
Expand All @@ -251,18 +256,24 @@ def test_funding_received_delete(budget_team_auth_client, mocker):


@pytest.mark.usefixtures("app_ctx")
def test_can_delete_404(budget_team_auth_client):
def test_can_delete_404(budget_team_auth_client, mocker):
test_can_id = 600

context_manager = DummyContextManager()
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__enter__")
mocker_ops_event_ctxt_mgr.return_value = context_manager
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__exit__")
response = budget_team_auth_client.delete(f"/api/v1/can-funding-received/{test_can_id}")

assert response.status_code == 404


@pytest.mark.usefixtures("app_ctx")
def test_basic_user_cannot_delete_cans(basic_user_auth_client):
def test_basic_user_cannot_delete_cans(basic_user_auth_client, mocker):
response = basic_user_auth_client.delete("/api/v1/can-funding-received/517")

context_manager = DummyContextManager()
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__enter__")
mocker_ops_event_ctxt_mgr.return_value = context_manager
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__exit__")
assert response.status_code == 403


Expand Down
33 changes: 18 additions & 15 deletions backend/ops_api/tests/ops/can_history/test_can_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,21 @@ def test_create_can_history_create_can_funding_budget(loaded_db):
)


# @pytest.mark.usefixtures("app_ctx")
# def test_create_can_history_create_can_funding_received(loaded_db):
# funding_budget_created_event = loaded_db.get(OpsEvent, 24)
# can_history_trigger(funding_budget_created_event, loaded_db)
# can_history_list = loaded_db.query(CANHistory).all()
# can_history_count = len(can_history_list)
# new_can_history_item = can_history_list[can_history_count - 1]

# assert new_can_history_item.history_type == CANHistoryType.CAN_FUNDING_CREATED
# assert new_can_history_item.history_title == "**FY 2025 Budget Entered**"
# assert new_can_history_item.history_message == "System Owner entered a FY 2025 budget of $10,000.00"
# assert (
# new_can_history_item.can_id == funding_budget_created_event.event_details["new_can_funding_budget"]["can"]["id"]
# )
# assert new_can_history_item.timestamp == funding_budget_created_event.created_on.strftime("%Y-%m-%d %H:%M:%S.%f")
@pytest.mark.usefixtures("app_ctx")
def test_create_can_history_delete_can_funding_received(loaded_db):
funding_received_deleted_event = loaded_db.get(OpsEvent, 25)
can_history_trigger(funding_received_deleted_event, loaded_db)
can_history_list = loaded_db.query(CANHistory).all()
can_history_count = len(can_history_list)
new_can_history_item = can_history_list[can_history_count - 1]

assert new_can_history_item.history_type == CANHistoryType.CAN_RECEIVED_DELETED
assert new_can_history_item.history_title == "**Funding Deleted**"
assert (
new_can_history_item.history_message == "System Owner deleted funding received 27 in the amount of $200,000.00"
)
assert (
new_can_history_item.can_id
== funding_received_deleted_event.event_details["new_can_funding_budget"]["can"]["id"]
)
assert new_can_history_item.timestamp == funding_received_deleted_event.created_on.strftime("%Y-%m-%d %H:%M:%S.%f")

0 comments on commit 6fd93eb

Please sign in to comment.