Events-MVC (example with htmx)

This commit is contained in:
Boris Milašinović
2026-04-25 22:21:35 +02:00
parent eb04483417
commit 0ee1b22f61
114 changed files with 7966 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class CountriesCrudShould
{
[Fact]
public async Task CreateCountry()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Countries",
"/Countries/Create",
[
new("Code", "DE"),
new("Alpha3", "DEU"),
new("Name", "Germany"),
new("Translations[0].LanguageCode", "hr"),
new("Translations[0].Name", "Germany")
]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Germany", html);
}
[Fact]
public async Task EditCountry()
{
await using var factory = new CustomWebApplicationFactory(ctx => ctx.Countries.Add(TestDataSeederCountry()));
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Countries",
"/Countries/Edit/HR",
[
new("Code", "HR"),
new("Alpha3", "HRV"),
new("Name", "Republic of Croatia"),
new("Translations[0].LanguageCode", ""),
new("Translations[0].Name", "")
]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Republic of Croatia", html);
}
[Fact]
public async Task DeleteCountry()
{
await using var factory = new CustomWebApplicationFactory(ctx => ctx.Countries.Add(TestDataSeederCountry()));
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(client, "/Countries", "/Countries/Delete/HR", []);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain("Croatia", html);
}
private static Events.EF.Models.Country TestDataSeederCountry()
{
return new()
{
Code = "HR",
Alpha3 = "HRV",
Name = "Croatia"
};
}
}

View File

@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Events.MVC\Events.MVC.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,54 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class EventsCrudShould
{
[Fact]
public async Task CreateEvent()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Events",
"/Events/Create",
[new("Name", "Autumn Cup"), new("EventDate", "2026-09-10")]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Autumn Cup", html);
}
[Fact]
public async Task EditEvent()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedEvents);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Events",
"/Events/Edit/100",
[new("Id", "100"), new("Name", "Updated Games"), new("EventDate", "2026-04-20")]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Updated Games", html);
}
[Fact]
public async Task DeleteEvent()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedEvents);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(client, "/Events", "/Events/Delete/100", []);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain("Spring Games", html);
}
}

View File

@@ -0,0 +1,25 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class EventsPageShould
{
[Fact]
public async Task ShowParticipantsCountAndRegistrationsLinkWhenRegistrationsExist()
{
await using var factory = new CustomWebApplicationFactory(ctx =>
{
TestDataSeeder.SeedRegistrationsScenario(ctx);
});
using var client = factory.CreateClient();
var response = await client.GetAsync("/Events");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Spring Games", html);
Assert.Contains("/Registrations?eventId=100", html);
Assert.Contains(">2<", html);
}
}

View File

@@ -0,0 +1 @@
global using Xunit;

View File

@@ -0,0 +1,20 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class HomePageShould
{
[Fact]
public async Task ReturnSuccessAndContainEnglishDescription()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient();
var response = await client.GetAsync("/");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("This sample demonstrates an ASP.NET Core MVC application", html);
}
}

View File

@@ -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();
}

View File

@@ -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();
});
}
}

View File

@@ -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)
});
}
}

View File

@@ -0,0 +1,89 @@
using System.Net;
#if POSTGRES
using Events.EF.Data.Postgres;
#else
using Events.EF.Data.MSSQL;
#endif
using Events.EF.Models;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class PeopleCrudShould
{
[Fact]
public async Task CreatePerson()
{
await using var factory = new CustomWebApplicationFactory(ctx => ctx.Countries.Add(new Country { Code = "HR", Alpha3 = "HRV", Name = "Croatia" }));
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/People",
"/People/Create",
[
new("FirstName", "Ana"),
new("LastName", "Kovac"),
new("FirstNameTranscription", "Ana"),
new("LastNameTranscription", "Kovac"),
new("Email", "ana.kovac@example.com"),
new("ContactPhone", "+38591123456"),
new("BirthDate", "1995-01-01"),
new("CountryCode", "HR"),
new("DocumentNumber", "DOC-2"),
new("AddressLine", "Main Street 1"),
new("PostalCode", "10000"),
new("City", "Zagreb"),
new("AddressCountry", "Croatia")
]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Ana Kovac", html);
}
[Fact]
public async Task EditPerson()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedPeople);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/People",
"/People/Edit/1",
[
new("Id", "1"),
new("FirstName", "Ivan"),
new("LastName", "Kovac"),
new("FirstNameTranscription", "Ivan"),
new("LastNameTranscription", "Kovac"),
new("Email", "ivan.kovac@example.com"),
new("ContactPhone", "+38591111222"),
new("BirthDate", "1990-05-01"),
new("CountryCode", "HR"),
new("DocumentNumber", "DOC-1"),
new("AddressLine", "Ilica 1"),
new("PostalCode", "10000"),
new("City", "Zagreb"),
new("AddressCountry", "Croatia")
]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Ivan Kovac", html);
}
[Fact]
public async Task DeletePerson()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedPeople);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(client, "/People", "/People/Delete/1", []);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain("Ivan Horvat", html);
}
}

