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
1 change: 1 addition & 0 deletions Api.Integration.Tests/Api.Integration.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.20" />
<PackageReference Include="Testcontainers.MsSql" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 2 additions & 5 deletions Api.Integration.Tests/ClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;
using System.Net.Http.Headers;
using Api.Integration.Tests;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using NRZMyk.Server;
using NRZMyk.Services.Data;
using NRZMyk.Services.Data.Entities;

namespace PublicApiIntegrationTests
namespace Api.Integration.Tests
{
public static class ClientFactory
{
Expand Down
5 changes: 2 additions & 3 deletions Api.Integration.Tests/Organizations/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using NRZMyk.Services.Data.Entities;
using NRZMyk.Services.Interfaces;
using NUnit.Framework;
using PublicApiIntegrationTests;
using Organization = NRZMyk.Services.Data.Entities.Organization;

namespace Api.Integration.Tests.Organizations
Expand All @@ -17,7 +16,7 @@ public class ListTest
[OneTimeSetUp]
protected void SeedData()
{
using (var scope = ClientFactory.ServiceProvider.CreateScope())
using (var scope = TestcontainerDbClientFactory.ServiceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetService<ApplicationDbContext>();
db.Organizations.Add(new Organization
Expand All @@ -29,7 +28,7 @@ protected void SeedData()
[Test]
public async Task WhenCreatingValidSentinelEntry_RespondsWithCreate()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();

var organizations = await client.GetFromJsonAsync<List<Organization>>("api/organizations").ConfigureAwait(true);

Expand Down
17 changes: 9 additions & 8 deletions Api.Integration.Tests/SentinelEntries/CreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using NRZMyk.Services.Data.Entities;
using NRZMyk.Services.Services;
using NUnit.Framework;
using PublicApiIntegrationTests;
using Tynamix.ObjectFiller;

namespace Api.Integration.Tests.SentinelEntries
Expand All @@ -18,7 +17,7 @@ public class CreateTests
[Test]
public async Task WhenCreatingValidSentinelEntry_RespondsWithCreate()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var request = CreateValidRequest();

var createdEntry = await CreateValidEntry(client, request).ConfigureAwait(true);
Expand All @@ -30,7 +29,7 @@ public async Task WhenCreatingValidSentinelEntry_RespondsWithCreate()
[Test]
public async Task WhenCreatingWithValidPredecessorEntry_RespondsWithCreate()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var predecessor = CreateValidRequest();

var predecessorEntry = await CreateValidEntry(client, predecessor).ConfigureAwait(true);
Expand All @@ -47,7 +46,7 @@ public async Task WhenCreatingWithValidPredecessorEntry_RespondsWithCreate()
[Test]
public async Task WhenCreatingPredecessorCircle_RespondsWithCreate()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var predecessorRequest = CreateValidRequest();

// Arrange: Create entry with follow-up
Expand All @@ -74,7 +73,7 @@ public async Task WhenCreatingPredecessorCircle_RespondsWithCreate()
[Test]
public async Task WhenCreatingUnknownPredecessorEntry_RespondsWithBadRequest()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var request = CreateValidRequest();
request.PredecessorLaboratoryNumber = "SN-4242-0042";

Expand All @@ -89,7 +88,7 @@ public async Task WhenCreatingUnknownPredecessorEntry_RespondsWithBadRequest()
[Test]
public async Task WhenCreatingWithMissingSamplingDate_RespondsWithBadRequest()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var request = CreateValidRequest();
request.SamplingDate = null;

Expand All @@ -103,7 +102,7 @@ public async Task WhenCreatingWithMissingSamplingDate_RespondsWithBadRequest()
[Test]
public async Task WhenCreatingWithInvalidDepartmentCombination_RespondsWithBadRequest()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var request = CreateValidRequest();
request.InternalHospitalDepartmentType = InternalHospitalDepartmentType.Angiological;

Expand All @@ -117,7 +116,7 @@ public async Task WhenCreatingWithInvalidDepartmentCombination_RespondsWithBadRe
[Test]
public async Task WhenCreatingWithFutureSamplingDate_RespondsWithBadRequest()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();
var request = CreateValidRequest();
request.SamplingDate = DateTime.Now.AddDays(1);

Expand Down Expand Up @@ -151,6 +150,8 @@ private static SentinelEntryRequest CreateValidRequest()
request.SamplingDate = DateTime.Now.AddDays(-3);
request.PredecessorLaboratoryNumber = string.Empty;
request.HasPredecessor = YesNo.No;
request.AntimicrobialSensitivityTests = null;
request.Id = 0;
return request;
}
}
Expand Down
3 changes: 1 addition & 2 deletions Api.Integration.Tests/Swagger/SwaggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using PublicApiIntegrationTests;

namespace Api.Integration.Tests.Swagger
{
Expand All @@ -11,7 +10,7 @@ public class SwaggerTests
[Test]
public async Task WhenAccessingIndex_ReturnsPage()
{
var client = ClientFactory.CreateClient();
var client = TestcontainerDbClientFactory.CreateClient();

var response = await client.GetAsync("swagger/index.html").ConfigureAwait(true);

Expand Down
97 changes: 97 additions & 0 deletions Api.Integration.Tests/TestcontainerDbClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using FluentAssertions.Common;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NRZMyk.Server;
using NRZMyk.Services.Configuration;
using NRZMyk.Services.Data;
using Testcontainers.MsSql;

namespace Api.Integration.Tests
{
public static class TestcontainerDbClientFactory
{
private static readonly WebApplicationFactory<Program> _factory;
public static IServiceProvider ServiceProvider => _factory.Services;

private static readonly MsSqlContainer _dbContainer = new MsSqlBuilder()
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
.WithPassword("Strong_password_123!")

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "Strong_password_123!" flows to the [password](1) parameter in [call to method WithPassword](2).
.Build();

static TestcontainerDbClientFactory()
{
_dbContainer.StartAsync().Wait();

_factory = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable

Disposable 'WebApplicationFactory<Program>' is created but not disposed.
{
builder.ConfigureTestServices(services =>
{
services.AddAuthentication(TestAuthHandler.AuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
TestAuthHandler.AuthenticationScheme, _ => { });

var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<ApplicationDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}

services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(_dbContainer.GetConnectionString());
});
});
});

using (var scope = _factory.Services.CreateScope())
{
var services = scope.ServiceProvider;
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var configuration = services.GetRequiredService<IConfiguration>();
var seedSettings = configuration.Get<DatabaseSeedSettings>();

try
{
var catalogContext = services.GetRequiredService<ApplicationDbContext>();
ApplicationDbContextSeed.SeedAsync(catalogContext, loggerFactory, seedSettings.DatabaseSeed).Wait();
}
catch (Exception ex)
{
var logger = loggerFactory.CreateLogger<Program>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
Comment on lines +70 to +74

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
}

AppDomain.CurrentDomain.ProcessExit += Dispose;
}

private static void Dispose(object? sender, EventArgs e) {
var disposeContainerTask = _dbContainer.DisposeAsync().AsTask();
disposeContainerTask.Wait();
_factory.Dispose();
}

public static HttpClient CreateClient()
{
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false,
});

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(TestAuthHandler.AuthenticationScheme);
return client;
}
}
}
1 change: 0 additions & 1 deletion NRZMyk.Services.Tests/Services/ReminderServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void WhenExpectedNextSendingIsThisMonth_HumanReadableExpectedNextSendingS
[TestCase(8, 6, "vor 6 Monaten")]
[TestCase(19, 6, "vor einem Jahr")]
[TestCase(43, 6, "vor 3 Jahren")]
[TestCase(10, 2, "in einem Monat")]
public void WhenExpectedNextSendingIsChecked_HumanReadableExpectedNextSendingShowsValidInformation(int monthSinceLatestStrainArrival, int monthUntilNextArrival, string expectedNextSending)
{
var sut = CreateSut();
Expand Down