Table of Contents

Class AuthenticationTests

Namespace
ProjectTemplate.Web.Tests
Assembly
ProjectTemplate.Web.Tests.dll

Provides integration tests for the application authentication configuration and baseline cookie authentication behavior.

public sealed class AuthenticationTests
Inheritance
AuthenticationTests
Inherited Members

Properties

EnabledExternalProviderConfigurations

Test data for enabled external authentication provider configurations. Each entry includes the provider key, display name, and a set of configuration values required to enable the provider. This data is used to verify that the application correctly binds and validates external authentication provider settings from configuration. The test cases cover various providers such as OpenIdConnect, SAML2, Google, and GitHub, ensuring that each provider's specific configuration requirements are met when enabled.

public static TheoryData<string, string, string> EnabledExternalProviderConfigurations { get; }

Property Value

TheoryData<string, string, string>

Methods

AuthenticationOptions_AreBoundFromConfiguration()

Verifies that authentication options are bound from configuration into the options model.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 35)]
public void AuthenticationOptions_AreBoundFromConfiguration()

AuthenticationOptions_MatchGeneratedConfiguration()

Verifies that the authentication options match the expected values based on the provided configuration.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 121)]
public void AuthenticationOptions_MatchGeneratedConfiguration()

Authentication_EmptyCookieScheme_FailsStartup()

Verifies that startup validation fails when authentication is enabled and the cookie scheme is empty.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 339)]
public void Authentication_EmptyCookieScheme_FailsStartup()

Authentication_ZeroCookieExpiration_FailsStartup()

Verifies that startup validation fails when authentication is enabled and cookie expiration is zero.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 363)]
public void Authentication_ZeroCookieExpiration_FailsStartup()

DisabledAuthentication_AllowsAnonymousEndpoint()

Verifies that anonymous endpoints remain accessible when authentication is disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 150)]
public Task DisabledAuthentication_AllowsAnonymousEndpoint()

Returns

Task

A task that represents the asynchronous test operation.

DisabledGitHubProvider_DoesNotRegisterGitHubScheme()

Verifies that the GitHub authentication provider does not register a scheme when disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 678)]
public Task DisabledGitHubProvider_DoesNotRegisterGitHubScheme()

Returns

Task

A task that represents the asynchronous test operation.

DisabledGoogleProvider_DoesNotRegisterGoogleScheme()

Verifies that the Google authentication provider does not register a scheme when disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 579)]
public Task DisabledGoogleProvider_DoesNotRegisterGoogleScheme()

Returns

Task

A task that represents the asynchronous test operation.

DisabledMicrosoftProvider_DoesNotRegisterScheme()

Verifies that the Microsoft authentication provider does not register a scheme when disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 523)]
public Task DisabledMicrosoftProvider_DoesNotRegisterScheme()

Returns

Task

A task that represents the asynchronous test operation.

DisabledOpenIdConnectProvider_DoesNotRegisterScheme()

Verifies that the OpenIdConnect provider does not register a scheme when disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 411)]
public Task DisabledOpenIdConnectProvider_DoesNotRegisterScheme()

Returns

Task

A task that represents the asynchronous test operation.

DisabledOpenIdConnectProvider_DoesNotRequireConfiguration()

Verifies that the OpenIdConnect authentication provider does not require configuration when it is disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 390)]
public void DisabledOpenIdConnectProvider_DoesNotRequireConfiguration()

Remarks

This test ensures that when the OpenIdConnect provider is explicitly disabled in the configuration, missing or empty configuration values for authority and client ID do not cause errors. Use this test to validate that the application can start without OpenIdConnect settings when the provider is not enabled.

DisabledSaml2Provider_DoesNotRegisterScheme()

Verifies that the SAML2 provider does not register a scheme when disabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 469)]
public Task DisabledSaml2Provider_DoesNotRegisterScheme()

Returns

Task

A task that represents the asynchronous test operation.

EnabledAuthentication_RegistersCookieScheme()

Verifies that cookie authentication is registered when application authentication is enabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 171)]
public Task EnabledAuthentication_RegistersCookieScheme()

Returns

Task

A task that represents the asynchronous test operation.

EnabledExternalAuthenticationProvider_RegistersConfiguredScheme(string, string, string)

Verifies that an enabled external authentication provider registers the expected authentication scheme with the correct display name. This test uses parameterized data to validate multiple providers, ensuring that when a provider is enabled in the configuration, it correctly adds its authentication scheme to the application's authentication system. The test checks both the presence of the scheme and that its properties match the expected values defined in the configuration.