View File

@@ -0,0 +1,23 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
using Microsoft.AspNetCore.Mvc.Testing;
namespace Events.Tests.IntegrationTests;
public class PeopleGuardShould
{
[Fact]
public async Task RedirectToCountriesWhenPeoplePageIsRequestedWithoutCountries()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
var response = await client.GetAsync("/People");
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal("/Countries", response.Headers.Location?.OriginalString);
}
}

View File

@@ -0,0 +1,74 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class PeoplePageShould
{
[Fact]
public async Task ReturnPageWithPeopleListWhenCountryAndPersonExist()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedPeople);
using var client = factory.CreateClient();
var response = await client.GetAsync("/People");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("People list", html);
Assert.Contains("First name", html);
Assert.Contains("Last name", html);
Assert.Contains("Ivan Horvat", html);
Assert.Contains("Croatia", html);
}
[Fact]
public async Task RenderNativeFullNameAndTranscribedNamesInSeparateColumns()
{
await using var factory = new CustomWebApplicationFactory(ctx =>
{
ctx.Countries.Add(new Events.EF.Models.Country
{
Code = "UA",
Alpha3 = "UKR",
Name = "Ukraine"
});
ctx.People.Add(new Events.EF.Models.Person
{
Id = 1,
FirstName = "Олексій",
LastName = "Шевченко",
FirstNameTranscription = "Oleksii",
LastNameTranscription = "Shevchenko",
AddressLine = "Khreshchatyk 1",
PostalCode = "01001",
City = "Kyiv",
AddressCountry = "Ukraine",
Email = "oleksii.shevchenko@example.com",
ContactPhone = "+38050111222",
BirthDate = new DateOnly(1991, 6, 1),
DocumentNumber = "DOC-1",
CountryCode = "UA"
});
});
using var client = factory.CreateClient();
var response = await client.GetAsync("/People");
var html = await response.Content.ReadAsStringAsync();
var decodedHtml = WebUtility.HtmlDecode(html);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("<th></th>", decodedHtml);
Assert.Contains("<td>Олексій Шевченко</td>", decodedHtml);
Assert.Contains("<td>Oleksii</td>", decodedHtml);
Assert.Contains("<td>Shevchenko</td>", decodedHtml);
var firstNameColumnIndex = decodedHtml.IndexOf("<td>Oleksii</td>", StringComparison.Ordinal);
var lastNameColumnIndex = decodedHtml.IndexOf("<td>Shevchenko</td>", StringComparison.Ordinal);
var nativeFullNameColumnIndex = decodedHtml.IndexOf("<td>Олексій Шевченко</td>", StringComparison.Ordinal);
Assert.True(firstNameColumnIndex < lastNameColumnIndex);
Assert.True(lastNameColumnIndex < nativeFullNameColumnIndex);
}
}

View File

@@ -0,0 +1,61 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class RegistrationsCrudShould
{
[Fact]
public async Task CreateRegistration()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Registrations?eventId=100",
"/Registrations/Create",
[new("EventId", "100"), new("PersonId", "1"), new("SportId", "20"), new("PersonLookup", "Ivan Horvat")]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Basketball", html);
}
[Fact]
public async Task EditRegistration()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Registrations?eventId=100",
"/Registrations/Edit/1000",
[
new("Id", "1000"),
new("EventId", "100"),
new("PersonId", "2"),
new("SportId", "20"),
new("PersonLookup", "Johann Schmidt")
]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Johann Schmidt", html);
Assert.Contains("Basketball", html);
}
[Fact]
public async Task DeleteRegistration()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(client, "/Registrations?eventId=100", "/Registrations/Delete/1000?eventId=100", []);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain(">1000<", html);
}
}

View File

