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:
Josh 2023-05-15 15:00:33 -05:00 committed by GitHub
commit 1bf4ed6218
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 2419 additions and 378 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Helpers;
namespace LBPUnion.ProjectLighthouse.Tests.Helpers;
public static class IntegrationHelper
{
private static readonly Lazy<bool> dbConnected = new(IsDbConnected);
private static bool IsDbConnected() => ServerStatics.DbConnected;
/// <summary>
/// Resets the database to a clean state and returns a new DatabaseContext.
/// </summary>
/// <returns>A new fresh instance of DatabaseContext</returns>
public static async Task<DatabaseContext> GetIntegrationDatabase()
{
if (!dbConnected.Value)
{
throw new Exception("Database is not connected.\n" +
"Please ensure that the database is running and that the connection string is correct.\n" +
$"Connection string: {ServerConfiguration.Instance.DbConnectionString}");
}
await ClearRooms();
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
await database.Database.EnsureDeletedAsync();
await database.Database.EnsureCreatedAsync();
return DatabaseContext.CreateNewInstance();
}
private static async Task ClearRooms()
{
await RoomHelper.Rooms.RemoveAllAsync();
}
}

View file

@ -0,0 +1,144 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Tests.Helpers;
public static class MockHelper
{
public static UserEntity GetUnitTestUser() =>
new()
{
Username = "unittest",
UserId = 1,
};
public static GameTokenEntity GetUnitTestToken() =>
new()
{
Platform = Platform.UnitTest,
UserId = 1,
ExpiresAt = DateTime.MaxValue,
TokenId = 1,
UserLocation = "127.0.0.1",
UserToken = "unittest",
};
public static async Task<DatabaseContext> GetTestDatabase(IEnumerable<IList> sets, [CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0)
{
Dictionary<Type, IList> setDict = new();
foreach (IList list in sets)
{
Type? type = list.GetType().GetGenericArguments().ElementAtOrDefault(0);
if (type == null) continue;
setDict[type] = list;
}
if (!setDict.TryGetValue(typeof(GameTokenEntity), out _))
{
setDict[typeof(GameTokenEntity)] = new List<GameTokenEntity>
{
GetUnitTestToken(),
};
}
if (!setDict.TryGetValue(typeof(UserEntity), out _))
{
setDict[typeof(UserEntity)] = new List<UserEntity>
{
GetUnitTestUser(),
};
}
DbContextOptions<DatabaseContext> options = new DbContextOptionsBuilder<DatabaseContext>()
.UseInMemoryDatabase($"{caller}_{lineNum}")
.Options;
await using DatabaseContext context = new(options);
foreach (IList list in setDict.Select(p => p.Value))
{
foreach (object item in list)
{
context.Add(item);
}
}
await context.SaveChangesAsync();
await context.DisposeAsync();
return new DatabaseContext(options);
}
public static async Task<DatabaseContext> GetTestDatabase(List<UserEntity>? users = null, List<GameTokenEntity>? tokens = null,
[CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0
)
{
users ??= new List<UserEntity>
{
GetUnitTestUser(),
};
tokens ??= new List<GameTokenEntity>
{
GetUnitTestToken(),
};
DbContextOptions<DatabaseContext> options = new DbContextOptionsBuilder<DatabaseContext>()
.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);
}
public static void SetupTestController(this ControllerBase controllerBase, string? body = null)
{
controllerBase.ControllerContext = GetMockControllerContext(body);
SetupTestGameToken(controllerBase, GetUnitTestToken());
}
public static ControllerContext GetMockControllerContext() =>
new()
{
HttpContext = new DefaultHttpContext(),
};
private static ControllerContext GetMockControllerContext(string? body) =>
new()
{
HttpContext = new DefaultHttpContext
{
Request =
{
ContentLength = body?.Length ?? 0,
Body = new MemoryStream(Encoding.ASCII.GetBytes(body ?? string.Empty)),
},
},
ActionDescriptor = new ControllerActionDescriptor
{
ActionName = "",
},
};
private static void SetupTestGameToken(ControllerBase controller, GameTokenEntity token)
{
controller.HttpContext.Items["Token"] = token;
}
}

View file

@ -3,7 +3,7 @@ using LBPUnion.ProjectLighthouse.Database;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests;
namespace LBPUnion.ProjectLighthouse.Tests.Integration;
public sealed class DatabaseFactAttribute : FactAttribute
{
@ -16,7 +16,7 @@ public sealed class DatabaseFactAttribute : FactAttribute
else
lock (migrateLock)
{
using DatabaseContext database = new();
using DatabaseContext database = DatabaseContext.CreateNewInstance();
database.Database.Migrate();
}
}

View file

@ -1,9 +1,10 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Tickets;
@ -12,48 +13,58 @@ using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests;
namespace LBPUnion.ProjectLighthouse.Tests.Integration;
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[Collection(nameof(LighthouseServerTest<TStartup>))]
public class LighthouseServerTest<TStartup> where TStartup : class
{
public readonly HttpClient Client;
public readonly TestServer Server;
protected readonly HttpClient Client;
private readonly TestServer server;
public LighthouseServerTest()
protected LighthouseServerTest()
{
this.Server = new TestServer(new WebHostBuilder().UseStartup<TStartup>());
this.Client = this.Server.CreateClient();
ServerConfiguration.Instance.DbConnectionString = "server=127.0.0.1;uid=root;pwd=lighthouse_tests;database=lighthouse_tests";
ServerConfiguration.Instance.DigestKey.PrimaryDigestKey = "lighthouse";
this.server = new TestServer(new WebHostBuilder().UseStartup<TStartup>());
this.Client = this.server.CreateClient();
}
public async Task<string> CreateRandomUser(int number = -1, bool createUser = true)
{
if (number == -1) number = new Random().Next();
const string username = "unitTestUser";
public TestServer GetTestServer() => this.server;
if (createUser)
protected async Task<UserEntity> CreateRandomUser()
{
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
int userId = RandomNumberGenerator.GetInt32(int.MaxValue);
const string username = "unitTestUser";
// if user already exists, find another random number
while (await database.Users.AnyAsync(u => u.Username == $"{username}{userId}"))
{
await using DatabaseContext database = new();
if (await database.Users.FirstOrDefaultAsync(u => u.Username == $"{username}{number}") == null)
{
UserEntity user = await database.CreateUser($"{username}{number}",
CryptoHelper.BCryptHash($"unitTestPassword{number}"));
user.LinkedPsnId = (ulong)number;
await database.SaveChangesAsync();
}
userId = RandomNumberGenerator.GetInt32(int.MaxValue);
}
return $"{username}{number}";
UserEntity user = new()
{
UserId = userId,
Username = $"{username}{userId}",
Password = CryptoHelper.BCryptHash($"unitTestPassword{userId}"),
LinkedPsnId = (ulong)userId,
};
database.Add(user);
await database.SaveChangesAsync();
return user;
}
public async Task<HttpResponseMessage> AuthenticateResponse(int number = -1, bool createUser = true)
protected async Task<HttpResponseMessage> AuthenticateResponse()
{
string username = await this.CreateRandomUser(number, createUser);
UserEntity user = await this.CreateRandomUser();
byte[] ticketData = new TicketBuilder()
.SetUsername($"{username}{number}")
.SetUserId((ulong)number)
.SetUsername($"{user.Username}{user.UserId}")
.SetUserId((ulong)user.UserId)
.Build();
HttpResponseMessage response = await this.Client.PostAsync
@ -61,9 +72,9 @@ public class LighthouseServerTest<TStartup> where TStartup : class
return response;
}
public async Task<LoginResult> Authenticate(int number = -1)
protected async Task<LoginResult> Authenticate()
{
HttpResponseMessage response = await this.AuthenticateResponse(number);
HttpResponseMessage response = await this.AuthenticateResponse();
string responseContent = await response.Content.ReadAsStringAsync();
@ -71,12 +82,18 @@ public class LighthouseServerTest<TStartup> where TStartup : class
return (LoginResult)serializer.Deserialize(new StringReader(responseContent))!;
}
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth) => this.AuthenticatedRequest(endpoint, mmAuth, HttpMethod.Get);
protected Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth) => this.AuthenticatedRequest(endpoint, mmAuth, HttpMethod.Get);
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method)
private static string GetDigestCookie(string mmAuth) => mmAuth["MM_AUTH=".Length..];
private Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method)
{
using HttpRequestMessage requestMessage = new(method, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
string path = endpoint.Split("?", StringSplitOptions.RemoveEmptyEntries)[0];
string digest = CryptoHelper.ComputeDigest(path, GetDigestCookie(mmAuth), Array.Empty<byte>(), "lighthouse");
requestMessage.Headers.Add("X-Digest-A", digest);
return this.Client.SendAsync(requestMessage);
}
@ -89,13 +106,15 @@ public class LighthouseServerTest<TStartup> where TStartup : class
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}
public async Task<HttpResponseMessage> AuthenticatedUploadFileEndpointRequest(string filePath, string mmAuth)
protected async Task<HttpResponseMessage> AuthenticatedUploadFileEndpointRequest(string filePath, string mmAuth)
{
byte[] bytes = await File.ReadAllBytesAsync(filePath);
string hash = CryptoHelper.Sha1Hash(bytes).ToLower();
using HttpRequestMessage requestMessage = new(HttpMethod.Post, $"/LITTLEBIGPLANETPS3_XML/upload/{hash}");
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new ByteArrayContent(bytes);
string digest = CryptoHelper.ComputeDigest($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", GetDigestCookie(mmAuth), bytes, "lighthouse", true);
requestMessage.Headers.Add("X-Digest-B", digest);
return await this.Client.SendAsync(requestMessage);
}
@ -112,11 +131,13 @@ public class LighthouseServerTest<TStartup> where TStartup : class
return await this.Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth)
protected async Task<HttpResponseMessage> AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new ByteArrayContent(data);
string digest = CryptoHelper.ComputeDigest(endpoint, GetDigestCookie(mmAuth), data, "lighthouse");
requestMessage.Headers.Add("X-Digest-A", digest);
return await this.Client.SendAsync(requestMessage);
}
}

