-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from uma-universal-money-address/08-14-Check_i…
…nput_params_of_get_balance_and_add_tests Check input params of get_balance and add tests
- Loading branch information
Showing
3 changed files
with
66 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright ©, 2022, Lightspark Group, Inc. - All Rights Reserved | ||
# pyre-strict | ||
|
||
import json | ||
from secrets import token_hex | ||
from typing import Any, Optional | ||
from unittest.mock import ANY, AsyncMock, Mock, patch | ||
|
||
import aiohttp | ||
import pytest | ||
|
||
from nwc_backend.event_handlers.get_balance_handler import get_balance | ||
from nwc_backend.exceptions import InvalidInputException | ||
from nwc_backend.models.nip47_request import Nip47Request | ||
|
||
|
||
@patch.object(aiohttp.ClientSession, "get") | ||
@pytest.mark.parametrize( | ||
"currency_code", | ||
["USD", None], | ||
) | ||
async def test_get_balance_success( | ||
mock_get: Mock, currency_code: Optional[str] | ||
) -> None: | ||
vasp_response: dict[str, Any] = {"balance": 1_000_000} | ||
if currency_code: | ||
vasp_response["currency_code"] = currency_code | ||
|
||
mock_response = AsyncMock() | ||
mock_response.text = AsyncMock(return_value=json.dumps(vasp_response)) | ||
mock_response.raise_for_status = Mock() | ||
mock_get.return_value.__aenter__.return_value = mock_response | ||
|
||
params = {} | ||
if currency_code: | ||
params["currency_code"] = currency_code | ||
|
||
response = await get_balance( | ||
access_token=token_hex(), | ||
request=Nip47Request(params=params), | ||
) | ||
|
||
mock_get.assert_called_once_with( | ||
url="/balance", params=params if params else None, headers=ANY | ||
) | ||
|
||
assert response.balance == vasp_response["balance"] | ||
assert response.currency_code == currency_code | ||
|
||
|
||
async def test_get_balance_failure__invalid_input() -> None: | ||
with pytest.raises(InvalidInputException): | ||
await get_balance( | ||
access_token=token_hex(), | ||
request=Nip47Request(params={"currency_code": 123}), # wrong value | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
# Copyright ©, 2022, Lightspark Group, Inc. - All Rights Reserved | ||
# pyre-strict | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from aiohttp import ClientResponseError | ||
from nostr_sdk import ErrorCode, Nip47Error | ||
from uma_auth.models.get_balance_response import GetBalanceResponse | ||
|
||
from nwc_backend.event_handlers.input_validator import get_optional_field | ||
from nwc_backend.models.nip47_request import Nip47Request | ||
from nwc_backend.vasp_client import vasp_uma_client | ||
|
||
|
||
async def get_balance( | ||
access_token: str, request: Nip47Request | ||
) -> dict[str, Any] | Nip47Error: | ||
try: | ||
response = await vasp_uma_client.get_balance( | ||
access_token=access_token, currency_code=request.params.get("currency_code") | ||
) | ||
return response.to_dict() | ||
except ClientResponseError as ex: | ||
logging.exception("Request get_balance %s failed", str(request.id)) | ||
# TODO: more granular error code | ||
return Nip47Error(code=ErrorCode.OTHER, message=ex.message) | ||
async def get_balance(access_token: str, request: Nip47Request) -> GetBalanceResponse: | ||
currency_code = get_optional_field(request.params, "currency_code", str) | ||
return await vasp_uma_client.get_balance( | ||
access_token=access_token, currency_code=currency_code | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters