diff --git a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs index c4a32508f..9f35a49de 100644 --- a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs +++ b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs @@ -87,15 +87,18 @@ private async Task CreateInstanceSimplified( string app, int instanceOwnerPartyId, HttpClient client, - string token + string token, + Dictionary? prefill = null ) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + prefill ??= new(); + // Create instance data var body = $$""" { - "prefill": {}, + "prefill": {{JsonSerializer.Serialize(prefill)}}, "instanceOwner": { "partyId": "{{instanceOwnerPartyId}}" } @@ -139,6 +142,38 @@ public async Task PostNewInstance_Simplified() readDataElementResponseParsed.Melding.Should().BeNull(); // No content yet } + [Fact] + public async Task PostNewInstance_Simplified_With_Prefill() + { + // Setup test data + string org = "tdd"; + string app = "contributer-restriction"; + int instanceOwnerPartyId = 501337; + HttpClient client = GetRootedClient(org, app); + string token = PrincipalUtil.GetToken(1337, null); + + var prefill = new Dictionary { { "melding.name", "TestName" }, }; + var createResponseParsed = await CreateInstanceSimplified( + org, + app, + instanceOwnerPartyId, + client, + token, + prefill + ); + var instanceId = createResponseParsed.Id; + createResponseParsed.Data.Should().HaveCount(1, "Create instance should create a data element"); + var dataGuid = createResponseParsed.Data.First().Id; + + // Verify stored data + var readDataElementResponse = await client.GetAsync($"/{org}/{app}/instances/{instanceId}/data/{dataGuid}"); + readDataElementResponse.StatusCode.Should().Be(HttpStatusCode.OK); + var readDataElementResponseContent = await readDataElementResponse.Content.ReadAsStringAsync(); + var readDataElementResponseParsed = JsonSerializer.Deserialize(readDataElementResponseContent)!; + Assert.NotNull(readDataElementResponseParsed.Melding); + readDataElementResponseParsed.Melding.Name.Should().Be("TestName"); + } + [Fact] public async Task PostNewInstanceWithInvalidData_EnsureInvalidResponse() {