Skip to content
Open
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
38 changes: 31 additions & 7 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
# TESTS: UNIT, INTEGRATION, UI
# ---------------------------
test:
name: Run Unit, Integration and UI Tests
name: Run Unit, Partial Integration and UI Tests
if: ${{ github.event_name != 'workflow_dispatch' }}
permissions:
pull-requests: write
Expand Down Expand Up @@ -110,12 +110,12 @@ jobs:
working-directory: SAPPub.Web
run: npm ci

- name: Run All Tests with Coverage
- name: Run Tests with Coverage
env:
HEADED: 0
PLAYWRIGHT_IGNORE_HTTPS_ERRORS: true
run: |
TEST_PROJECTS=$(find ./Tests -type f -name "*.Tests.csproj" | sort)
TEST_PROJECTS=$(find ./Tests -type f -name "*.Tests.csproj" ! -path "*/SAPPub.Integration.Tests/*" | sort)

for proj in $TEST_PROJECTS; do
echo "=== Running tests in $proj ==="
Expand Down Expand Up @@ -210,6 +210,8 @@ jobs:
name: Deployment To Review (no refresh)
concurrency: deploy_review_${{ github.event.pull_request.number }}
needs: [build, test]
outputs:
environment_url: ${{ steps.deploy_review.outputs.environment_url }}
if: ${{ github.event_name == 'pull_request'
&& (
github.event.action == 'labeled' ||
Expand Down Expand Up @@ -399,6 +401,8 @@ jobs:
name: Deployment To Review (before refresh)
concurrency: deploy_review_${{ github.event.pull_request.number }}
needs: [build, test]
outputs:
environment_url: ${{ steps.deploy_review.outputs.environment_url }}
if: ${{ github.event_name == 'pull_request'
&& (
github.event.action == 'labeled' ||
Expand Down Expand Up @@ -434,9 +438,30 @@ jobs:
gcp-wip: ${{ vars.GCP_WIP }}
gcp-project-id: ${{ vars.GCP_PROJECT_ID }}

# ---------------------------
# INTEGRATION TESTS (POST DEPLOYMENT)
# ---------------------------
integration-tests:
name: Integration Tests (Post Deployment)
needs: [deploy-review-app]
uses: ./.github/workflows/integration-tests.yml
with:
environment: review
base_url: ${{ needs.deploy-review-app.outputs.environment_url }}

# ---------------------------
# INTEGRATION TESTS (POST DEPLOYMENT)
# ---------------------------
integration-tests_with_refresh:
name: Integration Tests (Post Deployment)
needs: [deploy-review-app-with-refresh]
uses: ./.github/workflows/integration-tests.yml
with:
environment: review
base_url: ${{ needs.deploy-review-app-with-refresh.outputs.environment_url }}

# ---------------------------
# DEPLOY TEST (AUTO)
# DEPLOY TO TEST (AUTO)
# ---------------------------
deploy:
name: Deploy to Test
Expand Down Expand Up @@ -476,7 +501,7 @@ jobs:
gcp-project-id: ${{ vars.GCP_PROJECT_ID }}

# ---------------------------
# REFRESH TEST DATABSE (AUTO - IF REQUIRED)
# REFRESH TEST DATABASE (AUTO - IF REQUIRED)
# ---------------------------

detect-refresh-data-on-merge:
Expand Down Expand Up @@ -533,7 +558,6 @@ jobs:
actions: read
packages: write


# ---------------------------
# MANUAL DEPLOY
# ---------------------------
Expand Down Expand Up @@ -629,4 +653,4 @@ jobs:
healthcheck: healthcheck
teams-webhook-url: ${{ secrets.TEAMS_WEBHOOK_URL }}
service: ${{ vars.TEAMS_MSG_SERVICE_NAME }}
terraform-base: terraform/domains/environment_domains
terraform-base: terraform/domains/environment_domains
61 changes: 61 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Integration Tests

on:
workflow_call:
inputs:
environment:
required: true
type: string
base_url:
required: true
type: string

jobs:
integration-tests:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}

steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Restore dependencies
run: dotnet restore

- name: Build integration tests
run: dotnet build Tests/SAPPub.Integration.Tests/SAPPub.IntegrationTests.csproj --configuration Release

- name: Install Playwright browsers
run: pwsh ./Tests/SAPPub.Integration.Tests/bin/Release/net8.0/playwright.ps1 install

- name: Run integration tests
env:
HEADED: 0
PLAYWRIGHT_IGNORE_HTTPS_ERRORS: true
ASPNETCORE_ENVIRONMENT: CI
BASE_URL: ${{ inputs.base_url }}
run: |
dotnet test Tests/SAPPub.Integration.Tests/SAPPub.IntegrationTests.csproj \
--no-build \
--configuration Release \
--results-directory "TestResults" \
--logger "trx;LogFileName=integration-tests.trx"

- name: Upload Test Result Files
uses: actions/upload-artifact@v4
if: always()
with:
name: post-deployment-test-results
path: "**/TestResults/**/*"

- name: Publish Test Results
uses: dorny/test-reporter@v2
if: always()
with:
reporter: dotnet-trx
path: "**/TestResults/**/*.trx"
name: Post Deployment Test Results
10 changes: 6 additions & 4 deletions SAPPub.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ public static void Main(string[] args)
// Only required for real runtime environments
if (builder.Environment.IsDevelopment() || builder.Environment.IsProduction() || builder.Environment.IsStaging())
throw new InvalidOperationException("Connection string 'PostgresConnectionString' is not configured.");

// For Testing/UITests: use a harmless dummy so nothing accidentally connects
connectionString = "Host=127.0.0.1;Port=1;Database=x;Username=x;Password=x;Timeout=1;Command Timeout=1";
}

builder.Services.AddSingleton<NpgsqlDataSource>(_ => NpgsqlDataSource.Create(connectionString));
builder.Services.AddSingleton<NpgsqlDataSource>(_ =>
{
var builder = new NpgsqlDataSourceBuilder(connectionString);
builder.EnableParameterLogging();
return builder.Build();
});

// Big Query client configuration
builder.Services.AddDfeAnalytics().AddAspNetCoreIntegration(options =>
Expand Down
7 changes: 7 additions & 0 deletions SAPPub.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPData", "SAPData\SAPData.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPData.Unit.Tests", "Tests\SAPData.Tests.Unit\SAPData.Unit.Tests.csproj", "{292387F8-1210-7181-8056-86EC3D855329}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SAPPub.IntegrationTests", "Tests\SAPPub.Integration.Tests\SAPPub.IntegrationTests.csproj", "{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,6 +69,10 @@ Global
{292387F8-1210-7181-8056-86EC3D855329}.Debug|Any CPU.Build.0 = Debug|Any CPU
{292387F8-1210-7181-8056-86EC3D855329}.Release|Any CPU.ActiveCfg = Release|Any CPU
{292387F8-1210-7181-8056-86EC3D855329}.Release|Any CPU.Build.0 = Release|Any CPU
{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -75,6 +81,7 @@ Global
{FF6CC6BE-8BD4-4353-BA24-F0B562F73534} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{EB1D1EAE-E264-4FF5-ACB9-8614FD694C69} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{5F164DC3-0185-4D92-B689-7A004F81C232} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{D90BBB38-FA34-5ABD-F17F-F30AF1D84C6A} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{292387F8-1210-7181-8056-86EC3D855329} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
72 changes: 72 additions & 0 deletions Tests/SAPPub.Integration.Tests/Infrastructure/TestBasePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.Playwright;
using Microsoft.Playwright.Xunit;

namespace SAPPub.Integration.Tests;

public abstract class BasePageTest : PageTest
{
public override BrowserNewContextOptions ContextOptions()
{
var baseUrl = Environment.GetEnvironmentVariable("BASE_URL");

if (string.IsNullOrWhiteSpace(baseUrl))
{
throw new InvalidOperationException(
"BASE_URL environment variable has not been configured.");
}

return new BrowserNewContextOptions
{
BaseURL = baseUrl.TrimEnd('/'),
IgnoreHTTPSErrors = true,
ViewportSize = new() { Width = 1280, Height = 720 },
Locale = "en-GB",
TimezoneId = "Europe/London",
JavaScriptEnabled = true,
};
}

public override async Task InitializeAsync()
{
await base.InitializeAsync();

Page.SetDefaultTimeout((float)TimeSpan.FromSeconds(60).TotalMilliseconds);
Page.SetDefaultNavigationTimeout((float)TimeSpan.FromSeconds(100).TotalMilliseconds);
}

public async Task WaitForSearchInputsAsync(int timeoutMs = 5000)
{
var selector = "input[name='__Query'], input[name='Query'][type='hidden'], input[name='Query']";
await Page.WaitForSelectorAsync(selector, new() { Timeout = timeoutMs });
await Page.WaitForTimeoutAsync(100);
}
public async Task<ILocator> GetQueryInputLocatorAsync(int checkTimeoutMs = 1000)
{
var jsLocator = Page.Locator("input[name='__Query']");
try
{
if (await jsLocator.CountAsync() > 0)
{
var isVisible = await jsLocator.IsVisibleAsync();
if (isVisible) return jsLocator;
}

var serverLocator = Page.Locator("input[name='Query']");
if (await serverLocator.CountAsync() > 0) return serverLocator;

var found = await Page.WaitForSelectorAsync("input[name='__Query'], input[name='Query']", new() { Timeout = checkTimeoutMs });
if (found != null)
{
var nameAttr = await found.GetAttributeAsync("name");
if (nameAttr == "__Query")
return Page.Locator("input[name='__Query']");
return Page.Locator("input[name='Query']");
}
return Page.Locator("input[name='Query']");
}
catch
{
return Page.Locator("input[name='Query']");
}
}
}
10 changes: 10 additions & 0 deletions Tests/SAPPub.Integration.Tests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"PlaywrightTests": {
"commandName": "Project",
"environmentVariables": {
"BASE_URL": "https://localhost:5001"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<HEADED>1</HEADED>
<BASE_URL>http://localhost:5013</BASE_URL>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
34 changes: 34 additions & 0 deletions Tests/SAPPub.Integration.Tests/SAPPub.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<None Remove="Properties\launchSettings.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Microsoft.Playwright" Version="1.57.0" />
<PackageReference Include="Microsoft.Playwright.Xunit" Version="1.57.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Loading
Loading