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
4 changes: 4 additions & 0 deletions auth-api/src/auth_api/services/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from auth_api.exceptions import BusinessException
from auth_api.exceptions.errors import Error
from auth_api.models import AffiliationInvitation as AffiliationInvitationModel
from auth_api.models import Contact as ContactModel
from auth_api.models import ContactLink as ContactLinkModel
from auth_api.models.entity import Entity as EntityModel
Expand Down Expand Up @@ -266,6 +267,9 @@ def delete(self):
if self._model.affiliations:
raise BusinessException(Error.ENTITY_DELETE_FAILED, None)

for invitation in AffiliationInvitationModel.find_invitations_by_entity(self._model.id):
invitation.delete()

if self._model.contacts:
self.delete_contact()

Expand Down
26 changes: 24 additions & 2 deletions auth-api/tests/unit/services/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
Test suite to ensure that the Entity service routines are working as expected.
"""

from datetime import datetime

import pytest

from auth_api.exceptions import BusinessException
from auth_api.exceptions.errors import Error
from auth_api.models import AffiliationInvitation as AffiliationInvitationModel
from auth_api.models import ContactLink as ContactLinkModel
from auth_api.services.entity import Entity as EntityService
from tests.utilities.factory_scenarios import TestContactInfo, TestEntityInfo, TestJwtClaims, TestUserInfo
from tests.utilities.factory_utils import (
factory_contact_model,
factory_entity_model,
factory_org_model,
factory_org_service,
factory_user_model,
patch_token_info,
)

Expand Down Expand Up @@ -342,7 +347,7 @@ def test_validate_invalid_pass_code(app, session): # pylint:disable=unused-argu


def test_delete_entity(app, session): # pylint:disable=unused-argument
"""Assert that an entity can be deleted."""
"""Assert that an entity can be deleted and its affiliation invitations are removed."""
entity_model = factory_entity_model()
entity = EntityService(entity_model)

Expand All @@ -356,11 +361,28 @@ def test_delete_entity(app, session): # pylint:disable=unused-argument
contact_link.org = org._model # pylint:disable=protected-access
contact_link.save()

user = factory_user_model()
from_org = factory_org_model()
to_org = factory_org_model(org_info={"name": "Test Org 2"})

invitation = AffiliationInvitationModel()
invitation.sender = user
invitation.from_org_id = from_org.id
invitation.to_org_id = to_org.id
invitation.entity_id = entity._model.id # pylint:disable=protected-access
invitation.recipient_email = "test@test.com"
invitation.sent_date = datetime.now()
invitation.invitation_status_code = "PENDING"
invitation.save()

invitation_id = invitation.id
assert AffiliationInvitationModel.find_invitation_by_id(invitation_id) is not None

entity.delete()

entity = EntityService.find_by_entity_id(entity.identifier)

assert entity is None
assert AffiliationInvitationModel.find_invitation_by_id(invitation_id) is None


def test_reset_pass_code(app, session, monkeypatch): # pylint:disable=unused-argument
Expand Down