From 2c0da0bc02fbffca71e6787e7774825a8c73322e Mon Sep 17 00:00:00 2001 From: MSWS Date: Sat, 7 Sep 2024 18:42:42 -0700 Subject: [PATCH] Add deletion tests --- GangsAPI/Services/Player/IPlayerManager.cs | 8 +++++ GangsImpl/AbstractDB/AbstractDBGangManager.cs | 6 ++++ GangsImpl/Mock/MockGangManager.cs | 14 +++++++-- GangsImpl/Mock/MockPlayerManager.cs | 8 +++++ GangsTest/API/Services/Gang/CreationTests.cs | 31 ++++++++++++++++--- GangsTest/API/Services/Gang/DeletionTests.cs | 28 +++++++++++++++++ 6 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 GangsTest/API/Services/Gang/DeletionTests.cs diff --git a/GangsAPI/Services/Player/IPlayerManager.cs b/GangsAPI/Services/Player/IPlayerManager.cs index a853a1a..7236443 100644 --- a/GangsAPI/Services/Player/IPlayerManager.cs +++ b/GangsAPI/Services/Player/IPlayerManager.cs @@ -24,6 +24,14 @@ public interface IPlayerManager : IPluginBehavior { /// The new player. Task CreatePlayer(ulong steamId, string? name = null); + Task> GetAllPlayers(); + + Task> GetMembers(int gangId); + + Task> GetMembers(IGang gang) { + return GetMembers(gang.GangId); + } + Task UpdatePlayer(IGangPlayer player); /// diff --git a/GangsImpl/AbstractDB/AbstractDBGangManager.cs b/GangsImpl/AbstractDB/AbstractDBGangManager.cs index fc9626b..f7baf30 100644 --- a/GangsImpl/AbstractDB/AbstractDBGangManager.cs +++ b/GangsImpl/AbstractDB/AbstractDBGangManager.cs @@ -69,6 +69,12 @@ public async Task UpdateGang(IGang gang) { } public async Task DeleteGang(int id) { + var members = await playerMgr.GetMembers(id); + foreach (var member in members) { + member.GangId = null; + await playerMgr.UpdatePlayer(member); + } + var query = $"DELETE FROM {table} WHERE GangId = @id"; return await Connection.ExecuteAsync(query, new { id }, Transaction) > 0; } diff --git a/GangsImpl/Mock/MockGangManager.cs b/GangsImpl/Mock/MockGangManager.cs index 5aeda48..c643956 100644 --- a/GangsImpl/Mock/MockGangManager.cs +++ b/GangsImpl/Mock/MockGangManager.cs @@ -31,8 +31,14 @@ public virtual Task UpdateGang(IGang gang) { return Task.FromResult(true); } - public virtual Task DeleteGang(int id) { - return Task.FromResult(CachedGangs.RemoveWhere(g => g.GangId == id) > 0); + public virtual async Task DeleteGang(int id) { + var members = await PlayerMgr.GetMembers(id); + foreach (var member in members) { + member.GangId = null; + await PlayerMgr.UpdatePlayer(member); + } + + return CachedGangs.RemoveWhere(g => g.GangId == id) > 0; } public virtual async Task CreateGang(string name, ulong owner) { @@ -42,7 +48,9 @@ public virtual Task DeleteGang(int id) { var gang = new MockGang(id, name); var player = await PlayerMgr.GetPlayer(owner); if (player == null) return null; - if (player.GangId != null) return null; + if (player.GangId != null) + throw new InvalidOperationException( + $"Attempted to create a gang for {owner} who is already in gang {player.GangId}"); player.GangId = id; await PlayerMgr.UpdatePlayer(player); CachedGangs.Add(gang); diff --git a/GangsImpl/Mock/MockPlayerManager.cs b/GangsImpl/Mock/MockPlayerManager.cs index a812572..1eafee8 100644 --- a/GangsImpl/Mock/MockPlayerManager.cs +++ b/GangsImpl/Mock/MockPlayerManager.cs @@ -19,6 +19,14 @@ public Task CreatePlayer(ulong steamId, string? name = null) { return Task.FromResult(player); } + public Task> GetAllPlayers() { + return Task.FromResult>(players.Values); + } + + public Task> GetMembers(int gangId) { + return Task.FromResult(players.Values.Where(p => p.GangId == gangId)); + } + public Task UpdatePlayer(IGangPlayer player) { if (!players.ContainsKey(player.Steam)) return Task.FromResult(false); players[player.Steam] = player; diff --git a/GangsTest/API/Services/Gang/CreationTests.cs b/GangsTest/API/Services/Gang/CreationTests.cs index b94b062..c933ff5 100644 --- a/GangsTest/API/Services/Gang/CreationTests.cs +++ b/GangsTest/API/Services/Gang/CreationTests.cs @@ -1,13 +1,14 @@ using GangsAPI.Data.Gang; using GangsAPI.Services.Gang; using GangsAPI.Services.Player; +using MathNet.Numerics.Random; namespace GangsTest.API.Services.Gang; public class CreationTests(IPlayerManager playerMgr) { [Theory] [ClassData(typeof(TestData))] - public async Task Gang_Create(IGangManager mgr) { + public async Task Basic(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy); Assert.Equal("foobar", dummy.Name); @@ -15,7 +16,7 @@ public async Task Gang_Create(IGangManager mgr) { [Theory] [ClassData(typeof(TestData))] - public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { + public async Task From_Player(IGangManager mgr) { var player = await playerMgr.CreatePlayer(0); var dummy = await mgr.CreateGang("foobar", player); Assert.NotNull(dummy); @@ -24,7 +25,15 @@ public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { [Theory] [ClassData(typeof(TestData))] - public async Task Gang_Clone(IGangManager mgr) { + public async Task Already_In_Gang(IGangManager mgr) { + Assert.NotNull(await mgr.CreateGang("foobar", 0)); + await Assert.ThrowsAnyAsync(async () + => await mgr.CreateGang("barfoo", 0)); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Clone(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy); var clone = dummy.Clone() as IGang; @@ -37,7 +46,7 @@ public async Task Gang_Clone(IGangManager mgr) { [Theory] [ClassData(typeof(TestData))] - public async Task Gang_CreateMultiple(IGangManager mgr) { + public async Task Multiple(IGangManager mgr) { var steam1 = (ulong)new Random().NextInt64(); var steam2 = (ulong)new Random().NextInt64(); var dummy1 = await mgr.CreateGang("foobar", steam1); @@ -52,7 +61,7 @@ public async Task Gang_CreateMultiple(IGangManager mgr) { [Theory] [ClassData(typeof(TestData))] - public async Task Gang_CreateMultipleFromGangPlayer(IGangManager mgr) { + public async Task From_Player_Multiple(IGangManager mgr) { var player1 = await playerMgr.CreatePlayer((ulong)new Random().NextInt64()); var player2 = await playerMgr.CreatePlayer((ulong)new Random().NextInt64()); var dummy1 = await mgr.CreateGang("foobar", player1); @@ -64,4 +73,16 @@ public async Task Gang_CreateMultipleFromGangPlayer(IGangManager mgr) { Assert.NotSame(dummy1, dummy2); Assert.NotEqual(dummy1.GangId, dummy2.GangId); } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Injection_Test(IGangManager mgr) { + string[] strings = ["\"\"", "'' OR 1=1 --", "'; DROP TABLE users; --"]; + foreach (var str in strings) { + var dummy = await mgr.CreateGang(str, (ulong)new Random().NextInt64()); + Assert.NotNull(dummy); + Assert.Equal(str, dummy.Name); + await mgr.DeleteGang(dummy.GangId); + } + } } \ No newline at end of file diff --git a/GangsTest/API/Services/Gang/DeletionTests.cs b/GangsTest/API/Services/Gang/DeletionTests.cs new file mode 100644 index 0000000..4006c2d --- /dev/null +++ b/GangsTest/API/Services/Gang/DeletionTests.cs @@ -0,0 +1,28 @@ +using GangsAPI.Services.Gang; + +namespace GangsTest.API.Services.Gang; + +public class DeletionTests { + [Theory] + [ClassData(typeof(TestData))] + public async Task Basic(IGangManager mgr) { + var gang = await mgr.CreateGang("Test Gang", 0); + Assert.NotNull(gang); + Assert.Equal("Test Gang", gang.Name); + await mgr.DeleteGang(gang.GangId); + Assert.Null(await mgr.GetGang(gang.GangId)); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Multiple(IGangManager mgr) { + var gang1 = await mgr.CreateGang("Test Gang", 0); + var gang2 = await mgr.CreateGang("Other Gang", 1); + Assert.NotNull(gang1); + Assert.NotNull(gang2); + Assert.Equal("Test Gang", gang1.Name); + await mgr.DeleteGang(gang1.GangId); + Assert.Null(await mgr.GetGang(gang1.GangId)); + Assert.NotNull(await mgr.GetGang(gang2.GangId)); + } +} \ No newline at end of file