Skip to content

Commit

Permalink
[Backend] Update .EditedBy when updating a Word (#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Oct 26, 2023
1 parent d36914b commit 35a63cd
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 290 deletions.
4 changes: 2 additions & 2 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ public async Task TestDeletedWordsExportToLift()
word.Id = "";
word.Vernacular = "updated";

await _wordService.Update(_projId, wordToUpdate.Id, word);
await _wordService.DeleteFrontierWord(_projId, wordToDelete.Id);
await _wordService.Update(_projId, UserId, wordToUpdate.Id, word);
await _wordService.DeleteFrontierWord(_projId, UserId, wordToDelete.Id);

await _liftController.CreateLiftExportThenSignal(_projId, UserId);
var text = await DownloadAndReadLift(_liftController, _projId);
Expand Down
41 changes: 6 additions & 35 deletions Backend.Tests/Controllers/WordControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,6 @@ public void Setup()
_projId = _projRepo.Create(new Project { Name = "WordControllerTests" }).Result!.Id;
}

[Test]
public async Task TestDeleteAllWords()
{
await _wordRepo.Create(Util.RandomWord(_projId));
await _wordRepo.Create(Util.RandomWord(_projId));
const string diffProjId = "OTHER_PROJECT";
await _wordRepo.Create(Util.RandomWord(diffProjId));

await _wordController.DeleteProjectWords(_projId);
Assert.That(await _wordRepo.GetAllWords(_projId), Is.Empty);
Assert.That(await _wordRepo.GetFrontier(_projId), Is.Empty);
Assert.That(await _wordRepo.GetAllWords(diffProjId), Has.Count.EqualTo(1));
Assert.That(await _wordRepo.GetFrontier(diffProjId), Has.Count.EqualTo(1));
}

[Test]
public async Task TestDeleteAllWordsNoPermission()
{
_wordController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();
var result = await _wordController.DeleteProjectWords(_projId);
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

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

[Test]
public async Task TestDeleteFrontierWord()
{
Expand Down Expand Up @@ -275,7 +245,8 @@ public async Task TestUpdateDuplicate()
var dupWord = origWord.Clone();
dupWord.Flag = new Flag("New Flag");
var expectedWord = dupWord.Clone();
var id = (string)((ObjectResult)await _wordController.UpdateDuplicate(_projId, origWord.Id, "", dupWord)).Value!;
var result = (ObjectResult)await _wordController.UpdateDuplicate(_projId, origWord.Id, dupWord);
var id = (string)result.Value!;
var updatedWord = await _wordRepo.GetWord(_projId, id);
Assert.That(expectedWord.ContentEquals(updatedWord!), Is.True);
}
Expand All @@ -286,23 +257,23 @@ public async Task TestUpdateDuplicateNoPermission()
_wordController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext();

var word = await _wordRepo.Create(Util.RandomWord(_projId));
var result = await _wordController.UpdateDuplicate(_projId, word.Id, "", word);
var result = await _wordController.UpdateDuplicate(_projId, word.Id, word);
Assert.That(result, Is.InstanceOf<ForbidResult>());
}

[Test]
public async Task TestUpdateDuplicateMissingProject()
{
var word = await _wordRepo.Create(Util.RandomWord(_projId));
var result = await _wordController.UpdateDuplicate(MissingId, word.Id, "", word);
var result = await _wordController.UpdateDuplicate(MissingId, word.Id, word);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public async Task TestUpdateDuplicateMissingWord()
{
var word = Util.RandomWord(_projId);
var result = await _wordController.UpdateDuplicate(_projId, MissingId, "", word);
var result = await _wordController.UpdateDuplicate(_projId, MissingId, word);
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
}

Expand All @@ -312,7 +283,7 @@ public async Task TestUpdateDuplicateNonDuplicate()
var origWord = await _wordRepo.Create(Util.RandomWord(_projId));
var nonDup = origWord.Clone();
nonDup.Vernacular = "differentVern";
var result = await _wordController.UpdateDuplicate(_projId, origWord.Id, "", nonDup);
var result = await _wordController.UpdateDuplicate(_projId, origWord.Id, nonDup);
Assert.That(result, Is.InstanceOf<ConflictResult>());
}

Expand Down
5 changes: 5 additions & 0 deletions Backend.Tests/Mocks/WordRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public Task<bool> IsFrontierNonempty(string projectId)
return Task.FromResult(_frontier.Find(w => w.ProjectId == projectId) is not null);
}

public Task<bool> IsInFrontier(string projectId, string wordId)
{
return Task.FromResult(_frontier.Find(w => w.ProjectId == projectId && w.Id == wordId) is not null);
}

