From a569912a0b44600fb7598af4890c45f2c78de975 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 16:02:36 -0700 Subject: [PATCH 1/8] Implement LBP2+ developer leaderboard get --- .../Game/Levels/LeaderboardEndpoints.cs | 17 ++++++++ .../Tests/Levels/ScoreLeaderboardTests.cs | 40 ++++++++++--------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs index 73632203..ab4fc542 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs @@ -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); + } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs index 203f4830..b9dd7c50 100644 --- a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs @@ -66,13 +66,13 @@ public async Task SubmitsDeveloperScore() context.Database.Refresh(); - // message = await client.GetAsync($"/lbp/topscores/developer/{level.LevelId}/1"); - // Assert.That(message.StatusCode, Is.EqualTo(OK)); - // - // SerializedScoreList scores = await message.Content.ReadAsXML(); - // Assert.That(scores.Scores, Has.Count.EqualTo(1)); - // Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); - // Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); + message = await client.GetAsync($"/lbp/topscores/developer/1/1"); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedScoreList scores = await message.Content.ReadAsXML(); + Assert.That(scores.Scores, Has.Count.EqualTo(1)); + Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); + Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); message = await client.GetAsync($"/lbp/scoreboard/developer/1"); Assert.That(message.StatusCode, Is.EqualTo(OK)); @@ -138,7 +138,6 @@ public async Task DoesntSubmitDeveloperInvalidScore() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); - GameLevel level = context.CreateLevel(user); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); @@ -149,12 +148,12 @@ public async Task DoesntSubmitDeveloperInvalidScore() Score = -1, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/developer/{level.LevelId}", new StringContent(score.AsXML())); + HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); context.Database.Refresh(); - List scores = context.Database.GetTopScoresForLevel(level, 1, 0, 1).Items.ToList(); + List scores = context.Database.GetTopScoresForLevel(context.Database.GetStoryLevelById(1), 1, 0, 1).Items.ToList(); Assert.That(scores, Has.Count.EqualTo(0)); } @@ -197,24 +196,29 @@ public async Task DoesntSubmitDeveloperScoreToInvalidLevel() } [Test] - public async Task DoesntGetDeveloperScoresForInvalidLevel() + public async Task DoesntGetDeveloperMultiLeaderboardForInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - SerializedScore score = new() - { - Host = true, - ScoreType = 1, - Score = 0, - }; - HttpResponseMessage message = await client.GetAsync($"/lbp/scoreboard/developer/-1"); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } + [Test] + public async Task DoesntGetDeveloperScoresForInvalidLevel() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = await client.GetAsync($"/lbp/topscores/developer/-1/1"); + Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); + } + /// The number of scores to try to fetch from the database /// The expected index of the submitted score /// How many scores should be filled into the database From 1e539192bd793e8f01d94f10a6daf28b249cba5b Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 16:21:53 -0700 Subject: [PATCH 2/8] TestContext: Add method to get unauthenticated HttpClient --- RefreshTests.GameServer/TestContext.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/RefreshTests.GameServer/TestContext.cs b/RefreshTests.GameServer/TestContext.cs index 4f0e134e..0e6c8d8a 100644 --- a/RefreshTests.GameServer/TestContext.cs +++ b/RefreshTests.GameServer/TestContext.cs @@ -70,6 +70,11 @@ public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData, return client; } + + public HttpClient GetUnauthenticatedClient() + { + return this.Listener.GetClient(); + } public GameUser CreateUser(string? username = null) { From 3d799531c4a2d6e5ea2b6ed15536f2db3dd0538c Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 16:22:10 -0700 Subject: [PATCH 3/8] Enable match and category service in test context --- RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs b/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs index 60d20c6d..e71cf747 100644 --- a/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs +++ b/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs @@ -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; @@ -43,5 +44,7 @@ protected override void SetupMiddlewares() protected override void SetupServices() { this._server.AddService(this.DateTimeProvider); + this._server.AddService(); + this._server.AddService(); } } \ No newline at end of file From 42f820a4139475d37b1fea8d9bebcf08da9babd2 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 18:01:31 -0700 Subject: [PATCH 4/8] LevelEndpoints: Add explicit NotFound return for invalid user --- .../Endpoints/Game/Levels/LevelEndpoints.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs index dc755eff..d811cc18 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs @@ -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)] From ca408ff3b9354ab5e79f299f1c567403a8fd28f5 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 18:02:37 -0700 Subject: [PATCH 5/8] LevelEndpoints: Remove unnecessary null check GetLevels will never return null, since user != null and the route is guaranteed to be valid. --- Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs index d811cc18..a70a3c72 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs @@ -100,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(); @@ -130,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); } From 5fd1451385ab4e53707c986db4f2b2acf2475d0a Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 18:03:19 -0700 Subject: [PATCH 6/8] Add 100% test coverage for LevelEndpoints + make all tests synchronous --- .../HttpClientExtensions.cs | 10 + .../HttpContentExtensions.cs | 4 +- .../AuthenticationIntegrationTests.cs | 32 +- .../Tests/Levels/LevelTests.cs | 544 ++++++++++++++++++ .../Tests/Levels/ScoreLeaderboardTests.cs | 78 +-- .../Tests/Middlewares/ApiV2MiddlewareTests.cs | 12 +- .../Middlewares/CrossOriginMiddlewareTests.cs | 12 +- .../Middlewares/DigestMiddlewareTests.cs | 20 +- .../LegacyAdapterMiddlewareTests.cs | 12 +- 9 files changed, 639 insertions(+), 85 deletions(-) create mode 100644 RefreshTests.GameServer/HttpClientExtensions.cs create mode 100644 RefreshTests.GameServer/Tests/Levels/LevelTests.cs diff --git a/RefreshTests.GameServer/HttpClientExtensions.cs b/RefreshTests.GameServer/HttpClientExtensions.cs new file mode 100644 index 00000000..59158803 --- /dev/null +++ b/RefreshTests.GameServer/HttpClientExtensions.cs @@ -0,0 +1,10 @@ +namespace RefreshTests.GameServer; + +public static class HttpClientExtensions +{ + public static T WaitResult(this Task message) + { + message.Wait(); + return message.Result; + } +} \ No newline at end of file diff --git a/RefreshTests.GameServer/HttpContentExtensions.cs b/RefreshTests.GameServer/HttpContentExtensions.cs index bc8632f0..04ea847b 100644 --- a/RefreshTests.GameServer/HttpContentExtensions.cs +++ b/RefreshTests.GameServer/HttpContentExtensions.cs @@ -5,10 +5,10 @@ namespace RefreshTests.GameServer; public static class HttpContentExtensions { - public async static Task ReadAsXML(this HttpContent content) + public static T ReadAsXML(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()))!; } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs b/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs index 9efaf96e..b17364c6 100644 --- a/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs +++ b/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs @@ -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").WaitResult(); Assert.That(unauthedRequest.StatusCode, Is.EqualTo(Forbidden)); HttpClient authedClient = context.GetAuthenticatedClient(TokenType.Game, out string tokenData); @@ -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").WaitResult(); 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").WaitResult(); Assert.That(unauthedRequest.StatusCode, Is.EqualTo(Forbidden)); HttpClient authedClient = context.GetAuthenticatedClient(TokenType.Api, out string tokenData); @@ -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("/api/v3/user/me"); + HttpResponseMessage response = authedClient.GetAsync("/api/v3/users/me").WaitResult(); + // (GameUser? user, HttpResponseMessage response) = authedClient.GetJsonObjectAsync("/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().WaitResult(), 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(); @@ -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))).WaitResult(); Assert.That(response.StatusCode, Is.EqualTo(OK)); - string respString = await response.Content.ReadAsStringAsync(); + string respString = response.Content.ReadAsStringAsync().WaitResult(); Console.WriteLine(respString); ApiResponse? authResponse = JsonConvert.DeserializeObject>(respString); Assert.Multiple(() => @@ -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").WaitResult(); Assert.That(response.StatusCode, Is.EqualTo(OK)); // jump to when token expires @@ -109,7 +109,7 @@ 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").WaitResult(); Assert.That(response.StatusCode, Is.EqualTo(Forbidden)); ApiRefreshRequest refreshPayload = new() @@ -117,16 +117,16 @@ public async Task TokenRefreshingWorks() 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))).WaitResult(); Assert.That(response.StatusCode, Is.EqualTo(OK)); - respString = await response.Content.ReadAsStringAsync(); + respString = response.Content.ReadAsStringAsync().WaitResult(); Console.WriteLine(respString); authResponse = JsonConvert.DeserializeObject>(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").WaitResult(); Assert.That(response.StatusCode, Is.EqualTo(OK)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Levels/LevelTests.cs b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs new file mode 100644 index 00000000..eda3ccd9 --- /dev/null +++ b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs @@ -0,0 +1,544 @@ +using Refresh.GameServer.Authentication; +using Refresh.GameServer.Endpoints.Game.DataTypes.Response; +using Refresh.GameServer.Types.Levels; +using Refresh.GameServer.Types.Lists; +using Refresh.GameServer.Types.Reviews; +using Refresh.GameServer.Types.UserData; + +namespace RefreshTests.GameServer.Tests.Levels; + +public class LevelTests : GameServerTest +{ + [Test] + public void SlotsNewest() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/newest").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + + //slots without a route is equivalent to newest + message = client.GetAsync($"/lbp/slots").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsRandom() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/lbp2luckydip").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsQueued() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Get the queued slots + HttpResponseMessage message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + //Add the level to the queue + message = client.PostAsync($"/lbp/lolcatftw/add/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the queued slots now + message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + + //Remove the level from the queue + message = client.PostAsync($"/lbp/lolcatftw/remove/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the queued slots + message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + } + + [Test] + public void SlotsHearted() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Get the favourite slots + HttpResponseMessage message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure its empty + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + //Favourite the level + message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the favourite slots now + message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure the only entry is the level + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + + //Unfavourite the level + message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the favourite slots + message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure its now empty + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + } + + [Test] + public void SlotsHeartedQuirk() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Get the favourite slots + HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure its empty + SerializedMinimalFavouriteLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + //Favourite the level + message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the favourite slots now + message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure the only entry is the level + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); + + //Unfavourite the level + message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Get the favourite slots + message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + //Make sure its now empty + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + } + + [Test] + public void SlotsHeartedQuirkFailsWhenInvalidUser() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Get the favourite slots + HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/I_AM_NOT_A_REAL_USER").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + } + + [Test] + public void SlotsMostHearted() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameUser user2 = context.CreateUser(); + GameUser user3 = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + GameLevel level2 = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + context.Database.FavouriteLevel(level, user); + context.Database.FavouriteLevel(level, user2); + context.Database.FavouriteLevel(level2, user2); + + message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); + + context.Database.FavouriteLevel(level2, user); + context.Database.FavouriteLevel(level2, user3); + + message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level2.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsMostLiked() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + GameLevel level2 = context.CreateLevel(publisher); + + GameUser user = context.CreateUser(); + GameUser user2 = context.CreateUser(); + GameUser user3 = context.CreateUser(); + + context.Database.PlayLevel(level, user, 1); + context.Database.PlayLevel(level2, user, 1); + context.Database.PlayLevel(level, user2, 1); + context.Database.PlayLevel(level2, user2, 1); + context.Database.PlayLevel(level, user3, 1); + context.Database.PlayLevel(level2, user3, 1); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + bool a = context.Database.RateLevel(level, user, RatingType.Yay); + context.Database.RateLevel(level, user2, RatingType.Yay); + context.Database.RateLevel(level2, user3, RatingType.Yay); + + message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); + + context.Database.RateLevel(level2, user, RatingType.Yay); + context.Database.RateLevel(level, user3, RatingType.Boo); + + message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level2.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsMostPlayed() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + GameLevel level2 = context.CreateLevel(publisher); + + GameUser user = context.CreateUser(); + GameUser user2 = context.CreateUser(); + GameUser user3 = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostUniquePlays").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + context.Database.PlayLevel(level, user, 1); + context.Database.PlayLevel(level, user2, 1); + context.Database.PlayLevel(level2, user2, 1); + context.Database.PlayLevel(level, user3, 1); + context.Database.PlayLevel(level2, user3, 1); + + message = client.GetAsync($"/lbp/slots/mostUniquePlays").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); + } + + [Test] + public void SlotsMostReplayed() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + GameLevel level2 = context.CreateLevel(publisher); + + GameUser user = context.CreateUser(); + GameUser user2 = context.CreateUser(); + GameUser user3 = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostPlays").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + context.Database.PlayLevel(level, user, 1); + context.Database.PlayLevel(level, user2, 1); + context.Database.PlayLevel(level2, user2, 5); + context.Database.PlayLevel(level, user3, 1); + context.Database.PlayLevel(level2, user3, 1); + + message = client.GetAsync($"/lbp/slots/mostPlays").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level2.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsTeamPicked() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mmpicks").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + context.Database.AddTeamPickToLevel(level); + + message = client.GetAsync($"/lbp/slots/mmpicks").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + } + + [Test] + public void SlotsByUser() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + + GameLevel level = context.CreateLevel(publisher); + + message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(1)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + + message = client.GetAsync($"/lbp/slots/by/I_AM_NOT_REAL").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + } + + [Test] + public void GetLevelById() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/s/user/{level.LevelId}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + GameLevelResponse result = message.Content.ReadAsXML(); + Assert.That(result.LevelId, Is.EqualTo(level.LevelId)); + + message = client.GetAsync($"/lbp/s/user/{int.MaxValue}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + } + + [Test] + public void GetSlotList() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + GameLevel level2 = context.CreateLevel(publisher); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={level.LevelId}&s={level2.LevelId}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); + } + + [Test] + public void GetLevelsFromCategory() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + GameLevel level = context.CreateLevel(publisher); + GameLevel level2 = context.CreateLevel(publisher); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/searches/newest").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + SerializedMinimalLevelResultsList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(2)); + Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); + Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); + } + + [Test] + public void DoesntGetSlotListWhenNoQuery() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slotList").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); + } + + [Test] + public void DoesntGetSlotListWhenInvalidQuery() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s=NOT_A_NUMBER").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); + } + + [Test] + public void GetSlotListWhenInvalidLevel() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={int.MaxValue}").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + SerializedLevelList result = message.Content.ReadAsXML(); + Assert.That(result.Items, Has.Count.EqualTo(0)); + } + + [Test] + public void GetModernCategories() + { + using TestContext context = this.GetServer(); + GameUser publisher = context.CreateUser(); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); + + HttpResponseMessage message = client.GetAsync($"/lbp/genres").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Just throw away the value, but at least make sure it parses + _ = message.Content.ReadAsXML(); + + message = client.GetAsync($"/lbp/searches").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(OK)); + + //Just throw away the value, but at least make sure it parses + _ = message.Content.ReadAsXML(); + } + + [Test] + public void SlotsInvalidRoute() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage message = client.GetAsync($"/lbp/slots/waaaaaa").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + } + + [Test] + public void SlotsHeartedApiV3FailsWhenNoUser() + { + using TestContext context = this.GetServer(); + GameUser user = context.CreateUser(); + GameLevel level = context.CreateLevel(user); + + using HttpClient client = context.GetUnauthenticatedClient(); + + HttpResponseMessage message = client.GetAsync($"/api/v3/levels/hearted").WaitResult(); + Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + } +} \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs index b9dd7c50..7554001e 100644 --- a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs @@ -10,7 +10,7 @@ namespace RefreshTests.GameServer.Tests.Levels; public class ScoreLeaderboardTests : GameServerTest { [Test] - public async Task SubmitsScore() + public void SubmitsScore() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -25,21 +25,21 @@ public async Task SubmitsScore() Score = 5, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); - message = await client.GetAsync($"/lbp/topscores/user/{level.LevelId}/1"); + message = client.GetAsync($"/lbp/topscores/user/{level.LevelId}/1").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); - SerializedScoreList scores = await message.Content.ReadAsXML(); + SerializedScoreList scores = message.Content.ReadAsXML(); Assert.That(scores.Scores, Has.Count.EqualTo(1)); Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); - message = await client.GetAsync($"/lbp/scoreboard/user/{level.LevelId}"); + message = client.GetAsync($"/lbp/scoreboard/user/{level.LevelId}").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); - SerializedMultiLeaderboardResponse scoresMulti = await message.Content.ReadAsXML(); + SerializedMultiLeaderboardResponse scoresMulti = message.Content.ReadAsXML(); SerializedPlayerLeaderboardResponse singleplayerScores = scoresMulti.Scoreboards.First(s => s.PlayerCount == 1); Assert.That(singleplayerScores.Scores, Has.Count.EqualTo(1)); Assert.That(singleplayerScores.Scores[0].Player, Is.EqualTo(user.Username)); @@ -47,7 +47,7 @@ public async Task SubmitsScore() } [Test] - public async Task SubmitsDeveloperScore() + public void SubmitsDeveloperScore() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -61,23 +61,23 @@ public async Task SubmitsDeveloperScore() Score = 5, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); - message = await client.GetAsync($"/lbp/topscores/developer/1/1"); + message = client.GetAsync($"/lbp/topscores/developer/1/1").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); - SerializedScoreList scores = await message.Content.ReadAsXML(); + SerializedScoreList scores = message.Content.ReadAsXML(); Assert.That(scores.Scores, Has.Count.EqualTo(1)); Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); - message = await client.GetAsync($"/lbp/scoreboard/developer/1"); + message = client.GetAsync($"/lbp/scoreboard/developer/1").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); - SerializedMultiLeaderboardResponse scoresMulti = await message.Content.ReadAsXML(); + SerializedMultiLeaderboardResponse scoresMulti = message.Content.ReadAsXML(); SerializedPlayerLeaderboardResponse singleplayerScores = scoresMulti.Scoreboards.First(s => s.PlayerCount == 1); Assert.That(singleplayerScores.Scores, Has.Count.EqualTo(1)); Assert.That(singleplayerScores.Scores[0].Player, Is.EqualTo(user.Username)); @@ -85,31 +85,31 @@ public async Task SubmitsDeveloperScore() } [Test] - public async Task DosentGetLeaderboardForInvalidLevel() + public void DosentGetLeaderboardForInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message2 = await client.GetAsync($"/lbp/topscores/user/{int.MaxValue}/1"); + HttpResponseMessage message2 = client.GetAsync($"/lbp/topscores/user/{int.MaxValue}/1").WaitResult(); Assert.That(message2.StatusCode, Is.EqualTo(NotFound)); } [Test] - public async Task DosentGetMultiLeaderboardForInvalidLevel() + public void DosentGetMultiLeaderboardForInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.GetAsync($"/lbp/scoreboard/user/{int.MaxValue}"); + HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/user/{int.MaxValue}").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } [Test] - public async Task DoesntSubmitInvalidScore() + public void DoesntSubmitInvalidScore() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -124,7 +124,7 @@ public async Task DoesntSubmitInvalidScore() Score = -1, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); context.Database.Refresh(); @@ -134,7 +134,7 @@ public async Task DoesntSubmitInvalidScore() } [Test] - public async Task DoesntSubmitDeveloperInvalidScore() + public void DoesntSubmitDeveloperInvalidScore() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -148,7 +148,7 @@ public async Task DoesntSubmitDeveloperInvalidScore() Score = -1, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); context.Database.Refresh(); @@ -158,7 +158,7 @@ public async Task DoesntSubmitDeveloperInvalidScore() } [Test] - public async Task DoesntSubmitsScoreToInvalidLevel() + public void DoesntSubmitsScoreToInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -172,12 +172,12 @@ public async Task DoesntSubmitsScoreToInvalidLevel() Score = 0, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/user/{int.MaxValue}", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{int.MaxValue}", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } [Test] - public async Task DoesntSubmitDeveloperScoreToInvalidLevel() + public void DoesntSubmitDeveloperScoreToInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -191,31 +191,31 @@ public async Task DoesntSubmitDeveloperScoreToInvalidLevel() Score = 0, }; - HttpResponseMessage message = await client.PostAsync($"/lbp/scoreboard/developer/-1", new StringContent(score.AsXML())); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/-1", new StringContent(score.AsXML())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } [Test] - public async Task DoesntGetDeveloperMultiLeaderboardForInvalidLevel() + public void DoesntGetDeveloperMultiLeaderboardForInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.GetAsync($"/lbp/scoreboard/developer/-1"); + HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/developer/-1").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } [Test] - public async Task DoesntGetDeveloperScoresForInvalidLevel() + public void DoesntGetDeveloperScoresForInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.GetAsync($"/lbp/topscores/developer/-1/1"); + HttpResponseMessage message = client.GetAsync($"/lbp/topscores/developer/-1/1").WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -291,7 +291,7 @@ public void FailsWithInvalidNumber() } [Test] - public async Task PlayLevel() + public void PlayLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -299,7 +299,7 @@ public async Task PlayLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.PostAsync($"/lbp/play/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); @@ -308,19 +308,19 @@ public async Task PlayLevel() } [Test] - public async Task DoesntPlayInvalidLevel() + public void DoesntPlayInvalidLevel() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.PostAsync($"/lbp/play/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } [Test] - public async Task PlayLevelWithCount() + public void PlayLevelWithCount() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -329,7 +329,7 @@ public async Task PlayLevelWithCount() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - HttpResponseMessage message = await client.PostAsync($"/lbp/play/user/{level.LevelId}?count=2", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=2", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); @@ -338,7 +338,7 @@ public async Task PlayLevelWithCount() } [Test] - public async Task DoesntPlayLevelWithInvalidCount() + public void DoesntPlayLevelWithInvalidCount() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -347,15 +347,15 @@ public async Task DoesntPlayLevelWithInvalidCount() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - HttpResponseMessage message = await client.PostAsync($"/lbp/play/user/{level.LevelId}?count=gtgnyegth", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=gtgnyegth", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); - HttpResponseMessage message2 = await client.PostAsync($"/lbp/play/user/{level.LevelId}?count=-5", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message2 = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=-5", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message2.StatusCode, Is.EqualTo(BadRequest)); } [Test] - public async Task DoesntPlayLevelWithCountOnMainline() + public void DoesntPlayLevelWithCountOnMainline() { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); @@ -363,7 +363,7 @@ public async Task DoesntPlayLevelWithCountOnMainline() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = await client.PostAsync($"/lbp/play/user/{level.LevelId}?count=3", new ReadOnlyMemoryContent(Array.Empty())); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=3", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs index 965ccc20..cb2f694b 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs @@ -5,32 +5,32 @@ namespace RefreshTests.GameServer.Tests.Middlewares; public class ApiV2MiddlewareTests : GameServerTest { [Test] - public async Task ErrorsWhenGoingToV2() + public void ErrorsWhenGoingToV2() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.GetAsync("/api/v2/asdf"); + HttpResponseMessage response = context.Http.GetAsync("/api/v2/asdf").WaitResult(); Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(Gone)); - Assert.That(await response.Content.ReadAsStringAsync(), Does.Contain("error")); + Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Does.Contain("error")); }); } [Test] - public async Task OkWhenGoingAnywhereElse() + public void OkWhenGoingAnywhereElse() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.GetAsync("/api/v3/instance"); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(OK)); - Assert.That(await response.Content.ReadAsStringAsync(), Does.Not.Contain("error")); + Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Does.Not.Contain("error")); }); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs index 50a548b1..dfb8efb1 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs @@ -5,12 +5,12 @@ namespace RefreshTests.GameServer.Tests.Middlewares; public class CrossOriginMiddlewareTests : GameServerTest { [Test] - public async Task PutsCorrectHeadersWhenGoingToV3() + public void PutsCorrectHeadersWhenGoingToV3() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.GetAsync("/api/v3/instance"); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(OK)); @@ -20,12 +20,12 @@ public async Task PutsCorrectHeadersWhenGoingToV3() } [Test] - public async Task PutsCorrectHeadersWhenOptionsV3() + public void PutsCorrectHeadersWhenOptionsV3() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Options, "/api/v3/instance")); + HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Options, "/api/v3/instance")).WaitResult(); Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(OK)); @@ -35,12 +35,12 @@ public async Task PutsCorrectHeadersWhenOptionsV3() } [Test] - public async Task PutsNoHeadersWhenAvoidingV3() + public void PutsNoHeadersWhenAvoidingV3() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/lbp/eula")); + HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/lbp/eula")).WaitResult(); Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(Forbidden)); diff --git a/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs index b28152ba..d53aa688 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs @@ -6,12 +6,12 @@ namespace RefreshTests.GameServer.Tests.Middlewares; public class DigestMiddlewareTests : GameServerTest { [Test] - public async Task DoesntIncludeDigestWhenOutsideOfGame() + public void DoesntIncludeDigestWhenOutsideOfGame() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.GetAsync("/api/v3/instance"); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); Assert.Multiple(() => { @@ -21,13 +21,13 @@ public async Task DoesntIncludeDigestWhenOutsideOfGame() } [Test] - public async Task IncludesDigestInGame() + public void IncludesDigestInGame() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddEndpointGroup(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = await context.Http.GetAsync("/lbp/eula"); + HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").WaitResult(); Assert.Multiple(() => { @@ -37,7 +37,7 @@ public async Task IncludesDigestInGame() } [Test] - public async Task DigestIsCorrect() + public void DigestIsCorrect() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddEndpointGroup(); @@ -53,7 +53,7 @@ public async Task DigestIsCorrect() string clientDigest = DigestMiddleware.CalculateDigest(endpoint, blankMs, "", null, null); context.Http.DefaultRequestHeaders.Add("X-Digest-A", clientDigest); - HttpResponseMessage response = await context.Http.GetAsync(endpoint); + HttpResponseMessage response = context.Http.GetAsync(endpoint).WaitResult(); Assert.Multiple(() => { @@ -68,7 +68,7 @@ public async Task DigestIsCorrect() } [Test] - public async Task PspDigestIsCorrect() + public void PspDigestIsCorrect() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddEndpointGroup(); @@ -86,7 +86,7 @@ public async Task PspDigestIsCorrect() context.Http.DefaultRequestHeaders.Add("X-Digest-A", clientDigest); context.Http.DefaultRequestHeaders.Add("X-data-v", "5"); context.Http.DefaultRequestHeaders.Add("X-exe-v", "205"); - HttpResponseMessage response = await context.Http.GetAsync(endpoint); + HttpResponseMessage response = context.Http.GetAsync(endpoint).WaitResult(); Assert.Multiple(() => { @@ -101,14 +101,14 @@ public async Task PspDigestIsCorrect() } [Test] - public async Task FailsWhenDigestIsBad() + public void FailsWhenDigestIsBad() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddEndpointGroup(); context.Server.Value.Server.AddMiddleware(); context.Http.DefaultRequestHeaders.Add("X-Digest-A", "asdf"); - HttpResponseMessage response = await context.Http.GetAsync("/lbp/eula"); + HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").WaitResult(); Assert.Pass(); // TODO: we have no way of detecting a failed digest check } diff --git a/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs index 93e85abd..fbb926de 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs @@ -7,13 +7,13 @@ namespace RefreshTests.GameServer.Tests.Middlewares; public class LegacyAdapterMiddlewareTests : GameServerTest { [Test] - public async Task DoesntAdaptForOtherUrls() + public void DoesntAdaptForOtherUrls() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); context.Server.Value.Server.AddEndpointGroup(); - ApiOkResponse? response = await context.Http.GetFromJsonAsync("/api/v3/test"); + ApiOkResponse? response = context.Http.GetFromJsonAsync("/api/v3/test").WaitResult(); Assert.That(response, Is.Not.Null); Assert.Multiple(() => @@ -24,18 +24,18 @@ public async Task DoesntAdaptForOtherUrls() } [Test] - public async Task AdaptsForLegacyGameUrl() + public void AdaptsForLegacyGameUrl() { using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); context.Server.Value.Server.AddEndpointGroup(); - - HttpResponseMessage response = await context.Http.GetAsync("/LITTLEBIGPLANETPS3_XML/test"); + + HttpResponseMessage response = context.Http.GetAsync("/LITTLEBIGPLANETPS3_XML/test").WaitResult(); Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(OK)); - Assert.That(await response.Content.ReadAsStringAsync(), Is.EqualTo("test")); + Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Is.EqualTo("test")); }); } } \ No newline at end of file From 0c6f20f5a442e6af920f4505496261433d4f88b4 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 18:07:08 -0700 Subject: [PATCH 7/8] Remove unneeded "Wait + Result return" extension method --- .../HttpClientExtensions.cs | 10 --- .../AuthenticationIntegrationTests.cs | 24 ++--- .../Tests/Levels/LevelTests.cs | 90 +++++++++---------- .../Tests/Levels/ScoreLeaderboardTests.cs | 40 ++++----- .../Tests/Middlewares/ApiV2MiddlewareTests.cs | 8 +- .../Middlewares/CrossOriginMiddlewareTests.cs | 6 +- .../Middlewares/DigestMiddlewareTests.cs | 10 +-- .../LegacyAdapterMiddlewareTests.cs | 6 +- 8 files changed, 92 insertions(+), 102 deletions(-) delete mode 100644 RefreshTests.GameServer/HttpClientExtensions.cs diff --git a/RefreshTests.GameServer/HttpClientExtensions.cs b/RefreshTests.GameServer/HttpClientExtensions.cs deleted file mode 100644 index 59158803..00000000 --- a/RefreshTests.GameServer/HttpClientExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace RefreshTests.GameServer; - -public static class HttpClientExtensions -{ - public static T WaitResult(this Task message) - { - message.Wait(); - return message.Result; - } -} \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs b/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs index b17364c6..059d4115 100644 --- a/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs +++ b/RefreshTests.GameServer/Tests/Authentication/AuthenticationIntegrationTests.cs @@ -17,7 +17,7 @@ public void GameAuthenticationWorks() { using TestContext context = this.GetServer(); - HttpResponseMessage unauthedRequest = context.Http.GetAsync("/lbp/eula").WaitResult(); + HttpResponseMessage unauthedRequest = context.Http.GetAsync("/lbp/eula").Result; Assert.That(unauthedRequest.StatusCode, Is.EqualTo(Forbidden)); HttpClient authedClient = context.GetAuthenticatedClient(TokenType.Game, out string tokenData); @@ -26,7 +26,7 @@ public void GameAuthenticationWorks() Assert.That(token, Is.Not.Null); Assert.That(token?.User, Is.Not.Null); - HttpResponseMessage authedRequest = authedClient.GetAsync("/lbp/eula").WaitResult(); + HttpResponseMessage authedRequest = authedClient.GetAsync("/lbp/eula").Result; Assert.That(authedRequest.StatusCode, Is.EqualTo(OK)); } @@ -35,7 +35,7 @@ public void ApiAuthenticationWorks() { using TestContext context = this.GetServer(); - HttpResponseMessage unauthedRequest = context.Http.GetAsync("/api/v3/users/me").WaitResult(); + 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); @@ -45,12 +45,12 @@ public void ApiAuthenticationWorks() Assert.That(token?.User, Is.Not.Null); // TODO: Fix serialization of ObjectId - HttpResponseMessage response = authedClient.GetAsync("/api/v3/users/me").WaitResult(); + HttpResponseMessage response = authedClient.GetAsync("/api/v3/users/me").Result; // (GameUser? user, HttpResponseMessage response) = authedClient.GetJsonObjectAsync("/api/v3/user/me"); Assert.Multiple(async () => { // Assert.That(user, Is.Not.Null); - Assert.That(response.Content.ReadAsStringAsync().WaitResult(), 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)); }); } @@ -72,11 +72,11 @@ public void TokenRefreshingWorks() PasswordSha512 = Encoding.ASCII.GetString(SHA512.HashData(Encoding.ASCII.GetBytes(password))), }; - HttpResponseMessage response = context.Http.PostAsync("/api/v3/login", new StringContent(JsonConvert.SerializeObject(payload))).WaitResult(); + HttpResponseMessage response = context.Http.PostAsync("/api/v3/login", new StringContent(JsonConvert.SerializeObject(payload))).Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); - string respString = response.Content.ReadAsStringAsync().WaitResult(); + string respString = response.Content.ReadAsStringAsync().Result; Console.WriteLine(respString); ApiResponse? authResponse = JsonConvert.DeserializeObject>(respString); Assert.Multiple(() => @@ -96,7 +96,7 @@ public void TokenRefreshingWorks() }); context.Http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authResponse!.Data!.TokenData); - response = context.Http.GetAsync("/api/v3/users/me").WaitResult(); + response = context.Http.GetAsync("/api/v3/users/me").Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); // jump to when token expires @@ -109,7 +109,7 @@ public void TokenRefreshingWorks() Assert.That(context.Database.GetTokenFromTokenData(authResponse.Data.RefreshTokenData!, TokenType.ApiRefresh), Is.Not.Null); }); - response = context.Http.GetAsync("/api/v3/users/me").WaitResult(); + response = context.Http.GetAsync("/api/v3/users/me").Result; Assert.That(response.StatusCode, Is.EqualTo(Forbidden)); ApiRefreshRequest refreshPayload = new() @@ -117,16 +117,16 @@ public void TokenRefreshingWorks() TokenData = authResponse.Data.RefreshTokenData!, }; - response = context.Http.PostAsync("/api/v3/refreshToken", new StringContent(JsonConvert.SerializeObject(refreshPayload))).WaitResult(); + response = context.Http.PostAsync("/api/v3/refreshToken", new StringContent(JsonConvert.SerializeObject(refreshPayload))).Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); - respString = response.Content.ReadAsStringAsync().WaitResult(); + respString = response.Content.ReadAsStringAsync().Result; Console.WriteLine(respString); authResponse = JsonConvert.DeserializeObject>(respString); context.Http.DefaultRequestHeaders.Remove("Authorization"); context.Http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authResponse!.Data!.TokenData); - response = context.Http.GetAsync("/api/v3/users/me").WaitResult(); + response = context.Http.GetAsync("/api/v3/users/me").Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Levels/LevelTests.cs b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs index eda3ccd9..54e44e5b 100644 --- a/RefreshTests.GameServer/Tests/Levels/LevelTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs @@ -18,7 +18,7 @@ public void SlotsNewest() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/newest").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/newest").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -26,7 +26,7 @@ public void SlotsNewest() Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); //slots without a route is equivalent to newest - message = client.GetAsync($"/lbp/slots").WaitResult(); + message = client.GetAsync($"/lbp/slots").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -43,7 +43,7 @@ public void SlotsRandom() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/lbp2luckydip").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/lbp2luckydip").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -61,18 +61,18 @@ public void SlotsQueued() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); //Get the queued slots - HttpResponseMessage message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/lolcatftw").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(0)); //Add the level to the queue - message = client.PostAsync($"/lbp/lolcatftw/add/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/lolcatftw/add/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the queued slots now - message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + message = client.GetAsync($"/lbp/slots/lolcatftw").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -80,11 +80,11 @@ public void SlotsQueued() Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); //Remove the level from the queue - message = client.PostAsync($"/lbp/lolcatftw/remove/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/lolcatftw/remove/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the queued slots - message = client.GetAsync($"/lbp/slots/lolcatftw").WaitResult(); + message = client.GetAsync($"/lbp/slots/lolcatftw").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -101,18 +101,18 @@ public void SlotsHearted() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); //Get the favourite slots - HttpResponseMessage message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/favouriteSlots").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure its empty SerializedMinimalLevelList result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(0)); //Favourite the level - message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the favourite slots now - message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + message = client.GetAsync($"/lbp/slots/favouriteSlots").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure the only entry is the level result = message.Content.ReadAsXML(); @@ -120,11 +120,11 @@ public void SlotsHearted() Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); //Unfavourite the level - message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the favourite slots - message = client.GetAsync($"/lbp/slots/favouriteSlots").WaitResult(); + message = client.GetAsync($"/lbp/slots/favouriteSlots").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure its now empty result = message.Content.ReadAsXML(); @@ -141,18 +141,18 @@ public void SlotsHeartedQuirk() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); //Get the favourite slots - HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure its empty SerializedMinimalFavouriteLevelList result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(0)); //Favourite the level - message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the favourite slots now - message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure the only entry is the level result = message.Content.ReadAsXML(); @@ -160,11 +160,11 @@ public void SlotsHeartedQuirk() Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); //Unfavourite the level - message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Get the favourite slots - message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").WaitResult(); + message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Make sure its now empty result = message.Content.ReadAsXML(); @@ -181,7 +181,7 @@ public void SlotsHeartedQuirkFailsWhenInvalidUser() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); //Get the favourite slots - HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/I_AM_NOT_A_REAL_USER").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/favouriteSlots/I_AM_NOT_A_REAL_USER").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -197,7 +197,7 @@ public void SlotsMostHearted() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostHearted").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -207,7 +207,7 @@ public void SlotsMostHearted() context.Database.FavouriteLevel(level, user2); context.Database.FavouriteLevel(level2, user2); - message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + message = client.GetAsync($"/lbp/slots/mostHearted").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -218,7 +218,7 @@ public void SlotsMostHearted() context.Database.FavouriteLevel(level2, user); context.Database.FavouriteLevel(level2, user3); - message = client.GetAsync($"/lbp/slots/mostHearted").WaitResult(); + message = client.GetAsync($"/lbp/slots/mostHearted").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -248,7 +248,7 @@ public void SlotsMostLiked() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/highestRated").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -258,7 +258,7 @@ public void SlotsMostLiked() context.Database.RateLevel(level, user2, RatingType.Yay); context.Database.RateLevel(level2, user3, RatingType.Yay); - message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + message = client.GetAsync($"/lbp/slots/highestRated").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -269,7 +269,7 @@ public void SlotsMostLiked() context.Database.RateLevel(level2, user, RatingType.Yay); context.Database.RateLevel(level, user3, RatingType.Boo); - message = client.GetAsync($"/lbp/slots/highestRated").WaitResult(); + message = client.GetAsync($"/lbp/slots/highestRated").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -292,7 +292,7 @@ public void SlotsMostPlayed() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostUniquePlays").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostUniquePlays").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -304,7 +304,7 @@ public void SlotsMostPlayed() context.Database.PlayLevel(level, user3, 1); context.Database.PlayLevel(level2, user3, 1); - message = client.GetAsync($"/lbp/slots/mostUniquePlays").WaitResult(); + message = client.GetAsync($"/lbp/slots/mostUniquePlays").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -327,7 +327,7 @@ public void SlotsMostReplayed() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostPlays").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostPlays").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -339,7 +339,7 @@ public void SlotsMostReplayed() context.Database.PlayLevel(level, user3, 1); context.Database.PlayLevel(level2, user3, 1); - message = client.GetAsync($"/lbp/slots/mostPlays").WaitResult(); + message = client.GetAsync($"/lbp/slots/mostPlays").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -357,7 +357,7 @@ public void SlotsTeamPicked() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/mmpicks").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/mmpicks").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -365,7 +365,7 @@ public void SlotsTeamPicked() context.Database.AddTeamPickToLevel(level); - message = client.GetAsync($"/lbp/slots/mmpicks").WaitResult(); + message = client.GetAsync($"/lbp/slots/mmpicks").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); @@ -381,7 +381,7 @@ public void SlotsByUser() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelList result = message.Content.ReadAsXML(); @@ -389,14 +389,14 @@ public void SlotsByUser() GameLevel level = context.CreateLevel(publisher); - message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").WaitResult(); + message = client.GetAsync($"/lbp/slots/by/{publisher.Username}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(1)); Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); - message = client.GetAsync($"/lbp/slots/by/I_AM_NOT_REAL").WaitResult(); + message = client.GetAsync($"/lbp/slots/by/I_AM_NOT_REAL").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -409,13 +409,13 @@ public void GetLevelById() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/s/user/{level.LevelId}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/s/user/{level.LevelId}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); GameLevelResponse result = message.Content.ReadAsXML(); Assert.That(result.LevelId, Is.EqualTo(level.LevelId)); - message = client.GetAsync($"/lbp/s/user/{int.MaxValue}").WaitResult(); + message = client.GetAsync($"/lbp/s/user/{int.MaxValue}").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -429,7 +429,7 @@ public void GetSlotList() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={level.LevelId}&s={level2.LevelId}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={level.LevelId}&s={level2.LevelId}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedLevelList result = message.Content.ReadAsXML(); @@ -448,7 +448,7 @@ public void GetLevelsFromCategory() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/searches/newest").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/searches/newest").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMinimalLevelResultsList result = message.Content.ReadAsXML(); @@ -465,7 +465,7 @@ public void DoesntGetSlotListWhenNoQuery() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slotList").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slotList").Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -477,7 +477,7 @@ public void DoesntGetSlotListWhenInvalidQuery() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s=NOT_A_NUMBER").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s=NOT_A_NUMBER").Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -489,7 +489,7 @@ public void GetSlotListWhenInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={int.MaxValue}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slotList?s={int.MaxValue}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedLevelList result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(0)); @@ -503,13 +503,13 @@ public void GetModernCategories() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, publisher); - HttpResponseMessage message = client.GetAsync($"/lbp/genres").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/genres").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Just throw away the value, but at least make sure it parses _ = message.Content.ReadAsXML(); - message = client.GetAsync($"/lbp/searches").WaitResult(); + message = client.GetAsync($"/lbp/searches").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); //Just throw away the value, but at least make sure it parses @@ -525,7 +525,7 @@ public void SlotsInvalidRoute() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/slots/waaaaaa").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/slots/waaaaaa").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -538,7 +538,7 @@ public void SlotsHeartedApiV3FailsWhenNoUser() using HttpClient client = context.GetUnauthenticatedClient(); - HttpResponseMessage message = client.GetAsync($"/api/v3/levels/hearted").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/api/v3/levels/hearted").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs index 7554001e..75ce06d3 100644 --- a/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/ScoreLeaderboardTests.cs @@ -25,10 +25,10 @@ public void SubmitsScore() Score = 5, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); - message = client.GetAsync($"/lbp/topscores/user/{level.LevelId}/1").WaitResult(); + message = client.GetAsync($"/lbp/topscores/user/{level.LevelId}/1").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedScoreList scores = message.Content.ReadAsXML(); @@ -36,7 +36,7 @@ public void SubmitsScore() Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); - message = client.GetAsync($"/lbp/scoreboard/user/{level.LevelId}").WaitResult(); + message = client.GetAsync($"/lbp/scoreboard/user/{level.LevelId}").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMultiLeaderboardResponse scoresMulti = message.Content.ReadAsXML(); @@ -61,12 +61,12 @@ public void SubmitsDeveloperScore() Score = 5, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); - message = client.GetAsync($"/lbp/topscores/developer/1/1").WaitResult(); + message = client.GetAsync($"/lbp/topscores/developer/1/1").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedScoreList scores = message.Content.ReadAsXML(); @@ -74,7 +74,7 @@ public void SubmitsDeveloperScore() Assert.That(scores.Scores[0].Player, Is.EqualTo(user.Username)); Assert.That(scores.Scores[0].Score, Is.EqualTo(5)); - message = client.GetAsync($"/lbp/scoreboard/developer/1").WaitResult(); + message = client.GetAsync($"/lbp/scoreboard/developer/1").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); SerializedMultiLeaderboardResponse scoresMulti = message.Content.ReadAsXML(); @@ -92,7 +92,7 @@ public void DosentGetLeaderboardForInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message2 = client.GetAsync($"/lbp/topscores/user/{int.MaxValue}/1").WaitResult(); + HttpResponseMessage message2 = client.GetAsync($"/lbp/topscores/user/{int.MaxValue}/1").Result; Assert.That(message2.StatusCode, Is.EqualTo(NotFound)); } @@ -104,7 +104,7 @@ public void DosentGetMultiLeaderboardForInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/user/{int.MaxValue}").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/user/{int.MaxValue}").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -124,7 +124,7 @@ public void DoesntSubmitInvalidScore() Score = -1, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{level.LevelId}", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); context.Database.Refresh(); @@ -148,7 +148,7 @@ public void DoesntSubmitDeveloperInvalidScore() Score = -1, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/1", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); context.Database.Refresh(); @@ -172,7 +172,7 @@ public void DoesntSubmitsScoreToInvalidLevel() Score = 0, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{int.MaxValue}", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/user/{int.MaxValue}", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -191,7 +191,7 @@ public void DoesntSubmitDeveloperScoreToInvalidLevel() Score = 0, }; - HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/-1", new StringContent(score.AsXML())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/scoreboard/developer/-1", new StringContent(score.AsXML())).Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -203,7 +203,7 @@ public void DoesntGetDeveloperMultiLeaderboardForInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/developer/-1").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/scoreboard/developer/-1").Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -215,7 +215,7 @@ public void DoesntGetDeveloperScoresForInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.GetAsync($"/lbp/topscores/developer/-1/1").WaitResult(); + HttpResponseMessage message = client.GetAsync($"/lbp/topscores/developer/-1/1").Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } @@ -299,7 +299,7 @@ public void PlayLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); @@ -315,7 +315,7 @@ public void DoesntPlayInvalidLevel() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound)); } @@ -329,7 +329,7 @@ public void PlayLevelWithCount() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=2", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=2", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); context.Database.Refresh(); @@ -347,10 +347,10 @@ public void DoesntPlayLevelWithInvalidCount() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=gtgnyegth", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=gtgnyegth", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); - HttpResponseMessage message2 = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=-5", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message2 = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=-5", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message2.StatusCode, Is.EqualTo(BadRequest)); } @@ -363,7 +363,7 @@ public void DoesntPlayLevelWithCountOnMainline() using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=3", new ReadOnlyMemoryContent(Array.Empty())).WaitResult(); + HttpResponseMessage message = client.PostAsync($"/lbp/play/user/{level.LevelId}?count=3", new ReadOnlyMemoryContent(Array.Empty())).Result; Assert.That(message.StatusCode, Is.EqualTo(BadRequest)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs index cb2f694b..db06fcab 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/ApiV2MiddlewareTests.cs @@ -10,12 +10,12 @@ public void ErrorsWhenGoingToV2() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.GetAsync("/api/v2/asdf").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/api/v2/asdf").Result; Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(Gone)); - Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Does.Contain("error")); + Assert.That(response.Content.ReadAsStringAsync().Result, Does.Contain("error")); }); } @@ -25,12 +25,12 @@ public void OkWhenGoingAnywhereElse() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").Result; Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(OK)); - Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Does.Not.Contain("error")); + Assert.That(response.Content.ReadAsStringAsync().Result, Does.Not.Contain("error")); }); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs index dfb8efb1..f061a006 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/CrossOriginMiddlewareTests.cs @@ -10,7 +10,7 @@ public void PutsCorrectHeadersWhenGoingToV3() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").Result; Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(OK)); @@ -25,7 +25,7 @@ public void PutsCorrectHeadersWhenOptionsV3() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Options, "/api/v3/instance")).WaitResult(); + HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Options, "/api/v3/instance")).Result; Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(OK)); @@ -40,7 +40,7 @@ public void PutsNoHeadersWhenAvoidingV3() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/lbp/eula")).WaitResult(); + HttpResponseMessage response = context.Http.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/lbp/eula")).Result; Assert.Multiple(() => { Assert.That(response.StatusCode, Is.EqualTo(Forbidden)); diff --git a/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs index d53aa688..27d38260 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/DigestMiddlewareTests.cs @@ -11,7 +11,7 @@ public void DoesntIncludeDigestWhenOutsideOfGame() using TestContext context = this.GetServer(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/api/v3/instance").Result; Assert.Multiple(() => { @@ -27,7 +27,7 @@ public void IncludesDigestInGame() context.Server.Value.Server.AddEndpointGroup(); context.Server.Value.Server.AddMiddleware(); - HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").Result; Assert.Multiple(() => { @@ -53,7 +53,7 @@ public void DigestIsCorrect() string clientDigest = DigestMiddleware.CalculateDigest(endpoint, blankMs, "", null, null); context.Http.DefaultRequestHeaders.Add("X-Digest-A", clientDigest); - HttpResponseMessage response = context.Http.GetAsync(endpoint).WaitResult(); + HttpResponseMessage response = context.Http.GetAsync(endpoint).Result; Assert.Multiple(() => { @@ -86,7 +86,7 @@ public void PspDigestIsCorrect() context.Http.DefaultRequestHeaders.Add("X-Digest-A", clientDigest); context.Http.DefaultRequestHeaders.Add("X-data-v", "5"); context.Http.DefaultRequestHeaders.Add("X-exe-v", "205"); - HttpResponseMessage response = context.Http.GetAsync(endpoint).WaitResult(); + HttpResponseMessage response = context.Http.GetAsync(endpoint).Result; Assert.Multiple(() => { @@ -108,7 +108,7 @@ public void FailsWhenDigestIsBad() context.Server.Value.Server.AddMiddleware(); context.Http.DefaultRequestHeaders.Add("X-Digest-A", "asdf"); - HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/lbp/eula").Result; Assert.Pass(); // TODO: we have no way of detecting a failed digest check } diff --git a/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs b/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs index fbb926de..75b97581 100644 --- a/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs +++ b/RefreshTests.GameServer/Tests/Middlewares/LegacyAdapterMiddlewareTests.cs @@ -13,7 +13,7 @@ public void DoesntAdaptForOtherUrls() context.Server.Value.Server.AddMiddleware(); context.Server.Value.Server.AddEndpointGroup(); - ApiOkResponse? response = context.Http.GetFromJsonAsync("/api/v3/test").WaitResult(); + ApiOkResponse? response = context.Http.GetFromJsonAsync("/api/v3/test").Result; Assert.That(response, Is.Not.Null); Assert.Multiple(() => @@ -30,12 +30,12 @@ public void AdaptsForLegacyGameUrl() context.Server.Value.Server.AddMiddleware(); context.Server.Value.Server.AddEndpointGroup(); - HttpResponseMessage response = context.Http.GetAsync("/LITTLEBIGPLANETPS3_XML/test").WaitResult(); + HttpResponseMessage response = context.Http.GetAsync("/LITTLEBIGPLANETPS3_XML/test").Result; Assert.Multiple(async () => { Assert.That(response.StatusCode, Is.EqualTo(OK)); - Assert.That(response.Content.ReadAsStringAsync().WaitResult(), Is.EqualTo("test")); + Assert.That(response.Content.ReadAsStringAsync().Result, Is.EqualTo("test")); }); } } \ No newline at end of file From ebf92624b0e441fa0ad4311e370592588ff70bb7 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 18 Oct 2023 18:16:36 -0700 Subject: [PATCH 8/8] Resolve review comments --- RefreshTests.GameServer/TestContext.cs | 5 ----- RefreshTests.GameServer/Tests/Levels/LevelTests.cs | 14 +++++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/RefreshTests.GameServer/TestContext.cs b/RefreshTests.GameServer/TestContext.cs index 0e6c8d8a..320d8a17 100644 --- a/RefreshTests.GameServer/TestContext.cs +++ b/RefreshTests.GameServer/TestContext.cs @@ -71,11 +71,6 @@ public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData, return client; } - public HttpClient GetUnauthenticatedClient() - { - return this.Listener.GetClient(); - } - public GameUser CreateUser(string? username = null) { username ??= this.UserIncrement.ToString(); diff --git a/RefreshTests.GameServer/Tests/Levels/LevelTests.cs b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs index 54e44e5b..f536f184 100644 --- a/RefreshTests.GameServer/Tests/Levels/LevelTests.cs +++ b/RefreshTests.GameServer/Tests/Levels/LevelTests.cs @@ -189,13 +189,13 @@ public void SlotsHeartedQuirkFailsWhenInvalidUser() public void SlotsMostHearted() { using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); + GameUser user1 = context.CreateUser(); GameUser user2 = context.CreateUser(); GameUser user3 = context.CreateUser(); - GameLevel level = context.CreateLevel(user); - GameLevel level2 = context.CreateLevel(user); + GameLevel level = context.CreateLevel(user1); + GameLevel level2 = context.CreateLevel(user1); - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1); HttpResponseMessage message = client.GetAsync($"/lbp/slots/mostHearted").Result; Assert.That(message.StatusCode, Is.EqualTo(OK)); @@ -203,7 +203,7 @@ public void SlotsMostHearted() SerializedMinimalLevelList result = message.Content.ReadAsXML(); Assert.That(result.Items, Has.Count.EqualTo(0)); - context.Database.FavouriteLevel(level, user); + context.Database.FavouriteLevel(level, user1); context.Database.FavouriteLevel(level, user2); context.Database.FavouriteLevel(level2, user2); @@ -215,7 +215,7 @@ public void SlotsMostHearted() Assert.That(result.Items[0].LevelId, Is.EqualTo(level.LevelId)); Assert.That(result.Items[1].LevelId, Is.EqualTo(level2.LevelId)); - context.Database.FavouriteLevel(level2, user); + context.Database.FavouriteLevel(level2, user1); context.Database.FavouriteLevel(level2, user3); message = client.GetAsync($"/lbp/slots/mostHearted").Result; @@ -536,7 +536,7 @@ public void SlotsHeartedApiV3FailsWhenNoUser() GameUser user = context.CreateUser(); GameLevel level = context.CreateLevel(user); - using HttpClient client = context.GetUnauthenticatedClient(); + using HttpClient client = context.Http; HttpResponseMessage message = client.GetAsync($"/api/v3/levels/hearted").Result; Assert.That(message.StatusCode, Is.EqualTo(NotFound));