Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webAPI): Add legacy HTML support for MainContentReference #1256

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
Expand Down Expand Up @@ -68,22 +69,15 @@ x.MediaType is not null
.SetValidator(_ => new LocalizationDtosValidator(contentType.MaxLength));
}

[SuppressMessage("Style", "IDE0072:Add missing cases")]
private static string[] GetAllowedMediaTypes(DialogContentType contentType, IUser? user)
{
if (user == null)
{
return contentType.AllowedMediaTypes;
}

if (contentType.Id != DialogContentType.Values.AdditionalInfo)
=> contentType.Id switch
{
return contentType.AllowedMediaTypes;
}

var allowHtmlSupport = user.GetPrincipal().HasScope(Constants.LegacyHtmlScope);

return allowHtmlSupport
? contentType.AllowedMediaTypes.Append(LegacyHtmlMediaType).ToArray()
: contentType.AllowedMediaTypes;
}
DialogContentType.Values.AdditionalInfo when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(LegacyHtmlMediaType).ToArray(),
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
DialogContentType.Values.MainContentReference when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyEmbeddableHtml).ToArray(),
_ => contentType.AllowedMediaTypes
};
private static bool UserHasLegacyHtmlScope(IUser? user) => user is not null && user.GetPrincipal().HasScope(Constants.LegacyHtmlScope);
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/Digdir.Domain.Dialogporten.Domain/MediaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public static class MediaTypes
{
public const string EmbeddablePrefix = "application/vnd.dialogporten.frontchannelembed";
public const string EmbeddableMarkdown = $"{EmbeddablePrefix}+json;type=markdown";
public const string LegacyEmbeddableHtml = $"{EmbeddablePrefix}+json;type=html";

public const string Markdown = "text/markdown";
public const string PlainText = "text/plain";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Queries.Get;
using Digdir.Domain.Dialogporten.Application.Integration.Tests.Common;
using Digdir.Domain.Dialogporten.Domain;
using Digdir.Tool.Dialogporten.GenerateFakeData;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -350,4 +351,62 @@ public async Task Cannot_Create_Title_Content_With_Html_MediaType_With_Correct_S
.Should()
.Be(1);
}

[Fact]
public async Task Cannot_Create_Title_Content_With_Embeddable_Html_MediaType_With_Correct_Scope()
{
// Arrange
var createDialogCommand = DialogGenerator.GenerateSimpleFakeDialog();
createDialogCommand.Content.Title = new ContentValueDto
{
MediaType = MediaTypes.LegacyEmbeddableHtml,
Value = [new LocalizationDto { LanguageCode = "en", Value = "https://external.html" }]
};

var userWithLegacyScope = new IntegrationTestUser([new("scope", Constants.LegacyHtmlScope)]);
Application.ConfigureServiceCollection(services =>
{
services.RemoveAll<IUser>();
services.AddSingleton<IUser>(userWithLegacyScope);
});

// Act
var response = await Application.Send(createDialogCommand);

// Assert
response.TryPickT2(out var validationError, out _).Should().BeTrue();
validationError.Should().NotBeNull();
validationError.Errors
.Count(e => e.AttemptedValue.Equals(MediaTypes.LegacyEmbeddableHtml))
.Should()
.Be(1);
}

[Fact]
public async Task Can_Create_MainContentRef_Content_With_Embeddable_Html_MediaType_With_Correct_Scope()
{
// Arrange
var expectedDialogId = GenerateBigEndianUuidV7();
var createDialogCommand = DialogGenerator.GenerateSimpleFakeDialog(id: expectedDialogId);
createDialogCommand.Content.MainContentReference = new ContentValueDto
{
MediaType = MediaTypes.LegacyEmbeddableHtml,
Value = [new LocalizationDto { LanguageCode = "en", Value = "https://external.html" }]
};

var userWithLegacyScope = new IntegrationTestUser([new("scope", Constants.LegacyHtmlScope)]);
Application.ConfigureServiceCollection(services =>
{
services.RemoveAll<IUser>();
services.AddSingleton<IUser>(userWithLegacyScope);
});

// Act
var response = await Application.Send(createDialogCommand);

// Assert
response.TryPickT0(out var success, out _).Should().BeTrue();
success.Should().NotBeNull();
success.Value.Should().Be(expectedDialogId);
}
}
Loading