public Task<List<Word>> GetFrontier(string projectId)
{
return Task.FromResult(_frontier.Where(w => w.ProjectId == projectId).Select(w => w.Clone()).ToList());
Expand Down
16 changes: 8 additions & 8 deletions Backend.Tests/Services/MergeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void MergeWordsOneChildTest()
}
};

var newWords = _mergeService.Merge(ProjId, new List<MergeWords> { mergeObject }).Result;
var newWords = _mergeService.Merge(ProjId, UserId, new List<MergeWords> { mergeObject }).Result;

// There should only be 1 word added and it should be identical to what we passed in
Assert.That(newWords, Has.Count.EqualTo(1));
Expand Down Expand Up @@ -76,7 +76,7 @@ public void MergeWordsDeleteTest()
DeleteOnly = true
};

var newWords = _mergeService.Merge(ProjId, new List<MergeWords> { mergeObject }).Result;
var newWords = _mergeService.Merge(ProjId, UserId, new List<MergeWords> { mergeObject }).Result;

// There should be no word added and no words left in the frontier.
Assert.That(newWords, Is.Empty);
Expand All @@ -100,7 +100,7 @@ public void MergeWordsMultiChildTest()
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));

var mergeWordsList = new List<MergeWords> { mergeWords };
var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result;
var newWords = _mergeService.Merge(ProjId, UserId, mergeWordsList).Result;

// Check for correct history length.
var dbParent = newWords.First();
Expand All @@ -117,7 +117,7 @@ public void MergeWordsMultipleTest()
int wordCount = 100;
var randWords = Util.RandomWordList(wordCount, ProjId);
var mergeWordsList = randWords.Select(word => new MergeWords { Parent = word }).ToList();
var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result;
var newWords = _mergeService.Merge(ProjId, UserId, mergeWordsList).Result;

Assert.That(newWords, Has.Count.EqualTo(wordCount));
Assert.That(newWords.First().Id, Is.Not.EqualTo(newWords.Last().Id));
Expand All @@ -144,7 +144,7 @@ public void UndoMergeOneChildTest()
}
};

var newWords = _mergeService.Merge(ProjId, new List<MergeWords> { mergeObject }).Result;
var newWords = _mergeService.Merge(ProjId, UserId, new List<MergeWords> { mergeObject }).Result;

// There should only be 1 word added and it should be identical to what we passed in
Assert.That(newWords, Has.Count.EqualTo(1));
Expand All @@ -153,7 +153,7 @@ public void UndoMergeOneChildTest()
var childIds = mergeObject.Children.Select(word => word.SrcWordId).ToList();
var parentIds = new List<string> { newWords[0].Id };
var mergedWord = new MergeUndoIds(parentIds, childIds);
var undo = _mergeService.UndoMerge(ProjId, mergedWord).Result;
var undo = _mergeService.UndoMerge(ProjId, UserId, mergedWord).Result;
Assert.That(undo, Is.True);

var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
Expand All @@ -179,14 +179,14 @@ public void UndoMergeMultiChildTest()
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));

var mergeWordsList = new List<MergeWords> { mergeWords };
var newWords = _mergeService.Merge(ProjId, mergeWordsList).Result;
var newWords = _mergeService.Merge(ProjId, UserId, mergeWordsList).Result;

Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));

var childIds = mergeWords.Children.Select(word => word.SrcWordId).ToList();
var parentIds = new List<string> { newWords[0].Id };
var mergedWord = new MergeUndoIds(parentIds, childIds);
var undo = _mergeService.UndoMerge(ProjId, mergedWord).Result;
var undo = _mergeService.UndoMerge(ProjId, UserId, mergedWord).Result;
Assert.That(undo, Is.True);

var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
Expand Down
76 changes: 76 additions & 0 deletions Backend.Tests/Services/WordServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class WordServiceTests
private IWordService _wordService = null!;

private const string ProjId = "WordServiceTestProjId";
private const string UserId = "WordServiceTestUserId";
private const string WordId = "WordServiceTestWordId";

[SetUp]
public void Setup()
Expand All @@ -22,6 +24,80 @@ public void Setup()
_wordService = new WordService(_wordRepo);
}

[Test]
public void TestCreateAddsUserId()
{
var word = _wordService.Create(UserId, new Word { EditedBy = new List<string> { "other" } }).Result;
Assert.That(word.EditedBy, Has.Count.EqualTo(2));
Assert.That(word.EditedBy.Last(), Is.EqualTo(UserId));
}

