Skip to content

Commit

Permalink
Feat: add test and fix log
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Dec 14, 2024
1 parent 3b96865 commit 54ffb8d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/core/groups/endpoints_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ async def synchronize_group_with_association(
raise HTTPException(status_code=404, detail="Group not found")

Check warning on line 329 in app/core/groups/endpoints_groups.py

View check run for this annotation

Codecov / codecov/patch

app/core/groups/endpoints_groups.py#L329

Added line #L329 was not covered by tests

hyperion_security_logger.warning(
f"Synchronize_group_with_association: Admin user {user.id} ({user.name}) synchronized group {group_db.id} ({group_db.name}) with association membership {association_membership.value} ({request_id})",
f"Synchronize_group_with_association: Admin user {user.id} synchronized group {group_db.id} ({group_db.name}) with association membership '{association_membership.value}' ({request_id})",
)
await cruds_groups.delete_membership_by_group_id(group_id=group_id, db=db)

Expand Down
53 changes: 51 additions & 2 deletions tests/test_groups.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import uuid
from datetime import UTC, date, datetime, timedelta

import pytest_asyncio
from fastapi.testclient import TestClient

from app.core import models_core
from app.core.groups.groups_type import GroupType
from app.types.membership import AvailableAssociationMembership
from tests.commons import (
add_object_to_db,
create_api_access_token,
create_user_with_groups,
)

admin_user: models_core.CoreUser

user_test: models_core.CoreUser

id_test_eclair = "8aab79e7-1e15-456d-b6e2-11e4e9f77e4f"


@pytest_asyncio.fixture(scope="module", autouse=True)
async def init_objects() -> None:
global admin_user
global admin_user, user_test

eclair = models_core.CoreGroup(
id=id_test_eclair,
Expand All @@ -27,6 +31,32 @@ async def init_objects() -> None:
await add_object_to_db(eclair)

admin_user = await create_user_with_groups([GroupType.admin])
user_test = await create_user_with_groups([])

aeecl_membership_admin = models_core.CoreAssociationMembership(
id=uuid.uuid4(),
user_id=admin_user.id,
membership=AvailableAssociationMembership.aeecl,
start_date=date(2021, 9, 5),
end_date=date(2022, 9, 5),
)
useecl_membership_admin = models_core.CoreAssociationMembership(
id=uuid.uuid4(),
user_id=admin_user.id,
membership=AvailableAssociationMembership.useecl,
start_date=date(2021, 9, 5),
end_date=date(2027, 9, 5),
)
aeecl_membership_test = models_core.CoreAssociationMembership(
id=uuid.uuid4(),
user_id=user_test.id,
membership=AvailableAssociationMembership.aeecl,
start_date=date(2021, 9, 5),
end_date=datetime.now(tz=UTC).date() + timedelta(days=365),
)
await add_object_to_db(aeecl_membership_admin)
await add_object_to_db(useecl_membership_admin)
await add_object_to_db(aeecl_membership_test)


def test_read_groups(client: TestClient) -> None:
Expand Down Expand Up @@ -106,3 +136,22 @@ def test_delete_membership(client: TestClient) -> None:
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 204


def test_sync_group_with_association_membership(client: TestClient) -> None:
token = create_api_access_token(admin_user)

response = client.patch(
f"/groups/{id_test_eclair}/sync/AEECL",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 204

response = client.get(
f"/groups/{id_test_eclair}",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 200
data = response.json()
assert len(data["members"]) == 1
assert data["members"][0]["id"] == str(user_test.id)

0 comments on commit 54ffb8d

Please sign in to comment.