[Theory("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 205)]
[MemberData("EnabledExternalProviderConfigurations", new object?[] { })]
public Task EnabledExternalAuthenticationProvider_RegistersConfiguredScheme(string providerName, string schemeName, string displayName)

Parameters

providerName string

Name of the external authentication provider being tested (e.g., "OpenIdConnect", "Saml2", "Google", "GitHub"). This parameter is used to generate the appropriate configuration for the provider and to verify that the corresponding authentication scheme is registered correctly in the application's authentication system.

schemeName string

Name of the authentication scheme that should be registered for the enabled provider. This value is derived from the provider's configuration and is used to retrieve the scheme from the authentication scheme provider during the test. The test asserts that a scheme with this name exists and has the expected properties when the provider is enabled.

displayName string

Name of the display name that should be associated with the registered authentication scheme for the enabled provider. This value is defined in the provider's configuration and is used to verify that the scheme's display name matches the expected value when the provider is enabled. The test asserts that the display name of the registered scheme is correct, ensuring that the configuration is properly applied to the authentication system.

Returns

Task

EnabledGitHubProvider_MissingClientSecret_FailsStartup()

Verifies that application startup fails with an options validation exception when the GitHub authentication provider is enabled but the client secret is missing.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 747)]
public void EnabledGitHubProvider_MissingClientSecret_FailsStartup()

EnabledGitHubProvider_RegistersGitHubScheme()

Verifies that the GitHub authentication provider registers a scheme when enabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 709)]
public Task EnabledGitHubProvider_RegistersGitHubScheme()

Returns

Task

A task that represents the asynchronous test operation.

EnabledGoogleProvider_MissingClientSecret_FailsStartup()

Verifies that application startup fails with an options validation exception when the Google authentication provider is enabled but the client secret is missing.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 648)]
public void EnabledGoogleProvider_MissingClientSecret_FailsStartup()

EnabledGoogleProvider_RegistersGoogleScheme()

Verifies that the Google authentication provider registers a scheme when enabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 610)]
public Task EnabledGoogleProvider_RegistersGoogleScheme()

Returns

Task

A task that represents the asynchronous test operation.

EnabledMicrosoftProvider_MissingClientSecret_FailsStartup()

Verifies that application startup fails with an options validation exception when the Microsoft authentication provider is enabled but the client secret is missing.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 551)]
public void EnabledMicrosoftProvider_MissingClientSecret_FailsStartup()

Remarks

This test ensures that the configuration for the Microsoft authentication provider enforces the requirement for a client secret when the provider is enabled. It validates that an appropriate exception is thrown and that the error message clearly indicates the missing configuration.

EnabledOpenIdConnectProvider_MissingAuthority_FailsStartup()

Verifies that enabling the OpenIdConnect authentication provider without specifying an authority causes application startup to fail with an options validation exception.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 441)]
public void EnabledOpenIdConnectProvider_MissingAuthority_FailsStartup()

Remarks

This test ensures that the configuration is validated and that the authority setting is required when the OpenIdConnect provider is enabled. It helps prevent misconfiguration at startup by asserting that a missing authority results in a clear validation error.

EnabledSaml2Provider_MissingMetadataUrl_FailsStartup()

Verifies that application startup fails with an OptionsValidationException when the SAML2 provider is enabled but the MetadataUrl configuration is missing or empty.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 497)]
public void EnabledSaml2Provider_MissingMetadataUrl_FailsStartup()

Remarks

This test ensures that the SAML2 authentication provider enforces the requirement for a non-empty MetadataUrl when enabled. It validates that misconfiguration is detected early during application startup, preventing the application from running with incomplete authentication settings.

ProtectedApiEndpoint_ReturnsAuthenticatedIdentity_AfterCookieSignIn()

Verifies that the default cookie authentication pipeline establishes a principal that can reach a protected endpoint.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 269)]
public Task ProtectedApiEndpoint_ReturnsAuthenticatedIdentity_AfterCookieSignIn()

Returns

Task

A task that represents the asynchronous test operation.

ProtectedApiEndpoint_ReturnsUnauthorized_WhenCookieAuthenticationIsEnabled()

Verifies that a protected API endpoint returns unauthorized for unauthenticated users when cookie authentication is enabled.

[Fact("/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/tests/ProjectTemplate.Web.Tests/AuthenticationTests.cs", 237)]
public Task ProtectedApiEndpoint_ReturnsUnauthorized_WhenCookieAuthenticationIsEnabled()

Returns

Task

A task that represents the asynchronous test operation.