aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/http/__init__.py0
-rw-r--r--tests/http/conftest.py50
-rw-r--r--tests/http/test_basic.py35
-rw-r--r--tests/http/test_work.py24
4 files changed, 109 insertions, 0 deletions
diff --git a/tests/http/__init__.py b/tests/http/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/http/__init__.py
diff --git a/tests/http/conftest.py b/tests/http/conftest.py
new file mode 100644
index 0000000..200bf1c
--- /dev/null
+++ b/tests/http/conftest.py
@@ -0,0 +1,50 @@
+import httpx
+import pytest
+import requests_mock
+from asgi_lifespan import LifespanManager
+from httpx import AsyncClient, ASGITransport
+
+from jb.main import app
+
+
+@pytest.fixture(scope="session")
+def anyio_backend():
+ return "asyncio"
+
+
+@pytest.fixture(scope="session")
+async def httpxclient():
+ # limiter.enabled = True
+ # limiter.reset()
+ app.testing = True
+
+ async with LifespanManager(app):
+ # await FastAPICache.clear()
+ transport = ASGITransport(app=app)
+ async with AsyncClient(
+ transport=transport, base_url="http://127.0.0.1:8001/"
+ ) as client:
+ yield client
+ await client.aclose()
+
+
+@pytest.fixture()
+def no_limit():
+ """Fixture to execute asserts before and after a test is run"""
+ # limiter.enabled = False
+ yield # this is where the testing happens
+ # limiter.enabled = True
+
+
+@pytest.fixture()
+def httpxclient_ip(httpxclient):
+ """Fixture to execute asserts before and after a test is run"""
+ httpxclient._transport = httpx.ASGITransport(app=app, client=("1.2.3.4", 8001))
+ yield httpxclient # this is where the testing happens
+ httpxclient._transport = httpx.ASGITransport(app=app)
+
+
+@pytest.fixture
+def mock_requests():
+ with requests_mock.Mocker() as m:
+ yield m
diff --git a/tests/http/test_basic.py b/tests/http/test_basic.py
new file mode 100644
index 0000000..7b03a1e
--- /dev/null
+++ b/tests/http/test_basic.py
@@ -0,0 +1,35 @@
+import pytest
+from httpx import AsyncClient
+
+
+@pytest.mark.anyio
+async def test_base(httpxclient: AsyncClient):
+ client = httpxclient
+ res = await client.get("/")
+ # actually returns 404. old test expects 401. idk what is should be
+ print(res.text)
+ # assert res.status_code == 404
+ assert res.status_code == 200
+
+
+@pytest.mark.anyio
+async def test_static_file_alias(httpxclient: AsyncClient):
+ client = httpxclient
+ """
+ These are here for site crawlers and stuff..
+ """
+ for p in ["/robots.txt", "/sitemap.xml", "/favicon.ico"]:
+ res = await client.get(p)
+ assert res.status_code == 200, p
+ assert res.json() == {}
+
+
+@pytest.mark.anyio
+async def test_health(httpxclient: AsyncClient):
+ client = httpxclient
+ """
+ These are here for site crawlers and stuff..
+ """
+ res = await client.get("/health/")
+ assert res.status_code == 200
+ assert res.json() == {}
diff --git a/tests/http/test_work.py b/tests/http/test_work.py
new file mode 100644
index 0000000..59b8830
--- /dev/null
+++ b/tests/http/test_work.py
@@ -0,0 +1,24 @@
+import pytest
+from httpx import AsyncClient
+
+from jb.models.hit import Hit
+
+
+@pytest.mark.skip(reason="hits live api, need to look at this")
+async def test_work(
+ httpxclient: AsyncClient,
+ no_limit,
+ amt_worker_id,
+ amt_assignment_id,
+ hit_in_amt: Hit,
+):
+ client = httpxclient
+ params = {
+ "workerId": amt_worker_id,
+ "assignmentId": amt_assignment_id,
+ "hitId": hit_in_amt.amt_hit_id,
+ }
+ res = await client.get(f"/work/", params=params)
+ assert res.status_code == 200
+ # the response is an html page
+ assert res.headers["content-type"] == "text/html; charset=utf-8"