Skip to content

Commit

Permalink
test: switch to TUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
VMelnalksnis committed Oct 20, 2024
1 parent edf7a24 commit 61448fa
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 318 deletions.
190 changes: 95 additions & 95 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
<Project>
<ItemGroup>
<PackageVersion Include="Ardalis.SmartEnum" Version="2.1.0"/>
<PackageVersion Include="AdysTech.CredentialManager" Version="2.6.0"/>
<PackageVersion Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0"/>
<PackageVersion Include="AspNet.Security.OAuth.GitHub" Version="8.2.0"/>
<PackageVersion Include="AutoMapper" Version="13.0.1"/>
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.1.4"/>
<PackageVersion Include="Avalonia.Desktop" Version="11.1.4"/>
<PackageVersion Include="Avalonia.Diagnostics" Version="11.1.4"/>
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.1.4"/>
<PackageVersion Include="Avalonia.Xaml.Behaviors" Version="11.1.0.4"/>
<PackageVersion Include="Avalonia.Xaml.Interactivity" Version="11.1.0.4"/>
<PackageVersion Include="Bogus" Version="35.6.1"/>
<PackageVersion Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="CsvHelper" Version="33.0.1"/>
<PackageVersion Include="Dapper" Version="2.1.35"/>
<PackageVersion Include="dbup-core" Version="5.0.87"/>
<PackageVersion Include="dbup-postgresql" Version="5.0.40"/>
<PackageVersion Include="dbup-sqlite" Version="5.0.40"/>
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25"/>
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25"/>
<PackageVersion Include="FluentAssertions" Version="6.12.1"/>
<PackageVersion Include="FluentAssertions.NodaTime" Version="2.0.1"/>
<PackageVersion Include="FuzzySharp" Version="2.0.2"/>
<PackageVersion Include="HtmlAgilityPack" Version="1.11.65"/>
<PackageVersion Include="IdentityModel.OidcClient" Version="6.0.0"/>
<PackageVersion Include="JetBrains.Annotations" Version="2024.2.0"/>
<PackageVersion Include="JunitXml.TestLogger" Version="4.0.254"/>
<PackageVersion Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc2"/>
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10"/>
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.10"/>
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17"/>
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10"/>
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.10"/>
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10"/>
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.10"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0"/>
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1"/>
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1"/>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2"/>
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.1"/>
<PackageVersion Include="Microsoft.Extensions.Http.Polly" Version="8.0.10"/>
<PackageVersion Include="Microsoft.Extensions.Identity.Stores" Version="8.0.10"/>
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2"/>
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0"/>
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageVersion Include="NodaTime" Version="3.1.12"/>
<PackageVersion Include="NodaTime.Bogus" Version="3.0.2"/>
<PackageVersion Include="NodaTime.Serialization.SystemTextJson" Version="1.2.0"/>
<PackageVersion Include="Npgsql.DependencyInjection" Version="8.0.4"/>
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4"/>
<PackageVersion Include="Npgsql.NodaTime" Version="8.0.4"/>
<PackageVersion Include="Npgsql.OpenTelemetry" Version="8.0.4"/>
<PackageVersion Include="NUnit" Version="4.2.2"/>
<PackageVersion Include="NUnit.Analyzers" Version="4.3.0"/>
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0"/>
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0"/>
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0"/>
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0"/>
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0"/>
<PackageVersion Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.6"/>
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9.14"/>
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0"/>
<PackageVersion Include="PropertyChanged.SourceGenerator" Version="1.0.8"/>
<PackageVersion Include="Serilog" Version="4.0.1"/>
<PackageVersion Include="Serilog.Extensions.Hosting" Version="8.0.0"/>
<PackageVersion Include="Serilog.Settings.Configuration" Version="8.0.2"/>
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0"/>
<PackageVersion Include="Serilog.Sinks.Trace" Version="4.0.0"/>
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.8.0"/>
<PackageVersion Include="System.Linq.Async" Version="6.0.1"/>
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556"/>
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.5.0"/>
<PackageVersion Include="Verify.NUnit" Version="26.6.0"/>
<PackageVersion Include="VMelnalksnis.ISO20022DotNet" Version="0.3.4"/>
<PackageVersion Include="VMelnalksnis.NordigenDotNet.DependencyInjection" Version="1.4.0"/>
<PackageVersion Include="VMelnalksnis.PaperlessDotNet.DependencyInjection" Version="0.3.2"/>
<PackageVersion Include="VMelnalksnis.Testcontainers.Keycloak" Version="0.2.1"/>
</ItemGroup>

