Skip to content

Commit

Permalink
Add deletion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 8, 2024
1 parent 1a104b9 commit 2c0da0b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
8 changes: 8 additions & 0 deletions GangsAPI/Services/Player/IPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public interface IPlayerManager : IPluginBehavior {
/// <returns>The new player.</returns>
Task<IGangPlayer> CreatePlayer(ulong steamId, string? name = null);

Task<IEnumerable<IGangPlayer>> GetAllPlayers();

Task<IEnumerable<IGangPlayer>> GetMembers(int gangId);

Task<IEnumerable<IGangPlayer>> GetMembers(IGang gang) {
return GetMembers(gang.GangId);
}

Task<bool> UpdatePlayer(IGangPlayer player);

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions GangsImpl/AbstractDB/AbstractDBGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public async Task<bool> UpdateGang(IGang gang) {
}

public async Task<bool> 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;
}
Expand Down
14 changes: 11 additions & 3 deletions GangsImpl/Mock/MockGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public virtual Task<bool> UpdateGang(IGang gang) {
return Task.FromResult(true);
}

public virtual Task<bool> DeleteGang(int id) {
return Task.FromResult(CachedGangs.RemoveWhere(g => g.GangId == id) > 0);
public virtual async Task<bool> 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<IGang?> CreateGang(string name, ulong owner) {
Expand All @@ -42,7 +48,9 @@ public virtual Task<bool> 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);
Expand Down
8 changes: 8 additions & 0 deletions GangsImpl/Mock/MockPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public Task<IGangPlayer> CreatePlayer(ulong steamId, string? name = null) {
return Task.FromResult<IGangPlayer>(player);
}

public Task<IEnumerable<IGangPlayer>> GetAllPlayers() {
return Task.FromResult<IEnumerable<IGangPlayer>>(players.Values);
}

public Task<IEnumerable<IGangPlayer>> GetMembers(int gangId) {
return Task.FromResult(players.Values.Where(p => p.GangId == gangId));
}

public Task<bool> UpdatePlayer(IGangPlayer player) {
if (!players.ContainsKey(player.Steam)) return Task.FromResult(false);
players[player.Steam] = player;
Expand Down
31 changes: 26 additions & 5 deletions GangsTest/API/Services/Gang/CreationTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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);
}

[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);
Expand All @@ -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<InvalidOperationException>(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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}
}
28 changes: 28 additions & 0 deletions GangsTest/API/Services/Gang/DeletionTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 2c0da0b

Please sign in to comment.