public void ContextObjectTest()
        {
            string methodName = "CustomRuleAreEqualRule";

            PermissionRules      uut             = new PermissionRules();
            ICustomRuleContainer customContainer = new TestCustomRules();

            PermissionRules.IsAuthorizedMethod del = uut.RuleDelegateFromMethodName(customContainer, methodName);
            Assert.IsNotNull(del);

            Dictionary <string, object> contextPropertyBag = customContainer.PreparePropertiesForRule(methodName, null);

            contextPropertyBag.Add("ContextCompareObject", Guid.NewGuid());

            // No context object is passed. Should return false
            bool?isAuthorized = del.Invoke(PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles, null, contextPropertyBag);

            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsFalse(isAuthorized.Value);

            // A different context object is passed. Should return false
            isAuthorized = del.Invoke(PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles, Guid.NewGuid(), contextPropertyBag);
            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsFalse(isAuthorized.Value);

            // The same context object is passed. Should return true
            isAuthorized = del.Invoke(PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles, contextPropertyBag["ContextCompareObject"], contextPropertyBag);
            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsTrue(isAuthorized.Value);
        }
        public void NeverAuthorizedRuleTest()
        {
            TestCustomRules uut = new TestCustomRules();

            PermissionRules.IsAuthorizedMethod del = new PermissionRules.IsAuthorizedMethod(uut.CustomRuleIsNeverAuthorized);

            Dictionary <string, object> contextPropertyBag = uut.PreparePropertiesForRule("CustomRuleIsNeverAuthorized", null);

            bool?isAuthorized = del.Invoke(TestIdentity, TestRoles, null, contextPropertyBag);

            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsFalse(isAuthorized.Value);
        }
        public void RuleFromInvalidRuleTest()
        {
            ICustomRuleContainer testContainer = new TestCustomRules();

            PermissionRules uut = new PermissionRules();

            PermissionRules.IsAuthorizedMethod testRule = uut.RuleDelegateFromMethodName(testContainer, "BOGUS");
            Assert.IsNull(testRule);

            testRule = uut.RuleDelegateFromMethodName(testContainer, "CustomRuleIsNotValidRule");
            Assert.IsNull(testRule);


            testRule = uut.RuleDelegateFromMethodName(testContainer, "CustomRuleWrongSignatureRule");
            Assert.IsNull(testRule);
        }
        public void InvalidPropertyBagTest()
        {
            string methodName = "CustomRuleAreEqualRule";

            PermissionRules      uut             = new PermissionRules();
            ICustomRuleContainer customContainer = new TestCustomRules();

            PermissionRules.IsAuthorizedMethod del = uut.RuleDelegateFromMethodName(customContainer, methodName);
            Assert.IsNotNull(del);

            Dictionary <string, object> contextPropertyBag = customContainer.PreparePropertiesForRule(methodName, null);
            // contextPropertyBag.Add("ContextCompareObject", Guid.NewGuid()); // don't pass in a ContextCompareObject

            // No context compare object is passed. Should throw error
            bool?isAuthorized = del.Invoke(PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles, null, contextPropertyBag);
        }
        public void ParallelRulesTest()
        {
            List <Task <bool?> > ruleTasks   = new List <Task <bool?> >();
            TestCustomRules      customRules = new TestCustomRules();
            PermissionRules      uut         = new PermissionRules();

            Open.Common.Configuration.CommonConfiguration test = new Common.Configuration.CommonConfiguration();

            PermissionRules.IsAuthorizedMethod del1 = new PermissionRules.IsAuthorizedMethod(customRules.CustomRuleIsAlwaysAuthorized);
            Dictionary <string, object>        contextPropertyBag1 = customRules.PreparePropertiesForRule("CustomRuleIsAlwaysAuthorized", null);
            PermissionRuleContext ruleState1 = new PermissionRuleContext("CustomRuleIsAlwaysAuthorized", "CanDoStuff", contextPropertyBag1, del1);
            Task <bool?>          task1      = uut.TaskFromPermissionRuleContext(ruleState1, PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles);

            PermissionRules.IsAuthorizedMethod del2 = new PermissionRules.IsAuthorizedMethod(customRules.CustomRuleIsNeverAuthorized);
            Dictionary <string, object>        contextPropertyBag2 = customRules.PreparePropertiesForRule("CustomRuleIsNeverAuthorized", null);
            PermissionRuleContext ruleState2 = new PermissionRuleContext("CustomRuleIsNeverAuthorized", "MayNotDoThisStuff", contextPropertyBag2, del2);
            Task <bool?>          task2      = uut.TaskFromPermissionRuleContext(ruleState2, PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles);

            task1.Start();
            ruleTasks.Add(task1);

            task2.Start();
            ruleTasks.Add(task2);

            // It's almost pointless because both tasks have likely finished by this point, but it's worth excercising the proper code
            Task.WaitAll(ruleTasks.ToArray());

            bool wasAlwaysRuleFound = false;
            bool wasNeverRuleFound  = false;

            foreach (Task <bool?> ruleTask in ruleTasks)
            {
                if ((Guid)ruleTask.AsyncState == ruleState1.ContextId)
                {
                    Assert.IsTrue(ruleTask.Result.HasValue);
                    Assert.IsTrue(ruleTask.Result.Value);
                    wasAlwaysRuleFound = true;
                }
                else if ((Guid)ruleTask.AsyncState == ruleState2.ContextId)
                {
                    Assert.IsTrue(ruleTask.Result.HasValue);
                    Assert.IsFalse(ruleTask.Result.Value);
                    wasNeverRuleFound = true;
                }
            }
            Assert.IsTrue(wasAlwaysRuleFound && wasNeverRuleFound);
        }
        public void ParallelRulesTest()
        {
            List <Task <bool?> > ruleTasks = new List <Task <bool?> >();
            TestCustomRules      uut       = new TestCustomRules();

            PermissionRules.IsAuthorizedMethod del1 = new PermissionRules.IsAuthorizedMethod(uut.CustomRuleIsAlwaysAuthorized);
            Dictionary <string, object>        contextPropertyBag1 = uut.PreparePropertiesForRule("CustomRuleIsAlwaysAuthorized", null);

            PermissionRules.IsAuthorizedMethod del2 = new PermissionRules.IsAuthorizedMethod(uut.CustomRuleIsNeverAuthorized);
            Dictionary <string, object>        contextPropertyBag2 = uut.PreparePropertiesForRule("CustomRuleIsNeverAuthorized", null);

            Guid         alwaysRuleGuid = Guid.NewGuid();
            Task <bool?> task1          = new Task <bool?>(a => del1.Invoke(TestIdentity, TestRoles, null, contextPropertyBag1), alwaysRuleGuid);

            task1.Start();
            ruleTasks.Add(task1);

            Guid         neverRuleGuid = Guid.NewGuid();
            Task <bool?> task2         = new Task <bool?>(a => del2.Invoke(TestIdentity, TestRoles, null, contextPropertyBag2), neverRuleGuid);

            task2.Start();
            ruleTasks.Add(task2);

            // It's almost pointless because both tasks have likely finished by this point, but it's worth excercising the proper code
            Task.WaitAll(ruleTasks.ToArray());

            bool wasAlwaysRuleFound = false;
            bool wasNeverRuleFound  = false;

            foreach (Task <bool?> ruleTask in ruleTasks)
            {
                if ((Guid)ruleTask.AsyncState == alwaysRuleGuid)
                {
                    Assert.IsTrue(ruleTask.Result.HasValue);
                    Assert.IsTrue(ruleTask.Result.Value);
                    wasAlwaysRuleFound = true;
                }
                else if ((Guid)ruleTask.AsyncState == neverRuleGuid)
                {
                    Assert.IsTrue(ruleTask.Result.HasValue);
                    Assert.IsFalse(ruleTask.Result.Value);
                    wasNeverRuleFound = true;
                }
            }
            Assert.IsTrue(wasAlwaysRuleFound && wasNeverRuleFound);
        }
        public void RuleNameTests()
        {
            string ruleWithNoCustomName = "CustomRuleIsAlwaysAuthorized";
            string ruleWithCustomName   = "CustomRuleAreEqualRule";

            ICustomRuleContainer customContainer = new TestCustomRules();
            PermissionRules      uut             = new PermissionRules();

            string ruleName = uut.RuleNameFromMethodName(customContainer, ruleWithNoCustomName);

            Assert.IsNotNull(ruleName);
            Assert.AreEqual(ruleWithNoCustomName, ruleName);

            ruleName = uut.RuleNameFromMethodName(customContainer, ruleWithCustomName);
            Assert.IsNotNull(ruleName);
            Assert.AreEqual(ruleWithCustomName + "-CustomName", ruleName);
        }