fix: tests and simplify the test code

refs# 21
This commit is contained in:
Stefan Stammberger 2021-11-19 17:28:39 +01:00
parent 964514f90c
commit bd6d293259
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
3 changed files with 43 additions and 74 deletions

View file

@ -2,87 +2,30 @@ from app.models.lightning import LightningInfoLite
from app.routers import lightning
from fastapi import status
from starlette.testclient import TestClient
from tests.routers.utils import call_route
from tests.utils import monkeypatch_auth
from .test_lightning_utils import get_valid_lightning_info_lite
def test_route_authentications_latest(test_client: TestClient):
prefix = "/latest/lightning"
response = test_client.get(f"{prefix}/get-info-lite")
assert response.status_code == status.HTTP_403_FORBIDDEN
prefixes = ["/latest/lightning", "/v1/lightning"]
response = test_client.post(f"{prefix}/add-invoice", params={"value_msat": 1337})
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/get-balance")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-all-tx")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-invoices")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-onchain-tx")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-payments")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.post(
f"{prefix}/send-coins",
params={"amount": "", "address": ""},
)
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.post(f"{prefix}/send-payment", params={"pay_req": "1337"})
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/get-info")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/decode-pay-req", params={"pay_req": ""})
assert response.status_code == status.HTTP_403_FORBIDDEN
def test_route_authentications_v1(test_client: TestClient):
prefix = "/v1/lightning"
response = test_client.get(f"{prefix}/get-info-lite")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.post(f"{prefix}/add-invoice", params={"value_msat": 1337})
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/get-balance")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-all-tx")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-invoices")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-onchain-tx")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/list-payments")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.post(
f"{prefix}/send-coins",
params={"amount": "", "address": ""},
)
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.post(f"{prefix}/send-payment", params={"pay_req": ""})
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/get-info")
assert response.status_code == status.HTTP_403_FORBIDDEN
response = test_client.get(f"{prefix}/decode-pay-req", params={"pay_req": ""})
assert response.status_code == status.HTTP_403_FORBIDDEN
for prefix in prefixes:
p = {"value_msat": 1337}
call_route(test_client, f"{prefix}/add-invoice", params=p, method="p")
call_route(test_client, f"{prefix}/get-balance")
call_route(test_client, f"{prefix}/list-all-tx")
call_route(test_client, f"{prefix}/list-invoices")
call_route(test_client, f"{prefix}/list-onchain-tx")
call_route(test_client, f"{prefix}/list-payments")
p = {"amount": "", "address": ""}
call_route(test_client, f"{prefix}/send-coins", params=p, method="p")
p = {"pay_req": "1337"}
call_route(test_client, f"{prefix}/send-payment", params=p, method="p")
call_route(test_client, f"{prefix}/get-info-lite")
call_route(test_client, f"{prefix}/get-info")
call_route(test_client, f"{prefix}/decode-pay-req", params={"pay_req": ""})
def test_get_ln_status(test_client: TestClient, monkeypatch):

View file

@ -8,6 +8,7 @@ def get_valid_lightning_info_lite() -> LightningInfoLite:
num_pending_channels=1,
num_active_channels=4,
num_inactive_channels=2,
num_peers=3,
block_height=123456,
synced_to_chain=True,
synced_to_graph=True,

25
tests/routers/utils.py Normal file
View file

@ -0,0 +1,25 @@
from fastapi import status
from starlette.testclient import TestClient
invalid_auth_header = {"Authorization": "Bearer dsgfdg"}
def call_route(test_client: TestClient, route: str, params={}, method="g"):
# Test with no "Authentication" header at all
# Should return a 403 by FastAPI's HTTPBearer
response = None
if method == "g":
response = test_client.get(route, params=params)
elif method == "p":
response = test_client.post(route, params=params)
assert response.status_code == status.HTTP_403_FORBIDDEN
# Test with an invalid "Authentication" header
# Should return a 401 by BlitzAPI's AuthBearer
response = None
if method == "g":
response = test_client.get(route, params=params, headers=invalid_auth_header)
elif method == "p":
response = test_client.post(route, params=params, headers=invalid_auth_header)
assert response.status_code == status.HTTP_401_UNAUTHORIZED