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

Consumer API: Block new relationship when old one is in status deletion proposed #799

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
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this tested in the first test above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man... I forgot to delete the old tests and rename the new one (it still contains "2" in its name)

{
// 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))
tnotheis marked this conversation as resolved.
Show resolved Hide resolved
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
Loading