Events-MVC (example with htmx)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Events.Tests.IntegrationTests.Infrastructure;
|
||||
|
||||
internal static partial class AntiforgeryRequestHelper
|
||||
{
|
||||
private const string AntiforgeryFieldName = "__RequestVerificationToken";
|
||||
|
||||
public static async Task<HttpResponseMessage> PostFormAsync(
|
||||
HttpClient client,
|
||||
string pageUrl,
|
||||
string postUrl,
|
||||
IEnumerable<KeyValuePair<string, string?>> fields)
|
||||
{
|
||||
var pageHtml = await client.GetStringAsync(pageUrl);
|
||||
var token = ExtractAntiforgeryToken(pageHtml);
|
||||
|
||||
var payload = fields
|
||||
.Append(new KeyValuePair<string, string?>(AntiforgeryFieldName, token))
|
||||
.ToArray();
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, postUrl)
|
||||
{
|
||||
Content = new FormUrlEncodedContent(payload!)
|
||||
};
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
|
||||
return await client.SendAsync(request);
|
||||
}
|
||||
|
||||
private static string ExtractAntiforgeryToken(string html)
|
||||
{
|
||||
var match = AntiforgeryTokenRegex().Match(html);
|
||||
Assert.True(match.Success, "Expected antiforgery token field was not found in the HTML response.");
|
||||
return match.Groups["token"].Value;
|
||||
}
|
||||
|
||||
[GeneratedRegex("<input[^>]*name=\"__RequestVerificationToken\"[^>]*value=\"(?<token>[^\"]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled)]
|
||||
private static partial Regex AntiforgeryTokenRegex();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#if POSTGRES
|
||||
using Events.EF.Data.Postgres;
|
||||
#else
|
||||
using Events.EF.Data.MSSQL;
|
||||
#endif
|
||||
using Events.EF.Models;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Events.Tests.IntegrationTests.Infrastructure;
|
||||
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
private readonly Action<EventsContext>? seed;
|
||||
private readonly InMemoryDatabaseRoot databaseRoot = new();
|
||||
private readonly string databaseName = Guid.NewGuid().ToString();
|
||||
|
||||
public CustomWebApplicationFactory(Action<EventsContext>? seed = null)
|
||||
{
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseEnvironment("Development");
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll(typeof(DbContextOptions<EventsContext>));
|
||||
services.RemoveAll(typeof(IDbContextOptionsConfiguration<EventsContext>));
|
||||
services.RemoveAll(typeof(EventsContext));
|
||||
|
||||
services.AddDbContext<EventsContext>(options =>
|
||||
options
|
||||
.UseInMemoryDatabase(databaseName, databaseRoot)
|
||||
.ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
|
||||
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var ctx = scope.ServiceProvider.GetRequiredService<EventsContext>();
|
||||
ctx.Database.EnsureCreated();
|
||||
seed?.Invoke(ctx);
|
||||
ctx.SaveChanges();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
#if POSTGRES
|
||||
using Events.EF.Data.Postgres;
|
||||
#else
|
||||
using Events.EF.Data.MSSQL;
|
||||
#endif
|
||||
using Events.EF.Models;
|
||||
|
||||
namespace Events.Tests.IntegrationTests.Infrastructure;
|
||||
|
||||
internal static class TestDataSeeder
|
||||
{
|
||||
public static void SeedSports(EventsContext ctx)
|
||||
{
|
||||
ctx.Sports.AddRange(
|
||||
new Sport { Id = 1, Name = "Football" },
|
||||
new Sport { Id = 2, Name = "Basketball" });
|
||||
}
|
||||
|
||||
public static void SeedPeople(EventsContext ctx)
|
||||
{
|
||||
ctx.Countries.Add(new Country
|
||||
{
|
||||
Code = "HR",
|
||||
Alpha3 = "HRV",
|
||||
Name = "Croatia"
|
||||
});
|
||||
|
||||
ctx.People.Add(new Person
|
||||
{
|
||||
Id = 1,
|
||||
FirstName = "Ivan",
|
||||
LastName = "Horvat",
|
||||
FirstNameTranscription = "Ivan",
|
||||
LastNameTranscription = "Horvat",
|
||||
AddressLine = "Ilica 1",
|
||||
PostalCode = "10000",
|
||||
City = "Zagreb",
|
||||
AddressCountry = "Croatia",
|
||||
Email = "ivan.horvat@example.com",
|
||||
ContactPhone = "+38591111222",
|
||||
BirthDate = new DateOnly(1990, 5, 1),
|
||||
DocumentNumber = "DOC-1",
|
||||
CountryCode = "HR"
|
||||
});
|
||||
}
|
||||
|
||||
public static void SeedEvents(EventsContext ctx)
|
||||
{
|
||||
ctx.Events.AddRange(
|
||||
new Event { Id = 100, Name = "Spring Games", EventDate = new DateOnly(2026, 4, 15) },
|
||||
new Event { Id = 200, Name = "Summer Cup", EventDate = new DateOnly(2026, 6, 20) });
|
||||
}
|
||||
|
||||
public static void SeedRegistrationsScenario(EventsContext ctx)
|
||||
{
|
||||
ctx.Countries.AddRange(
|
||||
new Country
|
||||
{
|
||||
Code = "HR",
|
||||
Alpha3 = "HRV",
|
||||
Name = "Croatia"
|
||||
},
|
||||
new Country
|
||||
{
|
||||
Code = "DE",
|
||||
Alpha3 = "DEU",
|
||||
Name = "Germany"
|
||||
});
|
||||
|
||||
ctx.People.AddRange(
|
||||
new Person
|
||||
{
|
||||
Id = 1,
|
||||
FirstName = "Ivan",
|
||||
LastName = "Horvat",
|
||||
FirstNameTranscription = "Ivan",
|
||||
LastNameTranscription = "Horvat",
|
||||
AddressLine = "Ilica 1",
|
||||
PostalCode = "10000",
|
||||
City = "Zagreb",
|
||||
AddressCountry = "Croatia",
|
||||
Email = "ivan.horvat@example.com",
|
||||
ContactPhone = "+38591111222",
|
||||
BirthDate = new DateOnly(1990, 5, 1),
|
||||
DocumentNumber = "DOC-1",
|
||||
CountryCode = "HR"
|
||||
},
|
||||
new Person
|
||||
{
|
||||
Id = 2,
|
||||
FirstName = "Johann",
|
||||
LastName = "Schmidt",
|
||||
FirstNameTranscription = "Johann",
|
||||
LastNameTranscription = "Schmidt",
|
||||
AddressLine = "Unter den Linden 5",
|
||||
PostalCode = "10117",
|
||||
City = "Berlin",
|
||||
AddressCountry = "Germany",
|
||||
Email = "johann.schmidt@example.com",
|
||||
ContactPhone = "+49170111222",
|
||||
BirthDate = new DateOnly(1988, 3, 12),
|
||||
DocumentNumber = "DOC-2",
|
||||
CountryCode = "DE"
|
||||
});
|
||||
|
||||
ctx.Sports.AddRange(
|
||||
new Sport { Id = 10, Name = "Football" },
|
||||
new Sport { Id = 20, Name = "Basketball" });
|
||||
|
||||
ctx.Events.AddRange(
|
||||
new Event { Id = 100, Name = "Spring Games", EventDate = new DateOnly(2026, 4, 15) },
|
||||
new Event { Id = 200, Name = "Summer Cup", EventDate = new DateOnly(2026, 6, 20) });
|
||||
|
||||
ctx.Registrations.AddRange(
|
||||
new Registration
|
||||
{
|
||||
Id = 1000,
|
||||
EventId = 100,
|
||||
PersonId = 1,
|
||||
SportId = 10,
|
||||
RegisteredAt = new DateTime(2026, 3, 1, 9, 30, 0, DateTimeKind.Utc)
|
||||
},
|
||||
new Registration
|
||||
{
|
||||
Id = 1001,
|
||||
EventId = 100,
|
||||
PersonId = 2,
|
||||
SportId = 20,
|
||||
RegisteredAt = new DateTime(2026, 3, 2, 10, 0, 0, DateTimeKind.Utc)
|
||||
},
|
||||
new Registration
|
||||
{
|
||||
Id = 1002,
|
||||
EventId = 200,
|
||||
PersonId = 1,
|
||||
SportId = 20,
|
||||
RegisteredAt = new DateTime(2026, 3, 3, 11, 0, 0, DateTimeKind.Utc)
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user