mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-09 13:28:39 +00:00
Add more unit tests (#757)
* Reorganize tests into unit/integration pattern * Make DbSets virtual so they can be overridden by tests * Add MessageControllerTests * Implement DigestMiddlewareTests * Refactor SMTPHelper to follow DI pattern which allows for mocking in unit tests. * Fix MailQueueService service registration and shutdown * Implement tests for Status and StatisticsController and reorganize tests * Start working on UserControllerTests * Start refactoring tests to use In-Memory EF provider * Refactor integration tests to reset the database every time Change default unit testing database credentials * Update credentials to use default root with different passwords * Throw exception when integration db is not available instead of falling back to in-memory * Evaluate DbConnected every time * Remove default DbContext constructor * Setup DbContexts with options builder * Convert remaining Moq DbContexts to InMemory ones * Add more tests and use Assert.IsType for testing status code * Add collection attribute to LighthouseServerTest * Remove unused directives and calculate digest in tests * Fix digest calculation in tests * Add test database call * Clear rooms after each test * Fix CommentControllerTests.cs * Disable test parallelization for gameserver tests * Fix failing tests Fix SlotTests Make CreateUser actually add user to database Fix dbConnected Lazy and change expected status codes Properly Remove fragment from url for digest calculation Fix digest calculation for regular requests [skip ci] Remove unused directive Don't use Database CreateUser function Get rid of userId argument for generating random user Rewrite logic for generating random users Fix integration tests * Implement changes from self-code review * Fix registration tests * Replace MailQueueService usages with IMailService
This commit is contained in:
parent
02f520c717
commit
1bf4ed6218
71 changed files with 2419 additions and 378 deletions
|
@ -0,0 +1,84 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class AuthenticationTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
[Fact]
|
||||
public async Task ShouldReturnErrorOnNoPostData()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", null!);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Login_ShouldReturnWithValidData()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticateResponse();
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
string responseContent = await response.Content.ReadAsStringAsync();
|
||||
Assert.Contains("MM_AUTH=", responseContent);
|
||||
Assert.Contains(VersionHelper.EnvVer, responseContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Login_CanSerializeBack()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
Assert.NotNull(loginResult);
|
||||
Assert.NotNull(loginResult.AuthTicket);
|
||||
Assert.NotNull(loginResult.ServerBrand);
|
||||
|
||||
Assert.Contains("MM_AUTH=", loginResult.AuthTicket);
|
||||
Assert.Equal(VersionHelper.EnvVer, loginResult.ServerBrand);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Login_CanUseToken()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedRequest("/LITTLEBIGPLANETPS3_XML/enterLevel/420", loginResult.AuthTicket);
|
||||
await response.Content.ReadAsStringAsync();
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.NotFound;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Login_ShouldReturnForbiddenWhenNotAuthenticated()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
HttpResponseMessage response = await this.Client.GetAsync("/LITTLEBIGPLANETPS3_XML/announce");
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.Forbidden;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class DatabaseTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
[Fact]
|
||||
public async Task CanCreateUserTwice()
|
||||
{
|
||||
await using DatabaseContext database = await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
int rand = new Random().Next();
|
||||
|
||||
UserEntity userA = await database.CreateUser("unitTestUser" + rand, CryptoHelper.GenerateAuthToken());
|
||||
UserEntity userB = await database.CreateUser("unitTestUser" + rand, CryptoHelper.GenerateAuthToken());
|
||||
|
||||
Assert.NotNull(userA);
|
||||
Assert.NotNull(userB);
|
||||
}
|
||||
}
|
122
ProjectLighthouse.Tests.GameApiTests/Integration/LoginTests.cs
Normal file
122
ProjectLighthouse.Tests.GameApiTests/Integration/LoginTests.cs
Normal file
|
@ -0,0 +1,122 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Tickets;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class LoginTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
[Fact]
|
||||
public async Task ShouldLoginWithGoodTicket()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity user = await this.CreateRandomUser();
|
||||
byte[] ticketData = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.Build();
|
||||
HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new ByteArrayContent(ticketData));
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotLoginWithExpiredTicket()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity user = await this.CreateRandomUser();
|
||||
byte[] ticketData = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.setExpirationTime((ulong)TimeHelper.TimestampMillis - 1000 * 60)
|
||||
.Build();
|
||||
HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new ByteArrayContent(ticketData));
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotLoginWithBadTitleId()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity user = await this.CreateRandomUser();
|
||||
byte[] ticketData = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.SetTitleId("UP9000-BLUS30079_00")
|
||||
.Build();
|
||||
HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new ByteArrayContent(ticketData));
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotLoginWithBadSignature()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity user = await this.CreateRandomUser();
|
||||
byte[] ticketData = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.Build();
|
||||
// Create second ticket and replace the first tickets signature with the first.
|
||||
byte[] ticketData2 = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.Build();
|
||||
|
||||
Array.Copy(ticketData2, ticketData2.Length - 0x38, ticketData, ticketData.Length - 0x38, 0x38);
|
||||
|
||||
HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new ByteArrayContent(ticketData));
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotLoginIfBanned()
|
||||
{
|
||||
DatabaseContext database = await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity user = await this.CreateRandomUser();
|
||||
|
||||
user.PermissionLevel = PermissionLevel.Banned;
|
||||
|
||||
database.Users.Update(user);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
byte[] ticketData = new TicketBuilder()
|
||||
.SetUsername(user.Username)
|
||||
.SetUserId((ulong)user.UserId)
|
||||
.Build();
|
||||
HttpResponseMessage response =
|
||||
await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new ByteArrayContent(ticketData));
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.Forbidden;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class MatchTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
[Fact]
|
||||
public async Task Match_ShouldRejectEmptyData()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest("/LITTLEBIGPLANETPS3_XML/match", Array.Empty<byte>(), loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest;
|
||||
|
||||
Assert.Equal(expectedStatusCode, result.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Match_ShouldReturnOk_WithGoodRequest()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest
|
||||
("/LITTLEBIGPLANETPS3_XML/match", "[UpdateMyPlayerData,[\"Player\":\"1984\"]]"u8.ToArray(), loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, result.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Match_ShouldIncrementPlayerCount()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
|
||||
|
||||
int oldPlayerCount = await StatisticsHelper.RecentMatches(database);
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest
|
||||
("/LITTLEBIGPLANETPS3_XML/match", "[UpdateMyPlayerData,[\"Player\":\"1984\"]]"u8.ToArray(), loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, result.StatusCode);
|
||||
|
||||
int playerCount = await StatisticsHelper.RecentMatches(database);
|
||||
|
||||
Assert.Equal(oldPlayerCount + 1, playerCount);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class SlotTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
[Fact]
|
||||
public async Task ShouldOnlyShowUsersLevels()
|
||||
{
|
||||
await using DatabaseContext database = await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
UserEntity userA = await this.CreateRandomUser();
|
||||
UserEntity userB = await this.CreateRandomUser();
|
||||
|
||||
SlotEntity slotA = new()
|
||||
{
|
||||
CreatorId = userA.UserId,
|
||||
Name = "slotA",
|
||||
ResourceCollection = "",
|
||||
};
|
||||
|
||||
SlotEntity slotB = new()
|
||||
{
|
||||
CreatorId = userB.UserId,
|
||||
Name = "slotB",
|
||||
ResourceCollection = "",
|
||||
};
|
||||
|
||||
database.Slots.Add(slotA);
|
||||
database.Slots.Add(slotB);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage respMessageA = await this.AuthenticatedRequest
|
||||
($"/LITTLEBIGPLANETPS3_XML/slots/by?u={userA.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket);
|
||||
HttpResponseMessage respMessageB = await this.AuthenticatedRequest
|
||||
($"/LITTLEBIGPLANETPS3_XML/slots/by?u={userB.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, respMessageA.StatusCode);
|
||||
Assert.Equal(expectedStatusCode, respMessageB.StatusCode);
|
||||
|
||||
string respA = await respMessageA.Content.ReadAsStringAsync();
|
||||
string respB = await respMessageB.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.NotNull(respA);
|
||||
Assert.NotNull(respB);
|
||||
|
||||
Assert.NotEqual(respA, respB);
|
||||
Assert.DoesNotContain(respA, "slotB");
|
||||
Assert.DoesNotContain(respB, "slotA");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Startup;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Integration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Integration;
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public class UploadTests : LighthouseServerTest<GameServerTestStartup>
|
||||
{
|
||||
public UploadTests()
|
||||
{
|
||||
string assetsDirectory = Path.Combine(Environment.CurrentDirectory, "r");
|
||||
if (Directory.Exists(assetsDirectory)) Directory.Delete(assetsDirectory, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptScript()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedUploadFileEndpointRequest("ExampleFiles/TestScript.ff", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.Conflict;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptFarc()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedUploadFileEndpointRequest("ExampleFiles/TestFarc.farc", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.Conflict;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptGarbage()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedUploadFileEndpointRequest("ExampleFiles/TestGarbage.bin", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.Conflict;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldAcceptTexture()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedUploadFileEndpointRequest("ExampleFiles/TestTexture.tex", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldAcceptLevel()
|
||||
{
|
||||
await IntegrationHelper.GetIntegrationDatabase();
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedUploadFileEndpointRequest("ExampleFiles/TestLevel.lvl", loginResult.AuthTicket);
|
||||
|
||||
const HttpStatusCode expectedStatusCode = HttpStatusCode.OK;
|
||||
|
||||
Assert.Equal(expectedStatusCode, response.StatusCode);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue