-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[StatisticsController] Add tests (#2669)
- Loading branch information
1 parent
bb8d5b6
commit cfccd77
Showing
2 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Backend.Tests.Mocks; | ||
using BackendFramework.Controllers; | ||
using BackendFramework.Interfaces; | ||
using BackendFramework.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NUnit.Framework; | ||
|
||
namespace Backend.Tests.Controllers | ||
{ | ||
public class StatisticsControllerTests : IDisposable | ||
{ | ||
private IProjectRepository _projRepo = null!; | ||
private IUserRepository _userRepo = null!; | ||
private IPermissionService _permService = null!; | ||
private StatisticsController _statsController = null!; | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_statsController?.Dispose(); | ||
} | ||
} | ||
|
||
private User _jwtAuthenticatedUser = null!; | ||
private string _projId = null!; | ||
private const string MissingId = "MISSING_ID"; | ||
|
||
[SetUp] | ||
public async Task Setup() | ||
{ | ||
_projRepo = new ProjectRepositoryMock(); | ||
_userRepo = new UserRepositoryMock(); | ||
_permService = new PermissionServiceMock(_userRepo); | ||
_statsController = new StatisticsController(new StatisticsServiceMock(), _permService, _projRepo) | ||
{ | ||
// Mock the Http Context because this isn't an actual call controller | ||
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } | ||
}; | ||
|
||
_jwtAuthenticatedUser = new User { Username = "user", Password = "pass" }; | ||
await _userRepo.Create(_jwtAuthenticatedUser); | ||
_jwtAuthenticatedUser = await _permService.Authenticate(_jwtAuthenticatedUser.Username, | ||
_jwtAuthenticatedUser.Password) ?? throw new UserAuthenticationException(); | ||
_projId = (await _projRepo.Create(new Project { Name = "StatisticsControllerTests" }))!.Id; | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainCountsNoPermission() | ||
{ | ||
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
|
||
var result = await _statsController.GetSemanticDomainCounts(_projId, "en"); | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainCountsMissingProject() | ||
{ | ||
var result = await _statsController.GetSemanticDomainCounts(MissingId, "en"); | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainCounts() | ||
{ | ||
var result = await _statsController.GetSemanticDomainCounts(_projId, "en"); | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetWordsPerDayPerUserCountsNoPermission() | ||
{ | ||
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
|
||
var result = await _statsController.GetWordsPerDayPerUserCounts(_projId); | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetWordsPerDayPerUserCountsMissingProject() | ||
{ | ||
var result = await _statsController.GetWordsPerDayPerUserCounts(MissingId); | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetWordsPerDayPerUserCounts() | ||
{ | ||
var result = await _statsController.GetWordsPerDayPerUserCounts(_projId); | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetProgressEstimationLineChartRootNoPermission() | ||
{ | ||
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
|
||
var result = await _statsController.GetProgressEstimationLineChartRoot(_projId); | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetProgressEstimationLineChartRootMissingProject() | ||
{ | ||
var result = await _statsController.GetProgressEstimationLineChartRoot(MissingId); | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetProgressEstimationLineChartRoot() | ||
{ | ||
var result = await _statsController.GetProgressEstimationLineChartRoot(_projId); | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetLineChartRootDataNoPermission() | ||
{ | ||
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
|
||
var result = await _statsController.GetLineChartRootData(_projId); | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetLineChartRootDataMissingProject() | ||
{ | ||
var result = await _statsController.GetLineChartRootData(MissingId); | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetLineChartRootData() | ||
{ | ||
var result = await _statsController.GetLineChartRootData(_projId); | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainUserCountsNoPermission() | ||
{ | ||
_statsController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); | ||
|
||
var result = await _statsController.GetSemanticDomainUserCounts(_projId, "en"); | ||
Assert.That(result, Is.InstanceOf<ForbidResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainUserCountsMissingProject() | ||
{ | ||
var result = await _statsController.GetSemanticDomainUserCounts(MissingId, "en"); | ||
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>()); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetSemanticDomainUserCounts() | ||
{ | ||
var result = await _statsController.GetSemanticDomainUserCounts(_projId, "en"); | ||
Assert.That(result, Is.InstanceOf<OkObjectResult>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using BackendFramework.Interfaces; | ||
using BackendFramework.Models; | ||
|
||
namespace Backend.Tests.Mocks | ||
{ | ||
internal class StatisticsServiceMock : IStatisticsService | ||
{ | ||
public Task<List<SemanticDomainCount>> GetSemanticDomainCounts(string projectId, string lang) | ||
{ | ||
return Task.FromResult(new List<SemanticDomainCount>()); | ||
} | ||
public Task<List<WordsPerDayPerUserCount>> GetWordsPerDayPerUserCounts(string projectId) | ||
{ | ||
return Task.FromResult(new List<WordsPerDayPerUserCount>()); | ||
} | ||
public Task<ChartRootData> GetProgressEstimationLineChartRoot(string projectId, List<DateTime> schedule) | ||
{ | ||
return Task.FromResult(new ChartRootData()); | ||
} | ||
public Task<ChartRootData> GetLineChartRootData(string projectId) | ||
{ | ||
return Task.FromResult(new ChartRootData()); | ||
} | ||
public Task<List<SemanticDomainUserCount>> GetSemanticDomainUserCounts(string projectId) | ||
{ | ||
return Task.FromResult(new List<SemanticDomainUserCount>()); | ||
} | ||
} | ||
} |