public void TestEvaluateScopeClaim(string requirement, string scopeClaim, bool expected) { var mockLogger = new Mock <ILogger>(); var handler = new ClaimPatternAuthorizationHandler(requirement, new ScopePatternOptions() /*use defaults*/, new ConcurrentDictionary <string, bool>(), mockLogger.Object); var actual = handler.EvaluateScopeClaim(requirement, scopeClaim); Assert.Equal(expected, actual); }
public void TestEvaluatePattern(string policyScope, string scopeClaim, int testEvaluatePatternIndex) { var data = TestEvaluatePatternData[testEvaluatePatternIndex]; var mockLogger = new Mock <ILogger>(); var handler = new ClaimPatternAuthorizationHandler(policyScope, new ScopePatternOptions() /*use defaults*/, new ConcurrentDictionary <string, bool>(), mockLogger.Object); var input = data[IO.I]; var expected = data[IO.O]; handler.EvaluatePattern(policyScope, scopeClaim, ref input); Assert.Equal(expected, input); //input = actual after processing }
public void TestEvaluateScope(string policyScope, string[] scopeClaims, bool expected, bool isOidc, bool[] cachedValues, int[] expectedLogMessageFrequency) { var scopePatternOptions = new ScopePatternOptions { IsOidc = isOidc }; var claimType = $"{(isOidc ? scopePatternOptions.UserScopePrefix : "")}scope"; var mockClaimsPrincipal = new Mock <ClaimsPrincipal>(); mockClaimsPrincipal.SetupGet(cp => cp.Claims).Returns( scopeClaims.Select(c => new Claim(claimType, c))); var cache = new ConcurrentDictionary <string, bool>(); if (cachedValues != null) { for (int i = 0; i < cachedValues.Length; i++) { cache.TryAdd(scopeClaims[i], cachedValues[i]); } } var mockLogger = new Mock <TestLogger>(); mockLogger.SetupGet(m => m.InternalLogger).Returns(new Mock <ILogger>().Object); mockLogger.SetupGet(m => m.TestOutputHelper).Returns(_output); var handler = new ClaimPatternAuthorizationHandler(policyScope, scopePatternOptions, cache, mockLogger.Object); var actual = handler.EvaluateClaimsPrincipal(mockClaimsPrincipal.Object, handler); Assert.Equal(expected, actual); for (int i = 0; i < logMessages.Length; i++) { mockLogger.Verify(c => c.XLog(LogLevel.Trace, It.IsRegex(logMessages[i])), Times.Exactly(expectedLogMessageFrequency[i])); } }