Skip to content

Commit

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

Check input params of get_info and add tests
  • Loading branch information
yunyuyunyu authored Aug 15, 2024
2 parents f08f90d + 08da17d commit 2708c96
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
9 changes: 1 addition & 8 deletions nwc_backend/event_handlers/__tests__/fetch_quote_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ async def test_fetch_quote_success(mock_get: Mock) -> None:
params["receiver_address"] = receiver_address
mock_get.assert_called_once_with(url="/quote/lud16", params=params, headers=ANY)

assert quote.sending_currency_code == vasp_response["sending_currency_code"]
assert quote.receiving_currency_code == vasp_response["receiving_currency_code"]
assert quote.payment_hash == vasp_response["payment_hash"]
assert quote.expires_at == vasp_response["expires_at"]
assert quote.multiplier == vasp_response["multiplier"]
assert quote.fees == vasp_response["fees"]
assert quote.total_sending_amount == vasp_response["total_sending_amount"]
assert quote.created_at == vasp_response["created_at"]
assert quote.to_dict() == vasp_response


async def test_fetch_quote_failure__invalid_input() -> None:
Expand Down
48 changes: 48 additions & 0 deletions nwc_backend/event_handlers/__tests__/get_info_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright ©, 2022, Lightspark Group, Inc. - All Rights Reserved
# pyre-strict

import json
from secrets import token_hex
from unittest.mock import ANY, AsyncMock, Mock, patch

import aiohttp

from nwc_backend.event_handlers.get_info_handler import get_info
from nwc_backend.models.nip47_request import Nip47Request
from nwc_backend.models.nip47_request_method import Nip47RequestMethod


@patch.object(aiohttp.ClientSession, "get")
async def test_get_info_success(mock_get: Mock) -> None:
vasp_response = {
"pubkey": token_hex(),
"network": "mainnet",
"methods": [
Nip47RequestMethod.FETCH_QUOTE.value,
Nip47RequestMethod.EXECUTE_QUOTE.value,
],
"lud16": "$alice@uma.me",
"currencies": [
{
"code": "USD",
"symbol": "$",
"name": "US Dollar",
"multiplier": 15351.4798,
"decimals": 2,
"min": 1,
"max": 1000_00,
}
],
}
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

response = await get_info(
access_token=token_hex(),
request=Nip47Request(params={}),
)
mock_get.assert_called_once_with(url="/info", params=None, headers=ANY)

assert response.to_dict() == vasp_response
18 changes: 3 additions & 15 deletions nwc_backend/event_handlers/get_info_handler.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
# 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_info_response import GetInfoResponse

from nwc_backend.models.nip47_request import Nip47Request
from nwc_backend.vasp_client import vasp_uma_client


async def get_info(
access_token: str, request: Nip47Request
) -> dict[str, Any] | Nip47Error:
try:
response = await vasp_uma_client.get_info(access_token=access_token)
return response.to_dict()
except ClientResponseError as ex:
logging.exception("Request get_info %s failed", str(request.id))
# TODO: more granular error code
return Nip47Error(code=ErrorCode.OTHER, message=ex.message)
async def get_info(access_token: str, request: Nip47Request) -> GetInfoResponse:
return await vasp_uma_client.get_info(access_token=access_token)
2 changes: 1 addition & 1 deletion nwc_backend/event_handlers/nip47_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def handle_nip47_event(event: Event) -> None:
await get_balance(uma_access_token, nip47_request)
).to_dict()
case Nip47RequestMethod.GET_INFO:
response = await get_info(uma_access_token, nip47_request)
response = (await get_info(uma_access_token, nip47_request)).to_dict()
case Nip47RequestMethod.LIST_TRANSACTIONS:
response = await list_transactions(params)
case Nip47RequestMethod.LOOKUP_INVOICE:
Expand Down

0 comments on commit 2708c96

Please sign in to comment.