Skip to content

Commit 108a114

Browse files
feat(cat-gateway): Add integration test for health/ready endpoint (#2567)
* feat(cat-gateway): add integration test for health/ready endpoint * fix(cat-gateway): add index db outage test and fix spelling * fix(cat-gateway): add test wrappers to Earthfile, update poetry.lock * fix(cat-gateway): remove wrappers code from Earthfile * cleanup document.post funciton * fix --------- Co-authored-by: Mr-Leshiy <[email protected]>
1 parent 80421b6 commit 108a114

File tree

10 files changed

+1832
-531
lines changed

10 files changed

+1832
-531
lines changed

.config/dictionaries/project.dic

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ minicbor
206206
minijinja
207207
mithril
208208
mitigations
209+
mitmdump
209210
mocktail
210211
moderations
211212
moka
@@ -258,6 +259,7 @@ pubkey
258259
PUBLICKEY
259260
pubspec
260261
pycardano
262+
pyright
261263
pytest
262264
qrcode
263265
rapidoc

catalyst-gateway/tests/api_tests/Earthfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ builder:
1010
COPY --dir ./api .
1111
COPY --dir ./integration .
1212
COPY --dir ./scripts .
13-
COPY --dir ./utils .
1413
COPY --dir ./test_data .
14+
COPY --dir ./utils .
15+
COPY --dir ./wrappers .
1516
COPY cat-libs-rust+build/mk_signed_doc .
1617
DO python-ci+BUILDER
1718

@@ -22,6 +23,8 @@ test:
2223
ENV EVENT_DB_TEST_URL "postgres://catalyst-event-dev:CHANGE_ME@localhost/CatalystEventDev"
2324
ENV CAT_GATEWAY_TEST_URL "http://127.0.0.1:3030"
2425
ENV CAT_GATEWAY_INTERNAL_API_KEY "123"
26+
# for the current setup its not possible to provide a path to the cat-gateway binary, because its running under the container
27+
ENV CAT_GATEWAY_EXECUTABLE_PATH "SOME VALUE"
2528

2629
WITH DOCKER \
2730
--compose docker-compose.yml \

catalyst-gateway/tests/api_tests/api/v1/document.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from api import cat_gateway_endpoint_url
33

44
URL = cat_gateway_endpoint_url("api/v1/document")
5+
INDEX_URL = cat_gateway_endpoint_url("api/v1/document/index")
56

67

78
# Signed document GET
@@ -18,6 +19,15 @@ def put(data: str, token: str):
1819

1920

2021
# Signed document POST
21-
def post(document_url: str, filter: dict):
22+
def post(filter: dict, limit=None, page=None):
2223
headers = {"Content-Type": "application/json"}
23-
return requests.post(f"{URL}{document_url}", headers=headers, json=filter)
24+
url = f"{URL}/index"
25+
query_params = []
26+
if limit is not None:
27+
query_params.append(f"limit={limit}")
28+
if page is not None:
29+
query_params.append(f"page={page}")
30+
31+
if query_params:
32+
url += "?" + "&".join(query_params)
33+
return requests.post(url, headers=headers, json=filter)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pytest
2+
import time
3+
4+
from api.v1 import document, rbac
5+
from typing import Any, Generator
6+
from utils import health
7+
from utils.rbac_chain import rbac_chain_factory, Chain
8+
from wrappers import TestProxy
9+
from wrappers.cat_gateway import CatGateway
10+
11+
@pytest.fixture
12+
def event_db_proxy() -> Generator[Any, Any, Any]:
13+
proxy = TestProxy("Event DB", 18080, 5432)
14+
proxy.start()
15+
yield proxy
16+
proxy.stop()
17+
18+
19+
@pytest.fixture
20+
def index_db_proxy() -> Generator[Any, Any, Any]:
21+
proxy = TestProxy("Index DB", 18090, 9042)
22+
proxy.start()
23+
yield proxy
24+
proxy.stop()
25+
26+
@pytest.fixture
27+
def cat_gateway_service() -> Generator[Any, Any, Any]:
28+
cat = CatGateway()
29+
yield cat
30+
cat.stop()
31+
del cat
32+
print("dropped cat-gateway")
33+
34+
35+
@pytest.mark.health_endpoint
36+
def test_ready_endpoint_with_event_db_outage(event_db_proxy, index_db_proxy, cat_gateway_service, rbac_chain_factory):
37+
cat_gateway_service.start()
38+
time.sleep(2) # wait for cat-gateway API to start
39+
health.is_ready() #assertion
40+
41+
# suspend event db comms
42+
event_db_proxy.suspend()
43+
# fetch endpoint that uses event db
44+
resp = document.post(filter={})
45+
assert(resp.status_code == 503), f"Expected document index to fail: {resp.status_code} - {resp.text}"
46+
# call ready endpoint, expect 503
47+
health.is_not_ready() #assertion
48+
49+
# re-enable event db
50+
event_db_proxy.resume()
51+
#Call `ready` endpoint to attempt re-connection that should succeed
52+
health.is_ready()
53+
54+
# fetch endpoint that uses event db
55+
resp = document.post(filter={})
56+
assert(resp.status_code == 200), f"Expected document index to succeed: {resp.status_code} - {resp.text}"
57+
58+
# Index DB testing
59+
auth_token = rbac_chain_factory(Chain.Role0).auth_token()
60+
61+
# Not registered stake address lookup
62+
# Cardano test data CIP0019
63+
# <https://github.com/cardano-foundation/CIPs/blob/master/CIP-0019/README.md>
64+
# cspell:disable-next-line
65+
stake_address = "stake_test17rphkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcljw6kf"
66+
resp = rbac.get(lookup=stake_address, token=auth_token)
67+
assert(resp.status_code == 404), f"Expected not registered stake address: {resp.status_code} - {resp.text}"
68+
69+
# disable index db
70+
index_db_proxy.suspend()
71+
resp = rbac.get(lookup=stake_address, token=auth_token)
72+
assert(resp.status_code == 503), f"Expected RBAC lookup to fail: {resp.status_code} - {resp.text}"
73+
74+
# call ready endpoint, expect 503
75+
health.is_not_ready() #assertion
76+
77+
#Call `ready` endpoint to attempt re-connection that should succeed
78+
index_db_proxy.resume()
79+
health.is_ready() #assertion

catalyst-gateway/tests/api_tests/integration/test_signed_doc.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def test_proposal_doc(proposal_doc_factory, rbac_chain_factory):
220220
), f"Failed to get document: {resp.status_code} - {resp.text}"
221221

