Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Integration Tests #87

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Monaco.Template.Backend.IntegrationTests" />
</ItemGroup>

<!--#if (templateDebug)-->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);auth;commonLibraries;filesSupport</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Host.UseSerilog((context, config) => config.ReadFrom.Configuration(context.Configuration)
.WriteTo.Logger(l => l.WriteTo.Conditional(_ => context.HostingEnvironment.IsDevelopment(), // Only for dev
.WriteTo.Logger(l => l.WriteTo.Conditional(_ => context.HostingEnvironment.IsDevelopment(), // Only for dev
cfg => cfg.Debug()
.WriteTo.File("logs/log.txt",
rollingInterval: RollingInterval.Day,
Expand Down Expand Up @@ -120,4 +120,9 @@

app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true });

app.Run();
app.Run();

namespace Monaco.Template.Backend.Api
{
public partial class Program;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<!--#endif-->
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.2" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.3" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="AsyncEnumerator" Version="4.0.2" />
<PackageReference Include="ExifLibNet" Version="2.1.4" />
<!--#endif-->
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
<!--#if (massTransitIntegration)-->
<PackageReference Include="MassTransit" Version="8.2.5" />
<!--#endif-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MediatR" Version="12.4.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="Polly.Core" Version="8.4.1" />
<PackageReference Include="Polly.Extensions" Version="8.4.1" />
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Polly.Core" Version="8.4.2" />
<PackageReference Include="Polly.Extensions" Version="8.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.2" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.3" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,42 @@ public Page(IEnumerable<T> items, int offset, int limit, long count)
Pager = new Pager(offset, limit, count);
}

public Page(IEnumerable<T> items, Pager pager)
{
Items = items.ToList();
Pager = pager;
}

public Page() { }

/// <summary>
/// Page metadata
/// </summary>
public Pager Pager { get; }
public Pager Pager { get; init; }
/// <summary>
/// Paged items
/// </summary>
public IReadOnlyList<T> Items { get; }
public IReadOnlyList<T> Items { get; init; }
}

/// <summary>
/// Pagination metadata
/// </summary>
public record Pager(int Offset, int Limit, long Count);
public record Pager
{
public Pager() {}

/// <summary>
/// Pagination metadata
/// </summary>
public Pager(int offset, int limit, long count)
{
Offset = offset;
Limit = limit;
Count = count;
}

public int Offset { get; init; }
public int Limit { get; init; }
public long Count { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ItemGroup>
<PackageReference Include="DelegateDecompiler.EntityFrameworkCore5" Version="0.34.0" />
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="8.1.5" />
<PackageReference Include="MediatR" Version="12.4.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.2" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="MockQueryable.Moq" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Monaco.Template.Backend.Common.Domain\Monaco.Template.Backend.Common.Domain.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

<!--#endif-->
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace Monaco.Template.Backend.IntegrationTests;

public record AccessTokenDto
{
public AccessTokenDto(string accessToken, int expiresIn)
{
AccessToken = accessToken;
}

[JsonPropertyName("access_token")]
public string AccessToken { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Flurl;
using Monaco.Template.Backend.Application.DTOs;

namespace Monaco.Template.Backend.IntegrationTests;

internal static class ApiRoutes
{
private static readonly Url ApiVersion = "api/v1";
private const string ExpandParamName = "expand";
private const string OffsetParamName = "offset";
private const string LimitParamName = "limit";

private static Url Expand(this Url url,
bool expand,
string paramName) =>
expand
? url.AppendQueryParam(ExpandParamName, paramName)
: url;

private static Url Offset(this Url url, int? offset = null) =>
offset.HasValue
? url.SetQueryParam(OffsetParamName, offset.Value)
: url;

private static Url Limit(this Url url, int? limit = null) =>
limit.HasValue
? url.SetQueryParam(LimitParamName, limit.Value)
: url;

public static class Companies
{
private static Url Controller => ApiVersion.Clone()
.AppendPathSegment(nameof(Companies));

public static Url Query(bool expandCountry = false, int? offset = null, int? limit = null) =>
Controller.Expand(expandCountry, nameof(CompanyDto.Country))
.Offset(offset)
.Limit(limit);
public static Url Get(Guid id) => Controller.AppendPathSegment(id);
public static string Post() => Query();
public static string Put(Guid id) => Get(id);
public static string Delete(Guid id) => Get(id);
}

public static class Countries
{
private static Url Controller => ApiVersion.Clone()
.AppendPathSegment(nameof(Countries));

public static Url Query() => Controller;
public static Url Get(Guid id) => Controller.AppendPathSegment(id);
}

public static class Files
{
private static Url Controller => ApiVersion.Clone()
.AppendPathSegment(nameof(Files));

public static Url Post() => Controller;
}

public static class Products
{
private static Url Controller => ApiVersion.Clone()
.AppendPathSegment(nameof(Products));

public static Url Query(bool expandCompany = false,
bool expandPictures = false,
bool expandDefaultPicture = false,
int? offset = null,
int? limit = null) =>
Controller.Expand(expandCompany, nameof(ProductDto.Company))
.Expand(expandPictures, nameof(ProductDto.Pictures))
.Expand(expandDefaultPicture, nameof(ProductDto.DefaultPicture))
.Offset(offset)
.Limit(limit);

public static Url Get(Guid id) => Controller.AppendPathSegment(id);

public static Url DownloadPicture(Guid productId, Guid pictureId, bool? isThumbnail = null)
{
var url = Controller.AppendPathSegments(productId, "Pictures", pictureId);
return isThumbnail.HasValue
? url.SetQueryParam("thumbnail", isThumbnail.Value.ToString().ToLower())
: url;
}

public static string Post() => Query();
public static string Put(Guid id) => Get(id);
public static string Delete(Guid id) => Get(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Flurl;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;

namespace Monaco.Template.Backend.IntegrationTests;

public class ApiWebApplicationFactory : WebApplicationFactory<Api.Program>
{
public readonly string SqlConnectionString;
public readonly Url RabbitMqHost;
public readonly string RabbitMqUsername;
public readonly string RabbitMqPassword;
public readonly Url StorageConnectionString;
public readonly Url KeycloakRealmUrl;

public ApiWebApplicationFactory(AppFixture fixture)
{
SqlConnectionString = fixture.SqlContainer.GetConnectionString();
Url rabbitMqConnString = fixture.RabbitMqContainer.GetConnectionString();
RabbitMqUsername = rabbitMqConnString.UserInfo.Split(':').First();
RabbitMqPassword = rabbitMqConnString.UserInfo.Split(':').Last();
RabbitMqHost = rabbitMqConnString.Root.Replace($"{rabbitMqConnString.UserInfo}@", string.Empty);
StorageConnectionString = fixture.AzuriteContainer.GetConnectionString();
KeycloakRealmUrl = fixture.KeycloakContainer
.GetBaseAddress()
.AppendPathSegments("realms", AppFixture.KeycloakRealm);
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseConfiguration(new ConfigurationManager
{
["ConnectionStrings:AppDbContext"] = SqlConnectionString,
["SSO:Authority"] = KeycloakRealmUrl,
["BlobStorage:ConnectionString"] = StorageConnectionString,
["MessageBus:RabbitMQ:Host"] = RabbitMqHost,
["MessageBus:RabbitMQ:User"] = RabbitMqUsername,
["MessageBus:RabbitMQ:Password"] = RabbitMqPassword
})
.UseSetting("https_port", "8080");
}
}
Loading