Skip to content

Commit

Permalink
Add test for posting instance simplified with prefill (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar authored Oct 9, 2024
1 parent fa0d791 commit ba02911
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,18 @@ private async Task<Instance> CreateInstanceSimplified(
string app,
int instanceOwnerPartyId,
HttpClient client,
string token
string token,
Dictionary<string, string>? prefill = null
)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

prefill ??= new();

// Create instance data
var body = $$"""
{
"prefill": {},
"prefill": {{JsonSerializer.Serialize(prefill)}},
"instanceOwner": {
"partyId": "{{instanceOwnerPartyId}}"
}
Expand Down Expand Up @@ -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<string, string> { { "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<Skjema>(readDataElementResponseContent)!;
Assert.NotNull(readDataElementResponseParsed.Melding);
readDataElementResponseParsed.Melding.Name.Should().Be("TestName");
}

[Fact]
public async Task PostNewInstanceWithInvalidData_EnsureInvalidResponse()
{
Expand Down

0 comments on commit ba02911

Please sign in to comment.