Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pdct 313 Implement DB delete for family events. #30

Merged
merged 14 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/clients/db/models/app/authorisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ class AuthAccess(str, enum.Enum):
AuthOperation.CREATE: AuthAccess.USER,
AuthOperation.READ: AuthAccess.USER,
AuthOperation.UPDATE: AuthAccess.USER,
AuthOperation.DELETE: AuthAccess.ADMIN,
AuthOperation.DELETE: AuthAccess.USER,
},
# Collection
AuthEndpoint.COLLECTION: {
AuthOperation.CREATE: AuthAccess.USER,
AuthOperation.READ: AuthAccess.USER,
AuthOperation.UPDATE: AuthAccess.USER,
AuthOperation.DELETE: AuthAccess.ADMIN,
AuthOperation.DELETE: AuthAccess.USER,
},
# Collection
AuthEndpoint.DOCUMENT: {
AuthOperation.CREATE: AuthAccess.USER,
AuthOperation.READ: AuthAccess.USER,
AuthOperation.UPDATE: AuthAccess.USER,
AuthOperation.DELETE: AuthAccess.ADMIN,
AuthOperation.DELETE: AuthAccess.USER,
},
# Config
AuthEndpoint.CONFIG: {
Expand All @@ -82,6 +82,6 @@ class AuthAccess(str, enum.Enum):
AuthOperation.CREATE: AuthAccess.USER,
AuthOperation.READ: AuthAccess.USER,
AuthOperation.UPDATE: AuthAccess.USER,
AuthOperation.DELETE: AuthAccess.ADMIN,
AuthOperation.DELETE: AuthAccess.USER,
},
}
7 changes: 3 additions & 4 deletions app/repository/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
from typing import Optional, Tuple, cast

from sqlalchemy import or_, Column, update as db_update
from sqlalchemy import or_, Column, update as db_update, delete as db_delete
from sqlalchemy.orm import Query, Session
from sqlalchemy.exc import NoResultFound
from sqlalchemy_utils import escape_like
Expand Down Expand Up @@ -221,12 +221,11 @@ def delete(db: Session, import_id: str) -> bool:
db.query(FamilyEvent).filter(FamilyEvent.import_id == import_id).one_or_none()
)
if found is None:
_LOGGER.error(f"Event with id {import_id} not found")
return False

result = db.execute(
db_update(FamilyEvent)
.where(FamilyEvent.import_id == import_id)
.values(status=EventStatus.DELETED)
db_delete(FamilyEvent).where(FamilyEvent.import_id == import_id)
)
if result.rowcount == 0: # type: ignore
msg = f"Could not delete event : {import_id}"
Expand Down
22 changes: 6 additions & 16 deletions integration_tests/event/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ def test_delete_event(client: TestClient, test_db: Session, admin_user_header_to
setup_db(test_db)
response = client.delete("/api/v1/events/E.0.0.2", headers=admin_user_header_token)
assert response.status_code == status.HTTP_200_OK
assert test_db.query(FamilyEvent).count() == 3
assert test_db.query(FamilyEvent).count() == 2
assert (
test_db.query(FamilyEvent)
.filter(FamilyEvent.status == EventStatus.DELETED)
.count()
== 1
test_db.query(FamilyEvent).filter(FamilyEvent.import_id == "E.0.0.2").count()
== 0
)
assert test_db.query(FamilyEvent).count() == 3


def test_delete_event_when_not_authenticated(
Expand All @@ -36,7 +33,6 @@ def test_delete_event_when_not_authenticated(
.count()
== 0
)
assert test_db.query(FamilyEvent).count() == 3
assert event_repo.delete.call_count == 0


Expand All @@ -51,12 +47,9 @@ def test_delete_event_rollback(
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
assert test_db.query(FamilyEvent).count() == 3
assert (
test_db.query(FamilyEvent)
.filter(FamilyEvent.status == EventStatus.DELETED)
.count()
== 0
test_db.query(FamilyEvent).filter(FamilyEvent.import_id == "E.0.0.2").count()
== 1
)
assert test_db.query(FamilyEvent).count() == 3
assert rollback_event_repo.delete.call_count == 1


Expand All @@ -70,12 +63,9 @@ def test_delete_event_when_not_found(
assert data["detail"] == "Event not deleted: E.0.0.22"
assert test_db.query(FamilyEvent).count() == 3
assert (
test_db.query(FamilyEvent)
.filter(FamilyEvent.status == EventStatus.DELETED)
.count()
test_db.query(FamilyEvent).filter(FamilyEvent.import_id == "E.0.0.22").count()
== 0
)
assert test_db.query(FamilyEvent).count() == 3


def test_delete_event_when_db_error(
Expand Down
3 changes: 3 additions & 0 deletions unit_tests/routers/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

This uses a service mock and ensures each endpoint calls into the service.
"""
import pytest
from fastapi import status
from fastapi.testclient import TestClient

from unit_tests.helpers.collection import (
create_collection_write_dto,
)
Expand Down Expand Up @@ -108,6 +110,7 @@ def test_delete_when_ok(
assert collection_service_mock.delete.call_count == 1


@pytest.mark.skip(reason="No admin user for MVP")
katybaulch marked this conversation as resolved.
Show resolved Hide resolved
def test_delete_collection_fails_if_not_admin(
client: TestClient, collection_service_mock, user_header_token
):
Expand Down
3 changes: 3 additions & 0 deletions unit_tests/routers/test_document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from fastapi import status
from fastapi.testclient import TestClient

import app.service.document as document_service
from unit_tests.helpers.document import (
create_document_create_dto,
Expand Down Expand Up @@ -115,6 +117,7 @@ def test_delete_when_ok(
assert document_service_mock.delete.call_count == 1


@pytest.mark.skip(reason="No admin user for MVP")
def test_delete_document_fails_if_not_admin(
client: TestClient, document_service_mock, user_header_token
):
Expand Down
4 changes: 3 additions & 1 deletion unit_tests/routers/test_event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from fastapi import status
from fastapi.encoders import jsonable_encoder
from fastapi.testclient import TestClient
import app.service.event as event_service

import app.service.event as event_service
from unit_tests.helpers.event import (
create_event_create_dto,
create_event_write_dto,
Expand Down Expand Up @@ -119,6 +120,7 @@ def test_delete_when_ok(
assert event_service_mock.delete.call_count == 1


@pytest.mark.skip(reason="No admin user for MVP")
def test_delete_event_fails_if_not_admin(
client: TestClient, event_service_mock, user_header_token
):
Expand Down
2 changes: 2 additions & 0 deletions unit_tests/routers/test_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

This uses a service mock and ensures each endpoint calls into the service.
"""
import pytest
from fastapi import status
from fastapi.testclient import TestClient

Expand Down Expand Up @@ -98,6 +99,7 @@ def test_delete_when_ok(
assert family_service_mock.delete.call_count == 1


@pytest.mark.skip(reason="No admin user for MVP")
def test_delete_fails_when_not_admin(
client: TestClient, family_service_mock, user_header_token
):
Expand Down
Loading