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

@ -7,6 +7,7 @@ using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Mail;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -79,13 +80,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
/// The response sent is the text that will appear in-game.
/// </summary>
[HttpPost("filter")]
public async Task<IActionResult> Filter()
public async Task<IActionResult> Filter(IMailService mailService)
{
GameTokenEntity token = this.GetToken();
string message = await this.ReadBodyAsync();
if (message.StartsWith("/setemail "))
if (message.StartsWith("/setemail ") && ServerConfiguration.Instance.Mail.MailEnabled)
{
string email = message[(message.IndexOf(" ", StringComparison.Ordinal)+1)..];
if (!SanitizationHelper.IsValidEmail(email)) return this.Ok();
@ -96,7 +97,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
if (user == null || user.EmailAddressVerified) return this.Ok();
user.EmailAddress = email;
await SMTPHelper.SendVerificationEmail(this.database, user);
await SMTPHelper.SendVerificationEmail(this.database, mailService, user);
return this.Ok();
}
@ -105,8 +106,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
string username = await this.database.UsernameFromGameToken(token);
if (ServerConfiguration.Instance.LogChatFiltering)
Logger.Info($"{username}: {message} / {filteredText}", LogArea.Filter);
if (ServerConfiguration.Instance.LogChatFiltering)
Logger.Info($"{username}: {message} / {filteredText}", LogArea.Filter);
return this.Ok(filteredText);
}