@@ -0,0 +1,84 @@
using System.Net;
#if POSTGRES
using Events.EF.Data.Postgres;
#else
using Events.EF.Data.MSSQL;
#endif
using Events.EF.Models;
using Events.Tests.IntegrationTests.Infrastructure;
using Microsoft.AspNetCore.Mvc.Testing;
namespace Events.Tests.IntegrationTests;
public class RegistrationsGuardShould
{
[Fact]
public async Task RedirectToSportsWhenRegistrationsPageIsRequestedWithoutSports()
{
await using var factory = new CustomWebApplicationFactory(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"
});
ctx.Events.Add(new Event
{
Id = 100,
Name = "Spring Games",
EventDate = new DateOnly(2026, 4, 15)
});
});
using var client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
var response = await client.GetAsync("/Registrations");
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal("/Sports", response.Headers.Location?.OriginalString);
}
[Fact]
public async Task RedirectToPeopleWhenRegistrationsPageIsRequestedWithoutPeople()
{
await using var factory = new CustomWebApplicationFactory(ctx =>
{
ctx.Sports.Add(new Sport { Id = 10, Name = "Football" });
ctx.Events.Add(new Event
{
Id = 100,
Name = "Spring Games",
EventDate = new DateOnly(2026, 4, 15)
});
});
using var client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
var response = await client.GetAsync("/Registrations");
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal("/People", response.Headers.Location?.OriginalString);
}
}

View File

@@ -0,0 +1,57 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class RegistrationsPageShould
{
[Fact]
public async Task ReturnEventSpecificRegistrationsForSelectedEvent()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
var response = await client.GetAsync("/Registrations?eventId=100");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Registrations for: Spring Games", html);
Assert.Contains("Ivan Horvat", html);
Assert.Contains("Johann Schmidt", html);
Assert.Contains("Croatia", html);
Assert.Contains("Germany", html);
}
[Fact]
public async Task ReturnFilteredPartialForHtmxRequestWithCountryFilter()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
client.DefaultRequestHeaders.Add(Events.MVC.Constants.HtmxHeaders.Request, "true");
var response = await client.GetAsync("/Registrations?eventId=100&filters=CountryCode==HR");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("id=\"registrations-panel\"", html);
Assert.Contains("Ivan Horvat", html);
Assert.DoesNotContain("Johann Schmidt", html);
Assert.DoesNotContain("<html", html, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ReturnOnlyMatchingCountrySuggestionsForPersonLookup()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedRegistrationsScenario);
using var client = factory.CreateClient();
var response = await client.GetAsync("/Registrations/PersonSuggestions?personLookup=jo&countryFilter=DE");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Johann Schmidt", html);
Assert.Contains("Johann", html);
Assert.DoesNotContain("Ivan Horvat", html);
Assert.Contains("data-person-suggestion", html);
}
}

View File

@@ -0,0 +1,72 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class SportsCrudShould
{
[Fact]
public async Task CreateSport()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Sports",
"/Sports/Create",
[new("Name", "Volleyball")]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Volleyball", html);
}
[Fact]
public async Task ReturnValidationErrorWhenCreatingSportWithoutName()
{
await using var factory = new CustomWebApplicationFactory();
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Sports",
"/Sports/Create",
[new("Name", string.Empty)]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("The Name field is required.", html);
Assert.DoesNotContain("was added successfully", html);
}
[Fact]
public async Task EditSport()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedSports);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(
client,
"/Sports",
"/Sports/Edit/1",
[new("Id", "1"), new("Name", "Handball")]);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Handball", html);
}
[Fact]
public async Task DeleteSport()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedSports);
using var client = factory.CreateClient();
var response = await AntiforgeryRequestHelper.PostFormAsync(client, "/Sports", "/Sports/Delete/1", []);
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.DoesNotContain("Football", html);
}
}

View File

@@ -0,0 +1,38 @@
using System.Net;
using Events.Tests.IntegrationTests.Infrastructure;
namespace Events.Tests.IntegrationTests;
public class SportsPageShould
{
[Fact]
public async Task ReturnPageWithSportsListWhenSportsExist()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedSports);
using var client = factory.CreateClient();
var response = await client.GetAsync("/Sports");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Sports list", html);
Assert.Contains("Football", html);
Assert.Contains("Basketball", html);
}
[Fact]
public async Task ReturnOnlySportsPartialForHtmxRequest()
{
await using var factory = new CustomWebApplicationFactory(TestDataSeeder.SeedSports);
using var client = factory.CreateClient();
client.DefaultRequestHeaders.Add(Events.MVC.Constants.HtmxHeaders.Request, "true");
var response = await client.GetAsync("/Sports");
var html = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("id=\"sports-list\"", html);
Assert.DoesNotContain("<html", html, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("navbar", html, StringComparison.OrdinalIgnoreCase);
}
}