<!-- Vulnerable transitive dependencies -->
<ItemGroup>
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.4.0"/>
<PackageVersion Include="System.Text.Json" Version="8.0.5"/>
</ItemGroup>
</Project>
<ItemGroup>
<PackageVersion Include="Ardalis.SmartEnum" Version="2.1.0" />
<PackageVersion Include="AdysTech.CredentialManager" Version="2.6.0" />
<PackageVersion Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageVersion Include="AspNet.Security.OAuth.GitHub" Version="8.2.0" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.1.4" />
<PackageVersion Include="Avalonia.Desktop" Version="11.1.4" />
<PackageVersion Include="Avalonia.Diagnostics" Version="11.1.4" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.1.4" />
<PackageVersion Include="Avalonia.Xaml.Behaviors" Version="11.1.0.4" />
<PackageVersion Include="Avalonia.Xaml.Interactivity" Version="11.1.0.4" />
<PackageVersion Include="Bogus" Version="35.6.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="CsvHelper" Version="33.0.1" />
<PackageVersion Include="Dapper" Version="2.1.35" />
<PackageVersion Include="dbup-core" Version="5.0.87" />
<PackageVersion Include="dbup-postgresql" Version="5.0.40" />
<PackageVersion Include="dbup-sqlite" Version="5.0.40" />
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.2.25" />
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
<PackageVersion Include="FluentAssertions.NodaTime" Version="2.0.1" />
<PackageVersion Include="FuzzySharp" Version="2.0.2" />
<PackageVersion Include="HtmlAgilityPack" Version="1.11.65" />
<PackageVersion Include="IdentityModel.OidcClient" Version="6.0.0" />
<PackageVersion Include="JetBrains.Annotations" Version="2024.2.0" />
<PackageVersion Include="JunitXml.TestLogger" Version="4.0.254" />
<PackageVersion Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc2" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Http.Polly" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.Identity.Stores" Version="8.0.10" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="NodaTime" Version="3.1.12" />
<PackageVersion Include="NodaTime.Bogus" Version="3.0.2" />
<PackageVersion Include="NodaTime.Serialization.SystemTextJson" Version="1.2.0" />
<PackageVersion Include="Npgsql.DependencyInjection" Version="8.0.4" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageVersion Include="Npgsql.NodaTime" Version="8.0.4" />
<PackageVersion Include="Npgsql.OpenTelemetry" Version="8.0.4" />
<PackageVersion Include="NUnit" Version="4.2.2" />
<PackageVersion Include="NUnit.Analyzers" Version="4.3.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.6" />
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9.14" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageVersion Include="PropertyChanged.SourceGenerator" Version="1.0.8" />
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.Trace" Version="4.0.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.8.0" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.5.0" />
<PackageVersion Include="TUnit" Version="0.1.1090" />
<PackageVersion Include="Verify.NUnit" Version="26.6.0" />
<PackageVersion Include="VMelnalksnis.ISO20022DotNet" Version="0.3.4" />
<PackageVersion Include="VMelnalksnis.NordigenDotNet.DependencyInjection" Version="1.4.0" />
<PackageVersion Include="VMelnalksnis.PaperlessDotNet.DependencyInjection" Version="0.3.2" />
<PackageVersion Include="VMelnalksnis.Testcontainers.Keycloak" Version="0.2.1" />
</ItemGroup>
<!-- Vulnerable transitive dependencies -->
<ItemGroup>
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>
</Project>
Binary file modified source/Gnomeshade.Avalonia.Core/Help/licenses.json
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Gnomeshade.Avalonia.Core.Authentication;
using Gnomeshade.WebApi.Client;
using Gnomeshade.WebApi.Client.Results;
using Gnomeshade.WebApi.Tests.Integration.Oidc.Fixtures;
using Gnomeshade.WebApi.Tests.Integration.Oidc.Helpers;

using IdentityModel.OidcClient;
using IdentityModel.OidcClient.Browser;
Expand All @@ -18,24 +20,19 @@

using NodaTime;

[assembly: ArgumentDisplayFormatter<KeycloakFixtureFormatter>]

namespace Gnomeshade.WebApi.Tests.Integration.Oidc;

