Skip to content

Commit

Permalink
Merge branch 'main' into renovate/main-verify.xunit-28.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne authored Nov 21, 2024
2 parents 36135a9 + 8f25ea7 commit a21587b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup Condition="'$(DOTNET_TREATWARNINGSASERRORS)' == 'true' OR '$(CI)' == 'true'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -17,4 +18,4 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
</Project>
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestFeature",
"allowPrerelease": false
}
}
9 changes: 3 additions & 6 deletions src/Altinn.App.Api/Controllers/InstancesController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text;
using Altinn.App.Api.Extensions;
using Altinn.App.Api.Helpers.Patch;
using Altinn.App.Api.Helpers.RequestHandling;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class InstancesController : ControllerBase
private readonly IHostEnvironment _env;
private readonly ModelSerializationService _serializationService;
private readonly InternalPatchService _patchService;
private static readonly JsonSerializerOptions _jsonSerializerOptionsWeb = new(JsonSerializerDefaults.Web);

private const long RequestSizeLimit = 2000 * 1024 * 1024;

/// <summary>
Expand Down Expand Up @@ -1244,10 +1244,7 @@ string action
&& instancePart.ContentType.Contains("application/json", StringComparison.Ordinal)
)
{
return System.Text.Json.JsonSerializer.Deserialize<Instance>(
instancePart.Bytes,
_jsonSerializerOptionsWeb
);
return JsonConvert.DeserializeObject<Instance>(Encoding.UTF8.GetString(instancePart.Bytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,51 @@ public async Task PostNewInstanceWithInstanceTemplate()
TestData.DeleteInstanceAndData(org, app, instance.Id);
}

[Fact]
public async Task PostNewInstanceWithInstanceTemplateString()
{
string org = "tdd";
string app = "contributer-restriction";
int instanceOwnerPartyId = 501337;
// Get an org token
// (to avoid issues with read status being set when initialized by normal users)
HttpClient client = GetRootedClient(org, app, 0, null, serviceOwnerOrg: org);

using var content = new StringContent(
$$"""
{
"instanceOwner": {
"partyId": {{instanceOwnerPartyId}}
},
"status": {
"readStatus": "UpdatedSinceLastReview",
"substatus": {
"label": "min label",
"description": "min beskrivelse"
}
}
}
""",
Encoding.UTF8,
"application/json"
);

var response = await client.PostAsync($"{org}/{app}/instances", content);
var responseContent = await response.Content.ReadAsStringAsync();
OutputHelper.WriteLine(responseContent);
response.Should().HaveStatusCode(HttpStatusCode.Created);
var instance = JsonSerializer.Deserialize<Instance>(responseContent, JsonSerializerOptions)!;
instance.Should().NotBeNull();
instance.Id.Should().NotBeNullOrEmpty();
instance.Status.Should().NotBeNull();
instance.Status.ReadStatus.Should().Be(ReadStatus.UpdatedSinceLastReview);
instance.Status.Substatus.Should().NotBeNull();
instance.Status.Substatus!.Label.Should().Be("min label");
instance.Status.Substatus!.Description.Should().Be("min beskrivelse");

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

[Fact]
public async Task PostNewInstanceWithMissingTemplate()
{
Expand Down
11 changes: 9 additions & 2 deletions test/Altinn.App.Api.Tests/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ public Task Invoke(HttpContext httpContext)
}
}

public HttpClient GetRootedClient(string org, string app, int userId, int? partyId, int authenticationLevel = 2)
public HttpClient GetRootedClient(
string org,
string app,
int userId,
int? partyId,
int authenticationLevel = 2,
string? serviceOwnerOrg = null
)
{
var client = GetRootedClient(org, app);
string token = PrincipalUtil.GetToken(userId, partyId, authenticationLevel);
string token = PrincipalUtil.GetToken(userId, partyId, authenticationLevel, org: serviceOwnerOrg);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
return client;
}
Expand Down
24 changes: 6 additions & 18 deletions test/Altinn.App.Api.Tests/Mocks/InstanceClientMockSi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,14 @@ public InstanceClientMockSi(ILogger<IInstanceClient> logger, IHttpContextAccesso
_httpContextAccessor = httpContextAccessor;
}

public Task<Instance> CreateInstance(string org, string app, Instance instanceTemplate)
public Task<Instance> CreateInstance(string org, string app, Instance instance)
{
string partyId = instanceTemplate.InstanceOwner.PartyId;
string partyId = instance.InstanceOwner.PartyId;
Guid instanceGuid = Guid.NewGuid();

Instance instance =
new()
{
Id = $"{partyId}/{instanceGuid}",
AppId = $"{org}/{app}",
Org = org,
InstanceOwner = instanceTemplate.InstanceOwner,
Process = instanceTemplate.Process,
Data = new List<DataElement>(),
};

if (instanceTemplate.DataValues != null)
{
instance.DataValues = instanceTemplate.DataValues;
}
instance.Id = $"{partyId}/{instanceGuid}";
instance.AppId = $"{org}/{app}";
instance.Org = org;
instance.Data = new List<DataElement>();

string instancePath = GetInstancePath(app, org, int.Parse(partyId), instanceGuid);
string directory =
Expand Down

0 comments on commit a21587b

Please sign in to comment.