Skip to content

Commit

Permalink
test(kong): finish unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 13, 2024
1 parent 8f7753b commit 486d431
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gateway/models/kong.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LinkProjectAnalysis(BaseModel):
class Disconnect(BaseModel):
"""Response from disconnecting a project from a datastore."""
removed_routes: list[str] | None
status: str | None
status: int | None = None


HttpMethodCode = constr(pattern=r"(GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|CONNECT|TRACE|CUSTOM)")
Expand Down
43 changes: 37 additions & 6 deletions gateway/routers/kong.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def list_data_stores_by_project(
async def delete_data_store(
data_store_name: Annotated[str, Path(description="Unique name of the data store.")]
):
"""List all the data stores connected to this project."""
"""Delete the listed data store."""
configuration = kong_admin_client.Configuration(host=kong_admin_url)

try:
Expand All @@ -114,7 +114,7 @@ async def delete_data_store(
)


@kong_router.put("/datastore", response_model=Service, status_code=status.HTTP_201_CREATED)
@kong_router.post("/datastore", response_model=Service, status_code=status.HTTP_201_CREATED)
async def create_data_store(data: Annotated[ServiceRequest, Body(
description="Required information for creating a new data store.",
title="Data store metadata."
Expand Down Expand Up @@ -153,7 +153,7 @@ async def create_data_store(data: Annotated[ServiceRequest, Body(
)


@kong_router.put("/datestore/project", response_model=LinkDataStoreProject, status_code=status.HTTP_202_ACCEPTED)
@kong_router.post("/datastore/project", response_model=LinkDataStoreProject)
async def connect_project_to_datastore(
data_store_id: Annotated[str, Body(description="UUID of the data store or 'gateway'")],
project_id: Annotated[str, Body(description="UUID of the project")],
Expand Down Expand Up @@ -332,10 +332,10 @@ async def disconnect_project(
)


@kong_router.put("/project/analysis", response_model=LinkProjectAnalysis, status_code=status.HTTP_202_ACCEPTED)
@kong_router.post("/project/analysis", response_model=LinkProjectAnalysis, status_code=status.HTTP_202_ACCEPTED)
async def connect_analysis_to_project(
project_id: Annotated[str, Body(description="UUID of the project")],
analysis_id: Annotated[str, Body(description="UUID of the data store or 'gateway'")],
project_id: Annotated[str, Body(description="UUID or name of the project")],
analysis_id: Annotated[str, Body(description="UUID or name of the analysis")],
):
"""Create a new analysis and link it to a project."""
configuration = kong_admin_client.Configuration(host=kong_admin_url)
Expand Down Expand Up @@ -430,3 +430,34 @@ async def connect_analysis_to_project(
)

return response


@kong_router.delete("/analysis/{analysis_id}", status_code=status.HTTP_200_OK)
async def delete_analysis(
analysis_id: Annotated[str, Path(description="UUID or unique name of the analysis.")]
):
"""Delete the listed analysis."""
configuration = kong_admin_client.Configuration(host=kong_admin_url)

try:
with kong_admin_client.ApiClient(configuration) as api_client:
api_instance = kong_admin_client.ConsumersApi(api_client)
api_instance.delete_consumer(consumer_username_or_id=analysis_id)

logger.info(f"Analysis {analysis_id} deleted")

return status.HTTP_200_OK

except ApiException as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
headers={"WWW-Authenticate": "Bearer"},
)

except Exception as e:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Service error: {e}",
headers={"WWW-Authenticate": "Bearer"},
)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def setup_kong(test_client):
"protocols": ["http"],
}

test_client.put("/datastore", auth=fakeauth, json=test_datastore)
test_client.put("/datastore/project", auth=fakeauth, json=test_project_link)
test_client.post("/datastore", auth=fakeauth, json=test_datastore)
test_client.post("/datastore/project", auth=fakeauth, json=test_project_link)

yield

Expand Down
76 changes: 75 additions & 1 deletion tests/test_kong.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,84 @@ def test_list_data_stores_by_project(self, test_client, setup_kong):

json_data = r.json()
data = json_data["data"]

assert len(data) == 1 # should only be one named this

data_store = data[0]

assert data_store["protocols"] == ["http"]
assert data_store["name"] == KONG_TEST_DS
assert data_store["name"] == KONG_TEST_PROJECT
assert data_store["methods"] == ["GET", "POST", "PUT", "DELETE"]

def test_create_delete_data_store(self, test_client, setup_kong):
"""Test the create_data_store and delete_data_store methods."""
test_ds_name = "theWWW"
new_ds = {
"name": test_ds_name,
"port": 443,
"protocol": "http",
"host": "earth",
"path": "/cloud",
}
r = test_client.post("/datastore", auth=fakeauth, json=new_ds)
assert r.status_code == status.HTTP_201_CREATED

new_service = r.json()
for k, v in new_ds.items():
assert new_service[k] == v

d = test_client.delete(f"/datastore/{test_ds_name}", auth=fakeauth)
assert d.status_code == status.HTTP_200_OK

def test_connect_disconnect_project_to_datastore(self, test_client, setup_kong):
"""Test the connect_project_to_datastore and disconnect_project methods."""
test_project_name = "Manhattan"
proj_specs = {
"data_store_id": KONG_TEST_DS,
"project_id": test_project_name,
"methods": [
"GET",
"POST",
"PUT",
"DELETE"
],
"protocols": [
"http"
],
"ds_type": "fhir"
}
r = test_client.post("/datastore/project", auth=fakeauth, json=proj_specs)
assert r.status_code == status.HTTP_200_OK
link_data = r.json()

expected_keys = {"route", "keyauth", "acl"}
found_keys = [key in expected_keys for key in link_data.keys()]
assert all(found_keys)
assert link_data["route"]["name"] == test_project_name

d = test_client.put(f"/disconnect/{test_project_name}", auth=fakeauth)
assert d.status_code == status.HTTP_200_OK

removed_routes = d.json()["removed_routes"]
assert len(removed_routes) == 1

def test_connect_delete_analysis_to_project(self, test_client, setup_kong):
"""Test the connect_analysis_to_project method."""
test_analysis = "datalore"
analysis_request = {
"project_id": KONG_TEST_PROJECT,
"analysis_id": test_analysis,
}
r = test_client.post("/project/analysis", auth=fakeauth, json=analysis_request)
assert r.status_code == status.HTTP_202_ACCEPTED

link_data = r.json()

expected_keys = {"consumer", "keyauth", "acl"}
found_keys = [key in expected_keys for key in link_data.keys()]
assert all(found_keys)
assert link_data["consumer"]["username"] == test_analysis
assert link_data["consumer"]["tags"] == [KONG_TEST_PROJECT]

d = test_client.delete(f"/analysis/{test_analysis}", auth=fakeauth)
assert d.status_code == status.HTTP_200_OK

0 comments on commit 486d431

Please sign in to comment.