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

@ -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);
}
}