Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit Tests & Code Coverage
name: Tests & Code Coverage

on:
push:
Expand All @@ -12,7 +12,7 @@ permissions:

jobs:
test:
name: Run Unit Tests
name: Run Tests & Calculate Coverage
runs-on: ubuntu-latest

steps:
Expand All @@ -29,15 +29,24 @@ jobs:
- name: Restore dependencies
run: dotnet restore src/ModularNet.sln

- name: Run Unit Tests with Coverage
- name: Run Unit & Integration Tests with Merged Coverage
run: |
dotnet test src/ModularNet.UnitTests/ModularNet.UnitTests.csproj \
--configuration Release \
--no-restore \
--verbosity normal \
/p:CollectCoverage=true \
/p:CoverletOutputFormat=json \
/p:CoverletOutput=TestResults/coverage.json

dotnet test src/ModularNet.IntegrationTests/ModularNet.IntegrationTests.csproj \
--configuration Release \
--no-restore \
--verbosity normal \
/p:CollectCoverage=true \
/p:MergeWith="../ModularNet.UnitTests/TestResults/coverage.json" \
/p:CoverletOutputFormat=cobertura \
/p:CoverletOutput=TestResults/coverage.cobertura.xml
/p:CoverletOutput="../ModularNet.UnitTests/TestResults/coverage.cobertura.xml"

- name: Generate Code Coverage Badge
run: |
Expand Down
71 changes: 47 additions & 24 deletions src/ModularNet.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
catch (Exception ex)
{
Log.Fatal(ex, $"ERROR Starting the Application: {ex.Message}");
throw;
}
finally
{
Expand Down Expand Up @@ -106,31 +107,44 @@ async Task ConfigureServices(WebApplicationBuilder builder, IWebHostEnvironment

// Initialize Firebase App

if (env.IsDevelopment())
FirebaseApp.Create(
new AppOptions
{
Credential = GoogleCredential.FromFile("modularnet-firebase-adminsdk-dev.json")
});
else
try
{
// Configure FIREBASE_ADMIN_SDK in an Environment Variables Section (Azure App Service, for instance)
var firebaseAdminSdkJson = Environment.GetEnvironmentVariable("FIREBASE_ADMIN_SDK");

if (string.IsNullOrEmpty(firebaseAdminSdkJson))
throw new Exception("Error getting Firebase Admin SDK credentials from environment variable");

FirebaseApp.Create(
new AppOptions
{
Credential = GoogleCredential.FromJson(firebaseAdminSdkJson)
});
}
catch (Exception ex)
lock (FirebaseLock)
{
if (FirebaseApp.DefaultInstance == null)
{
Log.Error(ex, $"ERROR starting Firebase config: {ex.Message}");
try
{
if (env.IsDevelopment())
FirebaseApp.Create(
new AppOptions
{
Credential = GoogleCredential.FromFile("modularnet-firebase-adminsdk-dev.json")
});
else
try
{
// Configure FIREBASE_ADMIN_SDK in an Environment Variables Section (Azure App Service, for instance)
var firebaseAdminSdkJson = Environment.GetEnvironmentVariable("FIREBASE_ADMIN_SDK");

if (string.IsNullOrEmpty(firebaseAdminSdkJson))
throw new Exception("Error getting Firebase Admin SDK credentials from environment variable");

FirebaseApp.Create(
new AppOptions
{
Credential = GoogleCredential.FromJson(firebaseAdminSdkJson)
});
}
catch (Exception ex)
{
Log.Error(ex, $"ERROR starting Firebase config: {ex.Message}");
}
}
catch (ArgumentException) when (FirebaseApp.DefaultInstance != null)
{
// Ignored if created concurrently
}
}
}

builder.Services.AddAuthentication()
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme,
Expand All @@ -143,7 +157,11 @@ async Task ConfigureServices(WebApplicationBuilder builder, IWebHostEnvironment
});

// Application Insights
builder.Services.AddApplicationInsightsTelemetry();
var appInsightsConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
builder.Services.AddApplicationInsightsTelemetry();
}

RegisterDomainServices(builder.Services, builder.Configuration);
}
Expand Down Expand Up @@ -187,4 +205,9 @@ void RegisterDomainServices(IServiceCollection services, IConfiguration configur
services.AddInfrastructureServices(configuration);
services.AddBusinessServices(configuration);
services.AddApiServices(configuration);
}

public partial class Program
{
private static readonly object FirebaseLock = new();
}
2 changes: 1 addition & 1 deletion src/ModularNet.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"FrontEndBaseUrl": "https://localhost:7230",
"IsEmailServiceEnabled": true,
"EncryptionSalt": "modularNet",
"InitializationVector": "initializationVector",
"InitializationVector": "AAAAAAAAAAAAAAAAAAAAAA==",
"MemoryCacheEnabled": true,
"RedisCacheEnabled": false
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

namespace ModularNet.IntegrationTests.Infrastructure;

/// <summary>
/// Defines an xUnit test collection to share a single <see cref="ModularNetWebApplicationFactory"/> instance
/// across all integration test classes, avoiding redundant host startups and concurrent port/resource conflicts.
/// </summary>
[CollectionDefinition("IntegrationTests")]
public class IntegrationTestCollection : ICollectionFixture<ModularNetWebApplicationFactory>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;

namespace ModularNet.IntegrationTests.Infrastructure;

/// <summary>
/// Custom <see cref="WebApplicationFactory{TEntryPoint}"/> for bootstrapping ModularNet in-memory
/// for integration testing.
/// Sets the hosting environment to "Development" and layers test configuration from integration test appsettings.
/// </summary>
public class ModularNetWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");

builder.ConfigureAppConfiguration((context, configBuilder) =>
{
configBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
});
}
}
42 changes: 28 additions & 14 deletions src/ModularNet.IntegrationTests/ModularNet.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<RootNamespace>ModularNet.IntegrationTests</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition=" '$(RunConfiguration)' == 'https' " />
<PropertyGroup Condition=" '$(RunConfiguration)' == 'http' " />
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="10.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="Moq" Version="4.21.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ModularNet.Api\ModularNet.Api.csproj" />
<ProjectReference Include="..\ModularNet.Business\ModularNet.Business.csproj" />
<ProjectReference Include="..\ModularNet.Domain\ModularNet.Domain.csproj" />
<ProjectReference Include="..\ModularNet.Infrastructure\ModularNet.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Communication.Email" Version="1.1.0" />
<PackageReference Include="FirebaseAdmin" Version="3.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.14" />
<PackageReference Include="Microsoft.TestPlatform" Version="18.11.0" />
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="18.11.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="runtime.native.System.Net.Http" Version="4.3.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Encodings.Web" Version="10.0.14" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="modularnet-firebase-adminsdk-dev.json" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\ModularNet.Api\modularnet-firebase-adminsdk-dev.json" Link="modularnet-firebase-adminsdk-dev.json" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\ModularNet.Api\EmailTemplates\**" Link="EmailTemplates\%(RecursiveDir)%(FileName)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
71 changes: 0 additions & 71 deletions src/ModularNet.IntegrationTests/Program.cs

This file was deleted.

Loading
Loading