Skip to content

Commit

Permalink
Redo tests to test v3 and v4 more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondyr committed Dec 2, 2024
1 parent b741827 commit 5b7a457
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task<ActionResult> UpdateProcessNext(
CancellationToken cancellationToken
)
{
string refererHeader = Request.Headers["Referer"];
string refererHeader = Request.Headers.Referer;
Uri refererUri = new(refererHeader);
string layoutSetName = HttpUtility.ParseQueryString(refererUri.Query)["selectedLayoutSet"];
layoutSetName = string.IsNullOrEmpty(layoutSetName) ? null : layoutSetName;
Expand Down
20 changes: 19 additions & 1 deletion backend/src/Designer/Helpers/AppFrontendVersionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using HtmlAgilityPack;

namespace Altinn.Studio.Designer.Helpers;

Expand All @@ -9,13 +10,30 @@ public static class AppFrontendVersionHelper
private const string SemanticVersionRegex = @"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$";
private const string ExtendedVersion = @"^(\d+)(\.\d+)?$";

// allow overwriting altinn-app-frontend version with a meta tag
// i.e. <meta data-altinn-app-frontend-version="4" />
private static string getMetaTagVersion(HtmlDocument htmlDoc)
{
HtmlNode metaTag = htmlDoc.DocumentNode.SelectSingleNode("//meta[@data-altinn-app-frontend-version]");
return metaTag?.GetAttributeValue("data-altinn-app-frontend-version", null);
}

public static bool TryGetFrontendVersionFromIndexFile(string filePath, out string version)
{
version = null;

string fileContent = File.ReadAllText(filePath);
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(fileContent);

string metaTagVersion = getMetaTagVersion(htmlDoc);
System.Console.WriteLine(metaTagVersion);
if (metaTagVersion != null)
{
version = metaTagVersion;
return true;
}

var scriptTag = htmlDoc.DocumentNode.SelectSingleNode(
"//script[contains(@src, 'https://altinncdn.no/toolkits/altinn-app-frontend') and contains(@src, 'altinn-app-frontend.js')]");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -17,8 +18,9 @@ public AnonymousTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_Anonymous_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/data/anonymous";
string dataPathWithData = $"{Org}/{AppV4}/api/v1/data/anonymous";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -19,8 +20,9 @@ public CurrentPartyTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_CurrentParty_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/authorization/parties/current";
string dataPathWithData = $"{Org}/{AppV4}/api/authorization/parties/current";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -19,8 +20,9 @@ public CurrentUserTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_CurrentUser_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/profile/user";
string dataPathWithData = $"{Org}/{AppV4}/api/v1/profile/user";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Altinn.Platform.Storage.Interface.Models;
Expand All @@ -17,10 +18,9 @@ public DeleteAttachmentTests(WebApplicationFactory<Program> factory) : base(fact
[Fact]
public async Task Delete_Attachment_Ok()
{
Instance instance = await createInstance();
DataElement dataElement = await createDataElement(instance, "attachment");
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}/data/{dataElement.Id}";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{V3InstanceId}/data/asdf";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Delete, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3Path}&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
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -35,18 +36,5 @@ public async Task Get_Footer_Exists_Ok()

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 @@ -65,7 +65,7 @@ public async Task Get_Image_From_Sub_Sub_Folder_Ok()
[Fact]
public async Task Get_Image_Non_Existing_Folder_Returns_NotFound()
{
string dataPathWithData = $"{Org}/{AppV3}/images/subImagesFolder/SubSubImageFolder/AltinnD-logo.svg";
string dataPathWithData = $"{Org}/{AppV3Path}/images/subImagesFolder/SubSubImageFolder/AltinnD-logo.svg";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Expand All @@ -74,7 +74,7 @@ public async Task Get_Image_Non_Existing_Folder_Returns_NotFound()
[Fact]
public async Task Get_Image_Non_Existing_Image_Return_NotFound()
{
string dataPathWithData = $"{Org}/{AppV3}/images/subImagesFolder/non-existing-image.svg";
string dataPathWithData = $"{Org}/{AppV3Path}/images/subImagesFolder/non-existing-image.svg";

using HttpResponseMessage response = await HttpClient.GetAsync(dataPathWithData);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -47,8 +48,9 @@ public async Task Get_Options_when_options_exists_for_v4_app_Ok()
[Fact]
public async Task Get_Options_when_no_options_exist_returns_Ok_empty_list()
{
string dataPathWithData = $"{Org}/{AppV3}/api/options/non-existing-options";
string dataPathWithData = $"{Org}/{AppV4}/api/options/non-existing-options";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
Expand All @@ -16,8 +17,9 @@ public GetRuleHandlerTests(WebApplicationFactory<Program> factory) : base(factor
[Fact]
public async Task Get_RuleHandler_NoContent()
{
string dataPathWithData = $"{Org}/{AppV3}/api/resource/RuleHandler.js";
string dataPathWithData = $"{Org}/{AppV4}/api/resource/RuleHandler.js";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&selectedLayoutSet=");

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@

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

public InstanceForNextTaskTests(WebApplicationFactory<Program> factory) : base(factory)
{
}

[Fact]
public async Task Get_InstanceForNextProcess_Ok()
{
Instance instance = await createInstance();
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{V3InstanceId}";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3Path}&selectedLayoutSet=");

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

string responseBody = await response.Content.ReadAsStringAsync();
JsonDocument responseDocument = JsonDocument.Parse(responseBody);
Instance responseInstance = JsonConvert.DeserializeObject<Instance>(responseDocument.RootElement.ToString());
Assert.Equal(instance.Id, responseInstance.Id);
Assert.Equal(PartyId + "/" + V3InstanceId, responseInstance.Id);
Assert.Equal(Org, responseInstance.Org);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
Expand All @@ -16,8 +17,9 @@ public KeepAliveTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_KeepAlive_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/authentication/keepAlive";
string dataPathWithData = $"{Org}/{AppV4}/api/authentication/keepAlive";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV4}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
Expand All @@ -15,8 +16,9 @@ public LayoutSetsTests(WebApplicationFactory<Program> factory) : base(factory)
[Fact]
public async Task Get_LayoutSets_NotFound()
{
string dataPathWithData = $"{Org}/{AppV3}/api/layoutsets";
string dataPathWithData = $"{Org}/{AppV4}/api/layoutsets";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3Path}&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
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Designer.Tests.Utils;
Expand All @@ -9,19 +10,18 @@

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

[Fact]
public async Task Get_LayoutSettings_Ok()
{
string expectedLayoutSettings = TestDataHelper.GetFileFromRepo(Org, AppV3, Developer, "App/ui/Settings.json");

string dataPathWithData = $"{Org}/{AppV3}/api/layoutsettings";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3}&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 @@ -20,7 +20,7 @@ public PostAttachmentTests(WebApplicationFactory<Program> factory) : base(factor
public async Task Post_Attachment_Ok()
{
Instance instance = await createInstance();
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}/data?dataType=FileUploadId";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{instance.Id}/data?dataType=FileUploadId";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Post, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ public class PreviewControllerTestsBase<TTestClass>(WebApplicationFactory<Progra
where TTestClass : class
{
protected const string Org = "ttd";
protected const string AppV3 = "app-without-layoutsets/V3";
protected const string AppV3 = "app-without-layoutsets";
protected const string AppV3Path = "app-without-layoutsets/V3";
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";
protected const string PartyId = "51001";
protected const string V3InstanceId = "f1e23d45-6789-1bcd-8c34-56789abcdef0";
protected const string TaskId = "Task_1";
protected const string AttachmentGuId = "f47ac10b-58cc-4372-a567-0e02b2c3d479";
protected const string MockedReferrerUrl = "https://studio-mock-url.no";

protected override void ConfigureTestServices(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@

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

public ProcessNextTests(WebApplicationFactory<Program> factory) : base(factory)
{
}

[Fact]
public async Task Get_ProcessNext_Ok()
{
Instance instance = await createInstance();
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}/process/next";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{V3InstanceId}/process/next";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3Path}&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,7 +19,7 @@ public UpdateFormDataTests(WebApplicationFactory<Program> factory) : base(factor
public async Task Put_UpdateFormData_Ok()
{
Instance instance = await createInstance();
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}/data/test-datatask-id";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{instance.Id}/data/test-datatask-id";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Put, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public UpdateProcessNextTests(WebApplicationFactory<Program> factory) : base(fac
public async Task Put_ProcessNext_Ok()
{
Instance instance = await createInstance();
string dataPathWithData = $"{Org}/{AppV3}/instances/{PartyId}/{instance.Id}/process/next";
string dataPathWithData = $"{Org}/{AppV3Path}/instances/{PartyId}/{instance.Id}/process/next";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Put, dataPathWithData);
httpRequestMessage.Headers.Referrer = new Uri($"{MockedReferrerUrl}?org={Org}&app={AppV3Path}&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 @@ -18,7 +18,7 @@ public ValidateInstantiationTests(WebApplicationFactory<Program> factory) : base
[Fact]
public async Task Post_ValidateInstantiation_Ok()
{
string dataPathWithData = $"{Org}/{AppV3}/api/v1/parties/validateInstantiation";
string dataPathWithData = $"{Org}/{AppV4}/api/v1/parties/validateInstantiation";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Post, dataPathWithData);

using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);
Expand Down

0 comments on commit 5b7a457

Please sign in to comment.