backend + web: Add admin account lifecycle management
This commit is contained in:
1 parent
fcf7cd0596
commit
91d0c06c83
25 files changed
+1148
-97
No files matched your search
@@ -28,12 +28,15 @@ public sealed class AdminUsersTests
|
||||
SignIn(application.Services, client, role);
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
|
||||
using var list = await client.GetAsync(UsersPath, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, list.StatusCode);
|
||||
Assert.Equal(redirect, list.Headers.Location?.AbsolutePath);
|
||||
foreach (var action in new[] { "role", "active" })
|
||||
foreach (var path in new[] { UsersPath, UsersPath + "/new", UsersPath + "/7/edit" })
|
||||
{
|
||||
using var update = await client.PostAsync($"{UsersPath}/7/{action}", null, ct);
|
||||
using var page = await client.GetAsync(path, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, page.StatusCode);
|
||||
Assert.Equal(redirect, page.Headers.Location?.AbsolutePath);
|
||||
}
|
||||
foreach (var action in new[] { "7/role", "7/active", "new", "7/edit", "7/delete", "7/email-confirmation", "bulk" })
|
||||
{
|
||||
using var update = await client.PostAsync($"{UsersPath}/{action}", null, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, update.StatusCode);
|
||||
Assert.Equal(redirect, update.Headers.Location?.AbsolutePath);
|
||||
}
|
||||
@@ -63,8 +66,7 @@ public sealed class AdminUsersTests
|
||||
Assert.DoesNotContain($"{UsersPath}/1/active", html);
|
||||
Assert.Contains($"{UsersPath}/7/role{query}", html);
|
||||
Assert.Contains($"{UsersPath}/7/active{query}", html);
|
||||
var token = Regex.Match(html, "name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]+)\"").Groups[1].Value;
|
||||
Assert.NotEmpty(token);
|
||||
var token = AntiforgeryToken(html);
|
||||
|
||||
var search = Assert.Single(backend.Requests);
|
||||
Assert.Equal(HttpMethod.Get, search.Method);
|
||||
@@ -111,6 +113,133 @@ public sealed class AdminUsersTests
|
||||
Assert.Empty(backend.Requests);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AdminCanSubmitAccountFormsAndConfirmedBulkDeletion()
|
||||
{
|
||||
var backend = new AdminUsersBackend();
|
||||
await using var application = CreateApplication(backend);
|
||||
using var client = CreateClient(application);
|
||||
SignIn(application.Services, client, UserRoles.Admin);
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
|
||||
using var createPage = await client.GetAsync(UsersPath + "/new", ct);
|
||||
Assert.Equal(HttpStatusCode.OK, createPage.StatusCode);
|
||||
var page = WebUtility.HtmlDecode(await createPage.Content.ReadAsStringAsync(ct));
|
||||
Assert.Contains("Počáteční heslo", page);
|
||||
Assert.Contains("Ověřit e-mail ručně", page);
|
||||
Assert.Matches("""<input\b(?=[^>]*id="verification-email")(?=[^>]*checked="checked")[^>]*>""", page);
|
||||
Assert.True(page.IndexOf("Správa uživatelů", StringComparison.Ordinal) < page.IndexOf("Swagger API", StringComparison.Ordinal));
|
||||
var fields = new Dictionary<string, string>
|
||||
{
|
||||
["__RequestVerificationToken"] = AntiforgeryToken(page),
|
||||
["FirstName"] = "Jan",
|
||||
["LastName"] = "Test",
|
||||
["Email"] = "[email protected]",
|
||||
["Role"] = UserRoles.Student,
|
||||
["Password"] = "Initial-password",
|
||||
["IsActive"] = "true",
|
||||
["EmailConfirmed"] = "true",
|
||||
};
|
||||
using var createForm = new FormUrlEncodedContent(fields);
|
||||
using var created = await client.PostAsync(UsersPath + "/new", createForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, created.StatusCode);
|
||||
var createdRequest = Assert.Single(backend.Requests);
|
||||
Assert.Equal(HttpMethod.Post, createdRequest.Method);
|
||||
Assert.Equal("/api/v1/admin/users", createdRequest.Uri.AbsolutePath);
|
||||
Assert.Contains("\"email\":\"[email protected]\"", createdRequest.Body);
|
||||
Assert.Contains("\"emailConfirmed\":true", createdRequest.Body);
|
||||
|
||||
using var editPage = await client.GetAsync(UsersPath + "/7/edit", ct);
|
||||
Assert.Equal(HttpStatusCode.OK, editPage.StatusCode);
|
||||
Assert.Contains("[email protected]", await editPage.Content.ReadAsStringAsync(ct));
|
||||
fields.Remove("Password");
|
||||
using var editForm = new FormUrlEncodedContent(fields);
|
||||
using var edited = await client.PostAsync(UsersPath + "/7/edit", editForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, edited.StatusCode);
|
||||
Assert.Equal(HttpMethod.Put, backend.Requests[^1].Method);
|
||||
Assert.Contains("\"password\":null", backend.Requests[^1].Body);
|
||||
|
||||
var bulk = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new("__RequestVerificationToken", fields["__RequestVerificationToken"]),
|
||||
new("action", "delete"),
|
||||
new("userIds", "7"),
|
||||
new("userIds", "8"),
|
||||
};
|
||||
var count = backend.Requests.Count;
|
||||
using var unconfirmedForm = new FormUrlEncodedContent(bulk);
|
||||
using var unconfirmed = await client.PostAsync(UsersPath + "/bulk", unconfirmedForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, unconfirmed.StatusCode);
|
||||
Assert.Equal(count, backend.Requests.Count);
|
||||
|
||||
bulk.Add(new("confirmDelete", "true"));
|
||||
using var confirmedForm = new FormUrlEncodedContent(bulk);
|
||||
using var confirmed = await client.PostAsync(UsersPath + "/bulk", confirmedForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, confirmed.StatusCode);
|
||||
Assert.Equal("/api/v1/admin/users/bulk", backend.Requests[^1].Uri.AbsolutePath);
|
||||
Assert.Contains("\"userIds\":[7,8]", backend.Requests[^1].Body);
|
||||
Assert.Contains("\"action\":\"delete\"", backend.Requests[^1].Body);
|
||||
|
||||
using var singleForm = new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
["__RequestVerificationToken"] = fields["__RequestVerificationToken"],
|
||||
["confirmDelete"] = "true",
|
||||
});
|
||||
using var deleted = await client.PostAsync(UsersPath + "/7/delete", singleForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, deleted.StatusCode);
|
||||
Assert.Equal(HttpMethod.Delete, backend.Requests[^1].Method);
|
||||
Assert.Equal("/api/v1/admin/users/7", backend.Requests[^1].Uri.AbsolutePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AdminCanResendVerificationAfterCreationSavedAnAccountButDeliveryFailed()
|
||||
{
|
||||
var backend = new AdminUsersBackend(failVerification: true);
|
||||
await using var application = CreateApplication(backend);
|
||||
using var client = CreateClient(application);
|
||||
SignIn(application.Services, client, UserRoles.Admin);
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
|
||||
using var createPage = await client.GetAsync(UsersPath + "/new", ct);
|
||||
var page = WebUtility.HtmlDecode(await createPage.Content.ReadAsStringAsync(ct));
|
||||
using var createForm = new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
["__RequestVerificationToken"] = AntiforgeryToken(page),
|
||||
["FirstName"] = "Jan",
|
||||
["LastName"] = "Novák",
|
||||
["Email"] = "[email protected]",
|
||||
["Role"] = UserRoles.Student,
|
||||
["Password"] = "Initial-password",
|
||||
["IsActive"] = "true",
|
||||
["EmailConfirmed"] = "false",
|
||||
});
|
||||
using var created = await client.PostAsync(UsersPath + "/new", createForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, created.StatusCode);
|
||||
Assert.StartsWith(UsersPath + "/7/edit", created.Headers.Location?.OriginalString);
|
||||
|
||||
using var editPage = await client.GetAsync(created.Headers.Location, ct);
|
||||
var edit = WebUtility.HtmlDecode(await editPage.Content.ReadAsStringAsync(ct));
|
||||
Assert.Contains("Účet byl založen.", edit);
|
||||
Assert.Contains("Ověřovací e-mail se nepodařilo odeslat.", edit);
|
||||
Assert.Contains("Poslat ověřovací e-mail", edit);
|
||||
|
||||
using var resendForm = new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
["__RequestVerificationToken"] = AntiforgeryToken(edit),
|
||||
});
|
||||
using var resent = await client.PostAsync(UsersPath + "/7/email-confirmation", resendForm, ct);
|
||||
Assert.Equal(HttpStatusCode.Redirect, resent.StatusCode);
|
||||
Assert.Equal((HttpMethod.Post, "/api/v1/admin/users/7/email-confirmation"),
|
||||
(backend.Requests[^1].Method, backend.Requests[^1].Uri.AbsolutePath));
|
||||
}
|
||||
|
||||
private static string AntiforgeryToken(string html)
|
||||
{
|
||||
var token = Regex.Match(html, "name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]+)\"").Groups[1].Value;
|
||||
Assert.NotEmpty(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
private static WebApplicationFactory<Program> CreateApplication(AdminUsersBackend backend) =>
|
||||
new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
|
||||
builder.ConfigureTestServices(services => services.AddHttpClient(nameof(HelpdeskApiClient))
|
||||
@@ -123,7 +252,7 @@ public sealed class AdminUsersTests
|
||||
BaseAddress = new Uri("https://localhost"),
|
||||
});
|
||||
|
||||
private sealed class AdminUsersBackend : HttpMessageHandler
|
||||
private sealed class AdminUsersBackend(bool failVerification = false) : HttpMessageHandler
|
||||
{
|
||||
public List<(HttpMethod Method, Uri Uri, string? Body)> Requests { get; } = [];
|
||||
|
||||
@@ -132,9 +261,24 @@ public sealed class AdminUsersTests
|
||||
Assert.Equal("test-access", request.Headers.Authorization?.Parameter);
|
||||
var uri = Assert.IsType<Uri>(request.RequestUri);
|
||||
Requests.Add((request.Method, uri, request.Content is null ? null : await request.Content.ReadAsStringAsync(ct)));
|
||||
if (request.Method == HttpMethod.Put)
|
||||
if (request.Method == HttpMethod.Post && uri.AbsolutePath == "/api/v1/admin/users" && failVerification)
|
||||
return new HttpResponseMessage(HttpStatusCode.Created)
|
||||
{
|
||||
Content = new StringContent("""
|
||||
{"id":7,"email":"[email protected]","firstName":"Jan","lastName":"Novák","role":"STUDENT","isActive":true,"emailConfirmed":false,"emailNotificationsEnabled":true,"verificationEmailSent":false}
|
||||
""", Encoding.UTF8, "application/json"),
|
||||
};
|
||||
if (request.Method != HttpMethod.Get)
|
||||
return new HttpResponseMessage(HttpStatusCode.NoContent);
|
||||
|
||||
if (uri.AbsolutePath == "/api/v1/admin/users/7")
|
||||
return new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
Content = new StringContent("""
|
||||
{"id":7,"email":"[email protected]","firstName":"Jan","lastName":"Novák","role":"STUDENT","isActive":true,"emailConfirmed":false,"emailNotificationsEnabled":true}
|
||||
""", Encoding.UTF8, "application/json"),
|
||||
};
|
||||
|
||||
Assert.Equal("/api/v1" + UsersPath, uri.AbsolutePath);
|
||||
return new HttpResponseMessage(HttpStatusCode.OK)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user