Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add github action check for python code #50

Merged
merged 7 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/lint-and-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Lint and Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: "Setup Python"
uses: "actions/setup-python@v4"
with:
python-version: "3.11"
cache: "pipenv"
cache-dependency-path: "Pipfile.lock"
- name: "Install pipenv"
run: "pip install pipenv wheel"
- name: "Install dependencies"
run: "pipenv install --dev"
- name: "Run pyre"
run: |
set -o pipefail
pipenv run pyre | tee >(sed 's, ,:,' | awk -F: '{sub(" ", "", $5); print "::error file=" ENVIRON["PWD"] "/" $1 ",line=" $2 ",col=" $3 ",title=" $4 "::" $5}')
- name: "Run ruff"
run: "pipenv run ruff check --output-format github ."
- name: "Run black"
run: |
set -o pipefail
pipenv run black --check --diff .
- name: Run tests
run: |
PYTHONPATH=. pipenv run pytest
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ authlib = "*"
black = "*"
pyre-check = "*"
ruff = "*"
pytest = "*"
pytest-asyncio = "*"

[requires]
python_version = "3.11"
46 changes: 45 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions nwc_backend/event_handlers/__tests__/lookup_user_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
from typing import Optional

os.environ["NOSTR_PRIVKEY"] = (
"nsec166ah7ez498kjl87a088yn34gvcjpzmy9eymuwwgtwcywh84j865s0qxnul"
)
os.environ["RELAY"] = "wss://fake.relay.url"
os.environ["VASP_UMA_API_BASE_URL"] = "https://fake.vasp.uma.api.url"

import pytest
from nwc_backend.models.nip47_request import Nip47Request, ErrorCode, Nip47Error
from nwc_backend.vasp_client import ReceivingAddress, VaspUmaClient, LookupUserResponse
from nwc_backend.event_handlers.lookup_user_handler import lookup_user


class MockVaspUmaClient(VaspUmaClient):
lookup_user_response = LookupUserResponse.from_dict(
{
"currencies": [
{
"code": "USD",
"symbol": "$",
"name": "US Dollar",
"multiplier": 1000,
"decimals": 2,
"min": 1000,
"max": 1000000,
}
]
}
)

async def lookup_user(
self,
access_token: str,
receiving_address: ReceivingAddress,
base_sending_currency_code: Optional[str],
) -> LookupUserResponse:
return self.lookup_user_response


@pytest.fixture
def vasp_client_mock():
return MockVaspUmaClient()


async def test_lookup_user_success(vasp_client_mock):
access_token = "your_access_token"
request = Nip47Request(
params={
"receiver": {"lud16": "$alice@vasp.net"},
"base_sending_currency_code": "USD",
}
)

result = await lookup_user(access_token, request, vasp_client_mock)

assert isinstance(result, dict)
assert result == vasp_client_mock.lookup_user_response.to_dict()


async def test_lookup_user_missing_receiver(vasp_client_mock):
access_token = "your_access_token"
request = Nip47Request(params={})

result = await lookup_user(access_token, request, vasp_client_mock)

assert isinstance(result, Nip47Error)
assert result.code == ErrorCode.OTHER
assert result.message == "Require receiver in the request params."


async def test_lookup_user_multiple_receivers(vasp_client_mock):
access_token = "your_access_token"
request = Nip47Request(
params={"receiver": {"bolt12": "bolt12_address", "lud16": "lud16_address"}}
)

result = await lookup_user(access_token, request, vasp_client_mock)

assert isinstance(result, Nip47Error)
assert result.code == ErrorCode.OTHER
assert result.message == "Expect receiver to contain exactly one address."
15 changes: 11 additions & 4 deletions nwc_backend/event_handlers/lookup_user_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
from nostr_sdk import ErrorCode, Nip47Error

from nwc_backend.models.nip47_request import Nip47Request
from nwc_backend.vasp_client import AddressType, ReceivingAddress, vasp_uma_client
from nwc_backend.vasp_client import (
AddressType,
ReceivingAddress,
VaspUmaClient,
vasp_uma_client,
)


async def lookup_user(
access_token: str, request: Nip47Request
access_token: str,
request: Nip47Request,
vasp_client: VaspUmaClient = vasp_uma_client,
) -> dict[str, Any] | Nip47Error:
receiver = request.get("receiver")
receiver = request.params.get("receiver")
if receiver is None:
return Nip47Error(
code=ErrorCode.OTHER,
Expand All @@ -39,7 +46,7 @@ async def lookup_user(
)

try:
response = await vasp_uma_client.lookup_user(
response = await vasp_client.lookup_user(
access_token=access_token,
receiving_address=receiving_address,
base_sending_currency_code=request.params.get("base_sending_currency_code"),
Expand Down
1 change: 0 additions & 1 deletion nwc_backend/models/nwc_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from nwc_backend.db import UUID as DBUUID
from nwc_backend.models.nip47_request_method import Nip47RequestMethod
from nwc_backend.models.client_app import ClientApp
from nwc_backend.models.model_base import ModelBase
from nwc_backend.models.nip47_request_method import Nip47RequestMethod
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode=auto
Loading