Skip to content

Commit

Permalink
Error message for `error.platform.validation.message.relationshipToRe…
Browse files Browse the repository at this point in the history
…cipientNotActive` contains wrong address (#915)

* fix: use correct address in error message of "relationshipToRecipientNotActive"

* chore: fix formatting
  • Loading branch information
tnotheis authored Oct 17, 2024
1 parent 36343c0 commit ddaaf50
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private async Task<List<RecipientInformation>> ValidateRecipients(SendMessageCom
var numberOfUnreceivedMessagesFromActiveIdentity = await _messagesRepository.CountUnreceivedMessagesFromSenderToRecipient(sender, recipientDto.Address, cancellationToken);

relationshipBetweenSenderAndRecipient.EnsureSendingMessagesIsAllowed(
_userContext.GetAddress(),
numberOfUnreceivedMessagesFromActiveIdentity,
_options.MaxNumberOfUnreceivedMessagesFromOneSender);

Expand Down
2 changes: 1 addition & 1 deletion Modules/Messages/src/Messages.Domain/DomainErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static DomainError RelationshipToRecipientNotActive(string recipient = ""

return new DomainError(
"error.platform.validation.message.relationshipToRecipientNotActive",
$"Cannot send message to {recipientText} because the relationship to it is not active. In order to be able to send messages again, you have to reactivate the relationship.");
$"Cannot send message to {recipientText} because the relationship to it is not active. If it's terminated, you'll need to reactivate it to be able to send messages again.");
}

public static DomainError MaxNumberOfUnreceivedMessagesReached(string recipient = "")
Expand Down
9 changes: 7 additions & 2 deletions Modules/Messages/src/Messages.Domain/Entities/Relationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ private Relationship(RelationshipId id, IdentityAddress from, IdentityAddress to

public RelationshipStatus Status { get; }

public void EnsureSendingMessagesIsAllowed(int numberOfUnreceivedMessagesFromActiveIdentity, int maxNumberOfUnreceivedMessagesFromOneSender)
public void EnsureSendingMessagesIsAllowed(IdentityAddress activeIdentity, int numberOfUnreceivedMessagesFromActiveIdentity, int maxNumberOfUnreceivedMessagesFromOneSender)
{
if (Status != RelationshipStatus.Active)
throw new DomainException(DomainErrors.RelationshipToRecipientNotActive(To));
throw new DomainException(DomainErrors.RelationshipToRecipientNotActive(GetPeerOf(activeIdentity)));

if (numberOfUnreceivedMessagesFromActiveIdentity >= maxNumberOfUnreceivedMessagesFromOneSender)
throw new DomainException(DomainErrors.MaxNumberOfUnreceivedMessagesReached(To));
Expand All @@ -48,6 +48,11 @@ public static Relationship LoadForTesting(RelationshipId id, IdentityAddress fro
{
return new Relationship(id, from, to, createdAt, status);
}

private IdentityAddress GetPeerOf(IdentityAddress activeIdentity)
{
return From == activeIdentity ? To : From;
}
}

public enum RelationshipStatus
Expand Down
3 changes: 3 additions & 0 deletions Modules/Messages/test/Messages.Domain.Tests/ImplicitUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using FluentAssertions;
global using Xunit;
global using static Backbone.UnitTestTools.Data.TestDataGenerator;
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using Backbone.Modules.Messages.Domain.Entities;
using Backbone.Modules.Messages.Domain.Ids;
using Backbone.UnitTestTools.BaseClasses;
using Backbone.UnitTestTools.Data;
using FluentAssertions;
using Xunit;

namespace Backbone.Modules.Messages.Domain.Tests.Relationships;

Expand All @@ -18,7 +15,7 @@ public void Relationship_must_be_active_to_allow_sending_messages()
var relationship = CreateRelationship(RelationshipStatus.Pending);

// Act
var acting = () => relationship.EnsureSendingMessagesIsAllowed(0, 5);
var acting = () => relationship.EnsureSendingMessagesIsAllowed(CreateRandomIdentityAddress(), 0, 5);

// Assert
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.relationshipToRecipientNotActive");
Expand All @@ -31,7 +28,7 @@ public void Max_number_of_unreceived_messages_must_not_be_reached()
var relationship = CreateRelationship();

// Act
var acting = () => relationship.EnsureSendingMessagesIsAllowed(5, 5);
var acting = () => relationship.EnsureSendingMessagesIsAllowed(CreateRandomIdentityAddress(), 5, 5);

// Assert
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.maxNumberOfUnreceivedMessagesReached");
Expand All @@ -44,7 +41,7 @@ public void Relationship_cannot_be_terminated_to_allow_sending_messages()
var relationship = CreateRelationship(RelationshipStatus.Terminated);

// Act
var acting = () => relationship.EnsureSendingMessagesIsAllowed(0, 5);
var acting = () => relationship.EnsureSendingMessagesIsAllowed(CreateRandomIdentityAddress(), 0, 5);

// Assert
acting.Should().Throw<DomainException>().Which.Code.Should().Be("error.platform.validation.message.relationshipToRecipientNotActive");
Expand All @@ -61,8 +58,8 @@ private static Relationship CreateRelationship(string? relationshipId = null, Id
RelationshipStatus? status = null)
{
relationshipId ??= "REL00000000000000000";
from ??= TestDataGenerator.CreateRandomIdentityAddress();
to ??= TestDataGenerator.CreateRandomIdentityAddress();
from ??= CreateRandomIdentityAddress();
to ??= CreateRandomIdentityAddress();
createdAt ??= DateTime.UtcNow;
status ??= RelationshipStatus.Active;

Expand Down

0 comments on commit ddaaf50

Please sign in to comment.