mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-01 09:48:37 +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
|
@ -0,0 +1,231 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class CommentControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldPostProfileComment_WhenBodyIsValid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
const string expectedCommentMessage = "test";
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", null, 0);
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
CommentEntity? comment = dbMock.Comments.FirstOrDefault();
|
||||
Assert.NotNull(comment);
|
||||
Assert.Equal(expectedCommentMessage, comment.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldCensorComment_WhenFilterEnabled()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>zamn</message></comment>");
|
||||
|
||||
CensorConfiguration.Instance.FilteredWordList = new List<string>
|
||||
{
|
||||
"zamn",
|
||||
};
|
||||
CensorConfiguration.Instance.UserInputFilterMode = FilterMode.Asterisks;
|
||||
const string expectedCommentMessage = "****";
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", null, 0);
|
||||
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
CommentEntity? comment = dbMock.Comments.FirstOrDefault();
|
||||
Assert.NotNull(comment);
|
||||
Assert.Equal(expectedCommentMessage, comment.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldCensorComment_WhenFilterDisabled()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>zamn</message></comment>");
|
||||
|
||||
CensorConfiguration.Instance.FilteredWordList = new List<string>
|
||||
{
|
||||
"zamn",
|
||||
};
|
||||
CensorConfiguration.Instance.UserInputFilterMode = FilterMode.None;
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", null, 0);
|
||||
|
||||
const string expectedCommentMessage = "zamn";
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
CommentEntity? comment = dbMock.Comments.FirstOrDefault();
|
||||
Assert.NotNull(comment);
|
||||
Assert.Equal(expectedCommentMessage, comment.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldPostUserLevelComment_WhenBodyIsValid()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
CreatorId = 1,
|
||||
Type = SlotType.User,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]{slots,});
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
const string expectedCommentMessage = "test";
|
||||
|
||||
IActionResult result = await commentController.PostComment(null, "user", 1);
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
CommentEntity? comment = dbMock.Comments.FirstOrDefault();
|
||||
Assert.NotNull(comment);
|
||||
Assert.Equal(expectedCommentMessage, comment.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldPostDeveloperLevelComment_WhenBodyIsValid()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
InternalSlotId = 12345,
|
||||
CreatorId = 1,
|
||||
Type = SlotType.Developer,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] { slots, });
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
const string expectedCommentMessage = "test";
|
||||
|
||||
IActionResult result = await commentController.PostComment(null, "developer", 12345);
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
CommentEntity? comment = dbMock.Comments.FirstOrDefault();
|
||||
Assert.NotNull(comment);
|
||||
Assert.Equal(expectedCommentMessage, comment.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldNotPostProfileComment_WhenTargetProfileInvalid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest2", null, 0);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldNotPostUserLevelComment_WhenLevelInvalid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment(null, "user", 1);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldNotPostComment_WhenBodyIsEmpty()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("");
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", null, 0);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldNotPostComment_WhenBodyIsInvalid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", null, 0);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldFail_WhenSlotTypeIsInvalid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment(null, "banana", 0);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldFail_WhenAllArgumentsAreEmpty()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment(null, null, 0);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostComment_ShouldFail_WhenSlotTypeAndUsernameAreProvided()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
CommentController commentController = new(dbMock);
|
||||
commentController.SetupTestController("<comment><message>test</message></comment>");
|
||||
|
||||
IActionResult result = await commentController.PostComment("unittest", "user", 10);
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,316 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,192 @@
|
|||
using System.Collections.Generic;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
|
||||
using LBPUnion.ProjectLighthouse.Types.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types.Users;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class StatisticsControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async void PlanetStats_ShouldReturnCorrectCounts_WhenEmpty()
|
||||
{
|
||||
await using DatabaseContext db = await MockHelper.GetTestDatabase();
|
||||
|
||||
StatisticsController statsController = new(db);
|
||||
statsController.SetupTestController();
|
||||
|
||||
const int expectedSlots = 0;
|
||||
const int expectedTeamPicks = 0;
|
||||
|
||||
IActionResult result = await statsController.PlanetStats();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? objectResult = result as OkObjectResult;
|
||||
Assert.NotNull(objectResult);
|
||||
PlanetStatsResponse? response = objectResult.Value as PlanetStatsResponse;
|
||||
Assert.NotNull(response);
|
||||
Assert.Equal(expectedSlots, response.TotalSlotCount);
|
||||
Assert.Equal(expectedTeamPicks, response.TeamPickCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void PlanetStats_ShouldReturnCorrectCounts_WhenNotEmpty()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 2,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 3,
|
||||
TeamPick = true,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext db = await MockHelper.GetTestDatabase(new []{slots,});
|
||||
|
||||
StatisticsController statsController = new(db);
|
||||
statsController.SetupTestController();
|
||||
|
||||
const int expectedSlots = 3;
|
||||
const int expectedTeamPicks = 1;
|
||||
|
||||
IActionResult result = await statsController.PlanetStats();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? objectResult = result as OkObjectResult;
|
||||
Assert.NotNull(objectResult);
|
||||
PlanetStatsResponse? response = objectResult.Value as PlanetStatsResponse;
|
||||
Assert.NotNull(response);
|
||||
Assert.Equal(expectedSlots, response.TotalSlotCount);
|
||||
Assert.Equal(expectedTeamPicks, response.TeamPickCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void PlanetStats_ShouldReturnCorrectCounts_WhenSlotsAreIncompatibleGameVersion()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 2,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 3,
|
||||
TeamPick = true,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[]{slots,});
|
||||
|
||||
StatisticsController statsController = new(dbMock);
|
||||
statsController.SetupTestController();
|
||||
|
||||
const int expectedSlots = 0;
|
||||
const int expectedTeamPicks = 0;
|
||||
|
||||
IActionResult result = await statsController.PlanetStats();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? objectResult = result as OkObjectResult;
|
||||
Assert.NotNull(objectResult);
|
||||
PlanetStatsResponse? response = objectResult.Value as PlanetStatsResponse;
|
||||
Assert.NotNull(response);
|
||||
Assert.Equal(expectedSlots, response.TotalSlotCount);
|
||||
Assert.Equal(expectedTeamPicks, response.TeamPickCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void TotalLevelCount_ShouldReturnCorrectCount_WhenSlotsAreCompatible()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
GameVersion = GameVersion.LittleBigPlanet1,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 2,
|
||||
GameVersion = GameVersion.LittleBigPlanet1,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 3,
|
||||
TeamPick = true,
|
||||
GameVersion = GameVersion.LittleBigPlanet1,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] {slots,});
|
||||
|
||||
StatisticsController statsController = new(dbMock);
|
||||
statsController.SetupTestController();
|
||||
|
||||
const string expectedTotal = "3";
|
||||
|
||||
IActionResult result = await statsController.TotalLevelCount();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? objectResult = result as OkObjectResult;
|
||||
Assert.NotNull(objectResult);
|
||||
Assert.Equal(expectedTotal, objectResult.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void TotalLevelCount_ShouldReturnCorrectCount_WhenSlotsAreNotCompatible()
|
||||
{
|
||||
List<SlotEntity> slots = new()
|
||||
{
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 1,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 2,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
new SlotEntity
|
||||
{
|
||||
SlotId = 3,
|
||||
TeamPick = true,
|
||||
GameVersion = GameVersion.LittleBigPlanet2,
|
||||
},
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(new[] {slots,});
|
||||
|
||||
StatisticsController statsController = new(dbMock);
|
||||
statsController.SetupTestController();
|
||||
|
||||
const int expectedStatusCode = 200;
|
||||
const string expectedTotal = "0";
|
||||
|
||||
IActionResult result = await statsController.TotalLevelCount();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? objectResult = result as OkObjectResult;
|
||||
Assert.NotNull(objectResult);
|
||||
Assert.Equal(expectedStatusCode, objectResult.StatusCode);
|
||||
Assert.Equal(expectedTotal, objectResult.Value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class StatusControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public void Status_ShouldReturnOk()
|
||||
{
|
||||
StatusController statusController = new()
|
||||
{
|
||||
ControllerContext = MockHelper.GetMockControllerContext(),
|
||||
};
|
||||
|
||||
IActionResult result = statusController.GetStatus();
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,202 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using LBPUnion.ProjectLighthouse.Types.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class UserControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async void GetUser_WithValidUser_ShouldReturnUser()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
const int expectedId = 1;
|
||||
|
||||
IActionResult result = await userController.GetUser("unittest");
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
GameUser? gameUser = okObject.Value as GameUser;
|
||||
Assert.NotNull(gameUser);
|
||||
Assert.Equal(expectedId, gameUser.UserId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetUser_WithInvalidUser_ShouldReturnNotFound()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
IActionResult result = await userController.GetUser("notfound");
|
||||
|
||||
Assert.IsType<NotFoundResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetUserAlt_WithInvalidUser_ShouldReturnEmptyList()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
IActionResult result = await userController.GetUserAlt(new[]{"notfound",});
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
MinimalUserListResponse? userList = okObject.Value as MinimalUserListResponse? ?? default;
|
||||
Assert.NotNull(userList);
|
||||
Assert.Empty(userList.Value.Users);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetUserAlt_WithOnlyInvalidUsers_ShouldReturnEmptyList()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
IActionResult result = await userController.GetUserAlt(new[]
|
||||
{
|
||||
"notfound", "notfound2", "notfound3",
|
||||
});
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
MinimalUserListResponse? userList = okObject.Value as MinimalUserListResponse? ?? default;
|
||||
Assert.NotNull(userList);
|
||||
Assert.Empty(userList.Value.Users);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetUserAlt_WithTwoInvalidUsers_AndOneValidUser_ShouldReturnOne()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
|
||||
IActionResult result = await userController.GetUserAlt(new[]
|
||||
{
|
||||
"notfound", "unittest", "notfound3",
|
||||
});
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
MinimalUserListResponse? userList = okObject.Value as MinimalUserListResponse? ?? default;
|
||||
Assert.NotNull(userList);
|
||||
Assert.Single(userList.Value.Users);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void GetUserAlt_WithTwoValidUsers_ShouldReturnTwo()
|
||||
{
|
||||
List<UserEntity> users = new()
|
||||
{
|
||||
MockHelper.GetUnitTestUser(),
|
||||
new UserEntity
|
||||
{
|
||||
UserId = 2,
|
||||
Username = "unittest2",
|
||||
},
|
||||
};
|
||||
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(users);
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController();
|
||||
|
||||
const int expectedLength = 2;
|
||||
|
||||
IActionResult result = await userController.GetUserAlt(new[]
|
||||
{
|
||||
"unittest2", "unittest",
|
||||
});
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
MinimalUserListResponse? userList = okObject.Value as MinimalUserListResponse? ?? default;
|
||||
Assert.NotNull(userList);
|
||||
Assert.Equal(expectedLength, userList.Value.Users.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void UpdateMyPins_ShouldReturnBadRequest_WhenBodyIsInvalid()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController("{}");
|
||||
|
||||
|
||||
IActionResult result = await userController.UpdateMyPins();
|
||||
|
||||
Assert.IsType<BadRequestResult>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void UpdateMyPins_ShouldUpdatePins()
|
||||
{
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase();
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController("{\"profile_pins\": [1234]}");
|
||||
|
||||
const string expectedPins = "1234";
|
||||
const string expectedResponse = "[{\"StatusCode\":200}]";
|
||||
|
||||
IActionResult result = await userController.UpdateMyPins();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
Assert.Equal(expectedPins, dbMock.Users.First().Pins);
|
||||
Assert.Equal(expectedResponse, okObject.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void UpdateMyPins_ShouldNotSave_WhenPinsAreEqual()
|
||||
{
|
||||
UserEntity entity = MockHelper.GetUnitTestUser();
|
||||
entity.Pins = "1234";
|
||||
List<UserEntity> users = new()
|
||||
{
|
||||
entity,
|
||||
};
|
||||
await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(users);
|
||||
|
||||
UserController userController = new(dbMock);
|
||||
userController.SetupTestController("{\"profile_pins\": [1234]}");
|
||||
|
||||
const string expectedPins = "1234";
|
||||
const string expectedResponse = "[{\"StatusCode\":200}]";
|
||||
|
||||
IActionResult result = await userController.UpdateMyPins();
|
||||
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
OkObjectResult? okObject = result as OkObjectResult;
|
||||
Assert.NotNull(okObject);
|
||||
Assert.Equal(expectedPins, dbMock.Users.First().Pins);
|
||||
Assert.Equal(expectedResponse, okObject.Value);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue