Skip to content

Commit 89bda75

Browse files
adding unit tests
1 parent c7f6bdb commit 89bda75

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

services/web/server/src/simcore_service_webserver/projects/_wallets_handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ async def pay_project_debt(request: web.Request):
166166
# At present, once the wallet balance becomes positive, RUT updates all
167167
# projects connected to that wallet from IN_DEBT to BILLED.
168168

169-
return web.json_response(status=status.HTTP_501_NOT_IMPLEMENTED)
169+
raise web.HTTPNotImplemented
170170

171171
# The debt is being paid using a different wallet than the one currently connected to the project.
172172
# Steps:
@@ -177,8 +177,8 @@ async def pay_project_debt(request: web.Request):
177177
product_name=req_ctx.product_name,
178178
project_id=path_params.project_id,
179179
user_id=req_ctx.user_id,
180-
current_wallet_id=path_params.wallet_id,
180+
current_wallet_id=current_wallet.wallet_id,
181181
new_wallet_id=path_params.wallet_id,
182182
debt_amount=body_params.amount,
183183
)
184-
return envelope_json_response(web.json_response(status=status.HTTP_204_NO_CONTENT))
184+
return web.json_response(status=status.HTTP_204_NO_CONTENT)

services/web/server/tests/unit/with_dbs/02/test_projects_wallet_handlers.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections.abc import Iterator
99
from decimal import Decimal
1010
from http import HTTPStatus
11+
from unittest.mock import MagicMock
1112

1213
import pytest
1314
import sqlalchemy as sa
@@ -175,3 +176,63 @@ async def test_project_wallets_full_workflow(
175176
resp = await client.get(f"{base_url}")
176177
data, _ = await assert_status(resp, expected)
177178
assert data["walletId"] == setup_wallets_db[1].wallet_id
179+
180+
181+
@pytest.fixture
182+
def mock_pay_project_debt(mocker: MockerFixture):
183+
return mocker.patch(
184+
"simcore_service_webserver.projects._wallets_api.credit_transactions.pay_project_debt",
185+
spec=True,
186+
)
187+
188+
189+
@pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)])
190+
async def test_pay_project_debt(
191+
client: TestClient,
192+
logged_user: UserInfoDict,
193+
user_project: ProjectDict,
194+
expected: HTTPStatus,
195+
setup_wallets_db: list[WalletGet],
196+
mock_get_project_wallet_total_credits: None,
197+
mock_get_service_run_page: None,
198+
mock_pay_project_debt: MagicMock,
199+
):
200+
assert client.app
201+
# Use endpoint while project is not connected to any wallet
202+
base_url = client.app.router["pay_project_debt"].url_for(
203+
project_id=user_project["uuid"], wallet_id=f"{setup_wallets_db[1].wallet_id}"
204+
)
205+
resp = await client.post(f"{base_url}", json={"amount": -100})
206+
await assert_status(resp, status.HTTP_404_NOT_FOUND)
207+
208+
# Connect wallet to the project
209+
base_url = client.app.router["connect_wallet_to_project"].url_for(
210+
project_id=user_project["uuid"], wallet_id=f"{setup_wallets_db[0].wallet_id}"
211+
)
212+
resp = await client.put(f"{base_url}")
213+
data, _ = await assert_status(resp, expected)
214+
assert data["walletId"] == setup_wallets_db[0].wallet_id
215+
216+
# Use endpoint with the same wallet as the one that is connected
217+
base_url = client.app.router["pay_project_debt"].url_for(
218+
project_id=user_project["uuid"], wallet_id=f"{setup_wallets_db[0].wallet_id}"
219+
)
220+
resp = await client.post(f"{base_url}", json={"amount": -100})
221+
assert resp.status == status.HTTP_501_NOT_IMPLEMENTED
222+
223+
# Use endpoint with wrong input
224+
base_url = client.app.router["pay_project_debt"].url_for(
225+
project_id=user_project["uuid"], wallet_id=f"{setup_wallets_db[1].wallet_id}"
226+
)
227+
resp = await client.post(
228+
f"{base_url}", json={"amount": 100}
229+
) # <-- Error input (must be negative!)
230+
await assert_status(resp, status.HTTP_422_UNPROCESSABLE_ENTITY)
231+
232+
# Use endpoint properly
233+
base_url = client.app.router["pay_project_debt"].url_for(
234+
project_id=user_project["uuid"], wallet_id=f"{setup_wallets_db[1].wallet_id}"
235+
)
236+
resp = await client.post(f"{base_url}", json={"amount": -100})
237+
data, _ = await assert_status(resp, status.HTTP_204_NO_CONTENT)
238+
assert mock_pay_project_debt.assert_called_once

0 commit comments

Comments
 (0)