From bd6d293259ed459d04826c8e611cadfb02cacc0f Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Fri, 19 Nov 2021 17:28:39 +0100 Subject: [PATCH] fix: tests and simplify the test code refs# 21 --- tests/routers/test_lightning.py | 91 +++++---------------------- tests/routers/test_lightning_utils.py | 1 + tests/routers/utils.py | 25 ++++++++ 3 files changed, 43 insertions(+), 74 deletions(-) create mode 100644 tests/routers/utils.py diff --git a/tests/routers/test_lightning.py b/tests/routers/test_lightning.py index 098ea0d..d585a3e 100644 --- a/tests/routers/test_lightning.py +++ b/tests/routers/test_lightning.py @@ -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): diff --git a/tests/routers/test_lightning_utils.py b/tests/routers/test_lightning_utils.py index ae05965..3132bcc 100644 --- a/tests/routers/test_lightning_utils.py +++ b/tests/routers/test_lightning_utils.py @@ -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, diff --git a/tests/routers/utils.py b/tests/routers/utils.py new file mode 100644 index 0000000..d63a089 --- /dev/null +++ b/tests/routers/utils.py @@ -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