Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon committed Aug 22, 2023
1 parent 0aa4683 commit 67a002a
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions backend/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,76 @@
import typing

import pytest
from app.main import app
from app import main
from requests_mock import Mocker
from starlette.testclient import TestClient

client = TestClient(app)
#: Host name to use for the mocked backend.
MOCKED_BACKEND_HOST = "mocked-backend"

#: A "token" to be used in test URLs, does not carry a meaning as it is mocked.
MOCKED_URL_TOKEN = "xXTeStXxx"

#: FastAPI/startlette test client.
client = TestClient(main.app)


@pytest.fixture
def mock_get():
with Mocker() as m:
yield m.get
def non_mocked_hosts() -> typing.List[str]:
"""List of hosts that should not be mocked.
We read the host from ``client``.
"""
return [client._base_url.host]


def test_proxy_annonars(mock_get, monkeypatch):
monkeypatch.setenv("REEV_BACKEND_PREFIX_ANNONARS", "http://mocked-backend")
@pytest.mark.asyncio
async def test_proxy_annonars(monkeypatch, httpx_mock):
"""Test proxying to annonars backend."""
monkeypatch.setattr(main, "BACKEND_PREFIX_ANNONARS", f"http://{MOCKED_BACKEND_HOST}")
httpx_mock.add_response(
url=f"http://{MOCKED_BACKEND_HOST}/annos/{MOCKED_URL_TOKEN}",
method="GET",
text="Mocked response",
)

mock_get("http://mocked-backend/annos/some-resource", text="Mocked response")
response = client.get("/proxy/annonars/some-resource")
assert response.status_code == 404
response = client.get(f"/proxy/annonars/{MOCKED_URL_TOKEN}")
assert response.status_code == 200
assert response.text == "Mocked response"


def test_proxy_mehari(mock_get, monkeypatch):
monkeypatch.setenv("REEV_BACKEND_PREFIX_MEHARI", "http://mocked-backend")
@pytest.mark.asyncio
async def test_proxy_mehari(monkeypatch, httpx_mock):
"""Test proxying to mehari backend."""
monkeypatch.setattr(main, "BACKEND_PREFIX_MEHARI", f"http://{MOCKED_BACKEND_HOST}")
httpx_mock.add_response(
url=f"http://{MOCKED_BACKEND_HOST}/{MOCKED_URL_TOKEN}",
method="GET",
text="Mocked response",
)

mock_get("http://mocked-backend/some-resource", text="Mocked response")
response = client.get("/proxy/mehari/some-resource")
assert response.status_code == 404
response = client.get(f"/proxy/mehari/{MOCKED_URL_TOKEN}")
assert response.status_code == 200
assert response.text == "Mocked response"


def test_proxy_viguno(mock_get, monkeypatch):
monkeypatch.setenv("REEV_BACKEND_PREFIX_VIGUNO", "http://mocked-backend")
@pytest.mark.asyncio
async def test_proxy_viguno(monkeypatch, httpx_mock):
"""Test proxying to viguno backend."""
monkeypatch.setattr(main, "BACKEND_PREFIX_VIGUNO", f"http://{MOCKED_BACKEND_HOST}")
httpx_mock.add_response(
url=f"http://{MOCKED_BACKEND_HOST}/{MOCKED_URL_TOKEN}",
method="GET",
text="Mocked response",
)

mock_get("http://mocked-backend/some-resource", text="Mocked response")
response = client.get("/proxy/viguno/some-resource")
assert response.status_code == 404
response = client.get(f"/proxy/viguno/{MOCKED_URL_TOKEN}")
assert response.status_code == 200
assert response.text == "Mocked response"


def test_invalid_proxy_route():
@pytest.mark.asyncio
async def test_invalid_proxy_route(monkeypatch, httpx_mock):
"""Test invalid proxy route."""
response = client.get("/proxy/some-other-path")
assert response.status_code == 404
assert response.content == b"Reverse proxy route not found"
assert response.text == "Reverse proxy route not found"

0 comments on commit 67a002a

Please sign in to comment.