Skip to content

Commit

Permalink
Fix conflict response object type and add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zysim committed Oct 20, 2024
1 parent ac060b6 commit 3418ea1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 18 deletions.
111 changes: 100 additions & 11 deletions LeaderboardBackend.Test/Leaderboards.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http.Json;
using System.Threading.Tasks;
using LeaderboardBackend.Models.Entities;
using LeaderboardBackend.Models.Requests;
using LeaderboardBackend.Models.ViewModels;
using LeaderboardBackend.Services;
using LeaderboardBackend.Test.Lib;
using LeaderboardBackend.Test.TestApi;
using LeaderboardBackend.Test.TestApi.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -372,19 +375,55 @@ public async Task RestoreLeaderboard_OK()

res.Id.Should().Be(deletedBoard.Id);
res.Slug.Should().Be(deletedBoard.Slug);
res.UpdatedAt.Should().Be(res.CreatedAt + Duration.FromMinutes(1));
res.UpdatedAt.Should().Be(_clock.GetCurrentInstant());
res.DeletedAt.Should().BeNull();
}

[Test]
public async Task RestoreLeaderboard_Unauthenticated()
{
Func<Task<LeaderboardViewModel>> act = async () => await _apiClient.Put<LeaderboardViewModel>($"/leaderboard/100/restore", new()
Func<Task<LeaderboardViewModel>> act = async () => await _apiClient.Put<LeaderboardViewModel>($"/leaderboard/100/restore", new());

await act.Should().ThrowAsync<RequestFailureException>().Where(e => e.Response.StatusCode == HttpStatusCode.Unauthorized);
}

[Test]
public async Task RestoreLeaderboard_Banned_Unauthorized()
{
string email = "[email protected]";
string password = "P4ssword";

UserViewModel userModel = await _apiClient.RegisterUser(
"RestoreBoardBanned",
email,
password
);

string jwt = (await _apiClient.LoginUser(email, password)).Token;

ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService<ApplicationContext>();

context.Users.Update(new()
{
Jwt = ""
Id = userModel.Id,
Role = UserRole.Banned,
Username = userModel.Username,
Email = email,
Password = password
});

await act.Should().ThrowAsync<RequestFailureException>().Where(e => e.Response.StatusCode == HttpStatusCode.Unauthorized);
await context.SaveChangesAsync();

await FluentActions.Awaiting(
async () => await _apiClient.Put<LeaderboardViewModel>(
$"/leaderboard/1/restore",
new()
{
Jwt = jwt,
}
)
).Should().ThrowAsync<RequestFailureException>()
.Where(e => e.Response.StatusCode == HttpStatusCode.Forbidden);
}

[TestCase("[email protected]", "RestoreBoard1", UserRole.Confirmed)]
Expand All @@ -395,7 +434,7 @@ public async Task RestoreLeaderboard_Unauthorized(string email, string username,

ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService<ApplicationContext>();

User? user = await context.Users.FindAsync([userModel.Id]);
User? user = await context.Users.FindAsync(userModel.Id);

context.Users.Update(user!);
user!.Role = role;
Expand Down Expand Up @@ -430,20 +469,70 @@ public async Task RestoreLeaderboard_NotFound_WasNeverDeleted()

Leaderboard board = new()
{
Name = "Hyper Mario World",
Name = "Hyper Mario World Not Deleted",
Slug = "hyper-mario-world-non-deleted",
};

context.Leaderboards.Add(board);
await context.SaveChangesAsync();
board.Id.Should().NotBe(default);

Func<Task<LeaderboardViewModel>> act = async () => await _apiClient.Put<LeaderboardViewModel>($"/leaderboard/{board.Id}/restore", new()
try
{
Jwt = _jwt
});
await _apiClient.Put<LeaderboardViewModel>(
$"/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");
}
}

[Test]
public async Task RestoreLeaderboard_Conflict()
{
ApplicationContext context = _factory.Services.CreateScope().ServiceProvider.GetRequiredService<ApplicationContext>();

Leaderboard deleted = new()
{
Name = "Conflicted Mario World",
Slug = "conflicted-mario-world",
DeletedAt = _clock.GetCurrentInstant()
};

await act.Should().ThrowAsync<RequestFailureException>()
.Where(e => e.Response.StatusCode == HttpStatusCode.NotFound);
Leaderboard reclaimed = new()
{
Name = "Reclaimed Mario World",
Slug = "conflicted-mario-world",
};

context.Leaderboards.Add(deleted);
context.Leaderboards.Add(reclaimed);
await context.SaveChangesAsync();

try
{
await _apiClient.Put<LeaderboardViewModel>(
$"/leaderboard/{deleted.Id}/restore",
new()
{
Jwt = _jwt,
}
);
}
catch (RequestFailureException e)
{
e.Response.StatusCode.Should().Be(HttpStatusCode.Conflict);
LeaderboardViewModel? model = await e.Response.Content.ReadFromJsonAsync<LeaderboardViewModel>(TestInitCommonFields.JsonSerializerOptions);
model.Should().NotBeNull();
model!.Slug.Should().Be("conflicted-mario-world");
}
}
}
7 changes: 2 additions & 5 deletions LeaderboardBackend/Controllers/LeaderboardsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,8 @@ long id
board => Ok(LeaderboardViewModel.MapFrom(board)),
notFound => NotFound(),
neverDeleted =>
{
ModelState.AddModelError("Leaderboard", "LeaderboardWasNeverPreviouslyDeleted");
return NotFound("Was never deleted");
},
conflict => Conflict(conflict.Board)
NotFound(ProblemDetailsFactory.CreateProblemDetails(HttpContext, 404, "Not Deleted")),
conflict => Conflict(LeaderboardViewModel.MapFrom(conflict.Board))
);
}
}
2 changes: 0 additions & 2 deletions LeaderboardBackend/Services/Impl/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public async Task<RestoreLeaderboardResult> RestoreLeaderboard(long id)
return new RestoreLeaderboardConflict(maybe);
}

applicationContext.Leaderboards.Update(lb);

lb.DeletedAt = null;

await applicationContext.SaveChangesAsync();
Expand Down

0 comments on commit 3418ea1

Please sign in to comment.