Skip to content

Commit

Permalink
BFF integration tests that target aspire
Browse files Browse the repository at this point in the history
Only focusses on testing the BFF project
for now. Will integrate other tests later.
  • Loading branch information
Erwinvandervalk committed Jan 14, 2025
1 parent f8ebdbf commit 4ad3202
Show file tree
Hide file tree
Showing 37 changed files with 1,191 additions and 154 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bff-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
fail-fast: false
matrix:
runs-on: [macOS-latest, ubuntu-latest, windows-latest]
runs-on: [ubuntu-latest, windows-latest]
name: ${{ matrix.runs-on }}
runs-on: ${{ matrix.runs-on }}
defaults:
Expand Down
29 changes: 29 additions & 0 deletions bff/Duende.Bff.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.AppHost", "samples\Ho
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.ServiceDefaults", "samples\Hosts.ServiceDefaults\Hosts.ServiceDefaults.csproj", "{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosts.Tests", "samples\Hosts.Tests\Hosts.Tests.csproj", "{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "build", "build\build.csproj", "{E083402C-EB22-4F8A-BFD6-A4F448678376}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -351,6 +355,30 @@ Global
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x64.Build.0 = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x86.ActiveCfg = Release|Any CPU
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0}.Release|x86.Build.0 = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|x64.ActiveCfg = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|x64.Build.0 = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|x86.ActiveCfg = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Debug|x86.Build.0 = Debug|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|Any CPU.Build.0 = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|x64.ActiveCfg = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|x64.Build.0 = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|x86.ActiveCfg = Release|Any CPU
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07}.Release|x86.Build.0 = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|x64.ActiveCfg = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|x64.Build.0 = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|x86.ActiveCfg = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Debug|x86.Build.0 = Debug|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|Any CPU.Build.0 = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|x64.ActiveCfg = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|x64.Build.0 = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|x86.ActiveCfg = Release|Any CPU
{E083402C-EB22-4F8A-BFD6-A4F448678376}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -383,6 +411,7 @@ Global
{2A04808A-A06C-4F10-87B9-2D12E065F729} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
{8B943A54-F50C-4946-8D0E-DA1B886F13D6} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
{2740EDB1-6F59-4A99-B0EE-808D8F61BEC0} = {E14F66D1-EA3E-40C6-835A-91A4382D4646}
{A0B771BA-ACF9-4DE2-A2A6-430F6E6E8C07} = {B2A776DB-385B-4AD4-96A5-61746FD909C3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DAD5980-4688-4794-9CF0-6F3CB67194E7}
Expand Down
16 changes: 14 additions & 2 deletions bff/build/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
 using System;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

using static Bullseye.Targets;
Expand Down Expand Up @@ -43,7 +46,16 @@ internal static async Task Main(string[] args)

Target(Targets.Test, DependsOn(Targets.Build), () =>
{
Run("dotnet", "test -c Release --no-build --nologo");
// Only running the tests on linux on the build agents because trusting the SSL Cert doesn't work there.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Run("dotnet", "dev-certs https --trust");
Run("dotnet", "test -c Release --no-build --nologo");
}
else
{
Console.WriteLine("Skipping tests on windows and mac-os");
}
});