View file

@ -8,8 +8,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Serialization;
namespace LBPUnion.ProjectLighthouse.Tests.Integration.Serialization;
[Trait("Category", "Integration")]
public class SerializationDependencyTests
{
private static IServiceProvider GetTestServiceProvider(params object[] dependencies)

View file

@ -6,13 +6,14 @@ using LBPUnion.ProjectLighthouse.Types.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Serialization;
namespace LBPUnion.ProjectLighthouse.Tests.Integration.Serialization;
public class TestSerializable : ILbpSerializable, IHasCustomRoot
{
public virtual string GetRoot() => "xmlRoot";
}
[Trait("Category", "Integration")]
public class SerializationTests
{
private static IServiceProvider GetEmptyServiceProvider() => new DefaultServiceProviderFactory().CreateServiceProvider(new ServiceCollection());

View file

@ -29,6 +29,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.4" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,97 @@
using System.Collections.Concurrent;
using System.Reflection;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class EmailCooldownTests
{
private static ConcurrentDictionary<int, long>? GetInternalDict =>
typeof(SMTPHelper).GetField("recentlySentMail", BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null) as ConcurrentDictionary<int, long>;
/*
* TODO This way of testing sucks because it relies on internal implementation,
* but half of this codebase is static singletons so my hand has kinda been forced
*/
[Fact]
public void CanSendMail_WhenExpirationReached()
{
MethodInfo? canSendMethod = typeof(SMTPHelper).GetMethod("CanSendMail", BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(canSendMethod);
UserEntity userEntity = new()
{
UserId = 1,
};
Assert.NotNull(GetInternalDict);
GetInternalDict.Clear();
GetInternalDict.TryAdd(1, 0);
bool? canSend = (bool?)canSendMethod.Invoke(null, new object?[] { userEntity, });
const bool expectedValue = true;
Assert.NotNull(canSend);
Assert.Equal(expectedValue, canSend);
}
[Fact]
public void CanSendMail_WhenExpirationNotReached()
{
MethodInfo? canSendMethod =
typeof(SMTPHelper).GetMethod("CanSendMail", BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(canSendMethod);
UserEntity userEntity = new()
{
UserId = 1,
};
Assert.NotNull(GetInternalDict);
GetInternalDict.Clear();
GetInternalDict.TryAdd(1, long.MaxValue);
bool? canSend = (bool?)canSendMethod.Invoke(null, new object?[] { userEntity, });
const bool expectedValue = false;
Assert.NotNull(canSend);
Assert.Equal(expectedValue, canSend);
}
[Fact]
public void CanSendMail_ExpiredEntriesAreRemoved()
{
MethodInfo? canSendMethod = typeof(SMTPHelper).GetMethod("CanSendMail", BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(canSendMethod);
UserEntity userEntity = new()
{
UserId = 1,
};
Assert.NotNull(GetInternalDict);
GetInternalDict.Clear();
GetInternalDict.TryAdd(2, 0);
GetInternalDict.TryAdd(3, TimeHelper.TimestampMillis - 100);
GetInternalDict.TryAdd(4, long.MaxValue);
canSendMethod.Invoke(null, new object?[] { userEntity, });
Assert.False(GetInternalDict.TryGetValue(2, out _));
Assert.False(GetInternalDict.TryGetValue(3, out _));
Assert.True(GetInternalDict.TryGetValue(4, out _));
}
}

View file

@ -3,8 +3,9 @@ using System.IO;
using LBPUnion.ProjectLighthouse.Types.Resources;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class FileTypeTests
{
[Fact]

View file

@ -6,8 +6,9 @@ using LBPUnion.ProjectLighthouse.Types.Misc;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class LocationTests
{
[Fact]

View file

@ -4,8 +4,9 @@ using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Types.Resources;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class ResourceTests
{
[Fact]