From 935b8708642b62cfcfb374ad8d2d328f45527bd3 Mon Sep 17 00:00:00 2001 From: HauklandJ Date: Fri, 4 Oct 2024 13:35:39 +0200 Subject: [PATCH] add the tests i forgot in the last commit --- .../Conventions/AltinnApiJsonFormatter.cs | 1 - .../AltinnApiJsonFormatterTests.cs | 89 +++++++++++++++++++ .../AltinnControllerConventionTests.cs | 31 +++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnApiJsonFormatterTests.cs create mode 100644 test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnControllerConventionTests.cs diff --git a/src/Altinn.App.Api/Controllers/Conventions/AltinnApiJsonFormatter.cs b/src/Altinn.App.Api/Controllers/Conventions/AltinnApiJsonFormatter.cs index 1fd592b28..0cf2ea569 100644 --- a/src/Altinn.App.Api/Controllers/Conventions/AltinnApiJsonFormatter.cs +++ b/src/Altinn.App.Api/Controllers/Conventions/AltinnApiJsonFormatter.cs @@ -1,7 +1,6 @@ using System.Text.Encodings.Web; using System.Text.Json; using Altinn.App.Api.Extensions; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Formatters; namespace Altinn.App.Api.Controllers.Conventions; diff --git a/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnApiJsonFormatterTests.cs b/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnApiJsonFormatterTests.cs new file mode 100644 index 000000000..123aa0311 --- /dev/null +++ b/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnApiJsonFormatterTests.cs @@ -0,0 +1,89 @@ +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using Altinn.App.Api.Controllers.Attributes; +using Altinn.App.Api.Controllers.Conventions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace Altinn.App.Api.Tests.Controllers.Conventions; + +public class AltinnApiJsonFormatterTests +{ + [Fact] + public void CreateFormatter_WhenEncoderIsNull_SetsUnsafeRelaxedJsonEscaping() + { + // Arrange + string settingsName = "AltinnApi"; + var serializerOptions = new JsonSerializerOptions + { + Encoder = null, + TypeInfoResolver = new DefaultJsonTypeInfoResolver() + }; + + // Act + var formatter = AltinnApiJsonFormatter.CreateFormatter(settingsName, serializerOptions); + + // Assert + Assert.NotNull(formatter); + Assert.Equal(settingsName, formatter.SettingsName); + Assert.NotNull(formatter.SerializerOptions.Encoder); + Assert.Equal(JavaScriptEncoder.UnsafeRelaxedJsonEscaping, formatter.SerializerOptions.Encoder); + } + + [Fact] + public void CreateFormatter_WhenEncoderIsNotNull_PreservesEncoder() + { + // Arrange + string settingsName = "AltinnApi"; + var originalEncoder = JavaScriptEncoder.Default; + var serializerOptions = new JsonSerializerOptions + { + Encoder = originalEncoder, + TypeInfoResolver = new DefaultJsonTypeInfoResolver() + }; + + // Act + var formatter = AltinnApiJsonFormatter.CreateFormatter(settingsName, serializerOptions); + + // Assert + Assert.NotNull(formatter); + Assert.Equal(settingsName, formatter.SettingsName); + Assert.Equal(originalEncoder, formatter.SerializerOptions.Encoder); + } + + [Fact] + public void CanWriteResult_SettingsNameMatches_ReturnsTrue() + { + // Arrange + string settingsName = JsonSettingNames.AltinnApi; + var formatter = AltinnApiJsonFormatter.CreateFormatter( + settingsName, + new JsonSerializerOptions() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() } + ); + + var httpContext = new DefaultHttpContext(); + + // Create an Endpoint with JsonSettingsNameAttribute + var endpoint = new Endpoint( + requestDelegate: null, + metadata: new EndpointMetadataCollection(new JsonSettingsNameAttribute(settingsName)), + displayName: null + ); + + httpContext.SetEndpoint(endpoint); + + var context = new OutputFormatterWriteContext( + httpContext, + (stream, encoding) => new StreamWriter(stream, encoding), + typeof(object), + new object() + ); + + // Act + bool canWrite = formatter.CanWriteResult(context); + + // Assert + Assert.True(canWrite); + } +} diff --git a/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnControllerConventionTests.cs b/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnControllerConventionTests.cs new file mode 100644 index 000000000..c183cc109 --- /dev/null +++ b/test/Altinn.App.Api.Tests/Controllers/Conventions/AltinnControllerConventionTests.cs @@ -0,0 +1,31 @@ +using System.Reflection; +using Altinn.App.Api.Controllers.Attributes; +using Altinn.App.Api.Controllers.Conventions; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ApplicationModels; + +namespace Altinn.App.Api.Tests.Controllers.Conventions; + +public class AltinnControllerConventionsTests +{ + [Fact] + public void Apply_AddsJsonSettingsNameAttributeToControllerModel() + { + // Arrange + var convention = new AltinnControllerConventions(); + var controllerType = typeof(TestController).GetTypeInfo(); + var controllerModel = new ControllerModel(controllerType, []); + + // Act + convention.Apply(controllerModel); + + // Assert + var attribute = controllerModel.Filters.OfType().FirstOrDefault(); + + Assert.NotNull(attribute); + Assert.Equal(JsonSettingNames.AltinnApi, attribute.Name); + } + + // Dummy controller + private class TestController : ControllerBase { } +}