diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/CommentControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/CommentControllerTests.cs
index f8a95ce0..e6be7b40 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/CommentControllerTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/CommentControllerTests.cs
@@ -94,7 +94,7 @@ public class CommentControllerTests
Type = SlotType.User,
},
};
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]{slots,});
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(slots);
CommentController commentController = new(dbMock);
commentController.SetupTestController("test");
@@ -122,7 +122,7 @@ public class CommentControllerTests
Type = SlotType.Developer,
},
};
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] { slots, });
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(slots);
CommentController commentController = new(dbMock);
commentController.SetupTestController("test");
diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/MessageControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/MessageControllerTests.cs
index 130d30ec..a0b42a0a 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/MessageControllerTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/MessageControllerTests.cs
@@ -205,7 +205,6 @@ along with this program. If not, see ." + "\nuni
{
List users = new()
{
- MockHelper.GetUnitTestUser(),
new UserEntity
{
UserId = 2,
@@ -259,10 +258,7 @@ along with this program. If not, see ." + "\nuni
{
UserEntity unitTestUser = MockHelper.GetUnitTestUser();
unitTestUser.EmailAddressVerified = true;
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new List
- {
- unitTestUser,
- });
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
Mock mailMock = getMailServiceMock();
diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/SlotControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/SlotControllerTests.cs
index ca59ecbc..f55c7514 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/SlotControllerTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/SlotControllerTests.cs
@@ -1,5 +1,5 @@
-using System.Collections;
-using System.Collections.Generic;
+using System.Collections.Generic;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
@@ -57,26 +57,29 @@ public class SlotControllerTests
};
List users = new()
{
- MockHelper.GetUnitTestUser(),
new UserEntity
{
Username = "bytest",
UserId = 2,
},
+ new UserEntity
+ {
+ Username = "user3",
+ UserId = 3,
+ },
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new IList[]
- {
- slots, users,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots, users);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
IActionResult result = await slotsController.SlotsBy("bytest");
const int expectedElements = 2;
+ HashSet expectedSlotIds = new(){1, 2,};
GenericSlotResponse slotResponse = result.CastTo();
Assert.Equal(expectedElements, slotResponse.Slots.Count);
+ Assert.Equal(expectedSlotIds, slotResponse.Slots.OfType().Select(s => s.SlotId).ToHashSet());
}
[Fact]
@@ -105,17 +108,13 @@ public class SlotControllerTests
};
List users = new()
{
- MockHelper.GetUnitTestUser(),
new UserEntity
{
Username = "bytest",
UserId = 2,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new IList[]
- {
- slots, users,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots, users);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -143,13 +142,11 @@ public class SlotControllerTests
{
new SlotEntity
{
+ CreatorId = 1,
SlotId = 2,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -165,14 +162,12 @@ public class SlotControllerTests
{
new SlotEntity
{
+ CreatorId = 1,
SlotId = 2,
GameVersion = GameVersion.LittleBigPlanet2,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -195,13 +190,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanetVita,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new IList[]
- {
- slots, tokens,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots, tokens);
SlotsController slotsController = new(db);
slotsController.SetupTestController(token);
@@ -224,13 +217,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet1,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new IList[]
- {
- slots, tokens,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots, tokens);
SlotsController slotsController = new(db);
slotsController.SetupTestController(token);
@@ -259,14 +250,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 27,
- CreatorId = 4,
+ CreatorId = 1,
SubLevel = false,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -283,14 +271,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 27,
- CreatorId = 4,
+ CreatorId = 1,
Hidden = true,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -307,13 +292,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 27,
+ CreatorId = 1,
Type = SlotType.Developer,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -330,11 +313,11 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 27,
- CreatorId = 4,
+ CreatorId = 1,
SubLevel = true,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new []{slots,});
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController slotsController = new(db);
slotsController.SetupTestController();
@@ -353,14 +336,12 @@ public class SlotControllerTests
new SlotEntity
{
SlotId = 1,
+ CreatorId = 1,
InternalSlotId = 25,
Type = SlotType.Developer,
},
};
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
- {
- slots,
- });
+ DatabaseContext db = await MockHelper.GetTestDatabase(slots);
SlotsController controller = new(db);
controller.SetupTestController();
@@ -405,31 +386,32 @@ public class SlotControllerTests
roomMutex.WaitOne();
try
{
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
+ DatabaseContext db = await MockHelper.GetTestDatabase(new List
{
- new List
+ new()
{
- new()
- {
- SlotId = 1,
- Type = SlotType.User,
- },
- new()
- {
- SlotId = 2,
- Type = SlotType.User,
- },
- new()
- {
- SlotId = 3,
- Type = SlotType.User,
- },
- new()
- {
- SlotId = 4,
- Type = SlotType.Developer,
- InternalSlotId = 10,
- },
+ SlotId = 1,
+ CreatorId = 1,
+ Type = SlotType.User,
+ },
+ new()
+ {
+ SlotId = 2,
+ CreatorId = 1,
+ Type = SlotType.User,
+ },
+ new()
+ {
+ SlotId = 3,
+ CreatorId = 1,
+ Type = SlotType.User,
+ },
+ new()
+ {
+ SlotId = 4,
+ CreatorId = 1,
+ Type = SlotType.Developer,
+ InternalSlotId = 10,
},
});
SlotsController controller = new(db);
@@ -466,16 +448,14 @@ public class SlotControllerTests
roomMutex.WaitOne();
try
{
- DatabaseContext db = await MockHelper.GetTestDatabase(new[]
+ DatabaseContext db = await MockHelper.GetTestDatabase(new List
{
- new List
+ new()
{
- new()
- {
- SlotId = 4,
- Type = SlotType.Developer,
- InternalSlotId = 10,
- },
+ SlotId = 4,
+ CreatorId = 1,
+ Type = SlotType.Developer,
+ InternalSlotId = 10,
},
});
SlotsController controller = new(db);
@@ -515,4 +495,4 @@ public class SlotControllerTests
}
}
#endregion
-}
\ No newline at end of file
+}
diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/StatisticsControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/StatisticsControllerTests.cs
index c8da9fb7..26598f67 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/StatisticsControllerTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/StatisticsControllerTests.cs
@@ -40,18 +40,21 @@ public class StatisticsControllerTests
new SlotEntity
{
SlotId = 1,
+ CreatorId = 1,
},
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
},
new SlotEntity
{
SlotId = 3,
+ CreatorId = 1,
TeamPick = true,
},
};
- await using DatabaseContext db = await MockHelper.GetTestDatabase(new []{slots,});
+ await using DatabaseContext db = await MockHelper.GetTestDatabase(slots);
StatisticsController statsController = new(db);
statsController.SetupTestController();
@@ -74,21 +77,24 @@ public class StatisticsControllerTests
new SlotEntity
{
SlotId = 1,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
},
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
},
new SlotEntity
{
SlotId = 3,
+ CreatorId = 1,
TeamPick = true,
GameVersion = GameVersion.LittleBigPlanet2,
},
};
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]{slots,});
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(slots);
StatisticsController statsController = new(dbMock);
statsController.SetupTestController();
@@ -111,21 +117,24 @@ public class StatisticsControllerTests
new SlotEntity
{
SlotId = 1,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet1,
},
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet1,
},
new SlotEntity
{
SlotId = 3,
+ CreatorId = 1,
TeamPick = true,
GameVersion = GameVersion.LittleBigPlanet1,
},
};
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] {slots,});
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(slots);
StatisticsController statsController = new(dbMock);
statsController.SetupTestController();
@@ -146,21 +155,24 @@ public class StatisticsControllerTests
new SlotEntity
{
SlotId = 1,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
},
new SlotEntity
{
SlotId = 2,
+ CreatorId = 1,
GameVersion = GameVersion.LittleBigPlanet2,
},
new SlotEntity
{
SlotId = 3,
+ CreatorId = 1,
TeamPick = true,
GameVersion = GameVersion.LittleBigPlanet2,
},
};
- await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] {slots,});
+ await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(slots);
StatisticsController statsController = new(dbMock);
statsController.SetupTestController();
diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/DigestMiddlewareTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/DigestMiddlewareTests.cs
index 977a7f45..2df22d7c 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/DigestMiddlewareTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/DigestMiddlewareTests.cs
@@ -71,7 +71,8 @@ public class DigestMiddlewareTests
const int expectedCode = 403;
- Assert.Equal(expectedCode, context.Response.StatusCode);
+ Assert.True(expectedCode == context.Response.StatusCode,
+ "The digest middleware accepted the request when it shouldn't have (are you running this test in Debug mode?)");
Assert.False(context.Response.Headers.TryGetValue("X-Digest-A", out _));
Assert.False(context.Response.Headers.TryGetValue("X-Digest-B", out _));
}
diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/SetLastContactMiddlewareTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/SetLastContactMiddlewareTests.cs
index b1cd03f9..05b15078 100644
--- a/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/SetLastContactMiddlewareTests.cs
+++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Middlewares/SetLastContactMiddlewareTests.cs
@@ -87,7 +87,7 @@ public class SetLastContactMiddlewareTests
},
};
- DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]{lastContacts,});
+ DatabaseContext dbMock = await MockHelper.GetTestDatabase(lastContacts);
await middleware.InvokeAsync(context, dbMock);
@@ -132,10 +132,7 @@ public class SetLastContactMiddlewareTests
};
tokens[0].GameVersion = GameVersion.LittleBigPlanet2;
- DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]
- {
- tokens,
- });
+ DatabaseContext dbMock = await MockHelper.GetTestDatabase(tokens);
await middleware.InvokeAsync(context, dbMock);
diff --git a/ProjectLighthouse.Tests/Helpers/MockHelper.cs b/ProjectLighthouse.Tests/Helpers/MockHelper.cs
index ced0fc68..063bc552 100644
--- a/ProjectLighthouse.Tests/Helpers/MockHelper.cs
+++ b/ProjectLighthouse.Tests/Helpers/MockHelper.cs
@@ -1,9 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Data.Common;
using System.IO;
using System.Linq;
-using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
@@ -14,6 +14,7 @@ using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
+using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Xunit;
@@ -42,17 +43,22 @@ public static class MockHelper
public static T2 CastTo(this IActionResult result) where T1 : ObjectResult
{
- Assert.IsType(result);
- T1? typedResult = result as T1;
+ T1 typedResult = Assert.IsType(result);
Assert.NotNull(typedResult);
Assert.NotNull(typedResult.Value);
- Assert.IsType(typedResult.Value);
- T2? finalResult = (T2?)typedResult.Value;
+ T2 finalResult = Assert.IsType(typedResult.Value);
Assert.NotNull(finalResult);
return finalResult;
}
- public static async Task GetTestDatabase(IEnumerable sets, [CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0)
+ private static async Task> GetInMemoryDbOptions()
+ {
+ DbConnection connection = new SqliteConnection("DataSource=:memory:");
+ await connection.OpenAsync();
+ return new DbContextOptionsBuilder().UseSqlite(connection);
+ }
+
+ public static async Task GetTestDatabase(params object[] sets)
{
await RoomHelper.Rooms.RemoveAllAsync();
@@ -64,27 +70,32 @@ public static class MockHelper
setDict[type] = list;
}
- if (!setDict.TryGetValue(typeof(GameTokenEntity), out _))
+ setDict.TryAdd(typeof(GameTokenEntity), new List());
+ setDict.TryAdd(typeof(UserEntity), new List());
+
+ // add the default user token if another token with id 1 isn't specified
+ if (setDict.TryGetValue(typeof(GameTokenEntity), out IList? tokens))
{
- setDict[typeof(GameTokenEntity)] = new List
+ if (tokens.Cast().FirstOrDefault(t => t.TokenId == 1) == null)
{
- GetUnitTestToken(),
- };
+ setDict[typeof(GameTokenEntity)].Add(GetUnitTestToken());
+ }
}
- if (!setDict.TryGetValue(typeof(UserEntity), out _))
+ // add the default user if another user with id 1 isn't specified
+ if (setDict.TryGetValue(typeof(UserEntity), out IList? users))
{
- setDict[typeof(UserEntity)] = new List
+ if (users.Cast().FirstOrDefault(u => u.UserId == 1) == null)
{
- GetUnitTestUser(),
- };
+ setDict[typeof(UserEntity)].Add(GetUnitTestUser());
+ }
}
- DbContextOptions options = new DbContextOptionsBuilder()
- .UseInMemoryDatabase($"{caller}_{lineNum}")
- .Options;
+ DbContextOptions options = (await GetInMemoryDbOptions()).Options;
await using DatabaseContext context = new(options);
+ await context.Database.EnsureCreatedAsync();
+
foreach (IList list in setDict.Select(p => p.Value))
{
foreach (object item in list)
@@ -93,35 +104,8 @@ public static class MockHelper
}
}
-
await context.SaveChangesAsync();
- await context.DisposeAsync();
- return new DatabaseContext(options);
- }
- public static async Task GetTestDatabase(List? users = null, List? tokens = null,
- [CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0
- )
- {
- await RoomHelper.Rooms.RemoveAllAsync();
-
- users ??= new List
- {
- GetUnitTestUser(),
- };
-
- tokens ??= new List
- {
- GetUnitTestToken(),
- };
- DbContextOptions options = new DbContextOptionsBuilder()
- .UseInMemoryDatabase($"{caller}_{lineNum}")
- .Options;
- await using DatabaseContext context = new(options);
- context.Users.AddRange(users);
- context.GameTokens.AddRange(tokens);
- await context.SaveChangesAsync();
- await context.DisposeAsync();
return new DatabaseContext(options);
}
diff --git a/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj
index e9097a10..3d27a0f8 100644
--- a/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj
+++ b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj
@@ -30,7 +30,7 @@
all
-
+
diff --git a/ProjectLighthouse.Tests/Unit/ModerationTests.cs b/ProjectLighthouse.Tests/Unit/ModerationTests.cs
index 9aa4055b..32fc3d73 100644
--- a/ProjectLighthouse.Tests/Unit/ModerationTests.cs
+++ b/ProjectLighthouse.Tests/Unit/ModerationTests.cs
@@ -19,6 +19,7 @@ public class ModerationTests
ModerationCaseEntity @case = new()
{
CaseId = 1,
+ CreatorId = 1,
ExpiresAt = DateTime.UnixEpoch,
CreatorUsername = "unitTestUser",
};
@@ -41,6 +42,7 @@ public class ModerationTests
ModerationCaseEntity @case = new()
{
CaseId = 2,
+ CreatorId = 1,
ExpiresAt = DateTime.UtcNow.AddHours(1),
CreatorUsername = "unitTestUser",
};
@@ -63,6 +65,7 @@ public class ModerationTests
ModerationCaseEntity @case = new()
{
CaseId = 3,
+ CreatorId = 1,
ExpiresAt = DateTime.UnixEpoch,
DismissedAt = DateTime.UnixEpoch,
CreatorUsername = "unitTestUser",