Skip to content

Commit

Permalink
Merge pull request #59 from uma-universal-money-address/08-14-Check_i…
Browse files Browse the repository at this point in the history
…nput_params_of_get_balance_and_add_tests

Check input params of get_balance and add tests
  • Loading branch information
yunyuyunyu authored Aug 15, 2024
2 parents a290905 + b7790bd commit f08f90d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
56 changes: 56 additions & 0 deletions nwc_backend/event_handlers/__tests__/get_balance_test.py
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
)
24 changes: 7 additions & 17 deletions nwc_backend/event_handlers/get_balance_handler.py
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
)
4 changes: 3 additions & 1 deletion nwc_backend/event_handlers/nip47_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ async def handle_nip47_event(event: Event) -> None:
await fetch_quote(uma_access_token, nip47_request)
).to_dict()
case Nip47RequestMethod.GET_BALANCE:
response = await get_balance(uma_access_token, nip47_request)
response = (
await get_balance(uma_access_token, nip47_request)
).to_dict()
case Nip47RequestMethod.GET_INFO:
response = await get_info(uma_access_token, nip47_request)
case Nip47RequestMethod.LIST_TRANSACTIONS:
Expand Down

0 comments on commit f08f90d

Please sign in to comment.