Skip to content

Commit

Permalink
Implement LBP2+ developer leaderboard + 100% test coverage for LevelE…
Browse files Browse the repository at this point in the history
…ndpoints (#203)
  • Loading branch information
Beyley authored Oct 19, 2023
2 parents 7d7036d + ebf9262 commit 3949f2e
Show file tree
Hide file tree
Showing 12 changed files with 674 additions and 101 deletions.
17 changes: 17 additions & 0 deletions Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,21 @@ public Response GetTopScoresForLevel(RequestContext context, GameDatabaseContext
(int skip, int count) = context.GetPageData();
return new Response(SerializedScoreList.FromSubmittedEnumerable(database.GetTopScoresForLevel(level, count, skip, (byte)type).Items), ContentType.Xml);
}

[GameEndpoint("topscores/developer/{id}/{type}", ContentType.Xml)]
[MinimumRole(GameUserRole.Restricted)]
[RateLimitSettings(RequestTimeoutDuration, MaxRequestAmount, RequestBlockDuration, BucketName)]
public Response GetTopScoresForDeveloperLevel(RequestContext context, GameDatabaseContext database, int id, int type)
{
//No story levels have an ID < 0
if (id < 0)
{
return BadRequest;
}

GameLevel level = database.GetStoryLevelById(id);

(int skip, int count) = context.GetPageData();
return new Response(SerializedScoreList.FromSubmittedEnumerable(database.GetTopScoresForLevel(level, count, skip, (byte)type).Items), ContentType.Xml);
}
}
11 changes: 8 additions & 3 deletions Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ public class LevelEndpoints : EndpointGroup

[GameEndpoint("slots/{route}/{username}", ContentType.Xml)]
[MinimumRole(GameUserRole.Restricted)]
[NullStatusCode(NotFound)]
public SerializedMinimalLevelList? GetLevelsWithPlayer(RequestContext context, GameDatabaseContext database, CategoryService categories, MatchService matchService, Token token, string route, string username)
=> this.GetLevels(context, database, categories, matchService, database.GetUserByUsername(username), token, route);
{
GameUser? user = database.GetUserByUsername(username);
if (user == null) return null;

return this.GetLevels(context, database, categories, matchService, user, token, route);
}

[GameEndpoint("s/user/{id}", ContentType.Xml)]
[NullStatusCode(NotFound)]
Expand Down Expand Up @@ -94,7 +100,7 @@ public SerializedCategoryList GetModernCategories(RequestContext context, GameDa

[GameEndpoint("searches/{apiRoute}", ContentType.Xml)]
[MinimumRole(GameUserRole.Restricted)]
public SerializedMinimalLevelList GetLevelsFromCategory(RequestContext context, GameDatabaseContext database, CategoryService categories, MatchService matchService, GameUser user, Token token, string apiRoute)
public SerializedMinimalLevelResultsList GetLevelsFromCategory(RequestContext context, GameDatabaseContext database, CategoryService categories, MatchService matchService, GameUser user, Token token, string apiRoute)
{
(int skip, int count) = context.GetPageData();

Expand Down Expand Up @@ -124,7 +130,6 @@ public SerializedMinimalLevelList GetLevelsFromCategory(RequestContext context,
if (user == null) return null;

SerializedMinimalLevelList? levels = this.GetLevels(context, database, categories, matchService, user, token, "favouriteSlots");
if (levels == null) return null;

return new SerializedMinimalFavouriteLevelList(levels);
}
Expand Down
3 changes: 3 additions & 0 deletions RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Services;
using Refresh.GameServer.Time;
using Refresh.GameServer.Types.Levels.Categories;
using RefreshTests.GameServer.Time;

namespace RefreshTests.GameServer.GameServer;
Expand Down Expand Up @@ -43,5 +44,7 @@ protected override void SetupMiddlewares()
protected override void SetupServices()
{
this._server.AddService<TimeProviderService>(this.DateTimeProvider);
this._server.AddService<CategoryService>();
this._server.AddService<MatchService>();
}
}
4 changes: 2 additions & 2 deletions RefreshTests.GameServer/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace RefreshTests.GameServer;

public static class HttpContentExtensions
{
public async static Task<T> ReadAsXML<T>(this HttpContent content)
public static T ReadAsXML<T>(this HttpContent content)
{
XmlSerializer serializer = new(typeof(T));

return (T)serializer.Deserialize(new XmlTextReader(await content.ReadAsStreamAsync()))!;
return (T)serializer.Deserialize(new XmlTextReader(content.ReadAsStream()))!;
}
}
2 changes: 1 addition & 1 deletion RefreshTests.GameServer/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData,

return client;
}

