-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from POORT8/sync2
Sync merge.
- Loading branch information
Showing
188 changed files
with
17,840 additions
and
1,568 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using FastEndpoints.Testing; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Poort8.Dataspace.API.Tests; | ||
|
||
public class ApiApp : AppFixture<Program> | ||
{ | ||
protected override Task SetupAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected override void ConfigureApp(IWebHostBuilder builder) | ||
{ | ||
builder.UseConfiguration(new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.Test.json") | ||
.Build()); | ||
} | ||
|
||
protected override void ConfigureServices(IServiceCollection services) | ||
{ | ||
} | ||
|
||
protected override Task TearDownAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
Poort8.Dataspace.API.Tests/DelegationEvidenceCreatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using Poort8.Ishare.Core.Models; | ||
using static Poort8.Ishare.Core.Models.DelegationMask; | ||
|
||
namespace Poort8.Dataspace.API.Tests; | ||
|
||
public class DelegationEvidenceCreatorTests | ||
{ | ||
[Fact] | ||
public void CreateShouldReturnDelegationEvidenceWithPermit() | ||
{ | ||
var delegationMask = CreateDelegationMask("Permit"); | ||
var explainPolicy = new AuthorizationRegistry.Entities.Policy("policyIssuer", "accessSubject", "identifier", "action"); | ||
|
||
var delegationEvidence = DelegationEvidenceCreator.Create(true, delegationMask, explainPolicy); | ||
|
||
Assert.NotNull(delegationEvidence); | ||
Assert.Equal("Permit", delegationEvidence.PolicySets[0].Policies[0].Rules[0].Effect); | ||
Assert.Equal("policyIssuer", delegationEvidence.PolicyIssuer); | ||
Assert.Equal("accessSubject", delegationEvidence.Target.AccessSubject); | ||
Assert.Equal("identifier", delegationEvidence.PolicySets[0].Policies[0].Target.Resource.Identifiers[0]); | ||
Assert.Equal("action", delegationEvidence.PolicySets[0].Policies[0].Target.Actions[0]); | ||
Assert.Equal(0, delegationEvidence.PolicySets[0].MaxDelegationDepth); | ||
Assert.NotEqual(-1, delegationEvidence.NotBefore); | ||
Assert.NotEqual(-1, delegationEvidence.NotOnOrAfter); | ||
} | ||
|
||
[Fact] | ||
public void CreateShouldReturnDelegationEvidenceWithDeny() | ||
{ | ||
var delegationMask = CreateDelegationMask("Deny"); | ||
var explainPolicy = new AuthorizationRegistry.Entities.Policy("policyIssuer", "accessSubject", "identifier", "action"); | ||
|
||
var delegationEvidence = DelegationEvidenceCreator.Create(false, delegationMask, explainPolicy); | ||
|
||
Assert.NotNull(delegationEvidence); | ||
Assert.Equal("Deny", delegationEvidence.PolicySets[0].Policies[0].Rules[0].Effect); | ||
Assert.Equal("policyIssuer", delegationEvidence.PolicyIssuer); | ||
Assert.Equal("accessSubject", delegationEvidence.Target.AccessSubject); | ||
Assert.Equal("identifier", delegationEvidence.PolicySets[0].Policies[0].Target.Resource.Identifiers[0]); | ||
Assert.Equal("action", delegationEvidence.PolicySets[0].Policies[0].Target.Actions[0]); | ||
Assert.NotEqual(-1, delegationEvidence.NotBefore); | ||
Assert.NotEqual(-1, delegationEvidence.NotOnOrAfter); | ||
} | ||
|
||
[Fact] | ||
public void CreateShouldReturnDelegationEvidenceWithDenyNoExpain() | ||
{ | ||
var delegationMask = CreateDelegationMask("Deny"); | ||
|
||
var delegationEvidence = DelegationEvidenceCreator.Create(false, delegationMask, null); | ||
|
||
Assert.NotNull(delegationEvidence); | ||
Assert.Equal("Deny", delegationEvidence.PolicySets[0].Policies[0].Rules[0].Effect); | ||
Assert.Equal("policyIssuer", delegationEvidence.PolicyIssuer); | ||
Assert.Equal("accessSubject", delegationEvidence.Target.AccessSubject); | ||
Assert.Equal("identifier", delegationEvidence.PolicySets[0].Policies[0].Target.Resource.Identifiers[0]); | ||
Assert.Equal("action", delegationEvidence.PolicySets[0].Policies[0].Target.Actions[0]); | ||
Assert.Equal(-1, delegationEvidence.NotBefore); | ||
Assert.Equal(-1, delegationEvidence.NotOnOrAfter); | ||
} | ||
|
||
private static DelegationMask CreateDelegationMask(string effect) | ||
{ | ||
var delegationRequest = new DelegationRequestObject | ||
{ | ||
PolicyIssuer = "policyIssuer", | ||
Target = new DelegationRequestObject.TargetObject | ||
{ | ||
AccessSubject = "accessSubject" | ||
}, | ||
PolicySets = | ||
[ | ||
new DelegationRequestObject.PolicySet | ||
{ | ||
Policies = | ||
[ | ||
new DelegationRequestObject.PolicySet.Policy | ||
{ | ||
Target = new DelegationRequestObject.PolicySet.Policy.TargetObject | ||
{ | ||
Resource = new DelegationRequestObject.PolicySet.Policy.TargetObject.ResourceObject | ||
{ | ||
Type = "resourceType", | ||
Identifiers = ["identifier"], | ||
Attributes = ["attribute"] | ||
}, | ||
Actions = ["action"], | ||
Environment = new DelegationRequestObject.PolicySet.Policy.TargetObject.EnvironmentObject | ||
{ | ||
ServiceProviders = ["serviceProvider"] | ||
} | ||
}, | ||
Rules = | ||
[ | ||
new DelegationRequestObject.PolicySet.Policy.Rule | ||
{ | ||
Effect = effect | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
return new DelegationMask | ||
{ | ||
DelegationRequest = delegationRequest | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using FastEndpoints.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NSwag.Generation; | ||
using Snapshooter.Xunit; | ||
using System.Text.Json.Nodes; | ||
using FluentAssertions.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Poort8.Dataspace.API.Tests; | ||
|
||
public class OpenApiDefinitionTests(ApiApp App) : TestBase<ApiApp> | ||
{ | ||
[Fact] | ||
public async Task TestApiDefinitionChangedUsingDoc() | ||
{ | ||
var docGenerator = App.Services.GetRequiredService<IOpenApiDocumentGenerator>(); | ||
var apiDefinition = await docGenerator.GenerateAsync("v1"); | ||
|
||
var jsonNode = JsonNode.Parse(apiDefinition.ToJson()); | ||
var orderedApiDefinition = OrderApiDefinition(jsonNode!); | ||
|
||
try | ||
{ | ||
Snapshot.Match(orderedApiDefinition); | ||
} | ||
catch (Exception) | ||
{ | ||
//On GitHub workflow Snapshot.Match fails, so try again with FluentAssertions | ||
var snapshot = await File.ReadAllTextAsync("__snapshots__/OpenApiDefinitionTests.TestApiDefinitionChangedUsingDoc.snap"); | ||
var snapshotDoc = JToken.Parse(snapshot); | ||
var apiDefinitionDoc = JToken.Parse(apiDefinition.ToJson()); | ||
apiDefinitionDoc.Should().BeEquivalentTo(snapshotDoc); | ||
} | ||
} | ||
|
||
private static JsonNode? OrderApiDefinition(JsonNode? jsonNode) | ||
{ | ||
if (jsonNode is JsonObject jsonObject) | ||
{ | ||
var orderedObject = new JsonObject(); | ||
foreach (var property in jsonObject.OrderBy(p => p.Key)) | ||
{ | ||
orderedObject[property.Key] = OrderApiDefinition(property.Value?.DeepClone()); | ||
} | ||
return orderedObject; | ||
} | ||
else if (jsonNode is JsonArray jsonArray) | ||
{ | ||
var orderedArray = new JsonArray(jsonArray.Select(node => OrderApiDefinition(node?.DeepClone())).ToArray()); | ||
return orderedArray; | ||
} | ||
else | ||
{ | ||
return jsonNode; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
Poort8.Dataspace.API.Tests/Poort8.Dataspace.API.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors> | ||
<AnalysisMode>Minimum</AnalysisMode> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="appsettings.Test.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
<Content Include="__snapshots__\*"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="FastEndpoints.Testing" Version="5.26.0" /> | ||
<PackageReference Include="FluentAssertions.Json" Version="6.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" /> | ||
<PackageReference Include="Snapshooter.Xunit" Version="0.14.1" /> | ||
<PackageReference Include="xunit" Version="2.8.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Poort8.Dataspace.CoreManager\Poort8.Dataspace.CoreManager.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.