From 70517fdd734784a3ac0bdb44f6c94772be826345 Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Tue, 22 Oct 2024 10:21:54 +0200 Subject: [PATCH] Ensure that we cleanup instances after running tests that create instances (#855) --- .../Controllers/ActionsControllerTests.cs | 24 +++++++++---------- .../Controllers/DataControllerTests.cs | 8 +++---- .../Controllers/DataController_PutTests.cs | 4 ++++ .../DataController_UserAccessTests.cs | 5 ++++ .../InstancesController_PostNewInstance.cs | 12 ++++++++++ .../Controllers/ProcessControllerTests.cs | 4 ++-- .../UserDefinedMetadataControllerTests.cs | 4 ++++ test/Altinn.App.Api.Tests/Data/TestData.cs | 15 +++++------- 8 files changed, 49 insertions(+), 27 deletions(-) diff --git a/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs index 65e18ba86..28a18fd44 100644 --- a/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs @@ -35,7 +35,7 @@ public async Task Perform_returns_403_if_user_not_authorized() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1000, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -61,7 +61,7 @@ public async Task Perform_returns_401_if_user_not_authenticated() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); using var content = new StringContent( "{\"action\":\"lookup_unauthorized\"}", @@ -85,7 +85,7 @@ public async Task Perform_returns_401_if_userId_is_null() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(null, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -111,7 +111,7 @@ public async Task Perform_returns_400_if_action_is_null() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1000, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -133,7 +133,7 @@ public async Task Perform_returns_409_if_process_not_started() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef43"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1000, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -155,7 +155,7 @@ public async Task Perform_returns_409_if_process_ended() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef42"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1000, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -181,7 +181,7 @@ public async Task Perform_returns_200_if_action_succeeded() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1000, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -234,7 +234,7 @@ public async Task Perform_returns_400_if_action_failed_and_errorType_is_BadReque var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(400, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -260,7 +260,7 @@ public async Task Perform_returns_401_if_action_failed_and_errorType_is_Unauthor var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(401, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -286,7 +286,7 @@ public async Task Perform_returns_409_if_action_failed_and_errorType_is_Conflict var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(409, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -312,7 +312,7 @@ public async Task Perform_returns_500_if_action_failed_and_errorType_is_Internal var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(500, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); @@ -338,7 +338,7 @@ public async Task Perform_returns_404_if_action_implementation_not_found() var app = "task-action"; HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("b1135209-628e-4a6e-9efd-e4282068ef41"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); string token = PrincipalUtil.GetToken(1001, null, 3); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); diff --git a/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs index 86a82873e..6b52cf9dc 100644 --- a/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs @@ -30,7 +30,7 @@ public async Task PutDataElement_MissingDataType_ReturnsBadRequest() string token = PrincipalUtil.GetOrgToken("nav", "160694123"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); - TestData.DeleteInstance(org, app, instanceOwnerPartyId, guid); + TestData.DeleteInstanceAndData(org, app, instanceOwnerPartyId, guid); TestData.PrepareInstance(org, app, instanceOwnerPartyId, guid); using var content = new StringContent("{}", System.Text.Encoding.UTF8, "application/json"); // empty valid json @@ -55,7 +55,7 @@ public async Task CreateDataElement_BinaryPdf_AnalyserShouldRunOk() HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("0fc98a23-fe31-4ef5-8fb9-dd3f479354cd"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); // Setup the request @@ -89,7 +89,7 @@ public async Task CreateDataElement_ZeroBytes_BinaryPdf_AnalyserShouldReturnBadR HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("0fc98a23-fe31-4ef5-8fb9-dd3f479354cd"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); // Setup the request @@ -125,7 +125,7 @@ public async Task CreateDataElement_JpgFakedAsPdf_AnalyserShouldRunAndFail() HttpClient client = GetRootedClient(org, app); Guid guid = new Guid("1fc98a23-fe31-4ef5-8fb9-dd3f479354ce"); - TestData.DeleteInstance(org, app, 1337, guid); + TestData.DeleteInstanceAndData(org, app, 1337, guid); TestData.PrepareInstance(org, app, 1337, guid); // Setup the request diff --git a/test/Altinn.App.Api.Tests/Controllers/DataController_PutTests.cs b/test/Altinn.App.Api.Tests/Controllers/DataController_PutTests.cs index cd0f0ccde..22747853d 100644 --- a/test/Altinn.App.Api.Tests/Controllers/DataController_PutTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/DataController_PutTests.cs @@ -101,6 +101,8 @@ public async Task PutDataElement_TestSinglePartUpdate_ReturnsOk() ); readDataElementResponseParsed.Melding!.Name.Should().Be("Ola Olsen"); + TestData.DeleteInstanceAndData(org, app, instanceId); + _dataProcessor.Verify( p => p.ProcessDataRead( @@ -221,6 +223,8 @@ public async Task PutDataElement_TestMultiPartUpdateWithCustomDataProcessor_Retu readDataElementResponseParsed.Melding!.Name.Should().Be("Ola Olsen"); readDataElementResponseParsed.Melding.Toggle.Should().BeTrue(); + TestData.DeleteInstanceAndData(org, app, instanceId); + _dataProcessor.Verify(); _dataWriteProcessor.Verify(); } diff --git a/test/Altinn.App.Api.Tests/Controllers/DataController_UserAccessTests.cs b/test/Altinn.App.Api.Tests/Controllers/DataController_UserAccessTests.cs index 14b533818..b21488e37 100644 --- a/test/Altinn.App.Api.Tests/Controllers/DataController_UserAccessTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/DataController_UserAccessTests.cs @@ -1,5 +1,6 @@ using System.Net; using System.Net.Http.Headers; +using Altinn.App.Api.Tests.Data; using Altinn.App.Api.Tests.Utils; using Altinn.App.Core.Features; using Altinn.Platform.Storage.Interface.Models; @@ -48,6 +49,8 @@ HttpStatusCode expectedStatusCode // Assert response.Should().HaveStatusCode(expectedStatusCode); + + TestData.DeleteInstanceAndData(OrgId, AppId, instance.Id); } [Theory] @@ -86,6 +89,8 @@ HttpStatusCode expectedStatusCode // Assert response.Should().HaveStatusCode(expectedStatusCode); + + TestData.DeleteInstanceAndData(OrgId, AppId, instance.Id); } private async Task CreateAppInstance(string? tokenOrgClaim) diff --git a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs index 9f35a49de..3ce51ca68 100644 --- a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs +++ b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs @@ -4,6 +4,7 @@ using System.Text.Json; using System.Text.Json.Nodes; using Altinn.App.Api.Models; +using Altinn.App.Api.Tests.Data; using Altinn.App.Api.Tests.Data.apps.tdd.contributer_restriction.models; using Altinn.App.Api.Tests.Utils; using Altinn.App.Core.Features; @@ -80,6 +81,7 @@ public async Task PostNewInstanceWithContent_EnsureDataIsPresent() var readDataElementResponseContent = await readDataElementResponse.Content.ReadAsStringAsync(); var readDataElementResponseParsed = JsonSerializer.Deserialize(readDataElementResponseContent)!; readDataElementResponseParsed.Melding!.Name.Should().Be(testName); + TestData.DeleteInstanceAndData(org, app, instanceId); } private async Task CreateInstanceSimplified( @@ -140,6 +142,7 @@ public async Task PostNewInstance_Simplified() var readDataElementResponseContent = await readDataElementResponse.Content.ReadAsStringAsync(); var readDataElementResponseParsed = JsonSerializer.Deserialize(readDataElementResponseContent)!; readDataElementResponseParsed.Melding.Should().BeNull(); // No content yet + TestData.DeleteInstanceAndData(org, app, instanceId); } [Fact] @@ -172,6 +175,7 @@ public async Task PostNewInstance_Simplified_With_Prefill() var readDataElementResponseParsed = JsonSerializer.Deserialize(readDataElementResponseContent)!; Assert.NotNull(readDataElementResponseParsed.Melding); readDataElementResponseParsed.Melding.Name.Should().Be("TestName"); + TestData.DeleteInstanceAndData(org, app, instanceId); } [Fact] @@ -353,6 +357,14 @@ public async Task InstationAllowedByOrg_Returns_Ok_For_User_When_Copying_Simplif var createResponse = await client.PostAsync($"{org}/{app}/instances/create", content); var createResponseContent = await createResponse.Content.ReadAsStringAsync(); createResponse.StatusCode.Should().Be(HttpStatusCode.Created, createResponseContent); + + TestData.DeleteInstanceAndData(org, app, sourceInstance.Id); + + var createResponseParsed = JsonSerializer.Deserialize(createResponseContent, JsonSerializerOptions); + if (createResponseParsed is not null) + { + TestData.DeleteInstanceAndData(org, app, createResponseParsed.Id); + } } private async Task UpdateInstanceData( diff --git a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs index afe017607..5a0e5ae51 100644 --- a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.cs @@ -74,12 +74,12 @@ public async Task Get_ShouldReturnProcessTasks() HttpClient client = GetRootedClient(org, app, 1337, partyId, 3); - TestData.DeleteInstance(org, app, partyId, instanceId); + TestData.DeleteInstanceAndData(org, app, partyId, instanceId); TestData.PrepareInstance(org, app, partyId, instanceId); string url = $"/{org}/{app}/instances/{partyId}/{instanceId}/process"; HttpResponseMessage response = await client.GetAsync(url); - TestData.DeleteInstance(org, app, partyId, instanceId); + TestData.DeleteInstanceAndData(org, app, partyId, instanceId); response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); diff --git a/test/Altinn.App.Api.Tests/Controllers/UserDefinedMetadataControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/UserDefinedMetadataControllerTests.cs index 55f753e21..119495141 100644 --- a/test/Altinn.App.Api.Tests/Controllers/UserDefinedMetadataControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/UserDefinedMetadataControllerTests.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Http.Headers; using Altinn.App.Api.Models; +using Altinn.App.Api.Tests.Data; using Altinn.App.Api.Tests.Utils; using Altinn.Platform.Storage.Interface.Models; using FluentAssertions; @@ -56,6 +57,7 @@ public async Task PutUserDefinedMetadata_HappyPath_ReturnsOk() new() { Key = "TheKey", Value = "TheValue" } } ); + TestData.DeleteInstanceAndData(Org, App, instanceId); } [Fact] @@ -80,6 +82,7 @@ public async Task PutUserDefinedMetadata_DuplicatedKey_ReturnsBadRequest() string responseMessage = await response.Content.ReadAsStringAsync(); responseMessage.Should().Contain("The following keys are duplicated: DuplicatedKey"); + TestData.DeleteInstanceAndData(Org, App, instanceId); } [Fact] @@ -104,6 +107,7 @@ public async Task PutUserDefinedMetadata_NotAllowedKey_ReturnsBadRequest() string responseMessage = await response.Content.ReadAsStringAsync(); responseMessage.Should().Contain("The following keys are not allowed: SomeKeyThatIsNotAllowed"); + TestData.DeleteInstanceAndData(Org, App, instanceId); } [Fact] diff --git a/test/Altinn.App.Api.Tests/Data/TestData.cs b/test/Altinn.App.Api.Tests/Data/TestData.cs index ba491d121..88b7d6156 100644 --- a/test/Altinn.App.Api.Tests/Data/TestData.cs +++ b/test/Altinn.App.Api.Tests/Data/TestData.cs @@ -134,15 +134,6 @@ public static string GetRegisterProfilePath() return Path.Combine(testDataDirectory, "Profile", "User"); } - public static void DeleteInstance(string org, string app, int instanceOwnerId, Guid instanceGuid) - { - string instancePath = GetInstancePath(org, app, instanceOwnerId, instanceGuid); - if (File.Exists(instancePath)) - { - File.Delete(instancePath); - } - } - public static string GetInstancePath(string org, string app, int instanceOwnerId, Guid instanceGuid) { string instancesDirectory = GetInstancesDirectory(); @@ -177,6 +168,12 @@ public static void PrepareInstance(string org, string app, int instanceOwnerId, } } + public static void DeleteInstanceAndData(string org, string app, string instanceId) + { + string[] instanceIdParts = instanceId.Split('/'); + DeleteInstanceAndData(org, app, int.Parse(instanceIdParts[0]), Guid.Parse(instanceIdParts[1])); + } + public static void DeleteInstanceAndData(string org, string app, int instanceOwnerId, Guid instanceGuid) { DeleteDataForInstance(org, app, instanceOwnerId, instanceGuid);