222222
# Post a signed document with filter ID
223-
resp = document.post("/index", filter={"id": {"eq": proposal_doc_id}})
223+
resp = document.post(filter={"id": {"eq": proposal_doc_id}})
224224
assert (
225225
resp.status_code == 200
226226
), f"Failed to post document: {resp.status_code} - {resp.text}"
@@ -320,7 +320,7 @@ def test_comment_doc(comment_doc_factory, rbac_chain_factory):
320320
), f"Failed to get document: {resp.status_code} - {resp.text}"
321321

322322
# Post a signed document with filter ID
323-
resp = document.post("/index", filter={"id": {"eq": comment_doc_id}})
323+
resp = document.post(filter={"id": {"eq": comment_doc_id}})
324324
assert (
325325
resp.status_code == 200
326326
), f"Failed to post document: {resp.status_code} - {resp.text}"
@@ -400,7 +400,6 @@ def test_submission_action(submission_action_factory, rbac_chain_factory):
400400

401401
# Post a signed document with filter ID
402402
resp = document.post(
403-
"/index",
404403
filter={"id": {"eq": submission_action_id}},
405404
)
406405
assert (
@@ -543,7 +542,8 @@ def test_document_index_endpoint(
543542
page = 0
544543
filter = {"id": {"eq": doc.metadata["id"]}}
545544
resp = document.post(
546-
f"/index?limit={limit}&page={page}",
545+
limit=limit,
546+
page=page,
547547
filter=filter,
548548
)
549549
assert (
@@ -557,7 +557,8 @@ def test_document_index_endpoint(
557557

558558
page += 1
559559
resp = document.post(
560-
f"/index?limit={limit}&page={page}",
560+
limit=limit,
561+
page=page,
561562
filter=filter,
562563
)
563564
assert (
@@ -569,7 +570,7 @@ def test_document_index_endpoint(
569570
assert data["page"]["remaining"] == total_amount - 1 - page
570571

571572
resp = document.post(
572-
f"/index?limit={total_amount}",
573+
limit=total_amount,
573574
filter=filter,
574575
)
575576
assert (
@@ -582,7 +583,7 @@ def test_document_index_endpoint(
582583

583584
# Pagination out of range
584585
resp = document.post(
585-
"/index?page=92233720368547759",
586+
page=92233720368547759,
586587
filter={},
587588
)
588589
assert (

0 commit comments

Comments
 (0)