Skip to content

Commit

Permalink
Add test and fix bug in previous code
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne committed Nov 7, 2024
1 parent 1563b95 commit 7284fd0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Altinn.App.Api/Controllers/InstancesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,10 @@ string action
parts.Remove(instancePart);

// Some clients might set contentType to application/json even if the body is empty
if (instancePart is { Bytes.Length: > 0, ContentType: "application/json" })
if (
instancePart is { Bytes.Length: > 0 }
&& instancePart.ContentType.Contains("application/json", StringComparison.Ordinal)
)
{
return System.Text.Json.JsonSerializer.Deserialize<Instance>(
instancePart.Bytes,
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 System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -273,6 +274,32 @@ public async Task InstationAllowedByOrg_Returns_Forbidden_For_user()
createResponse.StatusCode.Should().Be(HttpStatusCode.Forbidden, createResponseContent);
}

[Fact]
public async Task PostNewInstanceWithInstanceTemplate()
{
string org = "tdd";
string app = "contributer-restriction";
int instanceOwnerPartyId = 501337;
int userId = 1337;
HttpClient client = GetRootedClient(org, app, userId, null);

using var content = JsonContent.Create(
new Instance()
{
InstanceOwner = new InstanceOwner() { PartyId = instanceOwnerPartyId.ToString() },
}
);

var response = await client.PostAsync($"{org}/{app}/instances", content);
response.Should().HaveStatusCode(HttpStatusCode.Created);
var responseContent = await response.Content.ReadAsStringAsync();
var instance = JsonSerializer.Deserialize<Instance>(responseContent, JsonSerializerOptions);
instance.Should().NotBeNull();
instance!.Id.Should().NotBeNullOrEmpty();

TestData.DeleteInstanceAndData(org, app, instance.Id);
}

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

0 comments on commit 7284fd0

Please sign in to comment.