Skip to content

Commit

Permalink
Merge pull request #28 from POORT8/sync2
Browse files Browse the repository at this point in the history
Sync merge.
  • Loading branch information
mabvanaartrijk authored Jul 3, 2024
2 parents fbce314 + 44c260c commit fe11ec4
Show file tree
Hide file tree
Showing 188 changed files with 17,840 additions and 1,568 deletions.
30 changes: 30 additions & 0 deletions Poort8.Dataspace.API.Tests/ApiApp.cs
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 Poort8.Dataspace.API.Tests/DelegationEvidenceCreatorTests.cs
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
};
}
}
57 changes: 57 additions & 0 deletions Poort8.Dataspace.API.Tests/OpenApiDefinitionTests.cs
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 Poort8.Dataspace.API.Tests/Poort8.Dataspace.API.Tests.csproj
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>
Loading

0 comments on commit fe11ec4

Please sign in to comment.