Skip to content

Commit

Permalink
merge main into feature
Browse files Browse the repository at this point in the history
  • Loading branch information
framitdavid committed Nov 21, 2024
2 parents 6d74368 + 7253c5d commit 866240e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 16 deletions.
1 change: 0 additions & 1 deletion backend/src/Designer/Controllers/AppScopesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Altinn.Studio.Designer.Controllers;
[ApiController]
[FeatureGate(StudioFeatureFlags.AnsattPorten)]
[Route("designer/api/{org}/{app:regex(^(?!datamodels$)[[a-z]][[a-z0-9-]]{{1,28}}[[a-z0-9]]$)}/app-scopes")]

public class AppScopesController(IMaskinPortenHttpClient maskinPortenHttpClient,
IAppScopesService appScopesService) : ControllerBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Models.Dto;
using Designer.Tests.Controllers.ApiTests;
using FluentAssertions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Designer.Tests.Controllers.AnsattPortenController;

public class AuthStatusTest : DesignerEndpointsTestsBase<AuthStatusTest>, IClassFixture<WebApplicationFactory<Program>>
{
private static string VersionPrefix => "/designer/api/ansattporten/auth-status";

// Setup unauthenticated http client
protected override HttpClient GetTestClient()
{
string configPath = GetConfigPath();
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile(configPath, false, false)
.AddJsonStream(GenerateJsonOverrideConfig())
.AddEnvironmentVariables()
.Build();

return Factory.WithWebHostBuilder(builder =>
{
builder.UseConfiguration(configuration);
builder.ConfigureAppConfiguration((_, conf) =>
{
conf.AddJsonFile(configPath);
conf.AddJsonStream(GenerateJsonOverrideConfig());
});
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureServices(ConfigureTestServicesForSpecificTest);
}).CreateDefaultClient(new ApiTestsAuthAndCookieDelegatingHandler(), new CookieContainerHandler());

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning test

Disposable 'CookieContainerHandler' is created but not disposed.
}

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

[Fact]
public async Task AuthStatus_Should_ReturnFalse_IfNotAuthenticated()
{
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, VersionPrefix);

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

AuthStatus authStatus = await response.Content.ReadAsAsync<AuthStatus>();
authStatus.IsLoggedIn.Should().BeFalse();
}

[Fact]
public async Task AuthStatus_Should_ReturnTrue_IfAuthenticated()
{
// Setup test authentication
ConfigureTestServicesForSpecificTest = services =>
{
services.AddAuthentication(defaultScheme: TestAuthConstants.TestAuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
TestAuthConstants.TestAuthenticationScheme, options => { });
services.AddTransient<IAuthenticationSchemeProvider, TestSchemeProvider>();
};

using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, VersionPrefix);

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

AuthStatus authStatus = await response.Content.ReadAsAsync<AuthStatus>();
authStatus.IsLoggedIn.Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Constants;
using Designer.Tests.Controllers.ApiTests;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

namespace Designer.Tests.Controllers.AnsattPortenController;

public class LoginTests : DesignerEndpointsTestsBase<LoginTests>, IClassFixture<WebApplicationFactory<Program>>
{
private static string VersionPrefix => "/designer/api/ansattporten/login";

public LoginTests(WebApplicationFactory<Program> factory) : base(factory)
{
JsonConfigOverrides.Add(
$$"""
{
"FeatureManagement": {
"{{StudioFeatureFlags.AnsattPorten}}": true
},
"AnsattPortenLoginSettings": {
"ClientId": "non-empty-for-testing",
"ClientSecret": "non-empty-for-testing"
}
}
""");
}

[Theory]
[InlineData("/test", HttpStatusCode.Redirect)]
[InlineData("/", HttpStatusCode.Redirect)]
[InlineData("https://docs.altinn.studio/", HttpStatusCode.Forbidden)]
public async Task LoginShouldReturn_ExpectedCode(string redirectTo, HttpStatusCode expectedStatusCode)
{
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get
, $"{VersionPrefix}?redirect_to={redirectTo}");

using var response = await HttpClient.SendAsync(httpRequestMessage);
Assert.Equal(expectedStatusCode, response.StatusCode);

if (expectedStatusCode == HttpStatusCode.Redirect)
{
Assert.Equal(redirectTo, response.Headers.Location?.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected HttpClient HttpClient
/// </summary>
protected abstract void ConfigureTestServices(IServiceCollection services);

protected Action<IServiceCollection> ConfigureTestForSpecificTest { get; set; } = delegate { };
protected Action<IServiceCollection> ConfigureTestServicesForSpecificTest { get; set; } = delegate { };

/// <summary>
/// Location of the assembly of the executing unit test.
Expand Down Expand Up @@ -97,7 +97,7 @@ protected virtual HttpClient GetTestClient()
TestAuthConstants.TestAuthenticationScheme, options => { });
services.AddTransient<IAuthenticationSchemeProvider, TestSchemeProvider>();
});
builder.ConfigureServices(ConfigureTestForSpecificTest);
builder.ConfigureServices(ConfigureTestServicesForSpecificTest);
}).CreateDefaultClient(new ApiTestsAuthAndCookieDelegatingHandler(), new CookieContainerHandler());
}

Expand Down Expand Up @@ -152,7 +152,7 @@ private void InitializeJsonConfigOverrides()
}


private Stream GenerateJsonOverrideConfig()
protected Stream GenerateJsonOverrideConfig()
{
var overrideJson = Newtonsoft.Json.Linq.JObject.Parse(JsonConfigOverrides.First());
if (JsonConfigOverrides.Count > 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Altinn.Studio.Designer.Constants;
using Designer.Tests.Controllers.ApiTests;
using Designer.Tests.Fixtures;
using Microsoft.AspNetCore.Mvc.Testing;
Expand All @@ -9,16 +10,17 @@ public class AppScopesControllerTestsBase<TControllerTest> : DbDesignerEndpoints
{
public AppScopesControllerTestsBase(WebApplicationFactory<Program> factory, DesignerDbFixture designerDbFixture) : base(factory, designerDbFixture)
{
JsonConfigOverrides.Add($@"
{{
""FeatureManagement"": {{
""AnsattPorten"": true
}},
""AnsattPortenLoginSettings"": {{
""ClientId"": ""non-empty-for-testing"",
""ClientSecret"": ""non-empty-for-testing""
}}
}}
");
JsonConfigOverrides.Add(
$$"""
{
"FeatureManagement": {
"{{StudioFeatureFlags.AnsattPorten}}": true
},
"AnsattPortenLoginSettings": {
"ClientId": "non-empty-for-testing",
"ClientSecret": "non-empty-for-testing"
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task Get_Image_Non_Existing_Image_Return_NotFound()
public async Task Call_To_Get_Designer_Iframe_Does_Not_Hit_Image_EndPoint()
{
Mock<IAltinnGitRepositoryFactory> factMock = new();
ConfigureTestForSpecificTest = s =>
ConfigureTestServicesForSpecificTest = s =>
{
s.AddTransient(_ => factMock.Object);
};
Expand Down

0 comments on commit 866240e

Please sign in to comment.