public GameUser CreateUser(string? username = null)
{
username ??= this.UserIncrement.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace RefreshTests.GameServer.Tests.Authentication;
public class AuthenticationIntegrationTests : GameServerTest
{
[Test]
public async Task GameAuthenticationWorks()
public void GameAuthenticationWorks()
{
using TestContext context = this.GetServer();

HttpResponseMessage unauthedRequest = await context.Http.GetAsync("/lbp/eula");
HttpResponseMessage unauthedRequest = context.Http.GetAsync("/lbp/eula").Result;
Assert.That(unauthedRequest.StatusCode, Is.EqualTo(Forbidden));

HttpClient authedClient = context.GetAuthenticatedClient(TokenType.Game, out string tokenData);
Expand All @@ -26,16 +26,16 @@ public async Task GameAuthenticationWorks()
Assert.That(token, Is.Not.Null);
Assert.That(token?.User, Is.Not.Null);

HttpResponseMessage authedRequest = await authedClient.GetAsync("/lbp/eula");
HttpResponseMessage authedRequest = authedClient.GetAsync("/lbp/eula").Result;
Assert.That(authedRequest.StatusCode, Is.EqualTo(OK));
}

[Test]
public async Task ApiAuthenticationWorks()
public void ApiAuthenticationWorks()
{
using TestContext context = this.GetServer();

HttpResponseMessage unauthedRequest = await context.Http.GetAsync("/api/v3/users/me");
HttpResponseMessage unauthedRequest = context.Http.GetAsync("/api/v3/users/me").Result;
Assert.That(unauthedRequest.StatusCode, Is.EqualTo(Forbidden));

HttpClient authedClient = context.GetAuthenticatedClient(TokenType.Api, out string tokenData);
Expand All @@ -45,18 +45,18 @@ public async Task ApiAuthenticationWorks()
Assert.That(token?.User, Is.Not.Null);

// TODO: Fix serialization of ObjectId
HttpResponseMessage response = await authedClient.GetAsync("/api/v3/users/me");
// (GameUser? user, HttpResponseMessage response) = await authedClient.GetJsonObjectAsync<GameUser>("/api/v3/user/me");
HttpResponseMessage response = authedClient.GetAsync("/api/v3/users/me").Result;
// (GameUser? user, HttpResponseMessage response) = authedClient.GetJsonObjectAsync<GameUser>("/api/v3/user/me");
Assert.Multiple(async () =>
{
// Assert.That(user, Is.Not.Null);
Assert.That(await response.Content.ReadAsStringAsync(), Contains.Substring(token!.User.UserId.ToString()));
Assert.That(response.Content.ReadAsStringAsync().Result, Contains.Substring(token!.User.UserId.ToString()));
Assert.That(response.StatusCode, Is.EqualTo(OK));
});
}

[Test]
public async Task TokenRefreshingWorks()
public void TokenRefreshingWorks()
{
using TestContext context = this.GetServer();

Expand All @@ -72,11 +72,11 @@ public async Task TokenRefreshingWorks()
PasswordSha512 = Encoding.ASCII.GetString(SHA512.HashData(Encoding.ASCII.GetBytes(password))),
};

HttpResponseMessage response = await context.Http.PostAsync("/api/v3/login", new StringContent(JsonConvert.SerializeObject(payload)));
HttpResponseMessage response = context.Http.PostAsync("/api/v3/login", new StringContent(JsonConvert.SerializeObject(payload))).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));


string respString = await response.Content.ReadAsStringAsync();
string respString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(respString);
ApiResponse<ApiAuthenticationResponse>? authResponse = JsonConvert.DeserializeObject<ApiResponse<ApiAuthenticationResponse>>(respString);
Assert.Multiple(() =>
Expand All @@ -96,7 +96,7 @@ public async Task TokenRefreshingWorks()
});

context.Http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authResponse!.Data!.TokenData);
response = await context.Http.GetAsync("/api/v3/users/me");
response = context.Http.GetAsync("/api/v3/users/me").Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

// jump to when token expires
Expand All @@ -109,24 +109,24 @@ public async Task TokenRefreshingWorks()
Assert.That(context.Database.GetTokenFromTokenData(authResponse.Data.RefreshTokenData!, TokenType.ApiRefresh), Is.Not.Null);
});

response = await context.Http.GetAsync("/api/v3/users/me");
response = context.Http.GetAsync("/api/v3/users/me").Result;
Assert.That(response.StatusCode, Is.EqualTo(Forbidden));

ApiRefreshRequest refreshPayload = new()
{
TokenData = authResponse.Data.RefreshTokenData!,
};

response = await context.Http.PostAsync("/api/v3/refreshToken", new StringContent(JsonConvert.SerializeObject(refreshPayload)));
response = context.Http.PostAsync("/api/v3/refreshToken", new StringContent(JsonConvert.SerializeObject(refreshPayload))).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

respString = await response.Content.ReadAsStringAsync();
respString = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(respString);
authResponse = JsonConvert.DeserializeObject<ApiResponse<ApiAuthenticationResponse>>(respString);

context.Http.DefaultRequestHeaders.Remove("Authorization");
context.Http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authResponse!.Data!.TokenData);
response = await context.Http.GetAsync("/api/v3/users/me");
response = context.Http.GetAsync("/api/v3/users/me").Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));
}
}
Loading

0 comments on commit 3949f2e

Please sign in to comment.