Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that we cleanup instances after running tests that create instances #855

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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\"}",
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,6 +49,8 @@ HttpStatusCode expectedStatusCode

// Assert
response.Should().HaveStatusCode(expectedStatusCode);

TestData.DeleteInstanceAndData(OrgId, AppId, instance.Id);
}

[Theory]
Expand Down Expand Up @@ -86,6 +89,8 @@ HttpStatusCode expectedStatusCode

// Assert
response.Should().HaveStatusCode(expectedStatusCode);

TestData.DeleteInstanceAndData(OrgId, AppId, instance.Id);
}

private async Task<AppInstance> CreateAppInstance(string? tokenOrgClaim)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +81,7 @@ public async Task PostNewInstanceWithContent_EnsureDataIsPresent()
var readDataElementResponseContent = await readDataElementResponse.Content.ReadAsStringAsync();
var readDataElementResponseParsed = JsonSerializer.Deserialize<Skjema>(readDataElementResponseContent)!;
readDataElementResponseParsed.Melding!.Name.Should().Be(testName);
TestData.DeleteInstanceAndData(org, app, instanceId);
}

private async Task<Instance> CreateInstanceSimplified(
Expand Down Expand Up @@ -140,6 +142,7 @@ public async Task PostNewInstance_Simplified()
var readDataElementResponseContent = await readDataElementResponse.Content.ReadAsStringAsync();
var readDataElementResponseParsed = JsonSerializer.Deserialize<Skjema>(readDataElementResponseContent)!;
readDataElementResponseParsed.Melding.Should().BeNull(); // No content yet
TestData.DeleteInstanceAndData(org, app, instanceId);
}

[Fact]
Expand Down Expand Up @@ -172,6 +175,7 @@ public async Task PostNewInstance_Simplified_With_Prefill()
var readDataElementResponseParsed = JsonSerializer.Deserialize<Skjema>(readDataElementResponseContent)!;
Assert.NotNull(readDataElementResponseParsed.Melding);
readDataElementResponseParsed.Melding.Name.Should().Be("TestName");
TestData.DeleteInstanceAndData(org, app, instanceId);
}

[Fact]
Expand Down Expand Up @@ -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<Instance>(createResponseContent, JsonSerializerOptions);
if (createResponseParsed is not null)
{
TestData.DeleteInstanceAndData(org, app, createResponseParsed.Id);
}
}

private async Task UpdateInstanceData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,6 +57,7 @@ public async Task PutUserDefinedMetadata_HappyPath_ReturnsOk()
new() { Key = "TheKey", Value = "TheValue" }
}
);
TestData.DeleteInstanceAndData(Org, App, instanceId);
}

[Fact]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
15 changes: 6 additions & 9 deletions test/Altinn.App.Api.Tests/Data/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Loading