From 813402b7e1904afbe6992b7499cff56c05204387 Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Tue, 24 Oct 2023 11:50:15 -0400 Subject: [PATCH] Refine LIFT testing --- .../Controllers/LiftControllerTests.cs | 117 ++++++++++++++++++ Backend.Tests/Mocks/ProjectRepositoryMock.cs | 3 +- Backend/Services/LiftService.cs | 29 +++-- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/Backend.Tests/Controllers/LiftControllerTests.cs b/Backend.Tests/Controllers/LiftControllerTests.cs index 530e618a80..fea0c6a494 100644 --- a/Backend.Tests/Controllers/LiftControllerTests.cs +++ b/Backend.Tests/Controllers/LiftControllerTests.cs @@ -225,6 +225,38 @@ private static async Task DownloadAndReadLift(LiftController liftControl return liftText; } + [Test] + public void TestUploadLiftFileNoPermission() + { + _liftController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); + var result = _liftController.UploadLiftFile(_projId, new FileUpload()).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestUploadLiftFileInvalidProjectId() + { + var result = _liftController.UploadLiftFile("../hack", new FileUpload()).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestUploadLiftFileAlreadyImported() + { + var projId = _projRepo.Create(new Project { Name = "already has import", LiftImported = true }).Result!.Id; + var result = _liftController.UploadLiftFile(projId, new FileUpload()).Result; + Assert.That(result, Is.InstanceOf()); + Assert.That(((BadRequestObjectResult)result).Value, Contains.Substring("LIFT")); + } + + [Test] + public void TestUploadLiftFileBadFile() + { + var result = _liftController.UploadLiftFile(_projId, new FileUpload()).Result; + Assert.That(result, Is.InstanceOf()); + Assert.That(((BadRequestObjectResult)result).Value, Is.InstanceOf()); + } + [Test] public void TestUploadLiftFileAndGetWritingSystems() { @@ -271,6 +303,21 @@ public void TestFinishUploadLiftFileNothingToFinish() Assert.That(_liftService.RetrieveImport(UserId), Is.Null); } + [Test] + public void TestFinishUploadLiftFileNoPermission() + { + _liftController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); + var result = _liftController.FinishUploadLiftFile(_projId).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestFinishUploadLiftFileInvalidProjectId() + { + var result = _liftController.FinishUploadLiftFile("../hack", UserId).Result; + Assert.That(result, Is.InstanceOf()); + } + [Test] public async Task TestModifiedTimeExportsToLift() { @@ -285,6 +332,35 @@ public async Task TestModifiedTimeExportsToLift() Assert.That(liftContents, Does.Contain("dateModified=\"2000-01-01T00:00:00Z\"")); } + [Test] + public void TestExportLiftFileNoPermission() + { + _liftController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); + var result = _liftController.ExportLiftFile(_projId).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestExportLiftFileInvalidProjectId() + { + var result = _liftController.ExportLiftFile("../hack").Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestExportLiftFileNoProject() + { + var result = _liftController.ExportLiftFile("non-existent-project").Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestExportLiftFileNoWordsInProject() + { + var result = _liftController.ExportLiftFile(_projId).Result; + Assert.That(result, Is.InstanceOf()); + } + [Test] public void TestExportInvalidProjectId() { @@ -294,6 +370,47 @@ public void TestExportInvalidProjectId() Throws.TypeOf()); } + [Test] + public void TestDownloadLiftFileNoPermission() + { + _liftController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); + var result = _liftController.DownloadLiftFile(_projId).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestCanUploadLiftNoPermission() + { + _liftController.ControllerContext.HttpContext = PermissionServiceMock.UnauthorizedHttpContext(); + var result = _liftController.CanUploadLift(_projId).Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestCanUploadLiftInvalidProjectId() + { + var result = _liftController.CanUploadLift("../hack").Result; + Assert.That(result, Is.InstanceOf()); + } + + [Test] + public void TestCanUploadLiftFalse() + { + var projId = _projRepo.Create(new Project { Name = "has import", LiftImported = true }).Result!.Id; + var result = _liftController.CanUploadLift(projId).Result; + Assert.That(result, Is.InstanceOf()); + Assert.That(((OkObjectResult)result).Value, Is.False); + } + + [Test] + public void TestCanUploadLiftTrue() + { + var projId = _projRepo.Create(new Project { Name = "has no import", LiftImported = false }).Result!.Id; + var result = _liftController.CanUploadLift(projId).Result; + Assert.That(result, Is.InstanceOf()); + Assert.That(((OkObjectResult)result).Value, Is.True); + } + /// /// Create three words and delete one. Ensure that the deleted word is still exported to Lift format and marked /// as deleted. diff --git a/Backend.Tests/Mocks/ProjectRepositoryMock.cs b/Backend.Tests/Mocks/ProjectRepositoryMock.cs index b38a8b0b6e..0b3e77628b 100644 --- a/Backend.Tests/Mocks/ProjectRepositoryMock.cs +++ b/Backend.Tests/Mocks/ProjectRepositoryMock.cs @@ -95,7 +95,8 @@ public Task Update(string projectId, Project project) public Task CanImportLift(string projectId) { - return Task.FromResult(true); + var project = _projects.Find(p => p.Id == projectId); + return Task.FromResult(project?.LiftImported != true); } } } diff --git a/Backend/Services/LiftService.cs b/Backend/Services/LiftService.cs index 183b5bff91..c8a29d71a9 100644 --- a/Backend/Services/LiftService.cs +++ b/Backend/Services/LiftService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Linq; @@ -944,16 +945,17 @@ public void ProcessRangeElement(string range, string id, string guid, string par // The following are unused and are not implemented, but may still be called by the Lexicon Merger // They may be useful later if we need to add more complex attributes to words in The Combine + [ExcludeFromCodeCoverage] public LiftExample GetOrMakeExample(LiftSense sense, Extensible info) { return new LiftExample { Content = new LiftMultiText() }; } - + [ExcludeFromCodeCoverage] public LiftObject GetOrMakeParentReversal(LiftObject parent, LiftMultiText contents, string type) { return new LiftReversal(); } - + [ExcludeFromCodeCoverage] public LiftSense GetOrMakeSubsense(LiftSense sense, Extensible info, string rawXml) { return new LiftSense(info, new Guid(), sense) @@ -962,35 +964,40 @@ public LiftSense GetOrMakeSubsense(LiftSense sense, Extensible info, string rawX Gloss = new LiftMultiText() }; } - + [ExcludeFromCodeCoverage] public LiftObject MergeInEtymology(LiftEntry entry, string source, string type, LiftMultiText form, - LiftMultiText gloss, string rawXml) + LiftMultiText gloss, string rawXml) { return new LiftEtymology(); } - + [ExcludeFromCodeCoverage] public LiftObject MergeInReversal( - LiftSense sense, LiftObject parent, LiftMultiText contents, string type, string rawXml) + LiftSense sense, LiftObject parent, LiftMultiText contents, string type, string rawXml) { return new LiftReversal(); } - + [ExcludeFromCodeCoverage] public LiftObject MergeInVariant(LiftEntry entry, LiftMultiText contents, string rawXml) { return new LiftVariant(); } - + [ExcludeFromCodeCoverage] public void EntryWasDeleted(Extensible info, DateTime dateDeleted) { } + [ExcludeFromCodeCoverage] public void MergeInExampleForm(LiftExample example, LiftMultiText multiText) { } - + [ExcludeFromCodeCoverage] public void MergeInPicture(LiftSense sense, string href, LiftMultiText caption) { } + [ExcludeFromCodeCoverage] public void MergeInRelation( - LiftObject extensible, string relationTypeName, string targetId, string rawXml) + LiftObject extensible, string relationTypeName, string targetId, string rawXml) { } + [ExcludeFromCodeCoverage] public void MergeInSource(LiftExample example, string source) { } + [ExcludeFromCodeCoverage] public void MergeInTranslationForm( - LiftExample example, string type, LiftMultiText multiText, string rawXml) + LiftExample example, string type, LiftMultiText multiText, string rawXml) { } + [ExcludeFromCodeCoverage] public void ProcessFieldDefinition(string tag, LiftMultiText description) { } } }