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
parent 02f520c717
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,143 @@
using System;
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;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Integration;
[Collection(nameof(LighthouseServerTest<TStartup>))]
public class LighthouseServerTest<TStartup> where TStartup : class
{
protected readonly HttpClient Client;
private readonly TestServer server;
protected LighthouseServerTest()
{
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 TestServer GetTestServer() => this.server;
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}"))
{
userId = RandomNumberGenerator.GetInt32(int.MaxValue);
}
UserEntity user = new()
{
UserId = userId,
Username = $"{username}{userId}",
Password = CryptoHelper.BCryptHash($"unitTestPassword{userId}"),
LinkedPsnId = (ulong)userId,
};
database.Add(user);
await database.SaveChangesAsync();
return user;
}
protected async Task<HttpResponseMessage> AuthenticateResponse()
{
UserEntity user = await this.CreateRandomUser();
byte[] ticketData = new TicketBuilder()
.SetUsername($"{user.Username}{user.UserId}")
.SetUserId((ulong)user.UserId)
.Build();
HttpResponseMessage response = await this.Client.PostAsync
($"/LITTLEBIGPLANETPS3_XML/login?titleID={GameVersionHelper.LittleBigPlanet2TitleIds[0]}", new ByteArrayContent(ticketData));
return response;
}
protected async Task<LoginResult> Authenticate()
{
HttpResponseMessage response = await this.AuthenticateResponse();
string responseContent = await response.Content.ReadAsStringAsync();
XmlSerializer serializer = new(typeof(LoginResult));
return (LoginResult)serializer.Deserialize(new StringReader(responseContent))!;
}
protected Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth) => this.AuthenticatedRequest(endpoint, mmAuth, HttpMethod.Get);
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);
}
public async Task<HttpResponseMessage> UploadFileEndpointRequest(string filePath)
{
byte[] bytes = await File.ReadAllBytesAsync(filePath);
string hash = CryptoHelper.Sha1Hash(bytes).ToLower();
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}
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);
}
public async Task<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath)
=> await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));
public async Task<HttpResponseMessage> UploadDataRequest(string endpoint, byte[] data) => await this.Client.PostAsync(endpoint, new ByteArrayContent(data));
public async Task<HttpResponseMessage> AuthenticatedUploadFileRequest(string endpoint, string filePath, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath));
return await this.Client.SendAsync(requestMessage);
}
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);
}
}