/// <summary>
        /// Returns methodInfo found by actionName and parameters
        /// <br/>
        /// Throws exception if action was not found
        /// </summary>
        /// <param name="actionName"></param>
        /// <param name="parameterInfosFromConfig"></param>
        /// <returns></returns>
        private static MethodInfo GetMethodInfo(string actionName, IEnumerable <MyParameterInfo> parameterInfosFromConfig)
        {
            var allActions = ControllerInfo.Actions(typeof(TController));

            foreach (var methodInfo in allActions)
            {
                var parameterInfos = ParameterInfosFromAction(methodInfo);

                //'All' must be preceded with a check for 0 parameterInfos,
                // since All is always true if the list is empty (memory from HM =) )
                if (!parameterInfos.Any())
                {
                    continue;
                }

                var isMatch = parameterInfos.All(param =>
                                                 parameterInfosFromConfig.Any(MatchParameterNameAndType(param)));

                if (isMatch)
                {
                    return(methodInfo);
                }
            }

            //action not found >
            var parameterNames   = parameterInfosFromConfig.Select(parameter => parameter.Name).ToArray();
            var parametersInText = string.Join(",", parameterNames);

            throw new ArgumentException(
                      $"could not find action '{actionName}' with parameters {parametersInText}, check configured rules");
        }
示例#2
0
        public void Populates_default_rules_and_merges_with_configured_rules()
        {
            //arrange
            var controllerRule = Helper.ControllerRule_with_policy_RequireAdmin();
            var actionRule     = Helper.ActionRule_withNoAttribute_Controller_hasAllowAnnonymous();

            var validationRules = new List <IValidationRuleConfiguredByUser>
            {
                controllerRule, actionRule
            };

            //arrange expects
            var expectedActionRulesFromController = ControllerInfo
                                                    //note: could not get type directly from controllerRule in an easy way, that is why it looks like this for now.
                                                    .Actions(typeof(FakeControllers.Controller_with_Policy_RequireAdmin))
                                                    .Select(action => new ValidationRuleActionInternal(action, controllerRule.AttributeRequired));

            var expectedRules = new List <ValidationRuleActionInternal>
            {
                new ValidationRuleActionInternal(actionRule.Action, actionRule.AttributeRequired)
            };

            expectedRules.AddRange(expectedActionRulesFromController);


            //acts and asserts
            var rulesContainer = new ValidationRulesContainer(
                controllerActionsToValidate: _defaultActionsFromAssemblyToValidate,
                validationRules);

            rulesContainer.AllRules().Should().BeEmpty("nothing is initialized yet");


            //act
            rulesContainer.PopulateDefaultRules(defaultAttribute: new AuthorizeAttribute("DefaultPolicyName"));

            //assert
            var nrOfControllerActionsInAssembly = rulesContainer._controllerActionsToValidate.Count();

            rulesContainer.AllRules().Count
            .Should().Be(nrOfControllerActionsInAssembly,
                         "one rule per action is created");

            //act
            rulesContainer.MergeDefaultWithConfiguredRules();
            //assert
            rulesContainer.AllRules().Count.Should().Be(nrOfControllerActionsInAssembly,
                                                        " since default rules are overwritten with configured rules the total count should not differ after merge");


            var actualRules = rulesContainer.AllRules();

            actualRules
            .Should()
            //note: Contain uses Equality comparer.
            .Contain(expectedRules
                     , "configured rules should overwrite the defaults");
        }