Skip to content

Commit

Permalink
Declare DataController with [ApiController] to automatically trigger …
Browse files Browse the repository at this point in the history
…bad request

Also add test
  • Loading branch information
ivarne committed Oct 20, 2023
1 parent a3dc24a commit b7bb5c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Altinn.App.Api/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Altinn.App.Api.Controllers
/// The data controller handles creation, update, validation and calculation of data elements.
/// </summary>
[AutoValidateAntiforgeryTokenIfAuthCookie]
[ApiController]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[Route("{org}/{app}/instances/{instanceOwnerPartyId:int}/{instanceGuid:guid}/data")]
public class DataController : ControllerBase
Expand Down Expand Up @@ -329,10 +330,10 @@ public async Task<ActionResult> Put(

if (appLogic == null)
{
_logger.LogError($"Could not determine if {dataType} requires app logic for application {org}/{app}");
_logger.LogError("Could not determine if {dataType} requires app logic for application {org}/{app}", dataType, org, app);

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
This log entry depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
return BadRequest($"Could not determine if data type {dataType} requires application logic.");
}
else if ((bool)appLogic)
else if (appLogic == true)
{
return await PutFormData(org, app, instance, dataGuid, dataType);
}
Expand Down
24 changes: 24 additions & 0 deletions test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Altinn.App.Core.Features.Validation;
using Altinn.App.Core.Models.Validation;
using Altinn.Platform.Storage.Interface.Models;
using FluentAssertions;

namespace Altinn.App.Api.Tests.Controllers
{
Expand All @@ -18,6 +19,29 @@ public DataControllerTests(WebApplicationFactory<Program> factory) : base(factor
{
}

[Fact]
public async Task PutDataElement_MissingDataType_ReturnsBadRequest()
{
// Setup test data
string org = "tdd";
string app = "contributer-restriction";
int instanceOwnerPartyId = 1337;
Guid guid = new Guid("0fc98a23-fe31-4ef5-8fb9-dd3f479354cd");
HttpClient client = GetRootedClient(org, app);
string token = PrincipalUtil.GetOrgToken("nav", "160694123");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

TestData.DeleteInstance(org, app, instanceOwnerPartyId, guid);
TestData.PrepareInstance(org, app, instanceOwnerPartyId, guid);


using var content = new StringContent("{}", System.Text.Encoding.UTF8, "application/json"); // empty valid json
var response = await client.PostAsync($"/{org}/{app}/instances/{instanceOwnerPartyId}/{guid}/data", content);
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var responseContent = await response.Content.ReadAsStringAsync();
responseContent.Should().Contain("dataType");
}

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

0 comments on commit b7bb5c1

Please sign in to comment.