Skip to content

Commit

Permalink
It is not possible to create a relationship with a password protected…
Browse files Browse the repository at this point in the history
… template (#908)

* fix: invert condition

* fix: in CanBeCollectedUsingPassword, return true if the template is already allocated by the active identity

* test. add tests
  • Loading branch information
tnotheis authored Oct 15, 2024
1 parent dcb2f14 commit bb56244
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Backbone.Modules.Relationships.Domain.TestHelpers;
using Backbone.UnitTestTools.BaseClasses;
using Backbone.UnitTestTools.Data;
using FluentAssertions;
using Xunit;

namespace Backbone.Modules.Relationships.Domain.Aggregates.RelationshipTemplates;

public class RelationshipTemplateCanBeCollectedWithPasswordTests : AbstractTestsBase
{
[Fact]
public void Can_collect_without_a_password_when_no_password_is_defined()
{
// Arrange
var creator = TestDataGenerator.CreateRandomIdentityAddress();
var collector = TestDataGenerator.CreateRandomIdentityAddress();

var template = TestData.CreateRelationshipTemplate(creator);

// Act
var result = template.CanBeCollectedUsingPassword(collector, null);

// Assert
result.Should().BeTrue();
}

[Fact]
public void Can_collect_with_correct_password()
{
// Arrange
var creator = TestDataGenerator.CreateRandomIdentityAddress();
var collector = TestDataGenerator.CreateRandomIdentityAddress();

var template = TestData.CreateRelationshipTemplate(creator, password: [1]);

// Act
var result = template.CanBeCollectedUsingPassword(collector, [1]);

// Assert
result.Should().BeTrue();
}

[Fact]
public void Cannot_collect_with_incorrect_password()
{
// Arrange
var creator = TestDataGenerator.CreateRandomIdentityAddress();
var collector = TestDataGenerator.CreateRandomIdentityAddress();

var template = TestData.CreateRelationshipTemplate(creator, password: [1]);

// Act
var result = template.CanBeCollectedUsingPassword(collector, [2]);

// Assert
result.Should().BeFalse();
}

[Fact]
public void Can_collect_as_owner_without_a_password()
{
// Arrange
var creator = TestDataGenerator.CreateRandomIdentityAddress();

var template = TestData.CreateRelationshipTemplate(creator, password: [1]);

// Act
var result = template.CanBeCollectedUsingPassword(creator, null);

// Assert
result.Should().BeTrue();
}

[Fact]
public void Can_collect_without_password_when_template_is_already_allocated_by_me()
{
// Arrange
var creator = TestDataGenerator.CreateRandomIdentityAddress();
var collector = TestDataGenerator.CreateRandomIdentityAddress();

var template = TestData.CreateRelationshipTemplate(creator, password: [1]);
template.AllocateFor(collector, TestDataGenerator.CreateRandomDeviceId());

// Act
var result = template.CanBeCollectedUsingPassword(collector, null);

// Assert
result.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ public void AllocateFor(IdentityAddress identity, DeviceId device)

public bool IsAllocatedBy(IdentityAddress identity)
{
return Allocations.All(x => x.AllocatedBy != identity);
return Allocations.Any(x => x.AllocatedBy == identity);
}

public bool CanBeCollectedUsingPassword(IdentityAddress address, byte[]? password)
public bool CanBeCollectedUsingPassword(IdentityAddress activeIdentity, byte[]? password)
{
return Password == null || password != null && Password.SequenceEqual(password) || CreatedBy == address;
return Password == null ||
password != null && Password.SequenceEqual(password) ||
CreatedBy == activeIdentity || // The owner shouldn't need a password to get the template
Allocations.Any(a => a.AllocatedBy == activeIdentity); // if the template has already been allocated by the active identity, it doesn't need to pass the password again
}

#region Expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static class TestData
public static readonly RelationshipTemplate RELATIONSHIP_TEMPLATE_OF_1 = new(IDENTITY_1, DEVICE_1, 1, null, []);
public static readonly RelationshipTemplate RELATIONSHIP_TEMPLATE_OF_2 = new(IDENTITY_2, DEVICE_2, 1, null, []);

public static RelationshipTemplate CreateRelationshipTemplate(IdentityAddress creatorAddress, IdentityAddress? forIdentityAddress)
public static RelationshipTemplate CreateRelationshipTemplate(IdentityAddress creatorAddress, IdentityAddress? forIdentityAddress = null, byte[]? password = null)
{
return new RelationshipTemplate(creatorAddress, DeviceId.New(), 999, null, [], forIdentityAddress);
return new RelationshipTemplate(creatorAddress, DeviceId.New(), 999, null, [], forIdentityAddress, password);
}

public static Relationship CreatePendingRelationship(IdentityAddress? from = null, IdentityAddress? to = null)
Expand Down

0 comments on commit bb56244

Please sign in to comment.