mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-21 23:01:26 +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
144
ProjectLighthouse.Tests/Helpers/MockHelper.cs
Normal file
144
ProjectLighthouse.Tests/Helpers/MockHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue