mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-18 16:31:33 +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
|
@ -4,14 +4,19 @@ using LBPUnion.ProjectLighthouse.Database;
|
|||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Mail;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Email;
|
||||
|
||||
public class SendVerificationEmailPage : BaseLayout
|
||||
{
|
||||
public SendVerificationEmailPage(DatabaseContext database) : base(database)
|
||||
{}
|
||||
public readonly IMailService Mail;
|
||||
|
||||
public SendVerificationEmailPage(DatabaseContext database, IMailService mail) : base(database)
|
||||
{
|
||||
this.Mail = mail;
|
||||
}
|
||||
|
||||
public bool Success { get; set; }
|
||||
|
||||
|
@ -24,7 +29,7 @@ public class SendVerificationEmailPage : BaseLayout
|
|||
|
||||
if (user.EmailAddressVerified) return this.Redirect("/");
|
||||
|
||||
this.Success = await SMTPHelper.SendVerificationEmail(this.Database, user);
|
||||
this.Success = await SMTPHelper.SendVerificationEmail(this.Database, this.Mail, user);
|
||||
|
||||
return this.Page();
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ using LBPUnion.ProjectLighthouse.Database;
|
|||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
||||
using LBPUnion.ProjectLighthouse.Types.Mail;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -12,18 +12,20 @@ namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Login;
|
|||
|
||||
public class PasswordResetRequestForm : BaseLayout
|
||||
{
|
||||
public IMailService Mail;
|
||||
|
||||
public string? Error { get; private set; }
|
||||
|
||||
public string? Status { get; private set; }
|
||||
|
||||
public PasswordResetRequestForm(DatabaseContext database) : base(database)
|
||||
{ }
|
||||
public PasswordResetRequestForm(DatabaseContext database, IMailService mail) : base(database)
|
||||
{
|
||||
this.Mail = mail;
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public async Task<IActionResult> OnPost(string email)
|
||||
{
|
||||
|
||||
if (!ServerConfiguration.Instance.Mail.MailEnabled)
|
||||
{
|
||||
this.Error = "Email is not configured on this server, so password resets cannot be issued. Please contact your instance administrator for more details.";
|
||||
|
@ -51,22 +53,7 @@ public class PasswordResetRequestForm : BaseLayout
|
|||
return this.Page();
|
||||
}
|
||||
|
||||
PasswordResetTokenEntity token = new()
|
||||
{
|
||||
Created = DateTime.Now,
|
||||
UserId = user.UserId,
|
||||
ResetToken = CryptoHelper.GenerateAuthToken(),
|
||||
};
|
||||
|
||||
string messageBody = $"Hello, {user.Username}.\n\n" +
|
||||
"A request to reset your account's password was issued. If this wasn't you, this can probably be ignored.\n\n" +
|
||||
$"If this was you, your {ServerConfiguration.Instance.Customization.ServerName} password can be reset at the following link:\n" +
|
||||
$"{ServerConfiguration.Instance.ExternalUrl}/passwordReset?token={token.ResetToken}";
|
||||
|
||||
SMTPHelper.SendEmail(user.EmailAddress, $"Project Lighthouse Password Reset Request for {user.Username}", messageBody);
|
||||
|
||||
this.Database.PasswordResetTokens.Add(token);
|
||||
await this.Database.SaveChangesAsync();
|
||||
await SMTPHelper.SendPasswordResetEmail(this.Database, this.Mail, user);
|
||||
|
||||
this.Status = $"A password reset request has been sent to the email {email}. " +
|
||||
"If you do not receive an email verify that you have entered the correct email address";
|
||||
|
|
|
@ -8,6 +8,7 @@ using LBPUnion.ProjectLighthouse.Servers.Website.Captcha;
|
|||
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
||||
using LBPUnion.ProjectLighthouse.Types.Mail;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -15,10 +16,12 @@ namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Login;
|
|||
|
||||
public class RegisterForm : BaseLayout
|
||||
{
|
||||
public readonly IMailService Mail;
|
||||
private readonly ICaptchaService captchaService;
|
||||
|
||||
public RegisterForm(DatabaseContext database, ICaptchaService captchaService) : base(database)
|
||||
public RegisterForm(DatabaseContext database, IMailService mail, ICaptchaService captchaService) : base(database)
|
||||
{
|
||||
this.Mail = mail;
|
||||
this.captchaService = captchaService;
|
||||
}
|
||||
|
||||
|
@ -80,6 +83,8 @@ public class RegisterForm : BaseLayout
|
|||
|
||||
UserEntity user = await this.Database.CreateUser(username, CryptoHelper.BCryptHash(password), emailAddress);
|
||||
|
||||
if(ServerConfiguration.Instance.Mail.MailEnabled) SMTPHelper.SendRegistrationEmail(this.Mail, user);
|
||||
|
||||
WebTokenEntity webToken = new()
|
||||
{
|
||||
UserId = user.UserId,
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
int yourThumb = commentAndReaction.Value?.Rating ?? 0;
|
||||
DateTimeOffset timestamp = DateTimeOffset.FromUnixTimeSeconds(comment.Timestamp / 1000).ToLocalTime();
|
||||
StringWriter messageWriter = new();
|
||||
HttpUtility.HtmlDecode(comment.getComment(), messageWriter);
|
||||
HttpUtility.HtmlDecode(comment.GetCommentMessage(), messageWriter);
|
||||
|
||||
string decodedMessage = messageWriter.ToString();
|
||||
string? url = Url.RouteUrl(ViewContext.RouteData.Values);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
string language = (string?)ViewData["Language"] ?? LocalizationManager.DefaultLang;
|
||||
string timeZone = (string?)ViewData["TimeZone"] ?? TimeZoneInfo.Local.Id;
|
||||
bool includeStatus = (bool?)ViewData["IncludeStatus"] ?? false;
|
||||
await using DatabaseContext database = new();
|
||||
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
|
||||
string userStatus = includeStatus ? Model.GetStatus(database).ToTranslatedString(language, timeZone) : "";
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
@model LBPUnion.ProjectLighthouse.Types.Entities.Moderation.ModerationCaseEntity
|
||||
|
||||
@{
|
||||
DatabaseContext database = new();
|
||||
DatabaseContext database = DatabaseContext.CreateNewInstance();
|
||||
string color = "blue";
|
||||
|
||||
string timeZone = (string?)ViewData["TimeZone"] ?? TimeZoneInfo.Local.Id;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
@{
|
||||
UserEntity? user = (UserEntity?)ViewData["User"];
|
||||
|
||||
await using DatabaseContext database = new();
|
||||
await using DatabaseContext database = DatabaseContext.CreateNewInstance();
|
||||
|
||||
string slotName = HttpUtility.HtmlDecode(string.IsNullOrEmpty(Model!.Name) ? "Unnamed Level" : Model.Name);
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
</h1>
|
||||
}
|
||||
@{
|
||||
await using DatabaseContext context = new();
|
||||
await using DatabaseContext context = DatabaseContext.CreateNewInstance();
|
||||
|
||||
int hearts = Model.GetHeartCount(context);
|
||||
int comments = Model.GetCommentCount(context);
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
string[] authorLabels;
|
||||
if (Model.Slot?.GameVersion == GameVersion.LittleBigPlanet1)
|
||||
{
|
||||
authorLabels = Model.Slot.LevelTags(new DatabaseContext());
|
||||
authorLabels = Model.Slot.LevelTags(DatabaseContext.CreateNewInstance());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue