Skip to content

Commit

Permalink
10469 should fetch actual footer layout if it exists (#12843)
Browse files Browse the repository at this point in the history
* Add endpoint in preview to get footer

* Add tests

* Fix PR comments

* Simplify Footer definition class
  • Loading branch information
standeren authored Jun 6, 2024
1 parent 6258b3b commit 06facb5
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 26 deletions.
16 changes: 14 additions & 2 deletions backend/src/Designer/Controllers/PreviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -933,12 +933,24 @@ public IActionResult UpdateAttachmentWithTag(string org, string app, [FromQuery]
/// </summary>
/// <param name="org">Unique identifier of the organisation responsible for the app.</param>
/// <param name="app">Application identifier which is unique within an organisation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>Empty response</returns>
[HttpGet]
[Route("api/v1/footer")]
public IActionResult Footer(string org, string app)
public async Task<ActionResult<FooterFile>> Footer(string org, string app, CancellationToken cancellationToken)
{
return Ok();
try
{
string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
AltinnAppGitRepository altinnAppGitRepository =
_altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
FooterFile footerFile = await altinnAppGitRepository.GetFooter(cancellationToken);
return Ok(footerFile);
}
catch (FileNotFoundException)
{
return Ok();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class AltinnAppGitRepository : AltinnGitRepository
private const string LayoutSettingsFilename = "Settings.json";
private const string AppMetadataFilename = "applicationmetadata.json";
private const string LayoutSetsFilename = "layout-sets.json";
private const string FooterFilename = "footer.json";
private const string RuleHandlerFilename = "RuleHandler.js";
private const string RuleConfigurationFilename = "RuleConfiguration.json";
private const string ProcessDefinitionFilename = "process.bpmn";
Expand Down Expand Up @@ -598,6 +599,15 @@ public async Task SaveLayoutSets(LayoutSets layoutSets)
}
}

public async Task<FooterFile> GetFooter(CancellationToken cancellationToken = default)
{
string footerFilePath = GetPathToFooterFile();
cancellationToken.ThrowIfCancellationRequested();
string fileContent = await ReadTextByRelativePathAsync(footerFilePath, cancellationToken);
FooterFile footerFile = JsonSerializer.Deserialize<FooterFile>(fileContent, JsonOptions);
return footerFile;
}

/// <summary>
/// Saves the RuleHandler.js for a specific layout set
/// </summary>
Expand Down Expand Up @@ -838,6 +848,11 @@ private static string GetPathToLayoutSetsFile()
return Path.Combine(LayoutsFolderName, LayoutSetsFilename);
}

private static string GetPathToFooterFile()
{
return Path.Combine(LayoutsFolderName, FooterFilename);
}

private static string GetPathToRuleHandler(string layoutSetName)
{
return layoutSetName.IsNullOrEmpty() ?
Expand Down
46 changes: 46 additions & 0 deletions backend/src/Designer/Models/FooterFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using JetBrains.Annotations;

namespace Altinn.Studio.Designer.Models;
public class FooterFile
{
[JsonPropertyName("$schema")]
public string Schema { get; set; }

[JsonPropertyName("footer")]
public List<FooterLayout> Footer { get; set; }
}

public class FooterLayout
{
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public ComponentType Type { get; set; }

[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("target")]
[CanBeNull]
public string Target { get; set; }

[JsonPropertyName("icon")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public IconType? Icon { get; set; }
}

public enum ComponentType
{
Email,
Link,
Phone,
Text
}

public enum IconType
{
Information,
Email,
Phone
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ protected override void ConfigureTestServices(IServiceCollection services)
[Fact]
public async Task Get_ApplicationMetadata_Ok()
{
string expectedApplicationMetadataString = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/config/applicationmetadata.json");
string expectedApplicationMetadataString = TestDataHelper.GetFileFromRepo(Org, PreviewApp, Developer, "App/config/applicationmetadata.json");
_appDevelopmentServiceMock
.Setup(rs => rs.GetAppLibVersion(It.IsAny<AltinnRepoEditingContext>()))
.Returns(NuGet.Versioning.NuGetVersion.Parse("1.0.0"));

string dataPathWithData = $"{Org}/{AppV3}/api/v1/applicationmetadata";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/applicationmetadata";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand All @@ -60,12 +60,12 @@ public async Task Get_ApplicationMetadata_Ok()
[Fact]
public async Task Get_ApplicationMetadata_With_V8_Altinn_Nuget_Version_Ok()
{
string expectedApplicationMetadataString = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/config/applicationmetadata.json");
string expectedApplicationMetadataString = TestDataHelper.GetFileFromRepo(Org, PreviewApp, Developer, "App/config/applicationmetadata.json");
_appDevelopmentServiceMock
.Setup(rs => rs.GetAppLibVersion(It.IsAny<AltinnRepoEditingContext>()))
.Returns(NuGet.Versioning.NuGetVersion.Parse("8.0.0"));

string dataPathWithData = $"{Org}/{AppV3}/api/v1/applicationmetadata";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/applicationmetadata";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ApplicationSettingsTests(WebApplicationFactory<Program> factory) : base(f
[Fact]
public async Task Get_ApplicationSettings_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/applicationsettings";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/applicationsettings";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public DatamodelTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_Datamodel_Ok()
{
string expectedDatamodel = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/models/custom-dm-name.schema.json");
string expectedDatamodel = TestDataHelper.GetFileFromRepo(Org, PreviewApp, Developer, "App/models/custom-dm-name.schema.json");

string dataPathWithData = $"{Org}/{AppV3}/api/jsonschema/custom-dm-name";
string dataPathWithData = $"{Org}/{PreviewApp}/api/jsonschema/custom-dm-name";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public GetApplicationLanguagesTests(WebApplicationFactory<Program> factory) : ba
[Fact]
public async Task Get_ApplicationLanguages_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/applicationlanguages";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/applicationlanguages";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Models;
using Designer.Tests.Utils;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using SharedResources.Tests;
using Xunit;

namespace Designer.Tests.Controllers.PreviewController
{
public class GetFooterTests : PreviewControllerTestsBase<GetFooterTests>, IClassFixture<WebApplicationFactory<Program>>
{
public GetFooterTests(WebApplicationFactory<Program> factory) : base(factory)
{
}

[Fact]
public async Task Get_Footer_Exists_Ok()
{
string expectedFooter = TestDataHelper.GetFileFromRepo(Org, AppV4, Developer, "App/ui/footer.json");
FooterFile actualFooterFile = JsonSerializer.Deserialize<FooterFile>(expectedFooter);

string dataPathWithData = $"{Org}/{AppV4}/api/v1/footer";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string responseString = await response.Content.ReadAsStringAsync();
FooterFile responseFooterFile = JsonSerializer.Deserialize<FooterFile>(responseString);

responseFooterFile.Footer.Should().BeEquivalentTo(actualFooterFile.Footer);
}

[Fact]
public async Task Get_Footer_Non_Existing_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/footer";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

string responseString = await response.Content.ReadAsStringAsync();
responseString.Should().BeEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public GetFormDataTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_FormData_Ok()
{
string expectedFormData = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/models/custom-dm-name.schema.json");
string expectedFormData = TestDataHelper.GetFileFromRepo(Org, PreviewApp, Developer, "App/models/custom-dm-name.schema.json");

string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{InstanceGuId}/data/test-datatask-id";
string dataPathWithData = $"{Org}/{PreviewApp}/instances/{PartyId}/{InstanceGuId}/data/test-datatask-id";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3}&selectedLayoutSet=");
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={PreviewApp}&selectedLayoutSet=");

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public GetFormLayoutsTests(WebApplicationFactory<Program> factory) : base(factor
[Fact]
public async Task Get_FormLayouts_Ok()
{
string expectedFormLayout = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/ui/layouts/layout.json");
string expectedFormLayout = TestDataHelper.GetFileFromRepo(Org, PreviewApp, Developer, "App/ui/layouts/layout.json");
string expectedFormLayouts = @"{""layout"": " + expectedFormLayout + "}";

string dataPathWithData = $"{Org}/{AppV3}/api/resource/FormLayout.json";
string dataPathWithData = $"{Org}/{PreviewApp}/api/resource/FormLayout.json";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public GetImageTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_Image_From_Wwww_Root_Folder_Ok()
{
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, AppV3, Developer, "App/wwwroot/AltinnD-logo.svg");
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, PreviewApp, Developer, "App/wwwroot/AltinnD-logo.svg");

string dataPathWithData = $"{Org}/{AppV3}/AltinnD-logo.svg";
string dataPathWithData = $"{Org}/{PreviewApp}/AltinnD-logo.svg";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand All @@ -35,9 +35,9 @@ public async Task Get_Image_From_Wwww_Root_Folder_Ok()
[Fact]
public async Task Get_Image_From_Sub_Folder_Ok()
{
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, AppV3, Developer, "App/wwwroot/images/AltinnD-logo.svg");
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, PreviewApp, Developer, "App/wwwroot/images/AltinnD-logo.svg");

string dataPathWithData = $"{Org}/{AppV3}/images/AltinnD-logo.svg";
string dataPathWithData = $"{Org}/{PreviewApp}/images/AltinnD-logo.svg";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand All @@ -50,9 +50,9 @@ public async Task Get_Image_From_Sub_Folder_Ok()
[Fact]
public async Task Get_Image_From_Sub_Sub_Folder_Ok()
{
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, AppV3, Developer, "App/wwwroot/images/subImagesFolder/AltinnD-logo.svg");
byte[] expectedImageData = TestDataHelper.GetFileAsByteArrayFromRepo(Org, PreviewApp, Developer, "App/wwwroot/images/subImagesFolder/AltinnD-logo.svg");

string dataPathWithData = $"{Org}/{AppV3}/images/subImagesFolder/AltinnD-logo.svg";
string dataPathWithData = $"{Org}/{PreviewApp}/images/subImagesFolder/AltinnD-logo.svg";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public GetOptionsTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_Options_when_options_exists_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/options/test-options";
string dataPathWithData = $"{Org}/{PreviewApp}/api/options/test-options";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task Get_RuleConfiguration_Ok()
[Fact]
public async Task Get_RuleConfiguration_NoContent()
{
string dataPathWithData = $"{Org}/{AppV3}/api/resource/RuleConfiguration.json";
string dataPathWithData = $"{Org}/{PreviewApp}/api/resource/RuleConfiguration.json";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public LanguageTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_Text_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/texts/nb";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/texts/nb";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public class PreviewControllerTestsBase<TTestClass> : DisagnerEndpointsTestsBase
where TTestClass : class
{
protected const string Org = "ttd";
protected const string AppV3 = "preview-app";
protected const string AppV3 = "app-without-layoutsets";
protected const string AppV4 = "app-with-layoutsets";
protected const string PreviewApp = "preview-app";
protected const string Developer = "testUser";
protected const string LayoutSetName = "layoutSet1";
protected const string LayoutSetName2 = "layoutSet2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public TextResourcesTests(WebApplicationFactory<Program> factory) : base(factory
[Fact]
public async Task Get_TextResources_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/textresources";
string dataPathWithData = $"{Org}/{PreviewApp}/api/v1/textresources";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/layout/footer.schema.v1.json",
"footer": [
{
"type": "Text",
"title": "**Frontend Test**<br>*Testdepartementet*"
},
{
"type": "Link",
"icon": "information",
"title": "general.accessibility",
"target": "general.accessibility_url"
},
{
"type": "Email",
"title": "hjelp@etaten.no",
"target": "hjelp@etaten.no"
},
{
"type": "Phone",
"title": "+47 987 65 432",
"target": "+4798765432"
}
]
}

0 comments on commit 06facb5

Please sign in to comment.