Skip to content

Commit

Permalink
Convert test suite to TUnit (#17)
Browse files Browse the repository at this point in the history
* Fix build

* Update to xunit v3

* Install .NET 8 as well.

* .

* Correct output type

* Remove assertion and retest

* .

* Test standalone

* Test adjustment to serializer

* Convert test suite to TUnit

---------

Co-authored-by: JT <[email protected]>
  • Loading branch information
Hawxy and Hawxy authored Jan 6, 2025
1 parent e9906b9 commit de069a3
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 322 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/Build_&_Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# - To turn off auto-generation set:
#
# [GitHubActions (AutoGenerate = false)]
# [GithubActionsExtended (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
Expand All @@ -29,10 +29,12 @@ jobs:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9'
dotnet-version: |
8.0
9.0
- uses: actions/checkout@v4
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/Manual_Nuget_Push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# - To turn off auto-generation set:
#
# [GitHubActions (AutoGenerate = false)]
# [GithubActionsExtended (AutoGenerate = false)]
#
# - To trigger manual generation invoke:
#
Expand All @@ -23,10 +23,12 @@ jobs:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9'
dotnet-version: |
8.0
9.0
- uses: actions/checkout@v4
- name: 'Cache: .nuke/temp, ~/.nuget/packages'
uses: actions/cache@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion Package.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageProjectUrl>https://github.com/Hawxy/Fga.Net</PackageProjectUrl>
<RepositoryUrl>https://github.com/Hawxy/Fga.Net</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Copyright>Hawxy 2022-2024</Copyright>
<Copyright>Hawxy 2022-2025</Copyright>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
58 changes: 58 additions & 0 deletions build/Build.SetupDotNet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.CI.GitHubActions.Configuration;
using Nuke.Common.Execution;
using Nuke.Common.Utilities;

public class GitHubActionsSetupDotNetStep : GitHubActionsStep
{
public GitHubActionsSetupDotNetStep(string[] versions)
{
Versions = versions;
}

string[] Versions { get; }

public override void Write(CustomFileWriter writer)
{
writer.WriteLine("- uses: actions/setup-dotnet@v4");

using (writer.Indent())
{
writer.WriteLine("with:");
using (writer.Indent())
{
writer.WriteLine("dotnet-version: |");
using (writer.Indent())
{
foreach (var version in Versions)
{
writer.WriteLine(version);
}
}
}
}
}
}

public class GithubActionsExtendedAttribute : GitHubActionsAttribute
{
public GithubActionsExtendedAttribute(string name, GitHubActionsImage image, params GitHubActionsImage[] images) : base(name, image, images)
{
}

protected override GitHubActionsJob GetJobs(GitHubActionsImage image,
IReadOnlyCollection<ExecutableTarget> relevantTargets)
{
var job = base.GetJobs(image, relevantTargets);

var newSteps = new List<GitHubActionsStep>(job.Steps);
newSteps.Insert(0, new GitHubActionsSetupDotNetStep([
"8.0", "9.0"
]));

job.Steps = newSteps.ToArray();

return job;
}
}
20 changes: 9 additions & 11 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@
using static Nuke.Common.Tools.DotNet.DotNetTasks;

[ShutdownDotNetAfterServerBuild]
[GitHubActions(
[GithubActionsExtended(
"Build & Test",
GitHubActionsImage.UbuntuLatest,
AutoGenerate = false,
OnPushBranches = new []{ "main" },
OnPullRequestBranches = new []{ "main" },
InvokedTargets = new[] { nameof(Test) },
ImportSecrets = new []{ nameof(FgaStoreId), nameof(FgaClientId), nameof(FgaClientSecret) })]
[GitHubActions(
OnPushBranches = ["main"],
OnPullRequestBranches = ["main"],
InvokedTargets = [nameof(Test)],
ImportSecrets = [nameof(FgaStoreId), nameof(FgaClientId), nameof(FgaClientSecret)])]
[GithubActionsExtended(
"Manual Nuget Push",
GitHubActionsImage.UbuntuLatest,
AutoGenerate = false,
On = new[] { GitHubActionsTrigger.WorkflowDispatch },
InvokedTargets = new[] { nameof(NugetPush) },
ImportSecrets = new[] { nameof(NugetApiKey) })]
On = [GitHubActionsTrigger.WorkflowDispatch],
InvokedTargets = [nameof(NugetPush)],
ImportSecrets = [nameof(NugetApiKey)])]
class Build : NukeBuild
{
/// Support plugins are available for:
Expand Down
4 changes: 2 additions & 2 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169</NoWarn>
<NukeRootDirectory>..</NukeRootDirectory>
Expand All @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="9.0.1" />
<PackageReference Include="Nuke.Common" Version="9.0.3" />
</ItemGroup>

</Project>
103 changes: 15 additions & 88 deletions tests/Fga.Net.Tests/Client/EndpointTests.cs
Original file line number Diff line number Diff line change
@@ -1,111 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Alba;
using Fga.Net.DependencyInjection.Configuration;
using Fga.Net.DependencyInjection.Configuration;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenFga.Sdk.Api;
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Client.Model;
using OpenFga.Sdk.Model;
using Xunit;

namespace Fga.Net.Tests.Client;

[Collection(nameof(EndpointWebAppCollection))]
public class EndpointTests
[ClassDataSource<EndpointWebAppFixture>(Shared = SharedType.PerAssembly)]
public class EndpointTests(EndpointWebAppFixture fixture) : EndpointWebAppBase(fixture)
{
private readonly IAlbaHost _host;

public EndpointTests(EndpointWebAppFixture fixture)
{
_host = fixture.AlbaHost;
}

[Fact]
[Test]
private async Task GetEndpoints_OpenFgaApi_Return_200()
{
using var scope = _host.Services.CreateScope();
using var scope = Host.Services.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<OpenFgaApi>();
var config = scope.ServiceProvider.GetRequiredService<IOptions<FgaClientConfiguration>>().Value;
var modelsResponse = await client.ReadAuthorizationModels(config.StoreId!);

Assert.NotNull(modelsResponse);
Assert.NotNull(modelsResponse.AuthorizationModels);
Assert.True(modelsResponse.AuthorizationModels?.Count > 0);

var modelId = modelsResponse.AuthorizationModels?.First().Id!;

var modelResponse = await client.ReadAuthorizationModel(config.StoreId!, modelId);

Assert.NotNull(modelResponse);
Assert.NotNull(modelResponse.AuthorizationModel?.Id);

var assertions = await client.ReadAssertions(config.StoreId!, modelId);

Assert.NotNull(assertions);
Assert.True(assertions.Assertions?.Count > 0);
var assertion = assertions.Assertions!.First().TupleKey;

Assert.NotEmpty(assertion!.Object!);
Assert.NotEmpty(assertion.Relation!);
Assert.NotEmpty(assertion.User!);

var graph = await client.Expand(config.StoreId!, new ExpandRequest()
{
AuthorizationModelId = modelId,
TupleKey = new ExpandRequestTupleKey(assertion.Relation, assertion.Object)
});

Assert.NotNull(graph.Tree);
Assert.NotNull(graph.Tree!.Root!.Name);

var watch = await client.ReadChanges(config.StoreId!);
Assert.NotNull(watch);



modelsResponse.Should().NotBeNull();
modelsResponse.AuthorizationModels.Should().NotBeNull();
modelsResponse.AuthorizationModels.Count.Should().BePositive();
}

[Fact]
[Test]
private async Task GetEndpoints_OpenFgaClient_Return_200()
{
using var scope = _host.Services.CreateScope();
using var scope = Host.Services.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<OpenFgaClient>();
var modelsResponse = await client.ReadAuthorizationModels();

Assert.NotNull(modelsResponse);
Assert.NotNull(modelsResponse.AuthorizationModels);
Assert.True(modelsResponse.AuthorizationModels?.Count > 0);

var modelId = modelsResponse.AuthorizationModels?.First().Id!;

var modelResponse = await client.ReadAuthorizationModel(new ClientReadAuthorizationModelOptions() {AuthorizationModelId = modelId});

Assert.NotNull(modelResponse);
Assert.NotNull(modelResponse.AuthorizationModel?.Id);

var assertions = await client.ReadAssertions(new ClientReadAssertionsOptions() { AuthorizationModelId = modelId});

Assert.NotNull(assertions);
Assert.True(assertions.Assertions?.Count > 0);
var assertion = assertions.Assertions!.First().TupleKey;

Assert.NotEmpty(assertion!.Object!);
Assert.NotEmpty(assertion.Relation!);
Assert.NotEmpty(assertion.User!);

var graph = await client.Expand(new ClientExpandRequest()
{
Object = assertion.Object!,
Relation = assertion.Relation!
});

Assert.NotNull(graph.Tree);
Assert.NotNull(graph.Tree!.Root!.Name);

var watch = await client.ReadChanges(new ClientReadChangesRequest() {Type = "document"});
Assert.NotNull(watch);
modelsResponse.Should().NotBeNull();
modelsResponse.AuthorizationModels.Should().NotBeNull();
modelsResponse.AuthorizationModels.Count.Should().BePositive();
}

}
13 changes: 6 additions & 7 deletions tests/Fga.Net.Tests/Client/EndpointWebAppFixture.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Threading.Tasks;
using Alba;
using Alba;
using Fga.Net.Tests.Middleware;
using Xunit;
using TUnit.Core.Interfaces;

namespace Fga.Net.Tests.Client;

public class EndpointWebAppFixture : IAsyncLifetime
public class EndpointWebAppFixture : IAsyncInitializer, IAsyncDisposable
{
public IAlbaHost AlbaHost = null!;

Expand All @@ -14,13 +13,13 @@ public async Task InitializeAsync()
AlbaHost = await Alba.AlbaHost.For<Program>(_ => { }, MockJwtConfiguration.GetDefaultStubConfiguration());
}

public async Task DisposeAsync()
public async ValueTask DisposeAsync()
{
await AlbaHost.DisposeAsync();
}
}

[CollectionDefinition(nameof(EndpointWebAppCollection))]
public class EndpointWebAppCollection : ICollectionFixture<EndpointWebAppFixture>
public abstract class EndpointWebAppBase(EndpointWebAppFixture fixture)
{
protected IAlbaHost Host => fixture.AlbaHost;
}
19 changes: 6 additions & 13 deletions tests/Fga.Net.Tests/Fga.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<Nullable>enable</Nullable>

<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Alba" Version="8.1.0" />
<PackageReference Include="HttpContextMoq" Version="1.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="HttpContextMoq" Version="1.6.0" />
<PackageReference Include="Ulid" Version="1.3.4" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="TUnit" Version="0.6.33" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit de069a3

Please sign in to comment.