Compare commits
10 Commits
0850a1518e
...
17427ca3d0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17427ca3d0 | ||
|
|
07bdf36eb1 | ||
|
|
1555144f03 | ||
|
|
5aaacbe3b1 | ||
|
|
38a28113a3 | ||
|
|
ba400ade05 | ||
|
|
03e31f4c52 | ||
|
|
5383c5d1b0 | ||
|
|
b75b7512c2 | ||
|
|
a1014bd009 |
2
Create-Migration.ps1
Normal file
2
Create-Migration.ps1
Normal file
@@ -0,0 +1,2 @@
|
||||
dotnet ef migrations add $args[0] --project .\src\WebApi\WebApi.csproj --output-dir Persistence/Migrations
|
||||
dotnet ef migrations script --idempotent --project .\src\WebApi\WebApi.csproj --output migrations/migration.sql
|
||||
59
README.md
59
README.md
@@ -1 +1,58 @@
|
||||
# vegasco-server
|
||||
# Vegasco Server
|
||||
|
||||
Backend for the vegasco (***VE***hicle ***GAS*** ***CO***nsumption) application.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Configuration
|
||||
|
||||
| Configuration | Description | Default | Required |
|
||||
| --- | --- | --- | --- |
|
||||
| JWT:Authority | The authority of the JWT token. | - | true |
|
||||
| JWT:Audience | The audience of the JWT token. | - | true |
|
||||
| JWT:Issuer | The issuer of the JWT token. | - | true |
|
||||
| JWT:NameClaimType | The type of the name claim. | `http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name` (C# constant `ClaimTypes.Name` | false |
|
||||
|
||||
The application uses the prefix `Vegasco_` for environment variable names. The prefix is removed when the application reads the environment variables and duplicate entries are overwritten by the environment variables.
|
||||
|
||||
Example:
|
||||
|
||||
- `foo=bar1`
|
||||
- `Vegasco_foo=bar2`
|
||||
|
||||
Results in:
|
||||
|
||||
- `foo=bar2`
|
||||
- `Vegasco_foo=bar2`
|
||||
|
||||
Configuration hierarchy in environment variables is usually denoted using a colon (`:`). But because on some systems the colon character is a reserved character, you can use a double underscore (`__`) as an alternative. The application will replace the double underscore with a colon when reading the environment variables.
|
||||
|
||||
Example:
|
||||
|
||||
The environment variable `foo__bar=value` (as well as `Vegasco_foo__bar=value`) will be converted to `foo:bar=value` in the application.
|
||||
|
||||
### Configuration examples
|
||||
|
||||
As environment variables:
|
||||
|
||||
```env
|
||||
Vegasco_JWT__Authority=https://example.authority.com
|
||||
Vegasco_JWT__Audience=example-audience
|
||||
Vegasco_JWT__Issuer=https://example.authority.com/realms/example-realm/
|
||||
Vegasco_JWT__NameClaimType=preferred_username
|
||||
```
|
||||
|
||||
As appsettings.json (or a environment specific appsettings.*.json):
|
||||
|
||||
**Note: the `Vegasco_` prefix is only for environment variables**
|
||||
|
||||
```json
|
||||
{
|
||||
"JWT": {
|
||||
"Authority": "https://example.authority.com/realms/example-realm",
|
||||
"Audience": "example-audience",
|
||||
"Issuer": "https://example.authority.com/realms/example-realm/",
|
||||
"NameClaimType": "preferred_username"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
1
Run-PostgresDb.ps1
Normal file
1
Run-PostgresDb.ps1
Normal file
@@ -0,0 +1 @@
|
||||
docker run --rm -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres:16.3-alpine
|
||||
48
migrations/migration.sql
Normal file
48
migrations/migration.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
|
||||
"MigrationId" character varying(150) NOT NULL,
|
||||
"ProductVersion" character varying(32) NOT NULL,
|
||||
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
|
||||
);
|
||||
|
||||
START TRANSACTION;
|
||||
|
||||
|
||||
DO $EF$
|
||||
BEGIN
|
||||
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240803173803_AddCarsAndUsers') THEN
|
||||
CREATE TABLE "Users" (
|
||||
"Id" text NOT NULL,
|
||||
CONSTRAINT "PK_Users" PRIMARY KEY ("Id")
|
||||
);
|
||||
END IF;
|
||||
END $EF$;
|
||||
|
||||
DO $EF$
|
||||
BEGIN
|
||||
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240803173803_AddCarsAndUsers') THEN
|
||||
CREATE TABLE "Cars" (
|
||||
"Id" uuid NOT NULL,
|
||||
"Name" character varying(50) NOT NULL,
|
||||
"UserId" text NOT NULL,
|
||||
CONSTRAINT "PK_Cars" PRIMARY KEY ("Id"),
|
||||
CONSTRAINT "FK_Cars_Users_UserId" FOREIGN KEY ("UserId") REFERENCES "Users" ("Id") ON DELETE CASCADE
|
||||
);
|
||||
END IF;
|
||||
END $EF$;
|
||||
|
||||
DO $EF$
|
||||
BEGIN
|
||||
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240803173803_AddCarsAndUsers') THEN
|
||||
CREATE INDEX "IX_Cars_UserId" ON "Cars" ("UserId");
|
||||
END IF;
|
||||
END $EF$;
|
||||
|
||||
DO $EF$
|
||||
BEGIN
|
||||
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240803173803_AddCarsAndUsers') THEN
|
||||
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
|
||||
VALUES ('20240803173803_AddCarsAndUsers', '8.0.7');
|
||||
END IF;
|
||||
END $EF$;
|
||||
COMMIT;
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>4bf893d3-0c16-41ec-8b46-2768d841215d</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>..\..</DockerfileContext>
|
||||
<RootNamespace>Vegasco.Api</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,6 +0,0 @@
|
||||
using Vegasco.Api;
|
||||
|
||||
WebApplication.CreateBuilder(args)
|
||||
.ConfigureServices()
|
||||
.ConfigureRequestPipeline()
|
||||
.Run();
|
||||
@@ -1,29 +0,0 @@
|
||||
namespace Vegasco.Api;
|
||||
|
||||
internal static class StartupExtensions
|
||||
{
|
||||
internal static WebApplication ConfigureServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
internal static WebApplication ConfigureRequestPipeline(this WebApplication app)
|
||||
{
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapHealthChecks("/health");
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/WebApi/Authentication/JwtOptions.cs
Normal file
28
src/WebApi/Authentication/JwtOptions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Vegasco.WebApi.Authentication;
|
||||
|
||||
public class JwtOptions
|
||||
{
|
||||
public const string SectionName = "JWT";
|
||||
|
||||
public string ValidAudience { get; set; } = "";
|
||||
|
||||
public string MetadataUrl { get; set; } = "";
|
||||
|
||||
public string? NameClaimType { get; set; }
|
||||
|
||||
public bool AllowHttpMetadataUrl { get; set; }
|
||||
}
|
||||
|
||||
public class JwtOptionsValidator : AbstractValidator<JwtOptions>
|
||||
{
|
||||
public JwtOptionsValidator()
|
||||
{
|
||||
RuleFor(x => x.ValidAudience)
|
||||
.NotEmpty();
|
||||
|
||||
RuleFor(x => x.MetadataUrl)
|
||||
.NotEmpty();
|
||||
}
|
||||
}
|
||||
78
src/WebApi/Authentication/UserAccessor.cs
Normal file
78
src/WebApi/Authentication/UserAccessor.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Vegasco.WebApi.Authentication;
|
||||
|
||||
public sealed class UserAccessor
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IOptions<JwtOptions> _jwtOptions;
|
||||
|
||||
/// <summary>
|
||||
/// Stores the username upon first retrieval
|
||||
/// </summary>
|
||||
private string? _cachedUsername;
|
||||
|
||||
/// <summary>
|
||||
/// Stores the id upon first retrieval
|
||||
/// </summary>
|
||||
private string? _cachedId;
|
||||
|
||||
public UserAccessor(IHttpContextAccessor httpContextAccessor, IOptions<JwtOptions> jwtOptions)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_jwtOptions = jwtOptions;
|
||||
}
|
||||
|
||||
public string GetUsername()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_cachedUsername))
|
||||
{
|
||||
_cachedUsername = GetClaimValue(_jwtOptions.Value.NameClaimType ?? ClaimTypes.Name);
|
||||
}
|
||||
|
||||
return _cachedUsername;
|
||||
}
|
||||
|
||||
public string GetUserId()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_cachedId))
|
||||
{
|
||||
_cachedId = GetClaimValue(ClaimTypes.NameIdentifier);
|
||||
}
|
||||
|
||||
return _cachedId;
|
||||
}
|
||||
|
||||
private string GetClaimValue(string claimType)
|
||||
{
|
||||
var httpContext = _httpContextAccessor.HttpContext;
|
||||
|
||||
if (httpContext is null)
|
||||
{
|
||||
ThrowForMissingHttpContext();
|
||||
}
|
||||
|
||||
var claimValue = httpContext.User.FindFirstValue(claimType);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(claimValue))
|
||||
{
|
||||
ThrowForMissingClaim(claimType);
|
||||
}
|
||||
|
||||
return claimValue;
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
private static void ThrowForMissingHttpContext()
|
||||
{
|
||||
throw new InvalidOperationException("No HttpContext available.");
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
private static void ThrowForMissingClaim(string claimType)
|
||||
{
|
||||
throw new InvalidOperationException($"No claim of type '{claimType}' found on the current user.");
|
||||
}
|
||||
}
|
||||
36
src/WebApi/Cars/Car.cs
Normal file
36
src/WebApi/Cars/Car.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vegasco.WebApi.Users;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public class Car
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string UserId { get; set; } = "";
|
||||
|
||||
public virtual User User { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class CarTableConfiguration : IEntityTypeConfiguration<Car>
|
||||
{
|
||||
public const int NameMaxLength = 50;
|
||||
|
||||
public void Configure(EntityTypeBuilder<Car> builder)
|
||||
{
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Name)
|
||||
.IsRequired()
|
||||
.HasMaxLength(NameMaxLength);
|
||||
|
||||
builder.Property(x => x.UserId)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasOne(x => x.User)
|
||||
.WithMany(x => x.Cars);
|
||||
}
|
||||
}
|
||||
69
src/WebApi/Cars/CreateCar.cs
Normal file
69
src/WebApi/Cars/CreateCar.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
using Vegasco.WebApi.Common;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
using Vegasco.WebApi.Users;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public static class CreateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPost("cars", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
ApplicationDbContext dbContext,
|
||||
UserAccessor userAccessor,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken: cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
var userId = userAccessor.GetUserId();
|
||||
|
||||
var user = await dbContext.Users.FindAsync([userId], cancellationToken: cancellationToken);
|
||||
if (user is null)
|
||||
{
|
||||
user = new User
|
||||
{
|
||||
Id = userId
|
||||
};
|
||||
await dbContext.Users.AddAsync(user, cancellationToken);
|
||||
}
|
||||
|
||||
Car car = new()
|
||||
{
|
||||
Name = request.Name,
|
||||
UserId = userId
|
||||
};
|
||||
|
||||
await dbContext.Cars.AddAsync(car, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id, car.Name);
|
||||
return TypedResults.Created($"/v1/cars/{car.Id}", response);
|
||||
}
|
||||
}
|
||||
31
src/WebApi/Cars/DeleteCar.cs
Normal file
31
src/WebApi/Cars/DeleteCar.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public static class DeleteCar
|
||||
{
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapDelete("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var car = await dbContext.Cars.FindAsync([id], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
dbContext.Cars.Remove(car);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.NoContent();
|
||||
}
|
||||
}
|
||||
31
src/WebApi/Cars/GetCar.cs
Normal file
31
src/WebApi/Cars/GetCar.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public static class GetCar
|
||||
{
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var car = await dbContext.Cars.FindAsync([id], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
var response = new Response(car.Id, car.Name);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
27
src/WebApi/Cars/GetCars.cs
Normal file
27
src/WebApi/Cars/GetCars.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public static class GetCars
|
||||
{
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("cars", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var cars = await dbContext.Cars
|
||||
.Select(x => new Response(x.Id, x.Name))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return TypedResults.Ok(cars);
|
||||
}
|
||||
}
|
||||
58
src/WebApi/Cars/UpdateCar.cs
Normal file
58
src/WebApi/Cars/UpdateCar.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
using Vegasco.WebApi.Common;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Cars;
|
||||
|
||||
public static class UpdateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPut("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
ApplicationDbContext dbContext,
|
||||
UserAccessor userAccessor,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
var car = await dbContext.Cars.FindAsync([id], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
car.Name = request.Name;
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id, car.Name);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
11
src/WebApi/Common/Constants.cs
Normal file
11
src/WebApi/Common/Constants.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
public const string AppOtelName = "Vegasco.Api";
|
||||
|
||||
public static class Authorization
|
||||
{
|
||||
public const string RequireAuthenticatedUserPolicy = "RequireAuthenticatedUser";
|
||||
}
|
||||
}
|
||||
165
src/WebApi/Common/DependencyInjectionExtensions.cs
Normal file
165
src/WebApi/Common/DependencyInjectionExtensions.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using Asp.Versioning;
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenTelemetry.Trace;
|
||||
using System.Diagnostics;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
using Vegasco.WebApi.Endpoints;
|
||||
using Vegasco.WebApi.Endpoints.OpenApi;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
public static class DependencyInjectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds all the WebApi related services to the Dependency Injection container.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="environment"></param>
|
||||
public static void AddWebApiServices(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
|
||||
{
|
||||
services
|
||||
.AddMiscellaneousServices()
|
||||
.AddOpenApi()
|
||||
.AddApiVersioning()
|
||||
.AddOtel()
|
||||
.AddAuthenticationAndAuthorization(environment)
|
||||
.AddDbContext(configuration);
|
||||
}
|
||||
|
||||
private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddResponseCompression();
|
||||
|
||||
services.AddValidatorsFromAssemblies(
|
||||
[
|
||||
typeof(IWebApiMarker).Assembly
|
||||
], ServiceLifetime.Singleton);
|
||||
|
||||
services.AddHealthChecks();
|
||||
services.AddEndpointsFromAssemblyContaining<IWebApiMarker>();
|
||||
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddOpenApi(this IServiceCollection services)
|
||||
{
|
||||
services.ConfigureOptions<ConfigureSwaggerGenOptions>();
|
||||
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(o =>
|
||||
{
|
||||
o.CustomSchemaIds(type =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(type.FullName))
|
||||
{
|
||||
return type.Name;
|
||||
}
|
||||
|
||||
var fullClassName = type.FullName;
|
||||
|
||||
if (!string.IsNullOrEmpty(type.Namespace))
|
||||
{
|
||||
fullClassName = fullClassName
|
||||
.Replace(type.Namespace, "")
|
||||
.TrimStart('.');
|
||||
}
|
||||
|
||||
return fullClassName;
|
||||
});
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddApiVersioning(this IServiceCollection services)
|
||||
{
|
||||
services.AddApiVersioning(o =>
|
||||
{
|
||||
o.DefaultApiVersion = new ApiVersion(1);
|
||||
o.ApiVersionReader = new UrlSegmentApiVersionReader();
|
||||
o.ReportApiVersions = true;
|
||||
})
|
||||
.AddApiExplorer(o =>
|
||||
{
|
||||
o.GroupNameFormat = "'v'V";
|
||||
o.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddOtel(this IServiceCollection services)
|
||||
{
|
||||
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
|
||||
|
||||
ActivitySource activitySource = new(Constants.AppOtelName);
|
||||
services.AddSingleton(activitySource);
|
||||
|
||||
services.AddOpenTelemetry()
|
||||
.WithTracing(t =>
|
||||
{
|
||||
t.AddAspNetCoreInstrumentation()
|
||||
.AddHttpClientInstrumentation()
|
||||
.AddOtlpExporter()
|
||||
.AddSource(activitySource.Name);
|
||||
})
|
||||
.WithMetrics();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services, IHostEnvironment environment)
|
||||
{
|
||||
services.AddOptions<JwtOptions>()
|
||||
.BindConfiguration(JwtOptions.SectionName)
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
var jwtOptions = services.BuildServiceProvider().GetRequiredService<IOptions<JwtOptions>>();
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
|
||||
{
|
||||
o.MetadataAddress = jwtOptions.Value.MetadataUrl;
|
||||
|
||||
o.TokenValidationParameters.ValidAudience = jwtOptions.Value.ValidAudience;
|
||||
o.TokenValidationParameters.ValidateAudience = true;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(jwtOptions.Value.NameClaimType))
|
||||
{
|
||||
o.TokenValidationParameters.NameClaimType = jwtOptions.Value.NameClaimType;
|
||||
}
|
||||
|
||||
o.RequireHttpsMetadata = !jwtOptions.Value.AllowHttpMetadataUrl && !environment.IsDevelopment();
|
||||
});
|
||||
|
||||
services.AddAuthorizationBuilder()
|
||||
.AddPolicy(Constants.Authorization.RequireAuthenticatedUserPolicy, p => p
|
||||
.RequireAuthenticatedUser()
|
||||
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
|
||||
|
||||
services.AddScoped<UserAccessor>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddDbContext(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddDbContext<ApplicationDbContext>(o =>
|
||||
{
|
||||
o.UseNpgsql(configuration.GetConnectionString("Database"), c =>
|
||||
{
|
||||
c.EnableRetryOnFailure();
|
||||
});
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
36
src/WebApi/Common/FluentValidationOptions.cs
Normal file
36
src/WebApi/Common/FluentValidationOptions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
public class FluentValidationOptions<TOptions> : IValidateOptions<TOptions>
|
||||
where TOptions : class
|
||||
{
|
||||
private readonly IEnumerable<IValidator<TOptions>> _validators;
|
||||
|
||||
public string? Name { get; set; }
|
||||
|
||||
public FluentValidationOptions(string? name, IEnumerable<IValidator<TOptions>> validators)
|
||||
{
|
||||
Name = name;
|
||||
_validators = validators;
|
||||
}
|
||||
|
||||
public ValidateOptionsResult Validate(string? name, TOptions options)
|
||||
{
|
||||
if (name is not null && name != Name)
|
||||
{
|
||||
return ValidateOptionsResult.Skip;
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
var failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
if (failedValidations.Count == 0)
|
||||
{
|
||||
return ValidateOptionsResult.Success;
|
||||
}
|
||||
|
||||
return ValidateOptionsResult.Fail(failedValidations.SelectMany(x => x.Errors.Select(x => x.ErrorMessage)));
|
||||
}
|
||||
}
|
||||
3
src/WebApi/Common/IWebApiMarker.cs
Normal file
3
src/WebApi/Common/IWebApiMarker.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
public interface IWebApiMarker;
|
||||
62
src/WebApi/Common/StartupExtensions.cs
Normal file
62
src/WebApi/Common/StartupExtensions.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Asp.Versioning.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using System.Globalization;
|
||||
using Vegasco.WebApi.Endpoints;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
internal static class StartupExtensions
|
||||
{
|
||||
internal static WebApplication ConfigureServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Configuration.AddEnvironmentVariables("Vegasco_");
|
||||
|
||||
builder.Services.AddWebApiServices(builder.Configuration, builder.Environment);
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
return app;
|
||||
}
|
||||
|
||||
internal static WebApplication ConfigureRequestPipeline(this WebApplication app)
|
||||
{
|
||||
app.UseRequestLocalization(o =>
|
||||
{
|
||||
o.SupportedCultures =
|
||||
[
|
||||
new CultureInfo("en")
|
||||
];
|
||||
|
||||
o.SupportedUICultures = o.SupportedCultures;
|
||||
|
||||
CultureInfo defaultCulture = o.SupportedCultures[0];
|
||||
o.DefaultRequestCulture = new RequestCulture(defaultCulture);
|
||||
});
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapHealthChecks("/health");
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapEndpoints();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(o =>
|
||||
{
|
||||
// Create a Swagger endpoint for each API version
|
||||
IReadOnlyList<ApiVersionDescription> apiVersions = app.DescribeApiVersions();
|
||||
foreach (ApiVersionDescription apiVersionDescription in apiVersions)
|
||||
{
|
||||
string url = $"/swagger/{apiVersionDescription.GroupName}/swagger.json";
|
||||
string name = apiVersionDescription.GroupName.ToUpperInvariant();
|
||||
o.SwaggerEndpoint(url, name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
62
src/WebApi/Common/ValidatorExtensions.cs
Normal file
62
src/WebApi/Common/ValidatorExtensions.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
|
||||
public static class ValidatorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously validates an instance of <typeparamref name="T"/> against all <see cref="IValidator{T}"/> instances in <paramref name="validators"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="validators"></param>
|
||||
/// <param name="instance"></param>
|
||||
/// <returns>The failed validation results.</returns>
|
||||
public static async Task<List<ValidationResult>> ValidateAllAsync<T>(this IEnumerable<IValidator<T>> validators, T instance, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var validationTasks = validators
|
||||
.Select(validator => validator.ValidateAsync(instance, cancellationToken))
|
||||
.ToList();
|
||||
|
||||
await Task.WhenAll(validationTasks);
|
||||
|
||||
List<ValidationResult> failedValidations = validationTasks
|
||||
.Select(x => x.Result)
|
||||
.Where(x => !x.IsValid)
|
||||
.ToList();
|
||||
|
||||
return failedValidations;
|
||||
}
|
||||
|
||||
public static Dictionary<string, string[]> ToCombinedDictionary(this IEnumerable<ValidationResult> validationResults)
|
||||
{
|
||||
// Use a hash set to avoid duplicate error messages.
|
||||
Dictionary<string, HashSet<string>> combinedErrors = [];
|
||||
|
||||
foreach (var error in validationResults.SelectMany(x => x.Errors))
|
||||
{
|
||||
if (!combinedErrors.TryGetValue(error.PropertyName, out HashSet<string>? value))
|
||||
{
|
||||
value = ([error.ErrorMessage]);
|
||||
combinedErrors[error.PropertyName] = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
value.Add(error.ErrorMessage);
|
||||
}
|
||||
|
||||
return combinedErrors.ToDictionary(x => x.Key, x => x.Value.ToArray());
|
||||
}
|
||||
|
||||
public static OptionsBuilder<T> ValidateFluently<T>(this OptionsBuilder<T> builder)
|
||||
where T : class
|
||||
{
|
||||
builder.Services.AddTransient<IValidateOptions<T>>(serviceProvider =>
|
||||
{
|
||||
var validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
return new FluentValidationOptions<T>(builder.Name, validators);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,17 @@ EXPOSE 8081
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["src/Api/Api.csproj", "src/Api/"]
|
||||
RUN dotnet restore "./src/Api/Api.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/src/Api"
|
||||
RUN dotnet build "./Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
COPY ["WebApi.csproj", "src/WebApi/"]
|
||||
RUN dotnet restore "./src/WebApi/WebApi.csproj"
|
||||
COPY . src/WebApi
|
||||
WORKDIR "/src/src/WebApi"
|
||||
RUN dotnet build "./WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
RUN dotnet publish "./WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Api.dll"]
|
||||
ENTRYPOINT ["dotnet", "WebApi.dll"]
|
||||
43
src/WebApi/Endpoints/EndpointExtensions.cs
Normal file
43
src/WebApi/Endpoints/EndpointExtensions.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Asp.Versioning.Builder;
|
||||
using Asp.Versioning.Conventions;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Common;
|
||||
|
||||
namespace Vegasco.WebApi.Endpoints;
|
||||
|
||||
public static class EndpointExtensions
|
||||
{
|
||||
public static IServiceCollection AddEndpointsFromAssemblyContaining<T>(this IServiceCollection services)
|
||||
{
|
||||
var assembly = typeof(T).Assembly;
|
||||
|
||||
ServiceDescriptor[] serviceDescriptors = assembly
|
||||
.DefinedTypes
|
||||
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
|
||||
type.IsAssignableTo(typeof(IEndpoint)))
|
||||
.Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type))
|
||||
.ToArray();
|
||||
|
||||
services.TryAddEnumerable(serviceDescriptors);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static void MapEndpoints(this IEndpointRouteBuilder builder)
|
||||
{
|
||||
ApiVersionSet apiVersionSet = builder.NewApiVersionSet()
|
||||
.HasApiVersion(1.0)
|
||||
.Build();
|
||||
|
||||
RouteGroupBuilder versionedApis = builder.MapGroup("/v{apiVersion:apiVersion}")
|
||||
.WithApiVersionSet(apiVersionSet)
|
||||
.RequireAuthorization(Constants.Authorization.RequireAuthenticatedUserPolicy);
|
||||
|
||||
GetCar.MapEndpoint(versionedApis);
|
||||
GetCars.MapEndpoint(versionedApis);
|
||||
CreateCar.MapEndpoint(versionedApis);
|
||||
UpdateCar.MapEndpoint(versionedApis);
|
||||
DeleteCar.MapEndpoint(versionedApis);
|
||||
}
|
||||
}
|
||||
6
src/WebApi/Endpoints/IEndpoint.cs
Normal file
6
src/WebApi/Endpoints/IEndpoint.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Vegasco.WebApi.Endpoints;
|
||||
|
||||
public interface IEndpoint
|
||||
{
|
||||
void MapEndpoint(IEndpointRouteBuilder builder);
|
||||
}
|
||||
56
src/WebApi/Endpoints/OpenApi/ConfigureSwaggerGenOptions.cs
Normal file
56
src/WebApi/Endpoints/OpenApi/ConfigureSwaggerGenOptions.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Asp.Versioning.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace Vegasco.WebApi.Endpoints.OpenApi;
|
||||
|
||||
/// <summary>
|
||||
/// Registers each api version as its own swagger document.
|
||||
/// </summary>
|
||||
/// <param name="versionDescriptionProvider"></param>
|
||||
public class ConfigureSwaggerGenOptions(
|
||||
IApiVersionDescriptionProvider versionDescriptionProvider)
|
||||
: IConfigureNamedOptions<SwaggerGenOptions>
|
||||
{
|
||||
private readonly IApiVersionDescriptionProvider _versionDescriptionProvider = versionDescriptionProvider;
|
||||
|
||||
public void Configure(SwaggerGenOptions options)
|
||||
{
|
||||
foreach (ApiVersionDescription description in _versionDescriptionProvider.ApiVersionDescriptions)
|
||||
{
|
||||
OpenApiSecurityScheme securityScheme = new()
|
||||
{
|
||||
Name = "Bearer",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Id = IdentityConstants.BearerScheme,
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
};
|
||||
options.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
|
||||
|
||||
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{ securityScheme, Array.Empty<string>() }
|
||||
});
|
||||
|
||||
OpenApiInfo openApiInfo = new()
|
||||
{
|
||||
Title = "Vegasco API",
|
||||
Version = description.ApiVersion.ToString()
|
||||
};
|
||||
|
||||
options.SwaggerDoc(description.GroupName, openApiInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void Configure(string? name, SwaggerGenOptions options)
|
||||
{
|
||||
Configure(options);
|
||||
}
|
||||
}
|
||||
6
src/WebApi/Endpoints/OpenApi/SwaggerDocConstants.cs
Normal file
6
src/WebApi/Endpoints/OpenApi/SwaggerDocConstants.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Vegasco.WebApi.Endpoints.OpenApi;
|
||||
|
||||
public static class SwaggerDocConstants
|
||||
{
|
||||
|
||||
}
|
||||
19
src/WebApi/Persistence/ApplicationDbContext.cs
Normal file
19
src/WebApi/Persistence/ApplicationDbContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Common;
|
||||
using Vegasco.WebApi.Users;
|
||||
|
||||
namespace Vegasco.WebApi.Persistence;
|
||||
|
||||
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Car> Cars { get; set; }
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IWebApiMarker).Assembly);
|
||||
}
|
||||
}
|
||||
78
src/WebApi/Persistence/Migrations/20240803173803_AddCarsAndUsers.Designer.cs
generated
Normal file
78
src/WebApi/Persistence/Migrations/20240803173803_AddCarsAndUsers.Designer.cs
generated
Normal file
@@ -0,0 +1,78 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.WebApi.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240803173803_AddCarsAndUsers")]
|
||||
partial class AddCarsAndUsers
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.7")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Cars.Car", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("Cars");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Cars.Car", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.WebApi.Users.User", "User")
|
||||
.WithMany("Cars")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Users.User", b =>
|
||||
{
|
||||
b.Navigation("Cars");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.WebApi.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddCarsAndUsers : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Cars",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
|
||||
UserId = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Cars", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Cars_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Cars_UserId",
|
||||
table: "Cars",
|
||||
column: "UserId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Cars");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.WebApi.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.7")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Cars.Car", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("Cars");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Cars.Car", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.WebApi.Users.User", "User")
|
||||
.WithMany("Cars")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.WebApi.Users.User", b =>
|
||||
{
|
||||
b.Navigation("Cars");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/WebApi/Program.cs
Normal file
6
src/WebApi/Program.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using Vegasco.WebApi.Common;
|
||||
|
||||
WebApplication.CreateBuilder(args)
|
||||
.ConfigureServices()
|
||||
.ConfigureRequestPipeline()
|
||||
.Run();
|
||||
10
src/WebApi/Users/User.cs
Normal file
10
src/WebApi/Users/User.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace Vegasco.WebApi.Users;
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Id { get; set; } = "";
|
||||
|
||||
public virtual IList<Car> Cars { get; set; } = [];
|
||||
}
|
||||
12
src/WebApi/Users/UserTableConfiguration.cs
Normal file
12
src/WebApi/Users/UserTableConfiguration.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Vegasco.WebApi.Users;
|
||||
|
||||
public class UserTableConfiguration : IEntityTypeConfiguration<User>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<User> builder)
|
||||
{
|
||||
builder.HasKey(user => user.Id);
|
||||
}
|
||||
}
|
||||
34
src/WebApi/WebApi.csproj
Normal file
34
src/WebApi/WebApi.csproj
Normal file
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>4bf893d3-0c16-41ec-8b46-2768d841215d</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>..\..</DockerfileContext>
|
||||
<RootNamespace>Vegasco.WebApi</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
11
src/WebApi/appsettings.Development.json
Normal file
11
src/WebApi/appsettings.Development.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"Database": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
19
tests/WebApi.Tests.Integration/CarFaker.cs
Normal file
19
tests/WebApi.Tests.Integration/CarFaker.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Bogus;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace WebApi.Tests.Integration;
|
||||
|
||||
internal class CarFaker
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
internal CreateCar.Request CreateCarRequest()
|
||||
{
|
||||
return new CreateCar.Request(_faker.Vehicle.Model());
|
||||
}
|
||||
|
||||
internal UpdateCar.Request UpdateCarRequest()
|
||||
{
|
||||
return new UpdateCar.Request(_faker.Vehicle.Model());
|
||||
}
|
||||
}
|
||||
70
tests/WebApi.Tests.Integration/Cars/CreateCarTests.cs
Normal file
70
tests/WebApi.Tests.Integration/Cars/CreateCarTests.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace WebApi.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class CreateCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public CreateCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateCar_ShouldCreateCar_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
var createdCar = await response.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
createdCar.Should().BeEquivalentTo(createCarRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Cars.Should().ContainEquivalentOf(createdCar);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateCar_ShouldReturnValidationProblems_WhenRequestIsNotValid()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = new CreateCar.Request("");
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(CreateCar.Request.Name), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Cars.Should().NotContainEquivalentOf(createCarRequest, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
64
tests/WebApi.Tests.Integration/Cars/DeleteCarTests.cs
Normal file
64
tests/WebApi.Tests.Integration/Cars/DeleteCarTests.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace WebApi.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class DeleteCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public DeleteCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteCar_ShouldReturnNotFound_WhenCarDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.DeleteAsync($"v1/cars/{randomCarId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteCar_ShouldDeleteCar_WhenCarExists()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
var createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.DeleteAsync($"v1/cars/{createdCar!.Id}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
_dbContext.Cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
57
tests/WebApi.Tests.Integration/Cars/GetCarTests.cs
Normal file
57
tests/WebApi.Tests.Integration/Cars/GetCarTests.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using FluentAssertions;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace WebApi.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public GetCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCar_ShouldReturnNotFound_WhenCarDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.GetAsync($"v1/cars/{randomCarId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCar_ShouldReturnCar_WhenCarExists()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
var createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.GetAsync($"v1/cars/{createdCar!.Id}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var car = await response.Content.ReadFromJsonAsync<GetCar.Response>();
|
||||
car.Should().BeEquivalentTo(createdCar);
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
66
tests/WebApi.Tests.Integration/Cars/GetCarsTests.cs
Normal file
66
tests/WebApi.Tests.Integration/Cars/GetCarsTests.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using FluentAssertions;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace WebApi.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetCarsTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public GetCarsTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCars_ShouldReturnEmptyList_WhenNoEntriesExist()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.GetAsync("v1/cars");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var cars = await response.Content.ReadFromJsonAsync<IEnumerable<GetCars.Response>>();
|
||||
cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCars_ShouldReturnEntries_WhenEntriesExist()
|
||||
{
|
||||
// Arrange
|
||||
List<CreateCar.Response> createdCars = [];
|
||||
|
||||
const int numberOfCars = 5;
|
||||
for (var i = 0; i < numberOfCars; i++)
|
||||
{
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
var createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
createdCars.Add(createdCar!);
|
||||
}
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.GetAsync("v1/cars");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var cars = await response.Content.ReadFromJsonAsync<IEnumerable<GetCars.Response>>();
|
||||
cars.Should().BeEquivalentTo(createdCars);
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
99
tests/WebApi.Tests.Integration/Cars/UpdateCarTests.cs
Normal file
99
tests/WebApi.Tests.Integration/Cars/UpdateCarTests.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace WebApi.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class UpdateCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public UpdateCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldUpdateCar_WhenCarExistsAndRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
var createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
var updateCarRequest = _carFaker.UpdateCarRequest();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var updatedCar = await response.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
updatedCar!.Id.Should().Be(createdCar.Id);
|
||||
updatedCar.Should().BeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Cars.Should().ContainEquivalentOf(updatedCar, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldReturnValidationProblems_WhenRequestIsNotValid()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = _carFaker.CreateCarRequest();
|
||||
var createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
var updateCarRequest = new UpdateCar.Request("");
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(CreateCar.Request.Name), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Cars.Should().ContainSingle(x => x.Id == createdCar.Id)
|
||||
.Which
|
||||
.Should().NotBeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldReturnNotFound_WhenNoCarWithIdExists()
|
||||
{
|
||||
// Arrange
|
||||
var updateCarRequest = _carFaker.UpdateCarRequest();
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
var response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{randomCarId}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
|
||||
_dbContext.Cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
40
tests/WebApi.Tests.Integration/PostgresRespawner.cs
Normal file
40
tests/WebApi.Tests.Integration/PostgresRespawner.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Npgsql;
|
||||
using Respawn;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace WebApi.Tests.Integration;
|
||||
internal sealed class PostgresRespawner : IDisposable
|
||||
{
|
||||
private readonly DbConnection _connection;
|
||||
private readonly Respawner _respawner;
|
||||
|
||||
private PostgresRespawner(Respawner respawner, DbConnection connection)
|
||||
{
|
||||
_respawner = respawner;
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
public static async Task<PostgresRespawner> CreateAsync(string connectionString)
|
||||
{
|
||||
DbConnection connection = new NpgsqlConnection(connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var respawner = await Respawner.CreateAsync(connection,
|
||||
new RespawnerOptions
|
||||
{
|
||||
SchemasToInclude = ["public"],
|
||||
DbAdapter = DbAdapter.Postgres
|
||||
});
|
||||
return new PostgresRespawner(respawner, connection);
|
||||
}
|
||||
|
||||
public async Task ResetDatabaseAsync()
|
||||
{
|
||||
await _respawner.ResetAsync(_connection);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
7
tests/WebApi.Tests.Integration/SharedTestCollection.cs
Normal file
7
tests/WebApi.Tests.Integration/SharedTestCollection.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace WebApi.Tests.Integration;
|
||||
|
||||
[CollectionDefinition(Name)]
|
||||
public class SharedTestCollection : ICollectionFixture<WebAppFactory>
|
||||
{
|
||||
public const string Name = nameof(SharedTestCollection);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization.Policy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace WebApi.Tests.Integration;
|
||||
|
||||
public sealed class TestUserAlwaysAuthorizedPolicyEvaluator : IPolicyEvaluator
|
||||
{
|
||||
public const string Username = "Test user";
|
||||
public static readonly string UserId = Guid.NewGuid().ToString();
|
||||
|
||||
public Task<AuthenticateResult> AuthenticateAsync(AuthorizationPolicy policy, HttpContext context)
|
||||
{
|
||||
Claim[] claims =
|
||||
[
|
||||
|
||||
new Claim(ClaimTypes.Name, Username),
|
||||
new Claim("name", Username),
|
||||
new Claim(ClaimTypes.NameIdentifier, UserId),
|
||||
new Claim("aud", "https://localhost")
|
||||
];
|
||||
|
||||
ClaimsIdentity identity = new(claims, JwtBearerDefaults.AuthenticationScheme);
|
||||
ClaimsPrincipal principal = new(identity);
|
||||
AuthenticationTicket ticket = new(principal, JwtBearerDefaults.AuthenticationScheme);
|
||||
var result = AuthenticateResult.Success(ticket);
|
||||
return Task.FromResult(result); ;
|
||||
}
|
||||
|
||||
public Task<PolicyAuthorizationResult> AuthorizeAsync(AuthorizationPolicy policy, AuthenticateResult authenticationResult, HttpContext context,
|
||||
object? resource)
|
||||
{
|
||||
return Task.FromResult(PolicyAuthorizationResult.Success());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Bogus" Version="35.6.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.7" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Respawn" Version="6.2.1" />
|
||||
<PackageReference Include="Testcontainers.PostgreSql" Version="3.9.0" />
|
||||
<PackageReference Include="xunit" Version="2.5.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WebApi\WebApi.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
78
tests/WebApi.Tests.Integration/WebAppFactory.cs
Normal file
78
tests/WebApi.Tests.Integration/WebAppFactory.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using DotNet.Testcontainers.Images;
|
||||
using Microsoft.AspNetCore.Authorization.Policy;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Testcontainers.PostgreSql;
|
||||
using Vegasco.WebApi.Common;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace WebApi.Tests.Integration;
|
||||
|
||||
public sealed class WebAppFactory : WebApplicationFactory<IWebApiMarker>, IAsyncLifetime
|
||||
{
|
||||
private readonly PostgreSqlContainer _database = new PostgreSqlBuilder()
|
||||
.WithImage(DockerImage)
|
||||
.WithImagePullPolicy(PullPolicy.Always)
|
||||
.Build();
|
||||
|
||||
private const string DockerImage = "postgres:16.3-alpine";
|
||||
|
||||
public HttpClient HttpClient => CreateClient();
|
||||
|
||||
private PostgresRespawner? _postgresRespawner;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _database.StartAsync();
|
||||
|
||||
// Force application startup (i.e. initialization and validation)
|
||||
_ = CreateClient();
|
||||
|
||||
using var scope = Services.CreateScope();
|
||||
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
await dbContext.Database.MigrateAsync();
|
||||
|
||||
_postgresRespawner = await PostgresRespawner.CreateAsync(_database.GetConnectionString());
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
IEnumerable<KeyValuePair<string, string?>> customConfig =
|
||||
[
|
||||
new KeyValuePair<string, string?>("ConnectionStrings:Database", _database.GetConnectionString()),
|
||||
new KeyValuePair<string, string?>("JWT:ValidAudience", "https://localhost"),
|
||||
new KeyValuePair<string, string?>("JWT:MetadataUrl", "https://localhost"),
|
||||
new KeyValuePair<string, string?>("JWT:NameClaimType", null),
|
||||
];
|
||||
|
||||
builder.UseConfiguration(new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(customConfig)
|
||||
.Build());
|
||||
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
});
|
||||
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll<IPolicyEvaluator>();
|
||||
services.AddSingleton<IPolicyEvaluator, TestUserAlwaysAuthorizedPolicyEvaluator>();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task ResetDatabaseAsync()
|
||||
{
|
||||
await _postgresRespawner!.ResetDatabaseAsync();
|
||||
}
|
||||
|
||||
async Task IAsyncLifetime.DisposeAsync()
|
||||
{
|
||||
_postgresRespawner!.Dispose();
|
||||
await _database.DisposeAsync();
|
||||
}
|
||||
}
|
||||
166
tests/WebApi.Tests.System/ComposeService.cs
Normal file
166
tests/WebApi.Tests.System/ComposeService.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using Ductus.FluentDocker.Services;
|
||||
using Ductus.FluentDocker.Services.Extensions;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
public abstract class ComposeService : IDisposable
|
||||
{
|
||||
public string ServiceName { get; init; }
|
||||
public string ServiceInternalPort { get; init; }
|
||||
public string ServiceInternalProtocol { get; init; }
|
||||
public string ServiceInternalPortAndProtocol => $"{ServiceInternalPort}/{ServiceInternalProtocol}";
|
||||
|
||||
private readonly ICompositeService _dockerService;
|
||||
private readonly bool _isTestRunningInContainer;
|
||||
|
||||
private IContainerService? _container;
|
||||
private bool _hasCheckedForContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Not null, if <see cref="ContainerExists"/> is true.
|
||||
/// </summary>
|
||||
public IContainerService? Container
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_hasCheckedForContainer)
|
||||
{
|
||||
return _container;
|
||||
}
|
||||
|
||||
_container ??= _dockerService.Containers.First(x => x.Name == ServiceName);
|
||||
_hasCheckedForContainer = true;
|
||||
|
||||
return _container;
|
||||
}
|
||||
}
|
||||
|
||||
[MemberNotNullWhen(returnValue: true, nameof(Container))]
|
||||
public bool ContainerExists => Container is not null;
|
||||
|
||||
public ComposeService(
|
||||
ICompositeService dockerService,
|
||||
bool isTestRunningInContainer,
|
||||
string serviceName,
|
||||
string serviceInternalPort,
|
||||
string serviceInternalProtocol = "tcp")
|
||||
{
|
||||
_dockerService = dockerService;
|
||||
_isTestRunningInContainer = isTestRunningInContainer;
|
||||
ServiceName = serviceName;
|
||||
ServiceInternalPort = serviceInternalPort;
|
||||
ServiceInternalProtocol = serviceInternalProtocol;
|
||||
}
|
||||
|
||||
public string? GetServiceUrl()
|
||||
{
|
||||
if (!ContainerExists)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _isTestRunningInContainer
|
||||
? GetServiceUrlWhenRunningInsideContainer()
|
||||
: GetUrlFromOutsideContainer(Container, ServiceInternalPortAndProtocol);
|
||||
}
|
||||
|
||||
private string GetServiceUrlWhenRunningInsideContainer()
|
||||
{
|
||||
return $"http://{ServiceName}:{ServiceInternalPort}";
|
||||
}
|
||||
|
||||
private static string GetUrlFromOutsideContainer(IContainerService container, string portAndProto)
|
||||
{
|
||||
var ipEndpoint = container.ToHostExposedEndpoint(portAndProto);
|
||||
return $"http://{ipEndpoint.Address}:{ipEndpoint.Port}";
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_container?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class AppContainer : ComposeService
|
||||
{
|
||||
public AppContainer(ICompositeService dockerService, bool isTestRunningInContainer)
|
||||
: base(dockerService, isTestRunningInContainer, "app", "8080")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LoginContainer : ComposeService
|
||||
{
|
||||
public LoginContainer(ICompositeService dockerService, bool isTestRunningInContainer)
|
||||
: base(dockerService, isTestRunningInContainer, "login", "8080")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TestAppContainer : IDisposable
|
||||
{
|
||||
private IContainerService? _testApplicationContainer;
|
||||
private bool _hasCheckedForThisContainer;
|
||||
public IContainerService? Container
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_hasCheckedForThisContainer)
|
||||
{
|
||||
_testApplicationContainer = _dockerHost.GetRunningContainers()
|
||||
.FirstOrDefault(x => x.Id.StartsWith(Environment.MachineName));
|
||||
|
||||
if (_testApplicationContainer is not null)
|
||||
{
|
||||
// If the test is running inside a container (i.e. usually in a pipeline), we do not want to mess with the container, just release the resources held by this program
|
||||
_testApplicationContainer.RemoveOnDispose = false;
|
||||
_testApplicationContainer.StopOnDispose = false;
|
||||
}
|
||||
|
||||
_hasCheckedForThisContainer = true;
|
||||
}
|
||||
|
||||
return _testApplicationContainer;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _hasCheckedIfTestRunInContainer;
|
||||
private bool _isTestRunningInContainer;
|
||||
public bool IsTestRunningInContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_hasCheckedIfTestRunInContainer)
|
||||
{
|
||||
_isTestRunningInContainer = Container is not null;
|
||||
_hasCheckedIfTestRunInContainer = true;
|
||||
}
|
||||
|
||||
return _isTestRunningInContainer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private readonly IHostService _dockerHost;
|
||||
|
||||
public TestAppContainer(IHostService dockerHost)
|
||||
{
|
||||
_dockerHost = dockerHost;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_testApplicationContainer?.Dispose();
|
||||
}
|
||||
}
|
||||
13
tests/WebApi.Tests.System/Constants.cs
Normal file
13
tests/WebApi.Tests.System/Constants.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
public static class Login
|
||||
{
|
||||
public const string ClientId = "vegasco";
|
||||
public const string ClientSecret = "siIgnkijkkIxeQ9BDNwnGGUb60S53QZh";
|
||||
public const string Username = "test.user";
|
||||
public const string Password = "T3sttest.";
|
||||
public const string Realm = "development";
|
||||
}
|
||||
}
|
||||
301
tests/WebApi.Tests.System/DockerComposeFixture.cs
Normal file
301
tests/WebApi.Tests.System/DockerComposeFixture.cs
Normal file
@@ -0,0 +1,301 @@
|
||||
using Ductus.FluentDocker.Builders;
|
||||
using Ductus.FluentDocker.Extensions;
|
||||
using Ductus.FluentDocker.Model.Common;
|
||||
using Ductus.FluentDocker.Model.Compose;
|
||||
using Ductus.FluentDocker.Services;
|
||||
using Ductus.FluentDocker.Services.Extensions;
|
||||
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
public sealed class DockerComposeFixture : IDisposable
|
||||
{
|
||||
private const string ComposeFileName = "compose.system.yaml";
|
||||
|
||||
private const string AppServiceName = "app";
|
||||
private const string AppServiceInternalPort = "8080";
|
||||
private const string AppServiceInternalPortAndProtocol = $"{AppServiceInternalPort}/tcp";
|
||||
|
||||
private const string LoginServiceName = "login";
|
||||
private const string LoginServiceInternalPort = "8080";
|
||||
private const string LoginServiceInternalPortAndProtocol = $"{LoginServiceInternalPort}/tcp";
|
||||
|
||||
private static readonly string ComposeFilePath = Path.GetFullPath(Path.Combine("../../..", ComposeFileName));
|
||||
|
||||
private readonly ICompositeService _dockerService;
|
||||
|
||||
|
||||
private bool _hasCheckedForThisContainer;
|
||||
private bool _hasCheckedIfTestRunInContainer;
|
||||
|
||||
private IHostService? _host;
|
||||
|
||||
private bool _isTestRunningInContainer;
|
||||
private INetworkService? _networkService;
|
||||
private IContainerService? _testApplicationContainer;
|
||||
|
||||
public DockerComposeFixture()
|
||||
{
|
||||
_dockerService = GetDockerComposeServices();
|
||||
_dockerService.Start();
|
||||
AttachDockerNetworksIfRunningInContainer();
|
||||
}
|
||||
|
||||
private IContainerService? _appContainer;
|
||||
public IContainerService AppContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
_appContainer ??= _dockerService.Containers.First(x => x.Name == AppServiceName);
|
||||
return _appContainer;
|
||||
}
|
||||
}
|
||||
|
||||
private IContainerService? _loginContainer;
|
||||
public IContainerService LoginContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
_loginContainer ??= _dockerService.Containers.First(x => x.Name == LoginServiceName);
|
||||
return _loginContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public IContainerService? TestApplicationContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_hasCheckedForThisContainer)
|
||||
{
|
||||
_testApplicationContainer = DockerHost.GetRunningContainers()
|
||||
.FirstOrDefault(x => x.Id.StartsWith(Environment.MachineName));
|
||||
|
||||
if (_testApplicationContainer is not null)
|
||||
{
|
||||
// If the test is running inside a container (i.e. usually in a pipeline), we do not want to mess with the container, just release the resources held by this program
|
||||
_testApplicationContainer.RemoveOnDispose = false;
|
||||
_testApplicationContainer.StopOnDispose = false;
|
||||
}
|
||||
|
||||
_hasCheckedForThisContainer = true;
|
||||
}
|
||||
|
||||
return _testApplicationContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public IHostService DockerHost
|
||||
{
|
||||
get
|
||||
{
|
||||
var hosts = new Hosts().Discover();
|
||||
_host = hosts.FirstOrDefault(x => x.IsNative) ??
|
||||
hosts.FirstOrDefault(x => x.Name == "default") ??
|
||||
hosts.FirstOrDefault();
|
||||
|
||||
if (_host is null) throw new InvalidOperationException("No docker host found");
|
||||
|
||||
return _host;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsTestRunningInContainer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_hasCheckedIfTestRunInContainer)
|
||||
{
|
||||
_isTestRunningInContainer = TestApplicationContainer is not null;
|
||||
_hasCheckedIfTestRunInContainer = true;
|
||||
}
|
||||
|
||||
return _isTestRunningInContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_networkService?.Dispose();
|
||||
|
||||
_testApplicationContainer?.Dispose();
|
||||
_appContainer?.Dispose();
|
||||
_loginContainer?.Dispose();
|
||||
|
||||
// Kill container because otherwise the _dockerService.Dispose() takes much longer
|
||||
KillDockerComposeServices();
|
||||
|
||||
_dockerService.Dispose();
|
||||
|
||||
_host?.Dispose();
|
||||
}
|
||||
|
||||
private ICompositeService GetDockerComposeServices()
|
||||
{
|
||||
var services = new Builder()
|
||||
.UseContainer()
|
||||
.UseCompose()
|
||||
.AssumeComposeVersion(ComposeVersion.V2)
|
||||
.FromFile((TemplateString)ComposeFilePath)
|
||||
.ForceBuild()
|
||||
.RemoveOrphans()
|
||||
.Wait("app", WaitForApplicationToListenToRequests)
|
||||
.Build();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private int WaitForApplicationToListenToRequests(IContainerService container, int iteration)
|
||||
{
|
||||
const int maxTryCount = 15;
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(iteration, maxTryCount);
|
||||
|
||||
var isStarted = container.Logs().ReadToEnd().Reverse().Any(x => x.Contains("Now listening on:"));
|
||||
return isStarted ? 0 : 500;
|
||||
}
|
||||
|
||||
private void AttachDockerNetworksIfRunningInContainer()
|
||||
{
|
||||
if (!IsTestRunningInContainer) return;
|
||||
|
||||
var randomNetworkName = Guid.NewGuid().ToString("N");
|
||||
_networkService = DockerHost.CreateNetwork(randomNetworkName, removeOnDispose: true);
|
||||
|
||||
_networkService.Attach(AppContainer, true, AppServiceName);
|
||||
_networkService.Attach(TestApplicationContainer, true);
|
||||
}
|
||||
|
||||
public string GetAppUrl()
|
||||
{
|
||||
return IsTestRunningInContainer
|
||||
? GetAppUrlWhenRunningInsideContainer()
|
||||
: GetUrlFromOutsideContainer(AppContainer, AppServiceInternalPortAndProtocol);
|
||||
}
|
||||
|
||||
private static string GetAppUrlWhenRunningInsideContainer()
|
||||
{
|
||||
return $"http://{AppServiceName}:{AppServiceInternalPort}";
|
||||
}
|
||||
|
||||
public string GetLoginUrl()
|
||||
{
|
||||
return IsTestRunningInContainer
|
||||
? GetLoginUrlWhenRunningInsideContainer()
|
||||
: GetUrlFromOutsideContainer(LoginContainer, LoginServiceInternalPortAndProtocol);
|
||||
}
|
||||
|
||||
private static string GetLoginUrlWhenRunningInsideContainer()
|
||||
{
|
||||
return $"http://{LoginServiceName}:{LoginServiceInternalPort}";
|
||||
}
|
||||
|
||||
private static string GetUrlFromOutsideContainer(IContainerService container, string portAndProto)
|
||||
{
|
||||
var ipEndpoint = container.ToHostExposedEndpoint(portAndProto);
|
||||
return $"http://{ipEndpoint.Address}:{ipEndpoint.Port}";
|
||||
}
|
||||
|
||||
private void KillDockerComposeServices()
|
||||
{
|
||||
foreach (var container in _dockerService.Containers) container.Remove(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class DockerComposeFixture2 : IDisposable
|
||||
{
|
||||
private const string ComposeFileName = "compose.system.yaml";
|
||||
|
||||
private static readonly string ComposeFilePath = Path.GetFullPath(Path.Combine("../../..", ComposeFileName));
|
||||
|
||||
private readonly ICompositeService _dockerService;
|
||||
|
||||
private IHostService? _host;
|
||||
|
||||
private INetworkService? _networkService;
|
||||
|
||||
public AppContainer AppContainer { get; init; }
|
||||
public LoginContainer LoginContainer { get; init; }
|
||||
public TestAppContainer TestApplicationContainer { get; init; }
|
||||
|
||||
public DockerComposeFixture2()
|
||||
{
|
||||
_dockerService = GetDockerComposeServices();
|
||||
_dockerService.Start();
|
||||
|
||||
TestApplicationContainer = new TestAppContainer(DockerHost);
|
||||
AppContainer = new AppContainer(_dockerService, TestApplicationContainer.IsTestRunningInContainer);
|
||||
LoginContainer = new LoginContainer(_dockerService, TestApplicationContainer.IsTestRunningInContainer);
|
||||
|
||||
AttachDockerNetworksIfRunningInContainer();
|
||||
}
|
||||
|
||||
public IHostService DockerHost
|
||||
{
|
||||
get
|
||||
{
|
||||
var hosts = new Hosts().Discover();
|
||||
_host = hosts.FirstOrDefault(x => x.IsNative) ??
|
||||
hosts.FirstOrDefault(x => x.Name == "default") ??
|
||||
hosts.FirstOrDefault();
|
||||
|
||||
if (_host is null) throw new InvalidOperationException("No docker host found");
|
||||
|
||||
return _host;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_networkService?.Dispose();
|
||||
|
||||
TestApplicationContainer.Dispose();
|
||||
AppContainer.Dispose();
|
||||
LoginContainer.Dispose();
|
||||
|
||||
// Kill container because otherwise the _dockerService.Dispose() takes much longer
|
||||
KillDockerComposeServices();
|
||||
|
||||
_dockerService.Dispose();
|
||||
|
||||
_host?.Dispose();
|
||||
}
|
||||
|
||||
private ICompositeService GetDockerComposeServices()
|
||||
{
|
||||
var services = new Builder()
|
||||
.UseContainer()
|
||||
.UseCompose()
|
||||
.AssumeComposeVersion(ComposeVersion.V2)
|
||||
.FromFile((TemplateString)ComposeFilePath)
|
||||
.ForceBuild()
|
||||
.RemoveOrphans()
|
||||
.Wait("app", WaitForApplicationToListenToRequests)
|
||||
.Build();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private int WaitForApplicationToListenToRequests(IContainerService container, int iteration)
|
||||
{
|
||||
const int maxTryCount = 15;
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(iteration, maxTryCount);
|
||||
|
||||
var isStarted = container.Logs().ReadToEnd().Reverse().Any(x => x.Contains("Now listening on:"));
|
||||
return isStarted ? 0 : 500;
|
||||
}
|
||||
|
||||
private void AttachDockerNetworksIfRunningInContainer()
|
||||
{
|
||||
if (!TestApplicationContainer.IsTestRunningInContainer) return;
|
||||
|
||||
var randomNetworkName = Guid.NewGuid().ToString("N");
|
||||
_networkService = DockerHost.CreateNetwork(randomNetworkName, removeOnDispose: true);
|
||||
|
||||
_networkService.Attach(AppContainer.Container!, true, AppContainer.ServiceName);
|
||||
_networkService.Attach(TestApplicationContainer.Container, true);
|
||||
}
|
||||
|
||||
private void KillDockerComposeServices()
|
||||
{
|
||||
foreach (var container in _dockerService.Containers) container.Remove(true);
|
||||
}
|
||||
}
|
||||
8
tests/WebApi.Tests.System/Dockerfile.keycloak
Normal file
8
tests/WebApi.Tests.System/Dockerfile.keycloak
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM registry.access.redhat.com/ubi9 AS ubi-micro-build
|
||||
RUN mkdir -p /mnt/rootfs
|
||||
RUN dnf install --installroot /mnt/rootfs curl --releasever 9 --setopt install_weak_deps=false --nodocs -y && \
|
||||
dnf --installroot /mnt/rootfs clean all && \
|
||||
rpm --root /mnt/rootfs -e --nodeps setup
|
||||
|
||||
FROM quay.io/keycloak/keycloak
|
||||
COPY --from=ubi-micro-build /mnt/rootfs /
|
||||
7
tests/WebApi.Tests.System/SharedTestCollection.cs
Normal file
7
tests/WebApi.Tests.System/SharedTestCollection.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
[CollectionDefinition(Name)]
|
||||
public class SharedTestCollection : ICollectionFixture<SharedTestContext>
|
||||
{
|
||||
public const string Name = nameof(SharedTestCollection);
|
||||
}
|
||||
11
tests/WebApi.Tests.System/SharedTestContext.cs
Normal file
11
tests/WebApi.Tests.System/SharedTestContext.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
public sealed class SharedTestContext : IDisposable
|
||||
{
|
||||
public DockerComposeFixture2 DockerComposeFixture { get; set; } = new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DockerComposeFixture.Dispose();
|
||||
}
|
||||
}
|
||||
57
tests/WebApi.Tests.System/Test.cs
Normal file
57
tests/WebApi.Tests.System/Test.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class Test
|
||||
{
|
||||
private readonly SharedTestContext _context;
|
||||
|
||||
public Test(SharedTestContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
//[Fact]
|
||||
public async Task Test1()
|
||||
{
|
||||
var loginUrl = _context.DockerComposeFixture.LoginContainer.GetServiceUrl();
|
||||
var baseUrl = new Uri(loginUrl!, UriKind.Absolute);
|
||||
var relativeUrl = new Uri($"/realms/{Constants.Login.Realm}/protocol/openid-connect/token", UriKind.Relative);
|
||||
var uri = new Uri(baseUrl, relativeUrl);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, uri);
|
||||
|
||||
var data = new Dictionary<string, string>
|
||||
{
|
||||
{ "grant_type", "password" },
|
||||
{ "audience", Constants.Login.ClientId },
|
||||
{ "username", Constants.Login.Username },
|
||||
{ "password", Constants.Login.Password }
|
||||
};
|
||||
request.Content = new FormUrlEncodedContent(data);
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
|
||||
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Constants.Login.ClientId}:{Constants.Login.ClientSecret}")));
|
||||
|
||||
using var client = new HttpClient();
|
||||
using var response = await client.SendAsync(request);
|
||||
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var tokenResponse = JsonSerializer.Deserialize<TokenResponse>(content);
|
||||
|
||||
var appUrl = _context.DockerComposeFixture.AppContainer.GetServiceUrl();
|
||||
baseUrl = new Uri(appUrl!, UriKind.Absolute);
|
||||
relativeUrl = new Uri("/v1/cars", UriKind.Relative);
|
||||
uri = new Uri(baseUrl, relativeUrl);
|
||||
|
||||
request = new HttpRequestMessage(HttpMethod.Get, uri);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse!.AccessToken);
|
||||
|
||||
using var response2 = await client.SendAsync(request);
|
||||
|
||||
var content2 = await response2.Content.ReadAsStringAsync();
|
||||
|
||||
}
|
||||
}
|
||||
9
tests/WebApi.Tests.System/TokenResponse.cs
Normal file
9
tests/WebApi.Tests.System/TokenResponse.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WebApi.Tests.System;
|
||||
|
||||
public class TokenResponse
|
||||
{
|
||||
[JsonPropertyName("access_token")]
|
||||
public required string AccessToken { get; init; }
|
||||
}
|
||||
24
tests/WebApi.Tests.System/WebApi.Tests.System.csproj
Normal file
24
tests/WebApi.Tests.System/WebApi.Tests.System.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Ductus.FluentDocker" Version="2.10.59" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="xunit" Version="2.5.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
72
tests/WebApi.Tests.System/compose.system.yaml
Normal file
72
tests/WebApi.Tests.System/compose.system.yaml
Normal file
@@ -0,0 +1,72 @@
|
||||
services:
|
||||
app:
|
||||
build: ../../src/WebApi
|
||||
environment:
|
||||
Vegasco_ConnectionStrings__Default: "Host=db;Port=5432;Database=postgres;Username=postgres;Password=postgres"
|
||||
Vegasco_JWT__MetadataUrl: http://login:8080/realms/development/.well-known/openid-configuration
|
||||
Vegasco_JWT__ValidAudience: vegasco
|
||||
Vegasco_JWT__NameClaimType: name
|
||||
Vegasco_JWT__AllowHttpMetadataUrl: "true"
|
||||
ports:
|
||||
- "8080"
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
login:
|
||||
condition: service_healthy
|
||||
|
||||
db:
|
||||
image: postgres:16.3-alpine
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
healthcheck:
|
||||
test: pg_isready -d postgres
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
|
||||
login:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.keycloak
|
||||
command: start --import-realm
|
||||
environment:
|
||||
KC_DB: postgres
|
||||
KC_DB_URL_HOST: login-db
|
||||
KC_DB_URL_PORT: 5432
|
||||
KC_DB_URL_DATABASE: keycloak
|
||||
KC_DB_USERNAME: keycloak
|
||||
KC_DB_PASSWORD: keycloak
|
||||
KEYCLOAK_ADMIN: admin
|
||||
KEYCLOAK_ADMIN_PASSWORD: admin1!
|
||||
KC_HOSTNAME_STRICT: false
|
||||
KC_HEALTH_ENABLED: true
|
||||
KC_METRICS_ENABLED: true
|
||||
KC_HTTP_ENABLED: true
|
||||
ports:
|
||||
- "8080"
|
||||
volumes:
|
||||
- ./test-realm.json:/opt/keycloak/data/import/test-realm.json:ro
|
||||
depends_on:
|
||||
login-db:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: curl --head -fsS http://localhost:9000/health/ready || exit 1
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 6
|
||||
start_period: 5s
|
||||
|
||||
login-db:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_USER: keycloak
|
||||
POSTGRES_PASSWORD: keycloak
|
||||
healthcheck:
|
||||
test: pg_isready -d keycloak
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
1960
tests/WebApi.Tests.System/test-realm.json
Normal file
1960
tests/WebApi.Tests.System/test-realm.json
Normal file
File diff suppressed because it is too large
Load Diff
179
tests/WebApi.Tests.Unit/Authentication/UserAccessorTests.cs
Normal file
179
tests/WebApi.Tests.Unit/Authentication/UserAccessorTests.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using System.Security.Claims;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
|
||||
namespace WebApi.Tests.Unit.Authentication;
|
||||
public sealed class UserAccessorTests
|
||||
{
|
||||
private readonly UserAccessor _sut;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
private static readonly string _nameClaimType = "name";
|
||||
private readonly JwtOptions _jwtOptions = new()
|
||||
{
|
||||
NameClaimType = _nameClaimType
|
||||
};
|
||||
|
||||
private readonly IOptions<JwtOptions> _options = Substitute.For<IOptions<JwtOptions>>();
|
||||
|
||||
private static readonly string _defaultUsername = "username";
|
||||
private static readonly string _defaultId = "id";
|
||||
private readonly ClaimsPrincipal _defaultUser = new(new ClaimsIdentity(
|
||||
[
|
||||
new Claim(_nameClaimType, _defaultUsername),
|
||||
new Claim(ClaimTypes.NameIdentifier, _defaultId)
|
||||
]));
|
||||
|
||||
public UserAccessorTests()
|
||||
{
|
||||
_httpContextAccessor = new HttpContextAccessor
|
||||
{
|
||||
HttpContext = new DefaultHttpContext()
|
||||
{
|
||||
User = _defaultUser
|
||||
}
|
||||
};
|
||||
|
||||
_options.Value.Returns(_jwtOptions);
|
||||
|
||||
_sut = new UserAccessor(_httpContextAccessor, _options);
|
||||
}
|
||||
|
||||
#region GetUsername
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldReturnUsername_WhenOptionsNameClaimTypeMatches()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldReturnUsername_WhenNameClaimTypeIsNotSetAndUsernameIsInUriNameClaimType()
|
||||
{
|
||||
// Arrange
|
||||
_jwtOptions.NameClaimType = null;
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
[
|
||||
new Claim(ClaimTypes.Name, _defaultUsername)
|
||||
]));
|
||||
|
||||
// Act
|
||||
var result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldCacheUsername_WhenFirstCalled()
|
||||
{
|
||||
// Arrange
|
||||
_ = _sut.GetUsername();
|
||||
_options.ClearReceivedCalls();
|
||||
|
||||
// Act
|
||||
var result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
_ = _options.Received(0).Value;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldThrowInvalidOperationException_WhenHttpContextIsNull()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
|
||||
// Act
|
||||
var action = () => _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be("No HttpContext available.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldThrowInvalidOperationException_WhenNameClaimIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal();
|
||||
|
||||
// Act
|
||||
var action = () => _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be($"No claim of type '{_nameClaimType}' found on the current user.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetUserId
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldReturnUserId_WhenUserIdClaimExists()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldCacheUserId_WhenFirstCalled()
|
||||
{
|
||||
// Arrange
|
||||
_ = _sut.GetUserId();
|
||||
_options.ClearReceivedCalls();
|
||||
|
||||
// Act
|
||||
var result = _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultId);
|
||||
_ = _options.Received(0).Value;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldThrowInvalidOperationException_WhenHttpContextIsNull()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
|
||||
// Act
|
||||
var action = () => _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be("No HttpContext available.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldThrowInvalidOperationException_WhenIdClaimIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal();
|
||||
|
||||
// Act
|
||||
var action = () => _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be($"No claim of type '{ClaimTypes.NameIdentifier}' found on the current user.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using FluentAssertions;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace WebApi.Tests.Unit.Cars;
|
||||
|
||||
public sealed class CreateCarRequestValidatorTests
|
||||
{
|
||||
private readonly CreateCar.Validator _sut = new();
|
||||
|
||||
private readonly CreateCar.Request _validRequest = new("Ford Focus");
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(50)]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenNameIsJustWithinTheLimits(int nameLength)
|
||||
{
|
||||
// Arrange
|
||||
var request = _validRequest with { Name = new string('s', nameLength) };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var request = _validRequest with { Name = "" };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsTooLong()
|
||||
{
|
||||
// Arrange
|
||||
const int nameMaxLength = 50;
|
||||
var request = _validRequest with { Name = new string('s', nameMaxLength + 1) };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using FluentAssertions;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace WebApi.Tests.Unit.Cars;
|
||||
|
||||
public sealed class UpdateCarRequestValidatorTests
|
||||
{
|
||||
private readonly UpdateCar.Validator _sut = new();
|
||||
|
||||
private readonly UpdateCar.Request _validRequest = new("Ford Focus");
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(50)]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenNameIsJustWithinTheLimits(int nameLength)
|
||||
{
|
||||
// Arrange
|
||||
var request = _validRequest with { Name = new string('s', nameLength) };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var request = _validRequest with { Name = "" };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(UpdateCar.Request.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsTooLong()
|
||||
{
|
||||
// Arrange
|
||||
const int nameMaxLength = 50;
|
||||
var request = _validRequest with { Name = new string('s', nameMaxLength + 1) };
|
||||
|
||||
// Act
|
||||
var result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(UpdateCar.Request.Name));
|
||||
}
|
||||
}
|
||||
35
tests/WebApi.Tests.Unit/WebApi.Tests.Unit.csproj
Normal file
35
tests/WebApi.Tests.Unit/WebApi.Tests.Unit.csproj
Normal file
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\WebApi\WebApi.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,9 +1,24 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
# 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api", "src\Api\Api.csproj", "{9FF3C98A-5085-4EBE-A980-DB2148B0C00A}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "src\WebApi\WebApi.csproj", "{9FF3C98A-5085-4EBE-A980-DB2148B0C00A}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C051A684-BD6A-43F2-B0CC-F3C2315D99E3}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A16251C2-47DB-4017-812B-CA18B280E049}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{437DE053-1DAB-4EEF-BEA6-E3B5179692F8}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Tests.Unit", "tests\WebApi.Tests.Unit\WebApi.Tests.Unit.csproj", "{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Tests.Integration", "tests\WebApi.Tests.Integration\WebApi.Tests.Integration.csproj", "{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.Tests.System", "tests\WebApi.Tests.System\WebApi.Tests.System.csproj", "{21418359-4A20-4F4A-B26C-A75A2B70AA10}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -15,8 +30,29 @@ Global
|
||||
{9FF3C98A-5085-4EBE-A980-DB2148B0C00A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9FF3C98A-5085-4EBE-A980-DB2148B0C00A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9FF3C98A-5085-4EBE-A980-DB2148B0C00A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{21418359-4A20-4F4A-B26C-A75A2B70AA10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{21418359-4A20-4F4A-B26C-A75A2B70AA10}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{21418359-4A20-4F4A-B26C-A75A2B70AA10}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{21418359-4A20-4F4A-B26C-A75A2B70AA10}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{9FF3C98A-5085-4EBE-A980-DB2148B0C00A} = {C051A684-BD6A-43F2-B0CC-F3C2315D99E3}
|
||||
{5BA94D65-1D04-49EA-B7CC-F3719DE2D97E} = {437DE053-1DAB-4EEF-BEA6-E3B5179692F8}
|
||||
{0B1F3D81-95E8-4CFC-8A90-8A3CB2549326} = {437DE053-1DAB-4EEF-BEA6-E3B5179692F8}
|
||||
{21418359-4A20-4F4A-B26C-A75A2B70AA10} = {437DE053-1DAB-4EEF-BEA6-E3B5179692F8}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7813E32D-AE19-479C-853B-063882D2D05A}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
3
vegasco-server.sln.DotSettings
Normal file
3
vegasco-server.sln.DotSettings
Normal file
@@ -0,0 +1,3 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=respawner/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vegasco/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
Reference in New Issue
Block a user