tests: remove db modifications from individual tests

All db seeding now down in conftest
This commit is contained in:
Chris Milne 2026-06-12 11:29:42 +01:00
parent 8b89595531
commit 778f1dbece
8 changed files with 160 additions and 394 deletions

View file

@ -5,8 +5,6 @@
import pytest
from httpx import AsyncClient
from src.organisation.models import Organisation, OrgUsers
from src.user.models import User
from .conftest import generate_query_and_status
@ -26,15 +24,15 @@ async def test_get_org_success(default_client: AsyncClient):
assert isinstance(org, dict)
assert org["organisation_id"] == 1
assert org["name"] == "Test Org"
assert org["name"] == "Org One"
assert org["status"] == "approved"
assert org["root_user_email"] == "admin@test.com"
assert "intake_questionnaire" in org
assert isinstance(org["intake_questionnaire"], dict)
assert org["billing_contact"]["email"] == "billing@test.org"
assert org["owner_contact"]["email"] == "owner@test.org"
assert org["security_contact"]["email"] == "security@test.org"
assert org["billing_contact"]["email"] == "billing@orgone.com"
assert org["owner_contact"]["email"] == "owner@orgone.com"
assert org["security_contact"]["email"] == "security@orgone.com"
@pytest.mark.parametrize(
@ -62,7 +60,7 @@ async def test_post_org_success(default_client: AsyncClient):
@pytest.mark.parametrize(
"body, expected_status",
[
({"name": "Test Org"}, 409),
({"name": "Org One"}, 409),
({"name": 42}, 422),
({}, 422),
({"name": "New Test Org", "intake_questionnaire": {"question_one": 42}}, 422),
@ -78,16 +76,11 @@ async def test_post_org_status_checks(
@pytest.mark.anyio
async def test_patch_org_questionnaire_partial_success(
default_client: AsyncClient, db_session
):
org_model = db_session.get(Organisation, 1)
org_model.status = "partial"
db_session.flush()
async def test_patch_org_questionnaire_partial_success(default_client: AsyncClient):
resp = await default_client.patch(
"/org/questionnaire",
json={
"organisation_id": 1,
"organisation_id": 3,
"intake_questionnaire": {
"question_one": "new answer one",
"question_two": None,
@ -99,7 +92,7 @@ async def test_patch_org_questionnaire_partial_success(
data = resp.json()
assert resp.status_code == 200
assert data["name"] == "Test Org"
assert data["name"] == "Org Three"
assert data["status"] == "partial"
assert "intake_questionnaire" in data
assert isinstance(data["intake_questionnaire"], dict)
@ -116,7 +109,7 @@ async def test_patch_org_questionnaire_partial_success(
"body, expected_status",
[
({"organisation_id": 42}, 404),
({"organisation_id": "Test Org"}, 422),
({"organisation_id": "Org One"}, 422),
({"organisation_id": ""}, 422),
({}, 422),
(
@ -151,16 +144,11 @@ async def test_patch_questionnaire_partial_status_checks(
@pytest.mark.anyio
async def test_patch_org_questionnaire_submit_success(
default_client: AsyncClient, db_session
):
org_model = db_session.get(Organisation, 1)
org_model.status = "partial"
db_session.flush()
async def test_patch_org_questionnaire_submit_success(default_client: AsyncClient):
resp = await default_client.patch(
"/org/questionnaire",
json={
"organisation_id": 1,
"organisation_id": 3,
"intake_questionnaire": {
"question_one": "new answer one",
"question_two": None,
@ -172,7 +160,7 @@ async def test_patch_org_questionnaire_submit_success(
data = resp.json()
assert resp.status_code == 200
assert data["name"] == "Test Org"
assert data["name"] == "Org Three"
assert data["status"] == "submitted"
assert "intake_questionnaire" in data
assert isinstance(data["intake_questionnaire"], dict)
@ -196,7 +184,7 @@ async def test_patch_org_status_success(default_client: AsyncClient, status: str
data = resp.json()
assert resp.status_code == 200
assert data["name"] == "Test Org"
assert data["name"] == "Org One"
assert data["status"] == status
@ -204,7 +192,7 @@ async def test_patch_org_status_success(default_client: AsyncClient, status: str
"body, expected_status",
[
({"organisation_id": 42}, 404),
({"organisation_id": "Test Org"}, 422),
({"organisation_id": "Org One"}, 422),
({"organisation_id": ""}, 422),
({}, 422),
({"organisation_id": "1", "status": True}, 422),
@ -229,7 +217,7 @@ async def test_get_org_users_success(default_client: AsyncClient):
assert "users" in data
assert isinstance(data["users"], list)
assert len(data["users"]) == 1
assert len(data["users"]) == 2
user = data["users"][0]
assert isinstance(user, dict)
@ -237,7 +225,7 @@ async def test_get_org_users_success(default_client: AsyncClient):
assert user["id"] == 1
assert "organisation" in data
assert data["organisation"]["name"] == "Test Org"
assert data["organisation"]["name"] == "Org One"
assert data["organisation"]["id"] == 1
@ -254,19 +242,9 @@ async def test_get_org_users_status_checks(
@pytest.mark.anyio
async def test_post_org_user_success(default_client: AsyncClient, db_session):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
async def test_post_org_user_success(default_client: AsyncClient):
resp = await default_client.post(
"/org/user", json={"organisation_id": 1, "user_id": 2}
"/org/user", json={"organisation_id": 1, "user_id": 3}
)
assert resp.status_code == 200
@ -276,12 +254,12 @@ async def test_post_org_user_success(default_client: AsyncClient, db_session):
assert "organisation" in data
assert isinstance(data["organisation"], dict)
assert data["organisation"]["id"] == 1
assert data["organisation"]["name"] == "Test Org"
assert data["organisation"]["name"] == "Org One"
assert "users" in data
assert isinstance(data["users"], list)
assert (
len([user for user in data["users"] if user["email"] == "user@test.org"]) == 1
len([user for user in data["users"] if user["email"] == "root@orgtwo.com"]) == 1
)
@ -298,37 +276,15 @@ async def test_post_org_user_success(default_client: AsyncClient, db_session):
)
@pytest.mark.anyio
async def test_post_org_user_status_checks(
default_client: AsyncClient, body: dict[str, str], expected_status: int, db_session
default_client: AsyncClient, body: dict[str, str], expected_status: int
):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
resp = await default_client.post("/org/user", json=body)
assert resp.status_code == expected_status
@pytest.mark.anyio
async def test_patch_org_root_user_success(default_client: AsyncClient, db_session):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
db_session.add(OrgUsers(org_id=1, user_id=2))
db_session.flush()
async def test_patch_org_root_user_success(default_client: AsyncClient):
resp = await default_client.patch(
"/org/root_user", json={"organisation_id": 1, "user_id": 2}
)
@ -336,15 +292,15 @@ async def test_patch_org_root_user_success(default_client: AsyncClient, db_sessi
data = resp.json()
assert data["name"] == "Test Org"
assert data["root_user_email"] == "user@test.org"
assert data["name"] == "Org One"
assert data["root_user_email"] == "user@orgone.com"
@pytest.mark.parametrize(
"body, expected_status",
[
({"organisation_id": 42, "user_id": 2}, 404),
({"organisation_id": "Test Org", "user_id": 2}, 422),
({"organisation_id": "Org One", "user_id": 2}, 422),
({"organisation_id": "", "user_id": 2}, 422),
({}, 422),
({"user_id": 2}, 422),
@ -354,39 +310,17 @@ async def test_patch_org_root_user_success(default_client: AsyncClient, db_sessi
)
@pytest.mark.anyio
async def test_patch_root_user_status_checks(
default_client: AsyncClient, body: dict[str, str], expected_status: int, db_session
default_client: AsyncClient, body: dict[str, str], expected_status: int
):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
db_session.add(OrgUsers(org_id=1, user_id=2))
db_session.flush()
resp = await default_client.patch("/org/root_user", json=body)
assert resp.status_code == expected_status
@pytest.mark.anyio
async def test_patch_org_root_user_non_member(default_client: AsyncClient, db_session):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
async def test_patch_org_root_user_non_member(default_client: AsyncClient):
resp = await default_client.patch(
"/org/root_user", json={"organisation_id": 1, "user_id": 2}
"/org/root_user", json={"organisation_id": 1, "user_id": 3}
)
data = resp.json()
@ -404,14 +338,14 @@ async def test_get_org_groups_success(default_client: AsyncClient):
assert "organisation" in data
assert isinstance(data["organisation"], dict)
assert data["organisation"]["id"] == 1
assert data["organisation"]["name"] == "Test Org"
assert data["organisation"]["name"] == "Org One"
assert "groups" in data
assert isinstance(data["groups"], list)
group = data["groups"][0]
assert isinstance(group, dict)
assert group["id"] == 1
assert group["name"] == "Test Group"
assert group["name"] == "Org One Group"
@pytest.mark.parametrize(
@ -438,7 +372,7 @@ async def test_get_org_contact_success(default_client: AsyncClient, contact_type
assert "organisation" in data
assert data["organisation"]["id"] == 1
assert data["organisation"]["name"] == "Test Org"
assert data["organisation"]["name"] == "Org One"
attributes = [
"email",
@ -516,7 +450,7 @@ async def test_patch_org_contact_success(
assert "organisation" in data
assert data["organisation"]["id"] == 1
assert data["organisation"]["name"] == "Test Org"
assert data["organisation"]["name"] == "Org One"
attributes = [
"email",
@ -557,7 +491,7 @@ async def test_patch_org_contact_success(
({"organisation_id": 42, "contact_type": "billing"}, 404),
({"organisation_id": 1, "contact_type": "security"}, 200),
({"organisation_id": 1, "contact_type": "owner"}, 200),
({"organisation_id": "Test Org", "contact_type": "billing"}, 422),
({"organisation_id": "Org One", "contact_type": "billing"}, 422),
({"organisation_id": "", "contact_type": "billing"}, 422),
({}, 422),
({"organisation_id": 1, "contact_type": "not_real"}, 422),
@ -582,26 +516,14 @@ async def test_delete_org_success(default_client: AsyncClient):
@pytest.mark.anyio
async def test_delete_org_users_success(db_session, default_client: AsyncClient):
db_session.add(
User(
email="user@test.org",
first_name="User",
last_name="Test",
oidc_id="abcd-efgh-ijkl-1234",
)
)
db_session.flush()
async def test_delete_org_users_success(default_client: AsyncClient):
resp = await default_client.delete("/org/user?org_id=1&user_id=2")
assert resp.status_code == 204
@pytest.mark.anyio
async def test_delete_preapproval_org_success(db_session, default_client: AsyncClient):
org_model = db_session.get(Organisation, 1)
org_model.status = "partial"
db_session.flush()
resp = await default_client.delete("/org/self?org_id=1")
async def test_delete_preapproval_org_success(default_client: AsyncClient):
resp = await default_client.delete("/org/self?org_id=3")
assert resp.status_code == 204