From 408be1684f8fac8fc95f2dc66013b3e262f9454c Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Tue, 7 Nov 2023 23:19:09 -0800 Subject: [PATCH 1/6] ResourceEndpoints: UploadAsset 100% test coverage --- .../GameServer/GameServerTest.cs | 5 +- .../GameServer/TestRefreshGameServer.cs | 2 +- .../GameServer/WriteFailingDataStore.cs | 15 +++ .../Tests/Assets/AssetUploadTests.cs | 111 +++++++++++++++++- 4 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs diff --git a/RefreshTests.GameServer/GameServer/GameServerTest.cs b/RefreshTests.GameServer/GameServer/GameServerTest.cs index 53aab93e..d4d98fed 100644 --- a/RefreshTests.GameServer/GameServer/GameServerTest.cs +++ b/RefreshTests.GameServer/GameServer/GameServerTest.cs @@ -1,3 +1,4 @@ +using Bunkum.Core.Storage; using Bunkum.Protocols.Http.Direct; using NotEnoughLogs; using RefreshTests.GameServer.Logging; @@ -15,7 +16,7 @@ public class GameServerTest }); // ReSharper disable once MemberCanBeMadeStatic.Global - protected TestContext GetServer(bool startServer = true) + protected TestContext GetServer(bool startServer = true, IDataStore? dataStore = null) { DirectHttpListener listener = new(Logger); HttpClient client = listener.GetClient(); @@ -25,7 +26,7 @@ protected TestContext GetServer(bool startServer = true) Lazy gameServer = new(() => { - TestRefreshGameServer gameServer = new(listener, () => provider); + TestRefreshGameServer gameServer = new(listener, () => provider, dataStore); gameServer.Start(); return gameServer; diff --git a/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs b/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs index 9cc57d4e..bd7c36d5 100644 --- a/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs +++ b/RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs @@ -20,7 +20,7 @@ namespace RefreshTests.GameServer.GameServer; public class TestRefreshGameServer : RefreshGameServer { - public TestRefreshGameServer(BunkumHttpListener listener, Func provider) : base(listener, provider, null, new InMemoryDataStore()) + public TestRefreshGameServer(BunkumHttpListener listener, Func provider, IDataStore? dataStore = null) : base(listener, provider, null, dataStore ?? new InMemoryDataStore()) {} public BunkumHttpServer Server => this._server; diff --git a/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs b/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs new file mode 100644 index 00000000..b0fe6150 --- /dev/null +++ b/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs @@ -0,0 +1,15 @@ +using Bunkum.Core.Storage; + +namespace RefreshTests.GameServer.GameServer; + +public class WriteFailingDataStore : IDataStore +{ + public bool ExistsInStore(string key) => false; + public bool WriteToStore(string key, byte[] data) => false; + public byte[] GetDataFromStore(string key) => throw new NotSupportedException(); + public bool RemoveFromStore(string key) => throw new NotSupportedException(); + public string[] GetKeysFromStore() => Array.Empty(); + public bool WriteToStoreFromStream(string key, Stream data) => false; + public Stream GetStreamFromStore(string key) => throw new NotSupportedException(); + public Stream OpenWriteStream(string key) => throw new NotSupportedException(); +} \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index c3aa80d7..ac107aa2 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -22,9 +22,116 @@ public void CanUploadAsset() .ToLower(); HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; - Assert.That(response.IsSuccessStatusCode, Is.True); Assert.That(response.StatusCode, Is.EqualTo(OK)); } + + [Test] + public void CantUploadAssetWithInvalidHash() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + ReadOnlySpan data = "TEX a"u8; + + HttpResponseMessage response = client.PostAsync("/lbp/upload/6e4d252f247e3aa99ef846df8c65493393e79f4f", new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); + } + + [Test] + public void CantUploadBlockedAsset() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + ReadOnlySpan data = "FSHbthsa"u8; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(Unauthorized)); + } + + [Test] + public void DataStoreWriteFailReturnsInternalServerError() + { + using TestContext context = this.GetServer(true, new WriteFailingDataStore()); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + ReadOnlySpan data = "TEX a"u8; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(InternalServerError)); + } + + [Test] + public void CantUploadDuplicateAssets() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + ReadOnlySpan data = "TEX a"u8; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(Conflict)); + } + + [Test] + public void CantUploadTooLarge() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //create 5mb array + ReadOnlySpan data = new byte[5000000]; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(RequestEntityTooLarge)); + } + + [Test] + public void InvalidAssetHashUploadFails() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + ReadOnlySpan data = "TEX a"u8; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/I_AM_NOT_REAL", new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); + } [Test] public void CanRetrieveAsset() @@ -41,7 +148,7 @@ public void CanRetrieveAsset() client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Wait(); HttpResponseMessage response = client.GetAsync("/lbp/r/" + hash).Result; - Assert.That(response.IsSuccessStatusCode, Is.True); + Assert.That(response.StatusCode, Is.EqualTo(OK)); byte[] returnedData = response.Content.ReadAsByteArrayAsync().Result; Assert.That(data.SequenceEqual(returnedData), Is.True); From a7304a67fbff95b84089a3234123dc182702eff2 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Tue, 7 Nov 2023 23:35:07 -0800 Subject: [PATCH 2/6] ResourceEndpoints: REAL 100% test coverage for UploadAsset (dotcover was being tricked by a ternary operation :^) --- .../Endpoints/Game/ResourceEndpoints.cs | 6 ++++-- .../Tests/Assets/AssetUploadTests.cs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs b/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs index 9ef930d8..86873cc3 100644 --- a/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs @@ -32,8 +32,10 @@ public Response UploadAsset(RequestContext context, string hash, string type, by if (!CommonPatterns.Sha1Regex().IsMatch(hash)) return BadRequest; bool isPSP = context.IsPSP(); - string assetPath = isPSP ? $"psp/{hash}" : hash; - + string assetPath = hash; + if (isPSP) + assetPath = $"psp/{hash}"; + if (dataStore.ExistsInStore(assetPath)) return Conflict; diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index ac107aa2..a2262dd7 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -25,6 +25,25 @@ public void CanUploadAsset() Assert.That(response.StatusCode, Is.EqualTo(OK)); } + [Test] + public void CanUploadAssetPsp() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); + + ReadOnlySpan data = "TEX a"u8; + + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + } + [Test] public void CantUploadAssetWithInvalidHash() { From 86e81866b3b8bf2a6559f4a762b1490a2b34f957 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Tue, 7 Nov 2023 23:43:32 -0800 Subject: [PATCH 3/6] ResourceEndpoints: GetResource 100% test coverage This also includes a ReadFailingDataStore, where writes always succeed, but reads always fail --- .../GameServer/ReadFailingDataStore.cs | 18 ++++++ .../GameServer/WriteFailingDataStore.cs | 3 + .../Tests/Assets/AssetUploadTests.cs | 60 ++++++++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 RefreshTests.GameServer/GameServer/ReadFailingDataStore.cs diff --git a/RefreshTests.GameServer/GameServer/ReadFailingDataStore.cs b/RefreshTests.GameServer/GameServer/ReadFailingDataStore.cs new file mode 100644 index 00000000..caad2f20 --- /dev/null +++ b/RefreshTests.GameServer/GameServer/ReadFailingDataStore.cs @@ -0,0 +1,18 @@ +using Bunkum.Core.Storage; + +namespace RefreshTests.GameServer.GameServer; + +/// +/// A data store that claims keys are always available, but returns failure when getting the data back +/// +public class ReadFailingDataStore : IDataStore +{ + public bool ExistsInStore(string key) => true; + public bool WriteToStore(string key, byte[] data) => true; + public byte[] GetDataFromStore(string key) => throw new NotSupportedException(); + public bool RemoveFromStore(string key) => throw new NotSupportedException(); + public string[] GetKeysFromStore() => throw new NotSupportedException(); + public bool WriteToStoreFromStream(string key, Stream data) => true; + public Stream GetStreamFromStore(string key) => throw new NotSupportedException(); + public Stream OpenWriteStream(string key) => throw new NotSupportedException(); +} \ No newline at end of file diff --git a/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs b/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs index b0fe6150..65194006 100644 --- a/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs +++ b/RefreshTests.GameServer/GameServer/WriteFailingDataStore.cs @@ -2,6 +2,9 @@ namespace RefreshTests.GameServer.GameServer; +/// +/// A data store that you are unable to write to, it always returns a failure operation +/// public class WriteFailingDataStore : IDataStore { public bool ExistsInStore(string key) => false; diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index a2262dd7..54e40ec4 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -7,6 +7,8 @@ namespace RefreshTests.GameServer.Tests.Assets; public class AssetUploadTests : GameServerTest { + private const string MissingHash = "6e4d252f247e3aa99ef846df8c65493393e79f4f"; + [Test] public void CanUploadAsset() { @@ -54,7 +56,7 @@ public void CantUploadAssetWithInvalidHash() ReadOnlySpan data = "TEX a"u8; - HttpResponseMessage response = client.PostAsync("/lbp/upload/6e4d252f247e3aa99ef846df8c65493393e79f4f", new ByteArrayContent(data.ToArray())).Result; + HttpResponseMessage response = client.PostAsync($"/lbp/upload/{MissingHash}", new ByteArrayContent(data.ToArray())).Result; Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); } @@ -165,14 +167,66 @@ public void CanRetrieveAsset() .Replace("-", "") .ToLower(); - client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Wait(); - HttpResponseMessage response = client.GetAsync("/lbp/r/" + hash).Result; + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + response = client.GetAsync("/lbp/r/" + hash).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + byte[] returnedData = response.Content.ReadAsByteArrayAsync().Result; + + Assert.That(data.SequenceEqual(returnedData), Is.True); + } + + [Test] + public void CanRetrieveAssetPsp() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); + + ReadOnlySpan data = "TEX a"u8; + string hash = BitConverter.ToString(SHA1.HashData(data)) + .Replace("-", "") + .ToLower(); + + HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + response = client.GetAsync("/lbp/r/" + hash).Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); + byte[] returnedData = response.Content.ReadAsByteArrayAsync().Result; Assert.That(data.SequenceEqual(returnedData), Is.True); } + + [Test] + public void CantRetrieveMissingAsset() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + HttpResponseMessage response = client.GetAsync($"/lbp/r/{MissingHash}").Result; + Assert.That(response.StatusCode, Is.EqualTo(NotFound)); + } + + [Test] + public void DataStoreReadFailReturnsInternalServerError() + { + using TestContext context = this.GetServer(true, new ReadFailingDataStore()); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + HttpResponseMessage response = client.GetAsync($"/lbp/r/{MissingHash}").Result; + Assert.That(response.StatusCode, Is.EqualTo(InternalServerError)); + } + [Test] public void InvalidHashFails() { From 71d47edc4a00e4dec4ff5b422d268bbff7a1e050 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 8 Nov 2023 00:04:17 -0800 Subject: [PATCH 4/6] ResourceEndpoints: GetAssetsMissingFromStore 100% test coverage --- .../Endpoints/Game/ResourceEndpoints.cs | 15 +--- .../Tests/Assets/AssetUploadTests.cs | 79 +++++++++++-------- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs b/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs index 86873cc3..7cc37c4b 100644 --- a/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs @@ -99,20 +99,9 @@ public Response GetResource(RequestContext context, string hash, IDataStore data [NullStatusCode(BadRequest)] public SerializedResourceList? GetAssetsMissingFromStore(RequestContext context, SerializedResourceList body, IDataStore dataStore) { - if (context.IsPSP()) - { - //Iterate over all the items - for (int i = 0; i < body.Items.Count; i++) - { - string item = body.Items[i]; - //Point them into the `psp` folder - body.Items[i] = $"psp/{item}"; - } - } - if(body.Items.Any(hash => !CommonPatterns.Sha1Regex().IsMatch(hash))) return null; - - return new SerializedResourceList(body.Items.Where(r => !dataStore.ExistsInStore(r))); + + return new SerializedResourceList(body.Items.Where(r => !dataStore.ExistsInStore(context.IsPSP() ? $"psp/{r}" : r))); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index 54e40ec4..d4a10b42 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -1,7 +1,9 @@ using System.Security.Cryptography; using Refresh.GameServer.Authentication; using Refresh.GameServer.Services; +using Refresh.GameServer.Types.Lists; using Refresh.GameServer.Types.UserData; +using RefreshTests.GameServer.Extensions; namespace RefreshTests.GameServer.Tests.Assets; @@ -9,32 +11,16 @@ public class AssetUploadTests : GameServerTest { private const string MissingHash = "6e4d252f247e3aa99ef846df8c65493393e79f4f"; - [Test] - public void CanUploadAsset() - { - using TestContext context = this.GetServer(); - context.Server.Value.Server.AddService(); - GameUser user = context.CreateUser(); - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - - ReadOnlySpan data = "TEX a"u8; - - string hash = BitConverter.ToString(SHA1.HashData(data)) - .Replace("-", "") - .ToLower(); - - HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; - Assert.That(response.StatusCode, Is.EqualTo(OK)); - } - - [Test] - public void CanUploadAssetPsp() + [TestCase(false)] + [TestCase(true)] + public void CanUploadAsset(bool psp) { using TestContext context = this.GetServer(); context.Server.Value.Server.AddService(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); ReadOnlySpan data = "TEX a"u8; @@ -153,14 +139,17 @@ public void InvalidAssetHashUploadFails() HttpResponseMessage response = client.PostAsync("/lbp/upload/I_AM_NOT_REAL", new ByteArrayContent(data.ToArray())).Result; Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); } - - [Test] - public void CanRetrieveAsset() + + [TestCase(false)] + [TestCase(true)] + public void CanRetrieveAsset(bool psp) { using TestContext context = this.GetServer(); context.Server.Value.Server.AddService(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if (psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); ReadOnlySpan data = "TEX a"u8; string hash = BitConverter.ToString(SHA1.HashData(data)) @@ -178,29 +167,53 @@ public void CanRetrieveAsset() Assert.That(data.SequenceEqual(returnedData), Is.True); } - [Test] - public void CanRetrieveAssetPsp() + [TestCase(false)] + [TestCase(true)] + public void CanCheckForMissingAssets(bool psp) { using TestContext context = this.GetServer(); context.Server.Value.Server.AddService(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); ReadOnlySpan data = "TEX a"u8; string hash = BitConverter.ToString(SHA1.HashData(data)) .Replace("-", "") .ToLower(); - - HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; - Assert.That(response.StatusCode, Is.EqualTo(OK)); - response = client.GetAsync("/lbp/r/" + hash).Result; + //Check the list initially, should have 1 item + HttpResponseMessage response = client.PostAsync("/lbp/filterResources", new StringContent(new SerializedResourceList(new[] { hash }).AsXML())).Result; Assert.That(response.StatusCode, Is.EqualTo(OK)); + + SerializedResourceList missingList = response.Content.ReadAsXML(); + Assert.That(missingList.Items, Has.Count.EqualTo(1)); + Assert.That(missingList.Items[0], Is.EqualTo(hash)); - byte[] returnedData = response.Content.ReadAsByteArrayAsync().Result; + //Upload an asset + response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); - Assert.That(data.SequenceEqual(returnedData), Is.True); + //Check the list after uploading, should now only have 0 items returned + response = client.PostAsync("/lbp/filterResources", new StringContent(new SerializedResourceList(new[] { hash }).AsXML())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + missingList = response.Content.ReadAsXML(); + Assert.That(missingList.Items, Has.Count.EqualTo(0)); + } + + [Test] + public void CanCheckForMissingAssets() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Check the list initially, should have 1 item + HttpResponseMessage response = client.PostAsync("/lbp/filterResources", new StringContent(new SerializedResourceList(new[] { "I_AM_NOT_HASH" }).AsXML())).Result; + Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); } [Test] From eae6ebdf64abf548b5e5659a282ad4d4ab65f0c3 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 8 Nov 2023 00:05:31 -0800 Subject: [PATCH 5/6] AssetUploadTests: Remove unneeded test --- .../Tests/Assets/AssetUploadTests.cs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index d4a10b42..6da1bca4 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -169,7 +169,7 @@ public void CanRetrieveAsset(bool psp) [TestCase(false)] [TestCase(true)] - public void CanCheckForMissingAssets(bool psp) + public void CheckForMissingAssets(bool psp) { using TestContext context = this.GetServer(); context.Server.Value.Server.AddService(); @@ -203,19 +203,6 @@ public void CanCheckForMissingAssets(bool psp) Assert.That(missingList.Items, Has.Count.EqualTo(0)); } - [Test] - public void CanCheckForMissingAssets() - { - using TestContext context = this.GetServer(); - context.Server.Value.Server.AddService(); - GameUser user = context.CreateUser(); - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - - //Check the list initially, should have 1 item - HttpResponseMessage response = client.PostAsync("/lbp/filterResources", new StringContent(new SerializedResourceList(new[] { "I_AM_NOT_HASH" }).AsXML())).Result; - Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); - } - [Test] public void CantRetrieveMissingAsset() { From 93d77175d834cde6e5f0c9a228f808b352e70c02 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Wed, 8 Nov 2023 00:14:13 -0800 Subject: [PATCH 6/6] Tests: Refactor all PSP tests + re-add accidentally removed test --- .../Tests/Assets/AssetUploadTests.cs | 13 ++ .../Tests/Relations/FavouriteSlotTests.cs | 113 ++++-------------- .../Tests/Relations/FavouriteUserTests.cs | 111 ++++------------- 3 files changed, 61 insertions(+), 176 deletions(-) diff --git a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs index 6da1bca4..c13437fd 100644 --- a/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs +++ b/RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs @@ -241,4 +241,17 @@ public void InvalidHashFails() response = client.GetAsync("/lbp/r/..%2Frpc.json").Result; Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); } + + [Test] + public void CantCheckForInvalidMissingAssets() + { + using TestContext context = this.GetServer(); + context.Server.Value.Server.AddService(); + GameUser user = context.CreateUser(); + using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + + //Check the list initially, should have 1 item + HttpResponseMessage response = client.PostAsync("/lbp/filterResources", new StringContent(new SerializedResourceList(new[] { "I_AM_NOT_HASH" }).AsXML())).Result; + Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); + } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Relations/FavouriteSlotTests.cs b/RefreshTests.GameServer/Tests/Relations/FavouriteSlotTests.cs index b46c3121..29939e9e 100644 --- a/RefreshTests.GameServer/Tests/Relations/FavouriteSlotTests.cs +++ b/RefreshTests.GameServer/Tests/Relations/FavouriteSlotTests.cs @@ -41,38 +41,20 @@ public void FavouriteAndUnfavouriteLevel() Assert.That(result.Items, Has.Count.EqualTo(0)); } - [Test] - public void CantFavouriteMissingLevel() + [TestCase(false)] + [TestCase(true)] + public void CantFavouriteMissingLevel(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if (psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Favourite an invalid level HttpResponseMessage message = client.PostAsync($"/lbp/favourite/slot/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(NotFound)); - - //Get the favourite slots - message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - //Make sure its now empty - SerializedMinimalFavouriteLevelList result = message.Content.ReadAsXML(); - Assert.That(result.Items, Has.Count.EqualTo(0)); - } - - [Test] - public void CantFavouriteMissingLevelPsp() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - - //Favourite an invalid level - HttpResponseMessage message = client.PostAsync($"/lbp/favourite/slot/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : NotFound)); //Get the favourite slots message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; @@ -82,41 +64,33 @@ public void CantFavouriteMissingLevelPsp() Assert.That(result.Items, Has.Count.EqualTo(0)); } - [Test] - public void CantUnfavouriteMissingLevel() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - - //Unfavourite an invalid level - HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/slot/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(NotFound)); - } - - [Test] - public void CantUnfavouriteMissingLevelPsp() + [TestCase(false)] + [TestCase(true)] + public void CantUnfavouriteMissingLevel(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Unfavourite an invalid level HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/slot/user/{int.MaxValue}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : NotFound)); } - [Test] - public void CantFavouriteLevelTwice() + [TestCase(false)] + [TestCase(true)] + public void CantFavouriteLevelTwice(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); GameLevel level = context.CreateLevel(user); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if (psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Favourite a level HttpResponseMessage message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; @@ -124,34 +98,7 @@ public void CantFavouriteLevelTwice() //Favourite another level message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(Unauthorized)); - - //Get the favourite slots - message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - //Make sure it has the level - SerializedMinimalFavouriteLevelList 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 CantFavouriteLevelTwicePsp() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - GameLevel level = context.CreateLevel(user); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - - //Favourite a level - HttpResponseMessage message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - - //Favourite the level again - message = client.PostAsync($"/lbp/favourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : Unauthorized)); //Get the favourite slots message = client.GetAsync($"/lbp/favouriteSlots/{user.Username}").Result; @@ -162,32 +109,20 @@ public void CantFavouriteLevelTwicePsp() Assert.That(result.Items.First().LevelId, Is.EqualTo(level.LevelId)); } - [Test] - public void CantUnfavouriteLevelTwice() + [TestCase(false)] + [TestCase(true)] + public void CantUnfavouriteLevelTwice(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); GameLevel level = context.CreateLevel(user); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Unfavourite a level, which we haven't favourited HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(Unauthorized)); - } - - [Test] - public void CantUnfavouriteLevelTwicePsp() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - GameLevel level = context.CreateLevel(user); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - - //Unfavourite a level, which we haven't favourited - HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/slot/user/{level.LevelId}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : Unauthorized)); } } \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Relations/FavouriteUserTests.cs b/RefreshTests.GameServer/Tests/Relations/FavouriteUserTests.cs index efce02dd..dcb459e0 100644 --- a/RefreshTests.GameServer/Tests/Relations/FavouriteUserTests.cs +++ b/RefreshTests.GameServer/Tests/Relations/FavouriteUserTests.cs @@ -41,17 +41,20 @@ public void FavouriteAndUnfavouriteUser() Assert.That(result.Items, Has.Count.EqualTo(0)); } - [Test] - public void CantFavouriteMissingUser() + [TestCase(false)] + [TestCase(true)] + public void CantFavouriteMissingUser(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Favourite an invalid user HttpResponseMessage message = client.PostAsync($"/lbp/favourite/user/pain peko", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : NotFound)); //Get the favourite users message = client.GetAsync($"/lbp/favouriteUsers/{user.Username}").Result; @@ -61,89 +64,34 @@ public void CantFavouriteMissingUser() Assert.That(result.Items, Has.Count.EqualTo(0)); } - [Test] - public void CantFavouriteMissingUserPsp() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - - //Favourite an invalid user - HttpResponseMessage message = client.PostAsync($"/lbp/favourite/user/painer peko", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - - //Get the favourite users - message = client.GetAsync($"/lbp/favouriteUsers/{user.Username}").Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - //Make sure its still empty - SerializedFavouriteUserList result = message.Content.ReadAsXML(); - Assert.That(result.Items, Has.Count.EqualTo(0)); - } - - [Test] - public void CantUnfavouriteMissingUser() + [TestCase(false)] + [TestCase(true)] + public void CantUnfavouriteMissingUser(bool psp) { using TestContext context = this.GetServer(); GameUser user = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Unfavourite an invalid user HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/user/womp womp", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(NotFound)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : NotFound)); } - [Test] - public void CantUnfavouriteMissingUserPsp() - { - using TestContext context = this.GetServer(); - GameUser user = context.CreateUser(); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - - //Unfavourite an invalid user - HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/user/gymbag", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - } - - [Test] - public void CantFavouriteUserTwice() + [TestCase(false)] + [TestCase(true)] + public void CantFavouriteUserTwice(bool psp) { using TestContext context = this.GetServer(); GameUser user1 = context.CreateUser(); GameUser user2 = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1); + if (psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - //Favourite a user - HttpResponseMessage message = client.PostAsync($"/lbp/favourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - - //Favourite the same user again - message = client.PostAsync($"/lbp/favourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(Unauthorized)); - - //Get the favourite users - message = client.GetAsync($"/lbp/favouriteUsers/{user1.Username}").Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); - //Make sure it has the user - SerializedFavouriteUserList result = message.Content.ReadAsXML(); - Assert.That(result.Items, Has.Count.EqualTo(1)); - Assert.That(result.Items.First().Handle.Username, Is.EqualTo(user2.Username)); - } - - [Test] - public void CantFavouriteUserTwicePsp() - { - using TestContext context = this.GetServer(); - GameUser user1 = context.CreateUser(); - GameUser user2 = context.CreateUser(); - - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); //Favourite a user HttpResponseMessage message = client.PostAsync($"/lbp/favourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; @@ -151,7 +99,7 @@ public void CantFavouriteUserTwicePsp() //Favourite the same user again message = client.PostAsync($"/lbp/favourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : Unauthorized)); //Get the favourite users message = client.GetAsync($"/lbp/favouriteUsers/{user1.Username}").Result; @@ -162,33 +110,22 @@ public void CantFavouriteUserTwicePsp() Assert.That(result.Items.First().Handle.Username, Is.EqualTo(user2.Username)); } - [Test] - public void CantUnfavouriteUserTwice() + [TestCase(false)] + [TestCase(true)] + public void CantUnfavouriteUserTwice(bool psp) { using TestContext context = this.GetServer(); GameUser user1 = context.CreateUser(); GameUser user2 = context.CreateUser(); using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1); + if(psp) + client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - //Unfavourite a user, which we haven't favourited - HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(Unauthorized)); - } - - [Test] - public void CantUnfavouriteUserTwicePsp() - { - using TestContext context = this.GetServer(); - GameUser user1 = context.CreateUser(); - GameUser user2 = context.CreateUser(); - using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1); - client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT"); - //Unfavourite a user, which we haven't favourited HttpResponseMessage message = client.PostAsync($"/lbp/unfavourite/user/{user2.Username}", new ReadOnlyMemoryContent(Array.Empty())).Result; - Assert.That(message.StatusCode, Is.EqualTo(OK)); + Assert.That(message.StatusCode, Is.EqualTo(psp ? OK : Unauthorized)); } [Test]