mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-25 22:51:29 +00:00
* 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
316 lines
No EOL
12 KiB
C#
316 lines
No EOL
12 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LBPUnion.ProjectLighthouse.Configuration;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Mail;
|
|
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
|
|
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
|
using LBPUnion.ProjectLighthouse.Types.Mail;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public class MessageControllerTests
|
|
{
|
|
[Fact]
|
|
public void Eula_ShouldReturnLicense_WhenConfigEmpty()
|
|
{
|
|
MessageController messageController = new(null!);
|
|
messageController.SetupTestController();
|
|
|
|
ServerConfiguration.Instance.EulaText = "";
|
|
|
|
const string expected = @"
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>." + "\n";
|
|
|
|
IActionResult result = messageController.Eula();
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expected, (string)okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void Eula_ShouldReturnLicenseAndConfigString_WhenConfigNotEmpty()
|
|
{
|
|
MessageController messageController = new(null!);
|
|
messageController.SetupTestController();
|
|
|
|
ServerConfiguration.Instance.EulaText = "unit test eula text";
|
|
|
|
const string expected = @"
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>." + "\nunit test eula text";
|
|
|
|
IActionResult result = messageController.Eula();
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expected, (string)okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Announcement_WithVariables_ShouldBeResolved()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController();
|
|
|
|
ServerConfiguration.Instance.AnnounceText = "you are now logged in as %user (id: %id)";
|
|
|
|
const string expected = "you are now logged in as unittest (id: 1)\n";
|
|
|
|
IActionResult result = await messageController.Announce();
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expected, (string)okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Announcement_WithEmptyString_ShouldBeEmpty()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController();
|
|
|
|
ServerConfiguration.Instance.AnnounceText = "";
|
|
|
|
const string expected = "";
|
|
|
|
IActionResult result = await messageController.Announce();
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expected, (string)okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Notification_ShouldReturn_Empty()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController();
|
|
|
|
IActionResult result = messageController.Notification();
|
|
|
|
Assert.IsType<OkResult>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Filter_ShouldNotCensor_WhenCensorDisabled()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
|
|
const string request = "unit test message";
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
CensorConfiguration.Instance.UserInputFilterMode = FilterMode.None;
|
|
|
|
const string expectedBody = "unit test message";
|
|
|
|
IActionResult result = await messageController.Filter(new NullMailService());
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expectedBody, (string)okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Filter_ShouldCensor_WhenCensorEnabled()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
|
|
const string request = "unit test message bruh";
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
CensorConfiguration.Instance.UserInputFilterMode = FilterMode.Asterisks;
|
|
CensorConfiguration.Instance.FilteredWordList = new List<string>
|
|
{
|
|
"bruh",
|
|
};
|
|
|
|
const string expectedBody = "unit test message ****";
|
|
|
|
IActionResult result = await messageController.Filter(new NullMailService());
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.NotNull(okObjectResult.Value);
|
|
Assert.Equal(expectedBody, (string)okObjectResult.Value);
|
|
}
|
|
|
|
private static Mock<IMailService> getMailServiceMock()
|
|
{
|
|
Mock<IMailService> mailMock = new();
|
|
mailMock.Setup(x => x.SendEmailAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
|
|
.Returns(Task.FromResult(true));
|
|
return mailMock;
|
|
}
|
|
|
|
[Fact]
|
|
public async void Filter_ShouldNotSendEmail_WhenMailDisabled()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
Mock<IMailService> mailMock = getMailServiceMock();
|
|
const string request = "/setemail unittest@unittest.com";
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
ServerConfiguration.Instance.Mail.MailEnabled = false;
|
|
CensorConfiguration.Instance.FilteredWordList = new List<string>();
|
|
|
|
const int expectedStatus = 200;
|
|
const string expected = "/setemail unittest@unittest.com";
|
|
|
|
IActionResult result = await messageController.Filter(mailMock.Object);
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
OkObjectResult? okObjectResult = result as OkObjectResult;
|
|
Assert.NotNull(okObjectResult);
|
|
Assert.Equal(expectedStatus, okObjectResult.StatusCode);
|
|
Assert.Equal(expected, okObjectResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Filter_ShouldSendEmail_WhenMailEnabled_AndEmailNotTaken()
|
|
{
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
|
|
|
Mock<IMailService> mailMock = getMailServiceMock();
|
|
|
|
const string request = "/setemail unittest@unittest.com";
|
|
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
ServerConfiguration.Instance.Mail.MailEnabled = true;
|
|
|
|
const string expectedEmail = "unittest@unittest.com";
|
|
|
|
IActionResult result = await messageController.Filter(mailMock.Object);
|
|
|
|
Assert.IsType<OkResult>(result);
|
|
Assert.Equal(expectedEmail, dbMock.Users.First().EmailAddress);
|
|
mailMock.Verify(x => x.SendEmailAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Filter_ShouldNotSendEmail_WhenMailEnabled_AndEmailTaken()
|
|
{
|
|
List<UserEntity> users = new()
|
|
{
|
|
MockHelper.GetUnitTestUser(),
|
|
new UserEntity
|
|
{
|
|
UserId = 2,
|
|
EmailAddress = "unittest@unittest.com",
|
|
EmailAddressVerified = false,
|
|
},
|
|
};
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(users);
|
|
Mock<IMailService> mailMock = getMailServiceMock();
|
|
|
|
const string request = "/setemail unittest@unittest.com";
|
|
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
ServerConfiguration.Instance.Mail.MailEnabled = true;
|
|
|
|
IActionResult result = await messageController.Filter(mailMock.Object);
|
|
|
|
Assert.IsType<OkResult>(result);
|
|
mailMock.Verify(x => x.SendEmailAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Filter_ShouldNotSendEmail_WhenMailEnabled_AndEmailAlreadyVerified()
|
|
{
|
|
UserEntity unitTestUser = MockHelper.GetUnitTestUser();
|
|
unitTestUser.EmailAddressVerified = true;
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new List<UserEntity>
|
|
{
|
|
unitTestUser,
|
|
});
|
|
|
|
Mock<IMailService> mailMock = getMailServiceMock();
|
|
|
|
const string request = "/setemail unittest@unittest.com";
|
|
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
ServerConfiguration.Instance.Mail.MailEnabled = true;
|
|
|
|
IActionResult result = await messageController.Filter(mailMock.Object);
|
|
|
|
Assert.IsType<OkResult>(result);
|
|
mailMock.Verify(x => x.SendEmailAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Filter_ShouldNotSendEmail_WhenMailEnabled_AndEmailFormatInvalid()
|
|
{
|
|
UserEntity unitTestUser = MockHelper.GetUnitTestUser();
|
|
unitTestUser.EmailAddressVerified = true;
|
|
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new List<UserEntity>
|
|
{
|
|
unitTestUser,
|
|
});
|
|
|
|
Mock<IMailService> mailMock = getMailServiceMock();
|
|
|
|
const string request = "/setemail unittestinvalidemail@@@";
|
|
|
|
MessageController messageController = new(dbMock);
|
|
messageController.SetupTestController(request);
|
|
|
|
ServerConfiguration.Instance.Mail.MailEnabled = true;
|
|
|
|
IActionResult result = await messageController.Filter(mailMock.Object);
|
|
|
|
Assert.IsType<OkResult>(result);
|
|
mailMock.Verify(x => x.SendEmailAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
|
}
|
|
} |