Target(Targets.CleanPackOutput, () =>
Expand Down
2 changes: 1 addition & 1 deletion bff/migrations/UserSessionDb/UserSessionDb.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Apis/Api.DPoP/Api.DPoP.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Apis/Api.Isolated/Api.Isolated.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Apis/Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Bff.DPoP/Bff.DPoP.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Bff.DPoP</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Bff.EF/Bff.EF.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Bff.DPoP</RootNamespace>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Bff/Bff.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Bff</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
78 changes: 48 additions & 30 deletions bff/samples/Bff/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
// See LICENSE in the project root for license information.

using System;
using System.Threading;
using Duende.Bff;
using Duende.Bff.Yarp;
using Host8;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ServiceDiscovery;
using Serilog;

internal static class Extensions
{
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
public static WebApplication ConfigureServices(
this WebApplicationBuilder builder,

// The serviceprovider is needed to do service discovery
Func<IServiceProvider> getServiceProvider
)
{
var services = builder.Services;

Expand Down Expand Up @@ -48,42 +55,53 @@ public static WebApplication ConfigureServices(this WebApplicationBuilder builde
// strict SameSite handling
options.Cookie.SameSite = SameSiteMode.Strict;
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";

// confidential client using code flow + PKCE
options.ClientId = "bff";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.ResponseMode = "query";

options.MapInboundClaims = false;
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;

// request scopes + refresh tokens
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api");
options.Scope.Add("scope-for-isolated-api");
options.Scope.Add("offline_access");

options.Resource = "urn:isolated-api";
});
.AddOpenIdConnect("oidc", options =>
{
// Normally, here you simply configure the authority. But here we want to
// use service discovery, because aspire can change the url's at run-time.
// So, it needs to be discovered at runtime.
var authority = DiscoverAuthorityByName(getServiceProvider, "identity-server");
options.Authority = authority;

// confidential client using code flow + PKCE
options.ClientId = "bff";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.ResponseMode = "query";

options.MapInboundClaims = false;
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;

// request scopes + refresh tokens
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api");
options.Scope.Add("scope-for-isolated-api");
options.Scope.Add("offline_access");

options.Resource = "urn:isolated-api";
});
services.AddSingleton<ImpersonationAccessTokenRetriever>();

services.AddUserAccessTokenHttpClient("api",
configureClient: client =>
{
client.BaseAddress = new Uri("https://localhost:5010/api");
});
configureClient: client => { client.BaseAddress = new Uri("https://localhost:5010/api"); });

return builder.Build();

}

private static string DiscoverAuthorityByName(Func<IServiceProvider> getServiceProvider, string serviceName)
{
// Use the ServiceEndpointResolver to perform service discovery
var resolver = getServiceProvider().GetRequiredService<ServiceEndpointResolver>();
var authorityEndpoint = resolver.GetEndpointsAsync("https://" + serviceName, CancellationToken.None)
.GetAwaiter().GetResult(); // Right now I have no way to add this async.
var authority = authorityEndpoint.Endpoints[0].ToString()!.TrimEnd('/');
return authority;
}

public static WebApplication ConfigurePipeline(this WebApplication app)
{
app.UseSerilogRequestLogging();
Expand Down Expand Up @@ -144,7 +162,7 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
.RequireAccessToken(TokenType.User)
.WithUserAccessTokenParameter(new BffUserAccessTokenParameters(resource: "urn:isolated-api"));


return app;
}

}
20 changes: 18 additions & 2 deletions bff/samples/Bff/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using Serilog.Events;

Expand All @@ -27,11 +28,17 @@
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.ReadFrom.Configuration(ctx.Configuration));

var serviceProviderAccessor = new ServiceProviderAccessor();

var app = builder
.ConfigureServices()
.ConfigureServices(() => serviceProviderAccessor.ServiceProvider ?? throw new InvalidOperationException("Service Provider must be set"))
.ConfigurePipeline();

serviceProviderAccessor.ServiceProvider = app.Services;

app.Run();


}
catch (Exception ex)
{
Expand All @@ -41,4 +48,13 @@
{
Log.Information("Shut down complete");
Log.CloseAndFlush();
}
}

/// <summary>
/// A workaround to get the service provider available in the ConfigureServices method
/// </summary>
class ServiceProviderAccessor
{
public IServiceProvider? ServiceProvider { get; set; }

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion bff/samples/Hosts.AppHost/Hosts.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
Expand Down
5 changes: 5 additions & 0 deletions bff/samples/Hosts.AppHost/Hosts.AppHost.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
Loading

0 comments on commit 4ad3202

Please sign in to comment.