Skip to content

Commit

Permalink
[WordController] Add tests for AreInFrontier(), RevertWords()
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Feb 21, 2024
1 parent cabc225 commit d3e47cd
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Backend.Tests/Controllers/WordControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public void Setup()
_projId = _projRepo.Create(new Project { Name = "WordControllerTests" }).Result!.Id;
}

[Test]
public async Task TestAreInFrontier()
{
var wordNotInFrontier = await _wordRepo.Add(Util.RandomWord(_projId));
var emptyResult = await _wordController.AreInFrontier(_projId, new() { wordNotInFrontier.Id, "non-id" });
Assert.That(((ObjectResult)emptyResult).Value, Is.Empty);

var wordInFrontier = await _wordRepo.AddFrontier(Util.RandomWord(_projId));
var nonemptyResult = await _wordController.AreInFrontier(_projId, new() { wordInFrontier.Id, "non-id" });
Assert.That(((OkObjectResult)nonemptyResult).Value, Is.EqualTo(new List<string> { wordInFrontier.Id }));
}

[Test]
public async Task TestAreInFrontierNoPermission()
{
var wordInFrontier = await _wordRepo.AddFrontier(Util.RandomWord(_projId));
_wordController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();
var result = await _wordController.AreInFrontier(_projId, new() { wordInFrontier.Id });
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestAreInFrontierMissingProject()
{
var result = await _wordController.AreInFrontier(MissingId, new());
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestDeleteFrontierWord()
{
Expand Down Expand Up @@ -266,6 +294,49 @@ public async Task TestGetDuplicateIdMissingProject()
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestRevertWords()
{
var frontierWord0 = await _wordRepo.Create(Util.RandomWord(_projId));
var frontierWord1 = await _wordRepo.AddFrontier(Util.RandomWord(_projId));
var nonFrontierWord0 = await _wordRepo.Add(Util.RandomWord(_projId));
var nonFrontierWord1 = await _wordRepo.Add(Util.RandomWord(_projId));
var nonFrontierWord2 = await _wordRepo.Add(Util.RandomWord(_projId));

var result = await _wordController.RevertWords(_projId, new()
{
["non-id"] = frontierWord1.Id, // Cannot revert with key not a word
[nonFrontierWord1.Id] = nonFrontierWord2.Id, // Cannot revert with value not in frontier
[nonFrontierWord0.Id] = frontierWord0.Id, // Can revert
});
var reverted = (Dictionary<string, string>)((OkObjectResult)result).Value!;
Assert.That(reverted, Has.Count.EqualTo(1));
var frontierIds = (await _wordRepo.GetFrontier(_projId)).Select(w => w.Id).ToList();
Assert.That(frontierIds, Has.Count.EqualTo(2));
Assert.That(frontierIds, Does.Contain(frontierWord1.Id));
Assert.That(frontierIds, Does.Contain(reverted[frontierWord0.Id]));
}

[Test]
public async Task TestRevertWordsNoPermission()
{
_wordController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var oldWord = await _wordRepo.Add(Util.RandomWord(_projId));
var newWord = await _wordRepo.Create(Util.RandomWord(_projId));
var result = await _wordController.RevertWords(_projId, new() { [oldWord.Id] = newWord.Id });
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestRevertWordsMissingProject()
{
var oldWord = await _wordRepo.Add(Util.RandomWord(_projId));
var newWord = await _wordRepo.Create(Util.RandomWord(_projId));
var result = await _wordController.RevertWords(MissingId, new() { [oldWord.Id] = newWord.Id });
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestUpdateDuplicate()
{
Expand Down

0 comments on commit d3e47cd

Please sign in to comment.