|
|
|
@@ -1,111 +0,0 @@
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using JobTrackerApi.Controllers;
|
|
|
|
|
using JobTrackerApi.Data;
|
|
|
|
|
using JobTrackerApi.Models;
|
|
|
|
|
using JobTrackerApi.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
using Moq;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace JobTrackerApi.MariaDb.Tests;
|
|
|
|
|
|
|
|
|
|
public sealed class PreferenceConcurrencyTests(ITestOutputHelper output)
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Concurrent_language_and_notification_writes_preserve_the_winner_and_allow_a_fresh_retry()
|
|
|
|
|
{
|
|
|
|
|
await using var database = await MariaDbDatabase.CreateAsync();
|
|
|
|
|
await using (var seed = database.Context())
|
|
|
|
|
{
|
|
|
|
|
seed.Users.AddRange(
|
|
|
|
|
new ApplicationUser { Id = "preference-owner", UserName = "owner@example.test", UiLanguage = "en", EmailFollowUpRemindersEnabled = true },
|
|
|
|
|
new ApplicationUser { Id = "preference-other", UserName = "other@example.test", UiLanguage = "nb-NO", EmailFollowUpRemindersEnabled = false });
|
|
|
|
|
await seed.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await using var languageScope = database.App.Services.CreateAsyncScope();
|
|
|
|
|
await using var notificationScope = database.App.Services.CreateAsyncScope();
|
|
|
|
|
await using var otherScope = database.App.Services.CreateAsyncScope();
|
|
|
|
|
var language = LanguageController(languageScope.ServiceProvider, "preference-owner");
|
|
|
|
|
var notification = NotificationController(notificationScope.ServiceProvider, "preference-owner");
|
|
|
|
|
var otherLanguage = LanguageController(otherScope.ServiceProvider, "preference-other");
|
|
|
|
|
var otherNotification = NotificationController(otherScope.ServiceProvider, "preference-other");
|
|
|
|
|
|
|
|
|
|
// Model two requests that have read the same Identity version before either writes.
|
|
|
|
|
var languageSnapshot = await languageScope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>().GetUserAsync(language.User);
|
|
|
|
|
var notificationSnapshot = await notificationScope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>().GetUserAsync(notification.User);
|
|
|
|
|
Assert.NotNull(languageSnapshot);
|
|
|
|
|
Assert.NotNull(notificationSnapshot);
|
|
|
|
|
Assert.NotSame(languageSnapshot, notificationSnapshot);
|
|
|
|
|
var originalStamp = languageSnapshot.ConcurrencyStamp;
|
|
|
|
|
Assert.Equal(originalStamp, notificationSnapshot.ConcurrencyStamp);
|
|
|
|
|
|
|
|
|
|
var languageWrite = language.UpdateLanguagePreference(new("nb-NO"));
|
|
|
|
|
var notificationWrite = notification.Put(new(false));
|
|
|
|
|
var otherWrite = otherLanguage.UpdateLanguagePreference(new("en"));
|
|
|
|
|
await Task.WhenAll(languageWrite, notificationWrite, otherWrite);
|
|
|
|
|
Assert.IsType<NoContentResult>(await otherWrite);
|
|
|
|
|
Assert.IsType<BadRequestObjectResult>(await otherLanguage.UpdateLanguagePreference(new("fr-FR")));
|
|
|
|
|
Assert.IsType<OkObjectResult>((await otherNotification.Put(new(true))).Result);
|
|
|
|
|
|
|
|
|
|
var languageWon = await languageWrite is NoContentResult;
|
|
|
|
|
if (languageWon)
|
|
|
|
|
{
|
|
|
|
|
var error = Assert.IsType<ObjectResult>((await notificationWrite).Result);
|
|
|
|
|
Assert.Equal(StatusCodes.Status500InternalServerError, error.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var error = Assert.IsType<BadRequestObjectResult>(await languageWrite);
|
|
|
|
|
Assert.Contains(new IdentityErrorDescriber().ConcurrencyFailure().Description, Assert.IsType<string>(error.Value));
|
|
|
|
|
Assert.IsType<OkObjectResult>((await notificationWrite).Result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await using (var read = database.Context())
|
|
|
|
|
{
|
|
|
|
|
var owner = await read.Users.AsNoTracking().SingleAsync(user => user.Id == "preference-owner");
|
|
|
|
|
Assert.Equal(languageWon ? "nb-NO" : "en", owner.UiLanguage);
|
|
|
|
|
Assert.Equal(languageWon, owner.EmailFollowUpRemindersEnabled);
|
|
|
|
|
Assert.NotEqual(originalStamp, owner.ConcurrencyStamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await using var retryScope = database.App.Services.CreateAsyncScope();
|
|
|
|
|
if (languageWon)
|
|
|
|
|
Assert.IsType<OkObjectResult>((await NotificationController(retryScope.ServiceProvider, "preference-owner").Put(new(false))).Result);
|
|
|
|
|
else
|
|
|
|
|
Assert.IsType<NoContentResult>(await LanguageController(retryScope.ServiceProvider, "preference-owner").UpdateLanguagePreference(new("nb-NO")));
|
|
|
|
|
|
|
|
|
|
await using var reload = database.Context();
|
|
|
|
|
var users = await reload.Users.AsNoTracking().Where(user => user.Id.StartsWith("preference-")).OrderBy(user => user.Id).ToListAsync();
|
|
|
|
|
Assert.Collection(users,
|
|
|
|
|
other => { Assert.Equal("preference-other", other.Id); Assert.Equal("en", other.UiLanguage); Assert.True(other.EmailFollowUpRemindersEnabled); },
|
|
|
|
|
owner => { Assert.Equal("preference-owner", owner.Id); Assert.Equal("nb-NO", owner.UiLanguage); Assert.False(owner.EmailFollowUpRemindersEnabled); });
|
|
|
|
|
output.WriteLine($"MariaDB {database.ActualVersion}: {(languageWon ? "language" : "notification")} won the overlapping writes; fresh retry preserved both preferences and the other owner's independent writes.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static AuthController LanguageController(IServiceProvider services, string owner) => new(
|
|
|
|
|
services.GetRequiredService<IConfiguration>(), services.GetRequiredService<UserManager<ApplicationUser>>(),
|
|
|
|
|
Mock.Of<ITokenService>(), Mock.Of<IAppEmailSender>(), Mock.Of<IGoogleTokenValidator>(), Mock.Of<IMicrosoftTokenValidator>(),
|
|
|
|
|
NullLogger<AuthController>.Instance, Mock.Of<ITwoFactorPendingTokenService>(), services.GetRequiredService<JobTrackerContext>(),
|
|
|
|
|
externalOrigin: ExternalOrigin.Parse("http://localhost:3300", production: false))
|
|
|
|
|
{ ControllerContext = Principal(services, owner) };
|
|
|
|
|
|
|
|
|
|
private static NotificationSettingsController NotificationController(IServiceProvider services, string owner) => new(
|
|
|
|
|
services.GetRequiredService<UserManager<ApplicationUser>>()) { ControllerContext = Principal(services, owner) };
|
|
|
|
|
|
|
|
|
|
private static ControllerContext Principal(IServiceProvider services, string owner) => new()
|
|
|
|
|
{
|
|
|
|
|
HttpContext = new DefaultHttpContext
|
|
|
|
|
{
|
|
|
|
|
RequestServices = services,
|
|
|
|
|
User = new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, owner)], "test")),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|