Skip to content

Commit

Permalink
add maskinporten endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Oct 28, 2024
1 parent c464382 commit ebc811f
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/packagegroups/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
<PackageReference Update="Testcontainers.PostgreSql" Version="3.10.0" />
<PackageReference Update="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
<PackageReference Update="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
<PackageReference Update="WireMock.Net" Version="1.6.7" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion backend/src/Designer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
},
"FeatureManagement": {
"EidLogging": false,
"AnsattPorten": false
"AnsattPorten": true
},
"OidcLoginSettings": {
"Authority": "http://studio.localhost/repos/",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Altinn.Studio.Designer;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Options;
Expand All @@ -21,7 +22,7 @@ protected TestSchemeProvider(IOptions<AuthenticationOptions> options, IDictionar
public override Task<AuthenticationScheme> GetSchemeAsync(string name)
{
// Replace cookies scheme used in oidc setup with test scheme
if (name == CookieAuthenticationDefaults.AuthenticationScheme)
if (name is CookieAuthenticationDefaults.AuthenticationScheme or AnsattPortenConstants.AnsattportenAuthenticationScheme)
{
return base.GetSchemeAsync(TestAuthConstants.TestAuthenticationScheme);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Models.Dto;
using Designer.Tests.Controllers.AppScopesController.Base;
using Designer.Tests.Controllers.AppScopesController.Utils;
using Designer.Tests.Fixtures;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

namespace Designer.Tests.Controllers.AppScopesController;

public class GetScopesFromMaskinPortenTests : AppScopesControllerTestsBase<GetAppScopesTests>, IClassFixture<WebApplicationFactory<Program>>, IClassFixture<MockServerFixture>
{
private static string VersionPrefix(string org, string repository) =>
$"/designer/api/{org}/{repository}/app-scopes/maskinporten";

private readonly MockServerFixture _mockServerFixture;

public GetScopesFromMaskinPortenTests(WebApplicationFactory<Program> factory, DesignerDbFixture designerDbFixture, MockServerFixture mockServerFixture) : base(factory, designerDbFixture)
{
_mockServerFixture = mockServerFixture;
JsonConfigOverrides.Add(
$$"""
{
"MaskinPortenHttpClientSettings" : {
"BaseUrl": "{{mockServerFixture.MockApi.Url}}"
}
}
"""
);
}

[Theory]
[MemberData(nameof(TestData))]
public async Task GetScopesFromMaskinPortens_Should_ReturnOk(string org, string app, string maskinPortenResponse)
{
_mockServerFixture.PrepareMaskinPortenScopesResponse(maskinPortenResponse);
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get
, VersionPrefix(org, app));

using var response = await HttpClient.SendAsync(httpRequestMessage);
response.StatusCode.Should().Be(HttpStatusCode.OK);

AppScopesResponse repsponseContent = await response.Content.ReadAsAsync<AppScopesResponse>();
JsonArray array = (JsonArray)JsonNode.Parse(maskinPortenResponse);
repsponseContent.Scopes.Count.Should().Be(array.Count);
}


public static IEnumerable<object[]> TestData()
{
yield return ["ttd",
"non-existing-app",
$@"[
{{
""scope"": ""altinn:demo.torsdag"",
""state"": ""APPROVED"",
""created"": ""2024-10-24T08:40:23Z"",
""description"": ""Dette er en test"",
""active"": true,
""consumer_orgno"": ""310461598"",
""last_updated"": ""2024-10-24T08:40:23Z"",
""owner_orgno"": ""991825827"",
""allowed_integration_types"": [
""maskinporten""
]
}},
{{
""scope"": ""altinn:mirko.dan.test"",
""state"": ""APPROVED"",
""created"": ""2024-10-28T11:10:49Z"",
""description"": ""Dette er bare en test for Altinn Studio integrasjon"",
""active"": true,
""consumer_orgno"": ""310461598"",
""last_updated"": ""2024-10-28T11:10:49Z"",
""owner_orgno"": ""991825827"",
""allowed_integration_types"": [
""maskinporten""
]
}}
]"
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net.Mime;
using Designer.Tests.Fixtures;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;

namespace Designer.Tests.Controllers.AppScopesController.Utils;

public static class MaskinPortenMockServerExtensions
{
public static void PrepareMaskinPortenScopesResponse(this MockServerFixture mockServerFixture, string responseJson)
{
var request = Request.Create()
.UsingGet()
.WithPath("/datasharing/consumer/scope/access");

var response = Response.Create()
.WithStatusCode(200)
.WithHeader("content-type", MediaTypeNames.Application.Json)
.WithBody(responseJson);

mockServerFixture.MockApi.Given(request)
.RespondWith(
response
);

}

}
1 change: 1 addition & 0 deletions backend/tests/Designer.Tests/Designer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="Polly" />
<PackageReference Include="Testcontainers" />
<PackageReference Include="Testcontainers.PostgreSql" />
<PackageReference Include="WireMock.Net" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" />
Expand Down
23 changes: 23 additions & 0 deletions backend/tests/Designer.Tests/Fixtures/MockServerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using WireMock.Server;
using Xunit;

namespace Designer.Tests.Fixtures;

public class MockServerFixture : IAsyncLifetime
{
public WireMockServer MockApi;

public async Task InitializeAsync()
{
await Task.CompletedTask;
int availablePort = TestUrlsProvider.GetRandomAvailablePort();
MockApi = WireMockServer.Start(availablePort);
}

public async Task DisposeAsync()
{
await Task.CompletedTask;
MockApi?.Stop();
}
}
2 changes: 1 addition & 1 deletion backend/tests/Designer.Tests/Fixtures/TestUrlsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private TestUrlsProvider()

}

private static int GetRandomAvailablePort()
public static int GetRandomAvailablePort()
{
using var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
Expand Down

0 comments on commit ebc811f

Please sign in to comment.