-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
56 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |