From 68e35b6d90398c9fd37ecc9aa1edee9cb70a0767 Mon Sep 17 00:00:00 2001 From: zysim <9867871+zysim@users.noreply.github.com> Date: Tue, 22 Oct 2024 00:40:36 +0800 Subject: [PATCH] Address more comments --- LeaderboardBackend.Test/Leaderboards.cs | 51 +++++++------------ .../Controllers/LeaderboardsController.cs | 4 +- .../Services/Impl/LeaderboardService.cs | 2 +- LeaderboardBackend/openapi.json | 6 +-- 4 files changed, 25 insertions(+), 38 deletions(-) diff --git a/LeaderboardBackend.Test/Leaderboards.cs b/LeaderboardBackend.Test/Leaderboards.cs index d41905e4..44b8f80d 100644 --- a/LeaderboardBackend.Test/Leaderboards.cs +++ b/LeaderboardBackend.Test/Leaderboards.cs @@ -3,6 +3,7 @@ using System.Net; using System.Net.Http.Json; using System.Threading.Tasks; +using FluentAssertions.Specialized; using LeaderboardBackend.Models.Entities; using LeaderboardBackend.Models.Requests; using LeaderboardBackend.Models.ViewModels; @@ -403,14 +404,8 @@ public async Task RestoreLeaderboard_Banned_Unauthorized() ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService(); - context.Users.Update(new() - { - Id = userModel.Id, - Role = UserRole.Banned, - Username = userModel.Username, - Email = email, - Password = password - }); + User update = await context.Users.FirstAsync(user => user.Id == userModel.Id); + update.Role = UserRole.Banned; await context.SaveChangesAsync(); @@ -436,7 +431,6 @@ public async Task RestoreLeaderboard_Unauthorized(string email, string username, User? user = await context.Users.FindAsync(userModel.Id); - context.Users.Update(user!); user!.Role = role; await context.SaveChangesAsync(); @@ -477,22 +471,19 @@ public async Task RestoreLeaderboard_NotFound_WasNeverDeleted() await context.SaveChangesAsync(); board.Id.Should().NotBe(default); - try - { - await _apiClient.Put( + ExceptionAssertions exAssert = await FluentActions.Awaiting(() => + _apiClient.Put( $"/leaderboard/{board.Id}/restore", new() { Jwt = _jwt, } - ); - } - catch (RequestFailureException e) - { - e.Response.StatusCode.Should().Be(HttpStatusCode.NotFound); - string? model = await e.Response.Content.ReadAsStringAsync(); - model!.Should().MatchRegex("Not Deleted"); - } + ) + ).Should().ThrowAsync().Where(ex => ex.Response.StatusCode == HttpStatusCode.NotFound); + + ProblemDetails? problemDetails = await exAssert.Which.Response.Content.ReadFromJsonAsync(TestInitCommonFields.JsonSerializerOptions); + problemDetails.Should().NotBeNull(); + problemDetails!.Title.Should().Be("Not Deleted"); } [Test] @@ -517,22 +508,18 @@ public async Task RestoreLeaderboard_Conflict() context.Leaderboards.Add(reclaimed); await context.SaveChangesAsync(); - try - { - await _apiClient.Put( + ExceptionAssertions exAssert = await FluentActions.Awaiting(() => + _apiClient.Put( $"/leaderboard/{deleted.Id}/restore", new() { Jwt = _jwt, } - ); - } - catch (RequestFailureException e) - { - e.Response.StatusCode.Should().Be(HttpStatusCode.Conflict); - LeaderboardViewModel? model = await e.Response.Content.ReadFromJsonAsync(TestInitCommonFields.JsonSerializerOptions); - model.Should().NotBeNull(); - model!.Slug.Should().Be("conflicted-mario-world"); - } + ) + ).Should().ThrowAsync().Where(ex => ex.Response.StatusCode == HttpStatusCode.Conflict); + + LeaderboardViewModel? model = await exAssert.Which.Response.Content.ReadFromJsonAsync(TestInitCommonFields.JsonSerializerOptions); + model.Should().NotBeNull(); + model!.Slug.Should().Be("conflicted-mario-world"); } } diff --git a/LeaderboardBackend/Controllers/LeaderboardsController.cs b/LeaderboardBackend/Controllers/LeaderboardsController.cs index b279ed5b..039871c5 100644 --- a/LeaderboardBackend/Controllers/LeaderboardsController.cs +++ b/LeaderboardBackend/Controllers/LeaderboardsController.cs @@ -93,8 +93,8 @@ public async Task> CreateLeaderboard( [SwaggerResponse(200, "The restored `Leaderboard`s view model.", typeof(LeaderboardViewModel))] [SwaggerResponse(401)] [SwaggerResponse(403, "The requesting `User` is unauthorized to restore `Leaderboard`s.")] - [SwaggerResponse(404, "The `Leaderboard` was not found, or it wasn't deleted in the first place.", typeof(string))] - [SwaggerResponse(409, "Another `Leaderboard` with the same slug has been created since, and therefore can't be restored.", typeof(LeaderboardViewModel))] + [SwaggerResponse(404, "The `Leaderboard` was not found, or it wasn't deleted in the first place. Includes a field, `title`, which will be \"Not Found\" in the former case, and \"Not Deleted\" in the latter.", typeof(ProblemDetails))] + [SwaggerResponse(409, "Another `Leaderboard` with the same slug has been created since, and therefore can't be restored. Will include the conflicting board in the response.", typeof(LeaderboardViewModel))] public async Task> RestoreLeaderboard( long id ) diff --git a/LeaderboardBackend/Services/Impl/LeaderboardService.cs b/LeaderboardBackend/Services/Impl/LeaderboardService.cs index d5f26cc1..1754ddac 100644 --- a/LeaderboardBackend/Services/Impl/LeaderboardService.cs +++ b/LeaderboardBackend/Services/Impl/LeaderboardService.cs @@ -46,7 +46,7 @@ public async Task CreateLeaderboard(CreateLeaderboardRe public async Task RestoreLeaderboard(long id) { - Leaderboard? lb = await applicationContext.Leaderboards.FindAsync([id]); + Leaderboard? lb = await applicationContext.Leaderboards.FindAsync(id); if (lb == null) { diff --git a/LeaderboardBackend/openapi.json b/LeaderboardBackend/openapi.json index 01166b48..4e8fabb1 100644 --- a/LeaderboardBackend/openapi.json +++ b/LeaderboardBackend/openapi.json @@ -713,17 +713,17 @@ "description": "The requesting `User` is unauthorized to restore `Leaderboard`s." }, "404": { - "description": "The `Leaderboard` was not found, or it wasn't deleted in the first place.", + "description": "The `Leaderboard` was not found, or it wasn't deleted in the first place. Includes a field, `title`, which will be \"Not Found\" in the former case, and \"Not Deleted\" in the latter.", "content": { "application/json": { "schema": { - "type": "string" + "$ref": "#/components/schemas/ProblemDetails" } } } }, "409": { - "description": "Another `Leaderboard` with the same slug has been created since, and therefore can't be restored.", + "description": "Another `Leaderboard` with the same slug has been created since, and therefore can't be restored. Will include the conflicting board in the response.", "content": { "application/json": { "schema": {