Add test
This commit is contained in:
parent
e114259525
commit
e99d12b81d
18
AS400API.Tests/AS400API.Tests.csproj
Normal file
18
AS400API.Tests/AS400API.Tests.csproj
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.9" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AS400API.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
107
AS400API.Tests/ApiIntegrationTests.cs
Normal file
107
AS400API.Tests/ApiIntegrationTests.cs
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using AS400API.Auth;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace AS400API.Tests;
|
||||||
|
|
||||||
|
public sealed class ApiIntegrationTests : IClassFixture<WebApplicationFactory<Program>>
|
||||||
|
{
|
||||||
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly WebApplicationFactory<Program> _factory;
|
||||||
|
|
||||||
|
public ApiIntegrationTests(WebApplicationFactory<Program> factory)
|
||||||
|
{
|
||||||
|
_factory = factory.WithWebHostBuilder(builder =>
|
||||||
|
{
|
||||||
|
builder.UseSetting("DOTNET_ENVIRONMENT", "Development");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetRoot_ReturnsExpectedPayload()
|
||||||
|
{
|
||||||
|
var client = _factory.CreateClient();
|
||||||
|
|
||||||
|
var response = await client.GetAsync("/");
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
Assert.True(response.IsSuccessStatusCode, FormatFailure("GET /", response.StatusCode, body));
|
||||||
|
|
||||||
|
var payload = JsonSerializer.Deserialize<RootResponse>(body, JsonOptions);
|
||||||
|
Assert.NotNull(payload);
|
||||||
|
Assert.Equal("AS400API", payload!.Name);
|
||||||
|
Assert.Equal("ok", payload.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Login_WithValidCredentials_ReturnsToken()
|
||||||
|
{
|
||||||
|
var client = _factory.CreateClient();
|
||||||
|
var request = new LoginRequest("admin", "Pass@123");
|
||||||
|
|
||||||
|
var response = await client.PostAsJsonAsync("/api/v1/auth/login", request, JsonOptions);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
Assert.True(response.IsSuccessStatusCode, FormatFailure("POST /api/v1/auth/login", response.StatusCode, body));
|
||||||
|
|
||||||
|
var payload = JsonSerializer.Deserialize<LoginResponse>(body, JsonOptions);
|
||||||
|
Assert.NotNull(payload);
|
||||||
|
Assert.Equal("Bearer", payload!.TokenType);
|
||||||
|
Assert.True(payload.ExpiresIn > 0);
|
||||||
|
Assert.False(string.IsNullOrWhiteSpace(payload.AccessToken));
|
||||||
|
Assert.Contains("Admin", payload.Roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Login_WithInvalidCredentials_ReturnsUnauthorized()
|
||||||
|
{
|
||||||
|
var client = _factory.CreateClient();
|
||||||
|
var request = new LoginRequest("admin", "wrong-password");
|
||||||
|
|
||||||
|
var response = await client.PostAsJsonAsync("/api/v1/auth/login", request, JsonOptions);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||||||
|
Assert.True(string.IsNullOrEmpty(body) || body.Contains("error", StringComparison.OrdinalIgnoreCase));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UsersMe_WithValidToken_ReturnsProfile()
|
||||||
|
{
|
||||||
|
var client = _factory.CreateClient();
|
||||||
|
var loginResponse = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("operator", "Pass@123"), JsonOptions);
|
||||||
|
var loginBody = await loginResponse.Content.ReadAsStringAsync();
|
||||||
|
Assert.True(loginResponse.IsSuccessStatusCode, FormatFailure("POST /api/v1/auth/login", loginResponse.StatusCode, loginBody));
|
||||||
|
|
||||||
|
var tokenPayload = JsonSerializer.Deserialize<LoginResponse>(loginBody, JsonOptions);
|
||||||
|
Assert.NotNull(tokenPayload);
|
||||||
|
|
||||||
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenPayload!.AccessToken);
|
||||||
|
|
||||||
|
var profileResponse = await client.GetAsync("/api/v1/users/me");
|
||||||
|
var profileBody = await profileResponse.Content.ReadAsStringAsync();
|
||||||
|
Assert.True(profileResponse.IsSuccessStatusCode, FormatFailure("GET /api/v1/users/me", profileResponse.StatusCode, profileBody));
|
||||||
|
|
||||||
|
var profile = JsonSerializer.Deserialize<UserProfileResponse>(profileBody, JsonOptions);
|
||||||
|
Assert.NotNull(profile);
|
||||||
|
Assert.Equal("operator", profile!.Username);
|
||||||
|
Assert.Contains("Operator", profile.Roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FormatFailure(string action, HttpStatusCode status, string body) =>
|
||||||
|
$"{action} failed with status {(int)status} ({status}). Body: {body}";
|
||||||
|
|
||||||
|
private sealed record RootResponse(string Name, string Status);
|
||||||
|
|
||||||
|
private sealed record LoginRequest(string Username, string Password);
|
||||||
|
|
||||||
|
private sealed record UserProfileResponse(string Username, string[] Roles);
|
||||||
|
}
|
@ -14,4 +14,7 @@
|
|||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="AS400API.Tests/**/*.cs" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
27
AS400API.sln
27
AS400API.sln
@ -1,19 +1,46 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.5.2.0
|
VisualStudioVersion = 17.5.2.0
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS400API", "AS400API.csproj", "{40CB5B53-77B8-B1FD-458C-805350CB09E8}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS400API", "AS400API.csproj", "{40CB5B53-77B8-B1FD-458C-805350CB09E8}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS400API.Tests", "AS400API.Tests\AS400API.Tests.csproj", "{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
Release|Any CPU = Release|Any CPU
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|Any CPU.Build.0 = Release|Any CPU
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{40CB5B53-77B8-B1FD-458C-805350CB09E8}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{37A21E3B-3EEB-4538-B085-0FD6BA85DE70}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -131,3 +131,5 @@ app.Run();
|
|||||||
|
|
||||||
// env DOTNET_ENVIRONMENT=Development dotnet run
|
// env DOTNET_ENVIRONMENT=Development dotnet run
|
||||||
// docker run -d --name sonarqube -p 9000:9000 sonarqube:lts-community
|
// docker run -d --name sonarqube -p 9000:9000 sonarqube:lts-community
|
||||||
|
|
||||||
|
public partial class Program { }
|
||||||
|
Loading…
Reference in New Issue
Block a user