Skip to content
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
2 changes: 1 addition & 1 deletion db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def hard_delete(self, session: Session, auth0_client: Auth0Client) -> None:
except HTTPStatusError as e:
logger.warning(f"Error deleting user {user_id} from Auth0: {e}")

for model in (EmailChangeOtp, GroupMembershipHistory, PlatformMembershipHistory, GroupMembership, PlatformMembership, BiocommonsUserHistory):
for model in (AccountMigrationRequest, EmailChangeOtp, GroupMembershipHistory, PlatformMembershipHistory, GroupMembership, PlatformMembership, BiocommonsUserHistory):
session.exec(delete(model).where(model.user_id == user_id))
session.exec(delete(BiocommonsUser).where(BiocommonsUser.id == user_id))
session.commit()
Expand Down
37 changes: 37 additions & 0 deletions tests/db/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sqlmodel import select

from db.models import (
AccountMigrationRequest,
ApprovalStatusEnum,
Auth0Role,
BiocommonsGroup,
Expand All @@ -24,6 +25,7 @@
PlatformMembership,
PlatformMembershipHistory,
)
from schemas.aaf_migration import MigrationDirection, MigrationStatus
from schemas.biocommons import BiocommonsUserAccountType
from tests.biocommons.datagen import RoleDataFactory
from tests.datagen import (
Expand Down Expand Up @@ -1711,6 +1713,41 @@ def test_hard_delete_removes_user_and_related_rows(test_db_session, mock_auth0_c
assert test_db_session.get(EmailChangeOtp, otp_id) is None


@pytest.mark.parametrize("is_deleted", [False, True])
@pytest.mark.parametrize("direction", list(MigrationDirection))
def test_hard_delete_removes_account_migration_requests(
test_db_session, mock_auth0_client, persistent_factories, is_deleted, direction
):
test_db_session.connection().exec_driver_sql("PRAGMA foreign_keys=ON")
user = BiocommonsUserFactory.create_sync(is_deleted=is_deleted, deleted_by_id=None)
other_user = BiocommonsUserFactory.create_sync(deleted_by_id=None)
test_db_session.commit()
user_id = user.id
requests = [
AccountMigrationRequest(
user_id=owner.id,
new_email="new@example.edu.au",
direction=direction,
status=status,
expires_at=datetime.now(timezone.utc) + timedelta(minutes=10),
)
for owner in (user, other_user)
for status in MigrationStatus
]
test_db_session.add_all(requests)
test_db_session.commit()
other_request_ids = {request.id for request in requests if request.user_id == other_user.id}

user.hard_delete(test_db_session, auth0_client=mock_auth0_client)

test_db_session.expire_all()
assert BiocommonsUser.get_by_id(user_id, test_db_session, include_deleted=True) is None
remaining_requests = test_db_session.exec(select(AccountMigrationRequest)).all()
assert {request.id for request in remaining_requests} == other_request_ids
assert test_db_session.get(BiocommonsUser, other_user.id) is not None
mock_auth0_client.delete_user.assert_called_once_with(user_id=user_id)


def test_hard_delete_refuses_if_user_acted_as_admin_for_another_user(test_db_session, mock_auth0_client, persistent_factories):
admin = BiocommonsUserFactory.create_sync()
other_user = BiocommonsUserFactory.create_sync()
Expand Down
Loading