Skip to content

Commit

Permalink
test(po): add unittests for podorc
Browse files Browse the repository at this point in the history
  • Loading branch information
brucetony committed Mar 28, 2024
1 parent b424e8b commit b389bbe
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gateway/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Settings(BaseModel):

# UI ID and secret
API_CLIENT_ID: str = os.getenv("API_CLIENT_ID", "api-client")
API_CLIENT_SECRET: str = os.getenv("API_CLIENT_SECRET")
API_CLIENT_SECRET: str = os.getenv("API_CLIENT_SECRET") # Not used currently

# Hub
HUB_AUTH_SERVICE_URL: str = os.getenv("HUB_AUTH_SERVICE_URL", "https://auth.privateaim.net")
Expand Down
4 changes: 3 additions & 1 deletion gateway/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ async def make_request(
A dictionary-like object defining the payload
files : dict | Nones
For passing on uploaded files. Should be packaged using the same form param and the read bytes
file_response : bool
Whether a file or stream data is expected as the response. Defaults to False
Returns
-------
Expand All @@ -59,7 +61,7 @@ async def make_request(
files = {}

async with httpx.AsyncClient(headers=headers) as client:
r = await client.request(url=url, method=method, params=query, data=data, files=files, follow_redirects=True)
r = await client.request(url=url, method=method, params=query, json=data, files=files, follow_redirects=True)
r.raise_for_status()

if file_response:
Expand Down
1 change: 1 addition & 0 deletions gateway/models/podorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import BaseModel


# Fake models for frontend
class ContainerData(BaseModel):
"""Formatted container information."""
id: UUID
Expand Down
8 changes: 5 additions & 3 deletions gateway/routers/podorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from typing import Annotated

from fastapi import APIRouter, Path, Security
from fastapi import APIRouter, Path, Body, Security
from starlette import status
from starlette.requests import Request
from starlette.responses import Response
Expand All @@ -21,14 +21,16 @@

@route(
request_method=po_router.post,
path="/po/{analysis_id}",
path="/po",
status_code=status.HTTP_200_OK,
service_url=gateway_settings.PODORC_SERVICE_URL,
body_params=["analysis_id", "project_id"],
)
async def create_analysis(
request: Request,
response: Response,
analysis_id: Annotated[str | None, Path(description="UUID of the analysis.")],
analysis_id: Annotated[str, Body(description="UUID of the analysis.")],
project_id: Annotated[str, Body(description="UUID of the analysis.")],
):
"""Get the logs for a specific analysis run."""
pass
Expand Down
6 changes: 3 additions & 3 deletions gateway/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import requests
import uvicorn
from fastapi import FastAPI, HTTPException, Query
from fastapi import FastAPI, HTTPException, Form
from starlette import status
from starlette.middleware.cors import CORSMiddleware

Expand Down Expand Up @@ -101,8 +101,8 @@ def get_health() -> HealthCheck:
response_model=Token,
)
def get_token(
username: Annotated[str, Query(description="Keycloak username")],
password: Annotated[str, Query(description="Keycloak password")],
username: Annotated[str, Form(description="Keycloak username")],
password: Annotated[str, Form(description="Keycloak password")],
) -> Token:
"""Get a JWT from the IDP by passing a valid username and password. This token can then be used to authenticate
yourself with this API."""
Expand Down
39 changes: 29 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"""Test FastAPI app instance."""
import os
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer

import pytest
import requests
from dotenv import load_dotenv
from fastapi.testclient import TestClient
from starlette import status

from gateway.conf import gateway_settings
from gateway.server import app
from tests.constants import KONG_TEST_DS, KONG_TEST_PROJECT
from tests.constants import TEST_DS, TEST_PROJECT, TEST_ANALYSIS
from tests.pseudo_auth import get_oid_test_jwk, BearerAuth, fakeauth


Expand Down Expand Up @@ -48,11 +50,11 @@ def hub_token() -> BearerAuth:
"""Create an endpoint by which to test the valid JWKS."""
load_dotenv(dotenv_path="../env/.env.dev")
# TODO: replace with robot account
HUB_USERNAME, HUB_PASSWORD = os.getenv("HUB_USERNAME"), os.getenv("HUB_PASSWORD")
HUB_AUTH_API = gateway_settings.HUB_AUTH_SERVICE_URL
HUB_TOKEN_EP = HUB_AUTH_API + "/token"
hub_username, hub_password = os.getenv("HUB_USERNAME"), os.getenv("HUB_PASSWORD")
hub_auth_api = gateway_settings.HUB_AUTH_SERVICE_URL
hub_token_ep = hub_auth_api + "/token"

resp = requests.post(HUB_TOKEN_EP, data={"username": HUB_USERNAME, "password": HUB_PASSWORD})
resp = requests.post(hub_token_ep, data={"username": hub_username, "password": hub_password})
assert resp.ok

token = resp.json()["access_token"]
Expand All @@ -65,15 +67,15 @@ def hub_token() -> BearerAuth:
def setup_kong(test_client):
"""Setup Kong instance with test data."""
test_datastore = {
"name": KONG_TEST_DS,
"name": TEST_DS,
"protocol": "http",
"host": "server.fire.ly",
"port": 80,
"path": "/mydefinedpath",
}
test_project_link = {
"data_store_id": KONG_TEST_DS,
"project_id": KONG_TEST_PROJECT,
"data_store_id": TEST_DS,
"project_id": TEST_PROJECT,
"methods": [
"GET",
"POST",
Expand All @@ -89,5 +91,22 @@ def setup_kong(test_client):

yield

test_client.put(f"/disconnect/{KONG_TEST_PROJECT}", auth=fakeauth)
test_client.delete(f"/datastore/{KONG_TEST_DS}", auth=fakeauth)
test_client.put(f"/disconnect/{TEST_PROJECT}", auth=fakeauth)
test_client.delete(f"/datastore/{TEST_DS}", auth=fakeauth)


@pytest.fixture(scope="module")
def setup_po(test_client):
"""Setup pod orchestrator instance with test data."""
test_pod = {
"analysis_id": TEST_ANALYSIS,
"project_id": TEST_PROJECT,
}

r = test_client.post("/po", auth=fakeauth, json=test_pod)
assert r.status_code == status.HTTP_200_OK
time.sleep(2) # Need time for k8s

yield

test_client.delete(f"/po/{TEST_ANALYSIS}/delete", auth=fakeauth)
5 changes: 3 additions & 2 deletions tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""String constants for tests."""
KONG_TEST_DS = "unitTestDataStore"
KONG_TEST_PROJECT = "unitTestProject"
TEST_DS = "unitTestDataStore"
TEST_PROJECT = "unittestproject" # Must be all lower case for podorc unittests
TEST_ANALYSIS = "unittestanalysis" # Must be all lower case for podorc unittests
14 changes: 7 additions & 7 deletions tests/test_kong.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Unit tests for the kong endpoints."""
from starlette import status

from tests.constants import KONG_TEST_DS, KONG_TEST_PROJECT
from tests.constants import TEST_DS, TEST_PROJECT
from tests.pseudo_auth import fakeauth


Expand All @@ -21,11 +21,11 @@ def test_list_data_stores(self, test_client, setup_kong):
assert len(data) > 0 # minimum 1

data_store_names = [ds["name"] for ds in data]
assert KONG_TEST_DS in data_store_names
assert TEST_DS in data_store_names

def test_list_data_stores_by_project(self, test_client, setup_kong):
"""Test the list_data_stores_by_project method."""
r = test_client.get(f"/datastore/{KONG_TEST_PROJECT}", auth=fakeauth)
r = test_client.get(f"/datastore/{TEST_PROJECT}", auth=fakeauth)
assert r.status_code == status.HTTP_200_OK

json_data = r.json()
Expand All @@ -36,7 +36,7 @@ def test_list_data_stores_by_project(self, test_client, setup_kong):
data_store = data[0]

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

def test_create_delete_data_store(self, test_client, setup_kong):
Expand All @@ -63,7 +63,7 @@ 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,
"data_store_id": TEST_DS,
"project_id": test_project_name,
"methods": [
"GET",
Expand Down Expand Up @@ -95,7 +95,7 @@ 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,
"project_id": TEST_PROJECT,
"analysis_id": test_analysis,
}
r = test_client.post("/project/analysis", auth=fakeauth, json=analysis_request)
Expand All @@ -107,7 +107,7 @@ def test_connect_delete_analysis_to_project(self, test_client, setup_kong):
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]
assert link_data["consumer"]["tags"] == [TEST_PROJECT]

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

0 comments on commit b389bbe

Please sign in to comment.