|
8 | 8 | from collections.abc import Iterator |
9 | 9 | from decimal import Decimal |
10 | 10 | from http import HTTPStatus |
| 11 | +from unittest.mock import MagicMock |
11 | 12 |
|
12 | 13 | import pytest |
13 | 14 | import sqlalchemy as sa |
@@ -175,3 +176,63 @@ async def test_project_wallets_full_workflow( |
175 | 176 | resp = await client.get(f"{base_url}") |
176 | 177 | data, _ = await assert_status(resp, expected) |
177 | 178 | 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