Skip to content

Commit

Permalink
add get_wallet unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgaard-itis committed Oct 11, 2023
1 parent 75ba207 commit 80e2ee8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion services/api-server/tests/mocks/get_wallet_failure.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
}
},
"status_code": 403
},
}
]
2 changes: 1 addition & 1 deletion services/api-server/tests/mocks/get_wallet_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
}
},
"status_code": 200
},
}
]
2 changes: 1 addition & 1 deletion services/api-server/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def mocked_webserver_service_api_base(

# pylint: disable=not-context-manager
with respx.mock(
base_url=settings.API_SERVER_WEBSERVER.api_base_url,
base_url=settings.API_SERVER_WEBSERVER.base_url,
assert_all_called=False,
assert_all_mocked=True,
) as respx_mock:
Expand Down
55 changes: 55 additions & 0 deletions services/api-server/tests/unit/test_api_wallets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pathlib import Path
from typing import Any, Callable

import httpx
import pytest
import respx
from httpx import AsyncClient
from models_library.api_schemas_webserver.wallets import WalletGet
from pydantic import parse_obj_as
from simcore_service_api_server._meta import API_VTAG
from simcore_service_api_server.utils.http_calls_capture import HttpApiCallCaptureModel
from unit.conftest import SideEffectCallback


def _get_wallet_side_effect(
request: httpx.Request,
path_params: dict[str, Any],
capture: HttpApiCallCaptureModel,
) -> Any:
response = capture.response_body
assert isinstance(response, dict)
if isinstance(response.get("data"), dict):
assert response.get("data").get("walletId") is not None
response["data"]["walletId"] = path_params["wallet_id"]
return response


@pytest.mark.parametrize(
"capture", ["get_wallet_success.json", "get_wallet_failure.json"]
)
async def test_get_wallet(
client: AsyncClient,
mocked_webserver_service_api_base,
respx_mock_from_capture: Callable[
[respx.MockRouter, Path, list[SideEffectCallback] | None], respx.MockRouter
],
auth: httpx.BasicAuth,
project_tests_dir: Path,
capture: str,
):
respx_mock = respx_mock_from_capture(
mocked_webserver_service_api_base,
project_tests_dir / "mocks" / capture,
[_get_wallet_side_effect],
)

wallet_id: int = 159873
response = await client.get(f"{API_VTAG}/wallets/{wallet_id}", auth=auth)
if "success" in capture:
assert response.status_code == 200
wallet: WalletGet = parse_obj_as(WalletGet, response.json())
assert wallet.wallet_id == wallet_id
elif "failure" in capture:
assert response.status_code == 403
assert response.json().get("errors") is not None

0 comments on commit 80e2ee8

Please sign in to comment.