From 0977c9ac59033e71fbcba23c7feb067afc4404a3 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Thu, 9 Jan 2020 14:33:29 -0800 Subject: [PATCH] Add test for AuthenticationSchemes (#17816) --- .../test/AuthorizationMiddlewareTests.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Security/Authorization/test/AuthorizationMiddlewareTests.cs b/src/Security/Authorization/test/AuthorizationMiddlewareTests.cs index 86858f4fe1..075a6ee655 100644 --- a/src/Security/Authorization/test/AuthorizationMiddlewareTests.cs +++ b/src/Security/Authorization/test/AuthorizationMiddlewareTests.cs @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Authorization.Test // Assert Assert.False(next.Called); } - + [Fact] public async Task HasEndpointWithoutAuth_AnonymousUser_Allows() { @@ -135,6 +135,27 @@ namespace Microsoft.AspNetCore.Authorization.Test Assert.True(authenticationService.ChallengeCalled); } + [Fact] + public async Task HasEndpointWithAuth_ChallengesAuthenticationSchemes() + { + // Arrange + var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build(); + var policyProvider = new Mock(); + policyProvider.Setup(p => p.GetDefaultPolicyAsync()).ReturnsAsync(policy); + var next = new TestRequestDelegate(); + var authenticationService = new TestAuthenticationService(); + + var middleware = CreateMiddleware(next.Invoke, policyProvider.Object); + var context = GetHttpContext(endpoint: CreateEndpoint(new AuthorizeAttribute() { AuthenticationSchemes = "whatever"}), authenticationService: authenticationService); + + // Act + await middleware.Invoke(context); + + // Assert + Assert.False(next.Called); + Assert.True(authenticationService.ChallengeCalled); + } + [Fact] public async Task HasEndpointWithAuth_AnonymousUser_ChallengePerScheme() {