forked from sr2/cloud-api
tests: remove db modifications from individual tests
All db seeding now down in conftest
This commit is contained in:
parent
8b89595531
commit
778f1dbece
8 changed files with 160 additions and 394 deletions
113
test/test_iam.py
113
test/test_iam.py
|
|
@ -3,10 +3,6 @@
|
|||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
from src.user.models import User
|
||||
from src.organisation.models import Organisation as Org, OrgUsers
|
||||
from src.iam.models import Group
|
||||
|
||||
from .conftest import generate_query_and_status
|
||||
|
||||
|
||||
|
|
@ -20,7 +16,7 @@ async def test_post_act_on_resource_endpoint_success(default_client: AsyncClient
|
|||
body = {
|
||||
"rn": {
|
||||
"service": "Test Service",
|
||||
"organisation": "Test Org",
|
||||
"organisation": "Org One",
|
||||
"resource": "test_resource",
|
||||
"instance": None,
|
||||
},
|
||||
|
|
@ -45,7 +41,7 @@ async def test_post_act_on_resource_endpoint_success(default_client: AsyncClient
|
|||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_act_on_resource_wrong_key(
|
||||
default_client: AsyncClient, db_session, service: str, api_key: str
|
||||
default_client: AsyncClient, service: str, api_key: str
|
||||
):
|
||||
body = {
|
||||
"rn": {
|
||||
|
|
@ -118,16 +114,15 @@ async def test_act_on_resource_endpoint_status_checks(
|
|||
@pytest.mark.parametrize(
|
||||
"service, org, resource, action, expected_response",
|
||||
[
|
||||
("Test Service", "Test Org", "test_resource", "read", True),
|
||||
("Test Service", "Test Org", "test_resource", "create", False),
|
||||
("Test Service", "Test Org", "no_access_here", "read", False),
|
||||
("Test Service", "Test Org Two", "test_resource", "read", False),
|
||||
("Test Service", "Org One", "test_resource", "read", True),
|
||||
("Test Service", "Org One", "test_resource", "create", False),
|
||||
("Test Service", "Org One", "no_access_here", "read", False),
|
||||
("Test Service", "Org Two", "test_resource", "read", False),
|
||||
],
|
||||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_act_on_resource_logic(
|
||||
default_client: AsyncClient,
|
||||
db_session,
|
||||
service,
|
||||
org,
|
||||
resource,
|
||||
|
|
@ -173,7 +168,7 @@ async def test_get_group_permissions_success(default_client: AsyncClient):
|
|||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_get_group_permissions_status_checks(
|
||||
default_client: AsyncClient, db_session, query: str, expected_status: int
|
||||
default_client: AsyncClient, query: str, expected_status: int
|
||||
):
|
||||
resp = await default_client.get(f"/iam/group/permissions?{query}")
|
||||
|
||||
|
|
@ -188,21 +183,7 @@ async def test_get_group_permissions_status_checks(
|
|||
],
|
||||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_get_group_permissions_mismatch(
|
||||
default_client: AsyncClient, db_session, query: str
|
||||
):
|
||||
db_session.add(
|
||||
Org(
|
||||
name="Another Test Org",
|
||||
root_user_id=1,
|
||||
billing_contact_id=1,
|
||||
owner_contact_id=2,
|
||||
security_contact_id=3,
|
||||
status="approved",
|
||||
)
|
||||
)
|
||||
db_session.add(Group(name="Another Test Group", org_id=2))
|
||||
db_session.flush()
|
||||
async def test_get_group_permissions_mismatch(default_client: AsyncClient, query: str):
|
||||
resp = await default_client.get(f"/iam/group/permissions?{query}")
|
||||
|
||||
assert resp.status_code == 403
|
||||
|
|
@ -226,12 +207,12 @@ async def test_get_group_users_success(default_client: AsyncClient):
|
|||
assert "group" in data
|
||||
assert isinstance(data["group"], dict)
|
||||
assert data["group"]["id"] == 1
|
||||
assert data["group"]["name"] == "Test Group"
|
||||
assert data["group"]["name"] == "Org One Group"
|
||||
|
||||
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"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -254,21 +235,7 @@ async def test_get_group_users_status_checks(
|
|||
],
|
||||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_get_group_users_mismatch(
|
||||
default_client: AsyncClient, db_session, query: str
|
||||
):
|
||||
db_session.add(
|
||||
Org(
|
||||
name="Another Test Org",
|
||||
root_user_id=1,
|
||||
billing_contact_id=1,
|
||||
owner_contact_id=2,
|
||||
security_contact_id=3,
|
||||
status="approved",
|
||||
)
|
||||
)
|
||||
db_session.add(Group(name="Another Test Group", org_id=2))
|
||||
db_session.flush()
|
||||
async def test_get_group_users_mismatch(default_client: AsyncClient, query: str):
|
||||
resp = await default_client.get(f"/iam/group/users?{query}")
|
||||
|
||||
assert resp.status_code == 403
|
||||
|
|
@ -287,15 +254,15 @@ async def test_post_group_success(default_client: AsyncClient):
|
|||
assert "group" in data
|
||||
assert isinstance(data["group"], dict)
|
||||
assert data["group"]["name"] == "New Group"
|
||||
assert data["group"]["id"] == 2
|
||||
assert data["group"]["id"] == 4
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"body, expected_status",
|
||||
[
|
||||
({"organisation_id": 1, "name": "Test Group"}, 409),
|
||||
({"organisation_id": 1, "name": "Org One Group"}, 409),
|
||||
(
|
||||
{"organisation_id": 2, "name": "new group"},
|
||||
{"organisation_id": 42, "name": "new group"},
|
||||
404,
|
||||
), # Non-existent organisation, valid name
|
||||
(
|
||||
|
|
@ -329,12 +296,10 @@ async def test_post_group_status_checks(
|
|||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_put_group_perm_success(default_client: AsyncClient, db_session):
|
||||
db_session.add(Group(name="Test Group Two", org_id=1))
|
||||
db_session.flush()
|
||||
async def test_put_group_perm_success(default_client: AsyncClient):
|
||||
resp = await default_client.put(
|
||||
"/iam/group/permission",
|
||||
json={"permission_id": 1, "group_id": 2, "organisation_id": 1},
|
||||
json={"permission_id": 1, "group_id": 3, "organisation_id": 1},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
|
||||
|
|
@ -342,8 +307,8 @@ async def test_put_group_perm_success(default_client: AsyncClient, db_session):
|
|||
|
||||
assert "group" in data
|
||||
assert isinstance(data["group"], dict)
|
||||
assert data["group"]["name"] == "Test Group Two"
|
||||
assert data["group"]["id"] == 2
|
||||
assert data["group"]["name"] == "Org One Group Two"
|
||||
assert data["group"]["id"] == 3
|
||||
|
||||
assert "permissions" in data
|
||||
assert isinstance(data["permissions"], list)
|
||||
|
|
@ -436,21 +401,7 @@ async def test_put_group_perm_status_checks(
|
|||
],
|
||||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_put_group_perm_mismatch(
|
||||
default_client: AsyncClient, db_session, body: dict
|
||||
):
|
||||
db_session.add(
|
||||
Org(
|
||||
name="Another Test Org",
|
||||
root_user_id=1,
|
||||
billing_contact_id=1,
|
||||
owner_contact_id=2,
|
||||
security_contact_id=3,
|
||||
status="approved",
|
||||
)
|
||||
)
|
||||
db_session.add(Group(name="Another Test Group", org_id=2))
|
||||
db_session.flush()
|
||||
async def test_put_group_perm_mismatch(default_client: AsyncClient, body: dict):
|
||||
resp = await default_client.put("/iam/group/permission", json=body)
|
||||
|
||||
assert resp.status_code == 403
|
||||
|
|
@ -458,19 +409,7 @@ async def test_put_group_perm_mismatch(
|
|||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_put_group_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(user_id=2, org_id=1))
|
||||
db_session.flush()
|
||||
|
||||
async def test_put_group_user_success(default_client: AsyncClient):
|
||||
resp = await default_client.put(
|
||||
"/iam/group/user", json={"user_id": 2, "group_id": 1, "organisation_id": 1}
|
||||
)
|
||||
|
|
@ -480,7 +419,7 @@ async def test_put_group_user_success(default_client: AsyncClient, db_session):
|
|||
|
||||
assert "group" in data
|
||||
assert isinstance(data["group"], dict)
|
||||
assert data["group"]["name"] == "Test Group"
|
||||
assert data["group"]["name"] == "Org One Group"
|
||||
assert data["group"]["id"] == 1
|
||||
|
||||
assert "users" in data
|
||||
|
|
@ -490,7 +429,7 @@ async def test_put_group_user_success(default_client: AsyncClient, db_session):
|
|||
assert user["id"] == 2
|
||||
assert user["first_name"] == "User"
|
||||
assert user["last_name"] == "Test"
|
||||
assert user["email"] == "user@test.org"
|
||||
assert user["email"] == "user@orgone.com"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -582,7 +521,7 @@ async def test_get_permissions_status_checks(
|
|||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_post_perm_success(default_client: AsyncClient, db_session):
|
||||
async def test_post_perm_success(default_client: AsyncClient):
|
||||
resp = await default_client.post(
|
||||
"/iam/permission",
|
||||
json={"service_id": 1, "resource": "test_resource", "action": "create"},
|
||||
|
|
@ -671,7 +610,7 @@ async def test_post_perm_status_checks(
|
|||
],
|
||||
)
|
||||
@pytest.mark.anyio
|
||||
async def test_post_perm_search_success(default_client: AsyncClient, db_session, body):
|
||||
async def test_post_perm_search_success(default_client: AsyncClient, body):
|
||||
resp = await default_client.post("/iam/permissions/search", json=body)
|
||||
data = resp.json()
|
||||
|
||||
|
|
@ -795,7 +734,7 @@ async def test_delete_group_permissions_success(default_client: AsyncClient):
|
|||
assert len(data["permissions"]) == 0
|
||||
assert "group" in data
|
||||
assert data["group"]["id"] == 1
|
||||
assert data["group"]["name"] == "Test Group"
|
||||
assert data["group"]["name"] == "Org One Group"
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
|
|
@ -816,4 +755,4 @@ async def test_delete_group_users_success(default_client: AsyncClient):
|
|||
assert len(data["users"]) == 0
|
||||
assert "group" in data
|
||||
assert data["group"]["id"] == 1
|
||||
assert data["group"]["name"] == "Test Group"
|
||||
assert data["group"]["name"] == "Org One Group"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue