Skip to content

Commit

Permalink
Consumer API: Block new relationship when old one is in status deleti…
Browse files Browse the repository at this point in the history
…on proposed (#799)
  • Loading branch information
tnotheis authored Aug 9, 2024
1 parent e5c2dcb commit fa7b28c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,52 +98,56 @@ public void There_can_only_be_one_active_relationship_between_two_identities()
acting.Should().Throw<DomainException>().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists");
}

[Fact]
public void Creating_a_new_relationship_is_possible_if_existing_ones_are_rejected_or_revoked()
[Theory]
[InlineData(RelationshipStatus.Pending)]
[InlineData(RelationshipStatus.Active)]
[InlineData(RelationshipStatus.Terminated)]
[InlineData(RelationshipStatus.DeletionProposed)]
public void There_can_only_be_one_active_relationship_between_two_identities2(RelationshipStatus relationshipStatus)
{
// Arrange
var existingRelationships = new List<Relationship>
{
CreateRejectedRelationship(),
CreateRevokedRelationship()
CreateRelationshipInStatus(relationshipStatus)
};

// Act
var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships);

// Assert
acting.Should().NotThrow<DomainException>();
acting.Should().Throw<DomainException>().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists");
}

[Fact]
public void Cannot_create_Relationship_if_terminated_Relationship_exists()
public void Creating_a_new_relationship_is_possible_if_existing_ones_are_rejected_or_revoked()
{
// Arrange
var existingRelationships = new List<Relationship>
{
CreateTerminatedRelationship()
CreateRejectedRelationship(),
CreateRevokedRelationship()
};

// Act
var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships);

// Assert
acting.Should().Throw<DomainException>().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists");
acting.Should().NotThrow<DomainException>();
}

[Fact]
public void A_new_relationship_can_be_created_after_decomposing_the_old_one()
public void Cannot_create_Relationship_if_terminated_Relationship_exists()
{
// Arrange
var existingRelationships = new List<Relationship>
{
CreateDecomposedRelationship(IDENTITY_1, IDENTITY_2)
CreateTerminatedRelationship()
};

// Act
var newRelationship = new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, [], existingRelationships);
var acting = () => new Relationship(RELATIONSHIP_TEMPLATE_OF_1, IDENTITY_2, DEVICE_2, null, existingRelationships);

// Assert
newRelationship.Status.Should().Be(RelationshipStatus.Pending);
acting.Should().Throw<DomainException>().WithError("error.platform.validation.relationshipRequest.relationshipToTargetAlreadyExists");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static void EnsureTargetIsNotSelf(RelationshipTemplate relationshipTempl

private static void EnsureNoOtherRelationshipToPeerExists(IdentityAddress target, IEnumerable<Relationship> existingRelationshipsToPeer)
{
if (existingRelationshipsToPeer.Any(r => r.Status is RelationshipStatus.Active or RelationshipStatus.Pending or RelationshipStatus.Terminated))
if (existingRelationshipsToPeer.Any(r => r.Status is RelationshipStatus.Active or RelationshipStatus.Pending or RelationshipStatus.Terminated or RelationshipStatus.DeletionProposed))
throw new DomainException(DomainErrors.RelationshipToTargetAlreadyExists(target));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public static Relationship CreatePendingRelationship(IdentityAddress? from = nul
return relationship;
}

public static Relationship CreateRelationshipInStatus(RelationshipStatus status, IdentityAddress? from = null, IdentityAddress? to = null)
{
return status switch
{
RelationshipStatus.Pending => CreatePendingRelationship(from, to),
RelationshipStatus.Active => CreateActiveRelationship(from, to),
RelationshipStatus.Terminated => CreateTerminatedRelationship(from, to),
RelationshipStatus.DeletionProposed => CreateRelationshipWithProposedDeletion(from, to),
_ => throw new NotSupportedException($"This method currently does not support relationship status {status}.")
};
}

public static Relationship CreateActiveRelationship(IdentityAddress? from = null, IdentityAddress? to = null)
{
to ??= IDENTITY_2;
Expand Down Expand Up @@ -54,7 +66,15 @@ public static Relationship CreateRevokedRelationship()
public static Relationship CreateTerminatedRelationship(IdentityAddress? from = null, IdentityAddress? to = null)
{
var relationship = CreateActiveRelationship(from, to);
relationship.Terminate(IDENTITY_1, DEVICE_1);
relationship.Terminate(relationship.From, DEVICE_1);
relationship.ClearDomainEvents();
return relationship;
}

public static Relationship CreateRelationshipWithProposedDeletion(IdentityAddress? from = null, IdentityAddress? to = null)
{
var relationship = CreateTerminatedRelationship(from, to);
relationship.Decompose(relationship.From, DEVICE_1);
relationship.ClearDomainEvents();
return relationship;
}
Expand Down

0 comments on commit fa7b28c

Please sign in to comment.