205 lines
5.6 KiB
Python
205 lines
5.6 KiB
Python
"""
|
|
[GET]/user/self/claims is not tested because it requires OIDC authentication.
|
|
[DELETE/user/ is not tested because the testing client cannot attach a body to a delete request.
|
|
"""
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
from fastapi.routing import APIRoute
|
|
|
|
from .conftest import generate_query_and_status
|
|
|
|
|
|
pytestmark = [
|
|
pytest.mark.user_module,
|
|
]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_self_db_success(default_client: AsyncClient):
|
|
resp = await default_client.get("/user/self/db")
|
|
data = resp.json()
|
|
|
|
assert resp.status_code == 200
|
|
assert data["first_name"] == "Admin"
|
|
assert data["last_name"] == "Test"
|
|
assert data["email"] == "admin@test.com"
|
|
assert "organisations" in data
|
|
assert isinstance(data["organisations"], list)
|
|
assert "groups" in data
|
|
assert isinstance(data["groups"], dict)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_user_success(default_client: AsyncClient):
|
|
resp = await default_client.get("/user/?user_id=1")
|
|
data = resp.json()
|
|
|
|
assert resp.status_code == 200
|
|
assert data["first_name"] == "Admin"
|
|
assert data["last_name"] == "Test"
|
|
assert data["email"] == "admin@test.com"
|
|
assert "organisations" in data
|
|
assert isinstance(data["organisations"], list)
|
|
assert "groups" in data
|
|
assert isinstance(data["groups"], dict)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.parametrize(
|
|
"query, expected_status", generate_query_and_status(["user_id"])
|
|
)
|
|
async def test_get_user_status_checks(
|
|
default_client: AsyncClient, query: str, expected_status: int
|
|
):
|
|
resp = await default_client.get(f"/user/?{query}")
|
|
|
|
assert resp.status_code == expected_status
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_delete_user_success(default_client: AsyncClient):
|
|
resp = await default_client.delete("/user/?user_id=1")
|
|
|
|
assert resp.status_code == 204
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_post_user_invitation_success(default_client: AsyncClient):
|
|
body = {"user_email": "admin@test.com", "organisation_id": 1}
|
|
resp = await default_client.post("/user/invitation", json=body)
|
|
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert "organisation" in data
|
|
assert isinstance(data["organisation"], dict)
|
|
assert data["organisation"]["id"] == 1
|
|
assert data["organisation"]["name"] == "Test Org"
|
|
|
|
assert "invited_email" in data
|
|
assert isinstance(data["invited_email"], str)
|
|
assert data["invited_email"] == "admin@test.com"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body, expected_status",
|
|
[
|
|
({"organisation_id": 42, "user_email": "admin@test.com"}, 404),
|
|
({"organisation_id": "Test Org", "user_email": "admin@test.com"}, 422),
|
|
({"organisation_id": "", "user_email": "admin@test.com"}, 422),
|
|
({}, 422),
|
|
({"user_email": 42}, 422),
|
|
({"organisation_id": 1, "user_email": "Test User"}, 422),
|
|
],
|
|
)
|
|
@pytest.mark.anyio
|
|
async def test_post_user_invitation_status_checks(
|
|
default_client: AsyncClient, body, expected_status
|
|
):
|
|
resp = await default_client.post("/user/invitation", json=body)
|
|
|
|
assert resp.status_code == expected_status
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body, expected_status",
|
|
[
|
|
({"jwt": "invalid"}, 401),
|
|
({"jwt": ""}, 401),
|
|
({"jwt": None}, 422),
|
|
({"jwt": 42}, 422),
|
|
],
|
|
)
|
|
@pytest.mark.anyio
|
|
async def test_post_user_invitation_accept_status_checks(
|
|
default_client: AsyncClient, body, expected_status
|
|
):
|
|
resp = await default_client.post("/user/invitation/accept", json=body)
|
|
|
|
assert resp.status_code == expected_status
|
|
|
|
if resp.status_code == 401:
|
|
assert resp.json()["detail"] == "Invalid JWS"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_self_orgs_success(default_client: AsyncClient):
|
|
resp = await default_client.get("/user/self/orgs")
|
|
assert resp.status_code == 200
|
|
|
|
data = resp.json()
|
|
|
|
assert "organisations" in data
|
|
assert isinstance(data["organisations"], list)
|
|
assert len(data["organisations"]) > 0
|
|
|
|
org = data["organisations"][0]
|
|
assert org["organisation_id"] == 1
|
|
assert org["name"] == "Test Org"
|
|
assert org["status"] == "approved"
|
|
assert org["root_user_email"] == "admin@test.com"
|
|
assert "intake_questionnaire" in org
|
|
assert isinstance(org["intake_questionnaire"], dict)
|
|
|
|
assert isinstance(org["billing_contact"], dict)
|
|
assert org["billing_contact"]["email"] == "billing@test.org"
|
|
assert org["billing_contact"]["id"] == 1
|
|
|
|
assert isinstance(org["owner_contact"], dict)
|
|
assert org["owner_contact"]["email"] == "owner@test.org"
|
|
assert org["owner_contact"]["id"] == 2
|
|
|
|
assert isinstance(org["security_contact"], dict)
|
|
assert org["security_contact"]["email"] == "security@test.org"
|
|
assert org["security_contact"]["id"] == 3
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_self_orgs_dynamic(default_client: AsyncClient):
|
|
method = "GET"
|
|
path = "/user/self/orgs"
|
|
expected_data = {
|
|
"organisations": [
|
|
{
|
|
"organisation_id": 1,
|
|
"name": "Test Org",
|
|
"status": "approved",
|
|
"root_user_email": "admin@test.com",
|
|
"owner_contact": {"email": "owner@test.org", "id": 2},
|
|
"security_contact": {"email": "security@test.org", "id": 3},
|
|
"billing_contact": {"email": "billing@test.org", "id": 1},
|
|
"intake_questionnaire": {
|
|
"questions": {
|
|
"question_one": None,
|
|
"question_three": None,
|
|
"question_two": "answer two",
|
|
},
|
|
"metadata": {"version": 0, "submission_date": None},
|
|
},
|
|
}
|
|
]
|
|
}
|
|
|
|
resp = await default_client.get(path)
|
|
|
|
route = next(
|
|
route
|
|
for route in default_client._transport.app.routes
|
|
if isinstance(route, APIRoute)
|
|
and path in route.path
|
|
and method in route.methods
|
|
)
|
|
|
|
assert resp.status_code == route.status_code
|
|
if route.status_code == 204:
|
|
return
|
|
|
|
expected_response_schema = route.response_model
|
|
data = resp.json()
|
|
|
|
response_model = expected_response_schema(**data)
|
|
assert isinstance(response_model, expected_response_schema)
|
|
|
|
expected_response_model = expected_response_schema(**expected_data)
|
|
|
|
assert response_model == expected_response_model
|