-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Ansattporten controller
- Loading branch information
1 parent
383c4b2
commit 7253c5d
Showing
7 changed files
with
146 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
backend/tests/Designer.Tests/Controllers/AnsattPortenController/AuthStatusTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/tests/Designer.Tests/Controllers/AnsattPortenController/LoginTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters