Skip to content

Commit

Permalink
Merge branch 'test'
Browse files Browse the repository at this point in the history
  • Loading branch information
Paahn committed Jul 16, 2024
2 parents 19905a4 + f101f4e commit 56014f2
Show file tree
Hide file tree
Showing 108 changed files with 1,761 additions and 1,251 deletions.
15 changes: 9 additions & 6 deletions backend/webapi.tests/Features/Parties/Demographics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PidpTests.Features.Parties;
public class DemographicsTests : InMemoryDbTest
{
[Fact]
public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEvent()
public async Task UpdatePartyDemographics_NewParty_PropertiesUpdatedDomainEvent()
{
var party = this.TestDb.HasAParty(party =>
{
Expand All @@ -19,7 +19,7 @@ public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEven
var command = new Command
{
Id = party.Id,
Email = "pidp.test@goh.com",
Email = "pidp.test@email.com",
Phone = "5555555555",
PreferredFirstName = "PFirst",
PreferredMiddleName = "PMid",
Expand All @@ -36,11 +36,14 @@ public async void UpdatePartyDemographics_NewParty_PropertiesUpdatedNoDomainEven
Assert.Equal(command.PreferredMiddleName, updatedParty.PreferredMiddleName);
Assert.Equal(command.PreferredLastName, updatedParty.PreferredLastName);

Assert.Empty(this.PublishedEvents.OfType<PartyEmailUpdated>());
Assert.Single(this.PublishedEvents.OfType<PartyEmailUpdated>());
var emailEvent = this.PublishedEvents.OfType<PartyEmailUpdated>().Single();
Assert.Equal(party.Id, emailEvent.PartyId);
Assert.Equal(command.Email, emailEvent.NewEmail);
}

[Fact]
public async void UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainEvent()
public async Task UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainEvent()
{
var party = this.TestDb.HasAParty(party =>
{
Expand Down Expand Up @@ -79,7 +82,7 @@ public async void UpdatePartyDemographics_ExistingParty_PropertiesUpdatedDomainE
}

[Fact]
public async void UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
public async Task UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
{
var party = this.TestDb.HasAParty(party =>
{
Expand Down Expand Up @@ -114,7 +117,7 @@ public async void UpdatePartyDemographics_ExistingParty_TrimPreferredNames()
}

[Fact]
public async void UpdatePartyDemographics_ExistingPartySameEmail_NoDomainEvent()
public async Task UpdatePartyDemographics_ExistingPartySameEmail_NoDomainEvent()
{
var party = this.TestDb.HasAParty(party => party.Email = "existing@email.com");
var command = new Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class EndorsementDataTests : InMemoryDbTest
&& record.StatusReasonCode == result.StatusReasonCode;

[Fact]
public async void GetEndorsementData_NoParty_404()
public async Task GetEndorsementData_NoParty_404()
{
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid"));
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
var query = new EndorsementData.Query { Hpdid = "NoMatch" };
var handler = this.MockDependenciesFor<EndorsementData.QueryHandler>();

Expand All @@ -29,33 +29,24 @@ public async void GetEndorsementData_NoParty_404()
}

[Fact]
public async void GetEndorsementData_NoEndorsements_Empty()
public async Task GetEndorsementData_NoEndorsements_Empty()
{
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid"));
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
var query = new EndorsementData.Query { Hpdid = "hpdid" };
var handler = this.MockDependenciesFor<EndorsementData.QueryHandler>();

var result = await handler.HandleAsync(query);

Assert.True(result.IsSuccess);
Assert.Equal(new List<EndorsementData.Model>(), result.Value);
Assert.Equal([], result.Value);
}

[Fact]
public async void GetEndorsementData_OneEndorsementNotActive_Empty()
public async Task GetEndorsementData_OneEndorsementNotActive_Empty()
{
var expectedHpdid = "expected";
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid"));
this.TestDb.Has(APartyWith(id: 2, hpdid: expectedHpdid));
this.TestDb.Has(new Endorsement
{
Active = false,
EndorsementRelationships = new[]
{
new EndorsementRelationship { PartyId = 1 },
new EndorsementRelationship { PartyId = 2 }
}
});
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
this.TestDb.Has(APartyWith(id: 2, hpdid: "anotherHpdid@bcsc"));
this.TestDb.Has(AnEndorsementBetween(1, 2, isActive: false));
var query = new EndorsementData.Query { Hpdid = "hpdid" };
var handler = this.MockDependenciesFor<EndorsementData.QueryHandler>();

Expand All @@ -68,21 +59,12 @@ public async void GetEndorsementData_OneEndorsementNotActive_Empty()
[Theory]
[InlineData("hpdid")]
[InlineData("hpdid@bcsc")]
public async void GetEndorsementData_OneEndorsementNoCpn_MatchingHpdidEmptyLicences(string partyHpdid)
public async Task GetEndorsementData_OneEndorsementNoCpn_MatchingHpdidEmptyLicences(string hpdidQuery)
{
var expectedHpdid = "expected";
this.TestDb.Has(APartyWith(id: 1, hpdid: partyHpdid));
this.TestDb.Has(APartyWith(id: 2, hpdid: expectedHpdid));
this.TestDb.Has(new Endorsement
{
Active = true,
EndorsementRelationships = new[]
{
new EndorsementRelationship { PartyId = 1 },
new EndorsementRelationship { PartyId = 2 }
}
});
var query = new EndorsementData.Query { Hpdid = "hpdid" }; // Should match either "hpdid" or "hpdid@bcsc" (the actual form for HPDID we get from Keycloak)
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
var otherParty = this.TestDb.Has(APartyWith(id: 2, hpdid: "anotherHpdid@bcsc"));
this.TestDb.Has(AnEndorsementBetween(1, 2));
var query = new EndorsementData.Query { Hpdid = hpdidQuery }; // Should match either "hpdid" or "hpdid@bcsc" (the actual form for HPDID we get from Keycloak and save in our database)
var handler = this.MockDependenciesFor<EndorsementData.QueryHandler>();

var result = await handler.HandleAsync(query);
Expand All @@ -91,15 +73,14 @@ public async void GetEndorsementData_OneEndorsementNoCpn_MatchingHpdidEmptyLicen
Assert.Single(result.Value);

var model = result.Value.Single();
Assert.Equal(expectedHpdid, model.Hpdid);
AssertBasicInfoMatches(otherParty, model);
Assert.NotNull(model.Licences);
Assert.Empty(model.Licences);
}

[Fact]
public async void GetEndorsementData_OneEndorsementWithCpn_MatchingHpdidWithLicences()
public async Task GetEndorsementData_OneEndorsementWithCpn_MatchingHpdidWithLicences()
{
var expectedHpdid = "expected";
var expectedCpn = "cpn";
var expectedPlrRecords = new[]
{
Expand All @@ -118,17 +99,9 @@ public async void GetEndorsementData_OneEndorsementWithCpn_MatchingHpdidWithLice
StatusReasonCode = "StatusReasonCode2"
}
};
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid"));
this.TestDb.Has(APartyWith(id: 2, hpdid: expectedHpdid, cpn: expectedCpn));
this.TestDb.Has(new Endorsement
{
Active = true,
EndorsementRelationships = new[]
{
new EndorsementRelationship { PartyId = 1 },
new EndorsementRelationship { PartyId = 2 }
}
});
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
var otherParty = this.TestDb.Has(APartyWith(id: 2, hpdid: "otherHpdid@bcsc", cpn: expectedCpn));
this.TestDb.Has(AnEndorsementBetween(1, 2));
var query = new EndorsementData.Query { Hpdid = "hpdid" };
var client = A.Fake<IPlrClient>();
A.CallTo(() => client.GetRecordsAsync(expectedCpn))
Expand All @@ -141,76 +114,59 @@ public async void GetEndorsementData_OneEndorsementWithCpn_MatchingHpdidWithLice
Assert.Single(result.Value);

var model = result.Value.Single();
Assert.Equal(expectedHpdid, model.Hpdid);
AssertBasicInfoMatches(otherParty, model);
Assert.NotNull(model.Licences);
AssertThat.CollectionsAreEquivalent(expectedPlrRecords, model.Licences, RecordModelPredicate);
}

[Fact]
public async void GetEndorsementData_TwoEndorsementsWithCpn_MatchingHpdidsWithLicences()
public async Task GetEndorsementData_TwoEndorsementsWithCpn_MatchingHpdidsWithLicences()
{
var expectedHpdid1 = "expected1";
var expectedHpdid2 = "expected2";
var expectedCpn1 = "cpn";
var expectedPlrRecords1 = new[]
var expectedCpn = "cpn";
var expectedPlrRecords = new[]
{
new PlrRecord
{
Cpn = expectedCpn1,
Cpn = expectedCpn,
IdentifierType = "IdentifierType1",
StatusCode = "StatusCode1",
StatusReasonCode = "StatusReasonCode1"
},
new PlrRecord
{
Cpn = expectedCpn1,
Cpn = expectedCpn,
IdentifierType = "IdentifierType2",
StatusCode = "StatusCode2",
StatusReasonCode = "StatusReasonCode2"
}
};

this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid"));
this.TestDb.Has(APartyWith(id: 2, hpdid: expectedHpdid1, cpn: expectedCpn1));
this.TestDb.Has(APartyWith(id: 3, hpdid: expectedHpdid2));
this.TestDb.Has(new Endorsement
{
Active = true,
EndorsementRelationships = new[]
{
new EndorsementRelationship { PartyId = 1 },
new EndorsementRelationship { PartyId = 2 }
}
});
this.TestDb.Has(new Endorsement
{
Active = true,
EndorsementRelationships = new[]
{
new EndorsementRelationship { PartyId = 1 },
new EndorsementRelationship { PartyId = 3 }
}
});
this.TestDb.Has(APartyWith(id: 1, hpdid: "hpdid@bcsc"));
var licencedOtherParty = this.TestDb.Has(APartyWith(id: 2, hpdid: "otherHpdid@bcsc", cpn: expectedCpn));
var unlicencedOtherParty = this.TestDb.Has(APartyWith(id: 3, hpdid: "otherHpdid2@bcsc"));
this.TestDb.Has(AnEndorsementBetween(1, 2));
this.TestDb.Has(AnEndorsementBetween(1, 3));
var query = new EndorsementData.Query { Hpdid = "hpdid" };
var client = A.Fake<IPlrClient>();
A.CallTo(() => client.GetRecordsAsync(expectedCpn1, null))
.Returns(expectedPlrRecords1);
A.CallTo(() => client.GetRecordsAsync(expectedCpn1))
.Returns(expectedPlrRecords1);
A.CallTo(() => client.GetRecordsAsync(expectedCpn, null))
.Returns(expectedPlrRecords);
var handler = this.MockDependenciesFor<EndorsementData.QueryHandler>(client);

var result = await handler.HandleAsync(query);

Assert.True(result.IsSuccess);
Assert.Equal(2, result.Value.Count);

var found = result.Value.SingleOrDefault(res => res.Hpdid == expectedHpdid1);
var found = result.Value.SingleOrDefault(res => res.Hpdid == licencedOtherParty.Credentials.Single().Hpdid);
Assert.NotNull(found);
AssertThat.CollectionsAreEquivalent(expectedPlrRecords1, found!.Licences, RecordModelPredicate);
AssertBasicInfoMatches(licencedOtherParty, found);
AssertThat.CollectionsAreEquivalent(expectedPlrRecords, found.Licences, RecordModelPredicate);

found = result.Value.SingleOrDefault(res => res.Hpdid == expectedHpdid2);
found = result.Value.SingleOrDefault(res => res.Hpdid == unlicencedOtherParty.Credentials.Single().Hpdid);
Assert.NotNull(found);
AssertThat.CollectionsAreEquivalent(Array.Empty<PlrRecord>(), found!.Licences, RecordModelPredicate);
AssertBasicInfoMatches(unlicencedOtherParty, found);
Assert.NotNull(found.Licences);
Assert.Empty(found.Licences);
}

public static Party APartyWith(int id, string hpdid, string? cpn = null)
Expand All @@ -219,14 +175,38 @@ public static Party APartyWith(int id, string hpdid, string? cpn = null)
{
Id = id,
Cpn = cpn,
Credentials = new List<Credential>
{
FirstName = $"First{id}",
LastName = $"Last{id}",
Email = $"email@{id}.com",
Credentials =
[
new Credential
{
IdentityProvider = IdentityProviders.BCServicesCard,
IdpId = hpdid
}
}
]
};
}

public static Endorsement AnEndorsementBetween(int partyId1, int partyId2, bool isActive = true)
{
return new Endorsement
{
Active = isActive,
EndorsementRelationships =
[
new EndorsementRelationship { PartyId = partyId1 },
new EndorsementRelationship { PartyId = partyId2 }
]
};
}

private static void AssertBasicInfoMatches(Party expected, EndorsementData.Model actual)
{
Assert.Equal(expected.Credentials.Single().Hpdid, actual.Hpdid);
Assert.Equal(expected.FirstName, actual.FirstName);
Assert.Equal(expected.LastName, actual.LastName);
Assert.Equal(expected.Email, actual.Email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Party HasAParty(this PidpDbContext context, Action<Party>? config
{
var party = new Party();
config?.Invoke(party);
if (!party.Credentials.Any())
if (party.Credentials.Count == 0)
{
party.Credentials.Add(new Credential { UserId = Guid.NewGuid() });
}
Expand Down
Loading

0 comments on commit 56014f2

Please sign in to comment.