[TestFixtureSource(typeof(OidcFixtureSource))]
public sealed class AuthorizationTests
[FixtureDataSource]
public sealed class AuthorizationTests(KeycloakFixture fixture) : WebApplicationTests(fixture)
{
private readonly KeycloakFixture _fixture;

public AuthorizationTests(KeycloakFixture fixture)
{
_fixture = fixture;
}

[Test]
public async Task Get_ShouldReturnUnauthorized()
{
var client = _fixture.CreateHttpClient();
using var client = Fixture.CreateHttpClient();

var response = await client.GetAsync("/api/v1.0/transactions");
using var response = await client.GetAsync("/api/v1.0/transactions");

response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
}
Expand All @@ -56,18 +53,18 @@ public async Task SocialRegister()
{
var handler = provider.GetRequiredService<IGnomeshadeProtocolHandler>();
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient("Keycloak");
var internalClient = _fixture.CreateHttpClient();
return new(handler, httpClient, internalClient, _fixture.User.Username, _fixture.User.Password);
var internalClient = Fixture.CreateHttpClient();
return new(handler, httpClient, internalClient, Fixture.User.Username, Fixture.User.Password);
})
.AddSingleton<IBrowser, MockBrowser>(provider => provider.GetRequiredService<MockBrowser>())
.AddSingleton<IGnomeshadeProtocolHandler, MockProtocolHandler>(_ => new(KeycloakFixture.DesktopBaseUri))
.AddSingleton<IGnomeshadeProtocolHandler, MockProtocolHandler>(_ => new(Fixture.DesktopBaseUri))
.AddTransient<OidcClient>(provider => new(new()
{
Authority = _fixture.Realm.ServerRealm.ToString(),
ClientId = _fixture.DesktopClient.Name,
ClientSecret = _fixture.DesktopClient.Secret,
Authority = Fixture.Realm.ServerRealm.ToString(),
ClientId = Fixture.DesktopClient.Name,
ClientSecret = Fixture.DesktopClient.Secret,
Scope = "openid profile",
RedirectUri = KeycloakFixture.DesktopBaseUri,
RedirectUri = Fixture.DesktopBaseUri,
Browser = provider.GetRequiredService<IBrowser>(),
LoggerFactory = provider.GetRequiredService<ILoggerFactory>(),
HttpClientFactory = _ => provider.GetRequiredService<HttpClient>(),
Expand All @@ -78,10 +75,10 @@ public async Task SocialRegister()
.AddSingleton<GnomeshadeJsonSerializerOptions>()
.AddTransient<TokenDelegatingHandler>();

var provider = services.BuildServiceProvider();
await using var provider = services.BuildServiceProvider();

var handler = provider.GetRequiredService<TokenDelegatingHandler>();
var gnomeshadeClient = _fixture.CreateUnauthorizedClient(handler);
var gnomeshadeClient = Fixture.CreateUnauthorizedClient(handler);

await FluentActions
.Awaiting(() => gnomeshadeClient.GetMyCounterpartyAsync())
Expand Down
59 changes: 25 additions & 34 deletions tests/Gnomeshade.WebApi.Tests.Integration.Oidc/EndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,48 @@
// See LICENSE.txt file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using Gnomeshade.WebApi.Tests.Integration.Oidc.Fixtures;

using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Gnomeshade.WebApi.Tests.Integration.Oidc;

[TestFixtureSource(typeof(OidcFixtureSource))]
public sealed class EndpointTests
[FixtureDataSource]
public sealed class EndpointTests(KeycloakFixture fixture) : WebApplicationTests(fixture)
{
private readonly KeycloakFixture _fixture;

private HttpClient _client = null!;

public EndpointTests(KeycloakFixture fixture)
public static IEnumerable<Uri> Endpoints()
{
_fixture = fixture;
yield return new("/", UriKind.Relative);
yield return new("/Identity/Account/Register", UriKind.Relative);
yield return new("/Identity/Account/Login", UriKind.Relative);
}

[OneTimeSetUp]
public void SetUp() => _client = _fixture.CreateHttpClient();

[OneTimeTearDown]
public void TearDown() => _client.Dispose();
public static IEnumerable<(Uri RequestUri, Uri RedirectUri)> Redirects()
{
yield return (
new("/Identity/Account/ExternalLogin", UriKind.Relative),
new("/Identity/Account/Login", UriKind.Relative));
}

[TestCaseSource(nameof(Endpoints))]
[Test]
[MethodDataSource(nameof(Endpoints))]
public async Task ShouldReturnOk(Uri requestUri)
{
using var response = await _client.GetAsync(requestUri);
using var client = Fixture.CreateHttpClient();
using var response = await client.GetAsync(requestUri);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}

[TestCaseSource(nameof(Redirects))]
[Test]
[MethodDataSource(nameof(Redirects))]
public async Task ShouldRedirect(Uri requestUri, Uri redirectUri)
{
using var response = await _client.GetAsync(requestUri);
using var client = Fixture.CreateHttpClient();
using var response = await client.GetAsync(requestUri);
response.StatusCode.Should().Be(HttpStatusCode.Found);

var location = response.Headers.Location;
Expand All @@ -50,24 +54,11 @@ public async Task ShouldRedirect(Uri requestUri, Uri redirectUri)
[Test]
public async Task Health_ShouldReturnHealthy()
{
using var response = await _client.GetAsync("/api/v1.0/Health");
using var client = Fixture.CreateHttpClient();
using var response = await client.GetAsync("/api/v1.0/Health");
response.StatusCode.Should().Be(HttpStatusCode.OK);

var content = await response.Content.ReadAsStringAsync();
content.Should().Be(nameof(HealthStatus.Healthy));
}

private static IEnumerable Endpoints()
{
yield return new Uri("/", UriKind.Relative);
yield return new Uri("/Identity/Account/Register", UriKind.Relative);
yield return new Uri("/Identity/Account/Login", UriKind.Relative);
}

private static IEnumerable Redirects()
{
yield return new TestCaseData(
new Uri("/Identity/Account/ExternalLogin", UriKind.Relative),
new Uri("/Identity/Account/Login", UriKind.Relative));
}
}
Loading

0 comments on commit 61448fa

Please sign in to comment.