public void Should_throw_when_no_violation_handler_has_been_set_and_no_violation_handler_match_name() { // Arrange var violationHandlers = Enumerable.Empty<IPolicyViolationHandler>(); var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied"); var policy = new DelegatePolicy("Test", c => failureResult); var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler); var exception = new PolicyViolationException(delegatePolicyResult); var handler = new DelegatePolicyViolationHandler(violationHandlers); // Act & assert var caughtException = Assert.Throws<PolicyViolationException>(() => handler.Handle(exception)); Assert.That(caughtException, Is.EqualTo(exception)); }
public void Should_return_action_result_from_explicitly_set_violation_handler() { // Arrange var violationHandlers = Enumerable.Empty<IPolicyViolationHandler>(); var expectedResult = new ContentResult { Content = "Some content" }; var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied"); var policy = new DelegatePolicy("Test", c => failureResult, e => expectedResult); var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler); var exception = new PolicyViolationException(delegatePolicyResult); var handler = new DelegatePolicyViolationHandler(violationHandlers); // Act var result = handler.Handle(exception); // Assert Assert.That(result, Is.EqualTo(expectedResult)); }
public void Should_return_action_result_from_violation_handler_that_match_name() { // Arrange var nonMatchingNameViolationHandler = new NonMatchingNameViolationHandler(); var matchingNameViolationHandler = new MatchingNameViolationHandler(); var violationHandlers = new List<IPolicyViolationHandler> { nonMatchingNameViolationHandler, matchingNameViolationHandler }; var failureResult = PolicyResult.CreateFailureResult(new IgnorePolicy(), "Access denied"); var policy = new DelegatePolicy("MatchingName", c => failureResult); var delegatePolicyResult = new DelegatePolicyResult(failureResult, policy.Name, policy.ViolationHandler); var exception = new PolicyViolationException(delegatePolicyResult); var handler = new DelegatePolicyViolationHandler(violationHandlers); // Act var result = handler.Handle(exception); // Assert Assert.That(result, Is.EqualTo(matchingNameViolationHandler.ActionResult)); }