[Test]
public void TestCreateDoesNotAddDuplicateUserId()
{
var word = _wordService.Create(UserId, new Word { EditedBy = new List<string> { UserId } }).Result;
Assert.That(word.EditedBy, Has.Count.EqualTo(1));
}

[Test]
public void TestCreateMultipleWords()
{
_ = _wordService.Create(
UserId, new List<Word> { new() { ProjectId = ProjId }, new() { ProjectId = ProjId } }).Result;
Assert.That(_wordRepo.GetAllWords(ProjId).Result, Has.Count.EqualTo(2));
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(2));
}

[Test]
public void TestUpdateNotInFrontierFalse()
{
Assert.That(_wordService.Update(ProjId, UserId, WordId, new Word()).Result, Is.False);
}

[Test]
public void TestUpdateReplacesFrontierWord()
{
var word = _wordRepo.AddFrontier(new Word { ProjectId = ProjId }).Result;
Assert.That(word, Is.Not.Null);
var oldId = word.Id;
word.Vernacular = "NewVern";
Assert.That(_wordService.Update(ProjId, UserId, oldId, word).Result, Is.True);
var frontier = _wordRepo.GetFrontier(ProjId).Result;
Assert.That(frontier, Has.Count.EqualTo(1));
var newWord = frontier.First();
Assert.That(newWord.Id, Is.Not.EqualTo(oldId));
Assert.That(newWord.History.Last(), Is.EqualTo(oldId));
}

[Test]
public void TestRestoreFrontierWordsMissingWordFalse()
{
var word = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
Assert.That(_wordService.RestoreFrontierWords(
ProjId, new List<string> { "NotAnId", word.Id }).Result, Is.False);
}

[Test]
public void TestRestoreFrontierWordsFrontierWordFalse()
{
var wordNoFrontier = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
var wordYesFrontier = _wordRepo.Create(new Word { ProjectId = ProjId }).Result;
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));
Assert.That(_wordService.RestoreFrontierWords(
ProjId, new List<string> { wordNoFrontier.Id, wordYesFrontier.Id }).Result, Is.False);
}

[Test]
public void TestRestoreFrontierWordsTrue()
{
var word1 = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
var word2 = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Is.Empty);
Assert.That(_wordService.RestoreFrontierWords(
ProjId, new List<string> { word1.Id, word2.Id }).Result, Is.True);
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(2));
}

[Test]
public void TestFindContainingWordNoFrontier()
{
Expand Down
6 changes: 4 additions & 2 deletions Backend/Controllers/AudioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public async Task<IActionResult> UploadAudioFile(string projectId, string wordId
{
return Forbid();
}
var userId = _permissionService.GetUserId(HttpContext);

// sanitize user input
try
Expand Down Expand Up @@ -117,7 +118,7 @@ public async Task<IActionResult> UploadAudioFile(string projectId, string wordId
word.Audio.Add(Path.GetFileName(fileUpload.FilePath));

// Update the word with new audio file
await _wordService.Update(projectId, wordId, word);
await _wordService.Update(projectId, userId, wordId, word);

return Ok(word.Id);
}
Expand All @@ -131,6 +132,7 @@ public async Task<IActionResult> DeleteAudioFile(string projectId, string wordId
{
return Forbid();
}
var userId = _permissionService.GetUserId(HttpContext);

// sanitize user input
try
Expand All @@ -144,7 +146,7 @@ public async Task<IActionResult> DeleteAudioFile(string projectId, string wordId
return new UnsupportedMediaTypeResult();
}

var newWord = await _wordService.Delete(projectId, wordId, fileName);
var newWord = await _wordService.Delete(projectId, userId, wordId, fileName);
if (newWord is not null)
{
return Ok(newWord.Id);
Expand Down
6 changes: 4 additions & 2 deletions Backend/Controllers/MergeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public async Task<IActionResult> MergeWords(
{
return Forbid();
}
var userId = _permissionService.GetUserId(HttpContext);

try
{
var newWords = await _mergeService.Merge(projectId, mergeWordsList);
var newWords = await _mergeService.Merge(projectId, userId, mergeWordsList);
return Ok(newWords.Select(w => w.Id).ToList());
}
catch
Expand All @@ -59,8 +60,9 @@ public async Task<IActionResult> UndoMerge(string projectId, [FromBody, BindRequ
{
return Forbid();
}
var userId = _permissionService.GetUserId(HttpContext);

var undo = await _mergeService.UndoMerge(projectId, merge);
var undo = await _mergeService.UndoMerge(projectId, userId, merge);
return Ok(undo);
}

Expand Down
Loading

0 comments on commit 35a63cd

Please sign in to comment.