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 ReadRulesFromConfigurationTest()
        {
            PermissionRules uut = new PermissionRules();
            Dictionary <string, PermissionRuleContextCollection> contextLookup = uut.ReadRulesFromConfiguration();

            Assert.IsNotNull(contextLookup);
            Assert.AreEqual(11, contextLookup.Keys.Count);

            int iCount = 0;

            foreach (string permissionName in contextLookup.Keys)
            {
                iCount += contextLookup[permissionName].Count;
            }
            Assert.AreEqual(12, iCount);
        }
        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 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);
        }
        public void CoreUserRoledRuleTest()
        {
            PermissionRules uut = new PermissionRules();

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

            Dictionary <string, object> contextPropertyBag = uut.PrepareStateForRoleRule(TestRoles[0]);

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

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

            contextPropertyBag = uut.PrepareStateForRoleRule("BOGUS");

            isAuthorized = del.Invoke(TestIdentity, TestRoles, null, contextPropertyBag);
            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsFalse(isAuthorized.Value);
        }
        public void RuleFromTypeAndMethodNameTest()
        {
            string typeName   = "Open.SPF.Core.Test.TestCustomRules, Open.SPF.Core.Test, Version=1.2.1, Culture=neutral, PublicKeyToken=b4313207536550be";
            string methodName = "CustomRuleIsAlwaysAuthorized";

            PermissionRules      uut             = new PermissionRules();
            ICustomRuleContainer customContainer = uut.CustomRuleContainerFromTypeName(typeName);

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

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

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

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

            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsTrue(isAuthorized.Value);
        }
        public void RuleArgumentTests()
        {
            string ruleWithArgument = "CheckArgument1";

            PermissionRules uut = new PermissionRules();
            Dictionary <string, PermissionRuleContextCollection> contextLookup = uut.ReadRulesFromConfiguration();

            Assert.IsNotNull(contextLookup);

            Assert.IsTrue(contextLookup.ContainsKey(ruleWithArgument));
            PermissionRuleContextCollection ruleContextCollection = contextLookup[ruleWithArgument];

            Assert.IsNotNull(ruleContextCollection);
            foreach (PermissionRuleContext rule in ruleContextCollection.Items)
            {
                foreach (string key in rule.PropertyBag.Keys)
                {
                    if (rule.PropertyBag[key] is string)
                    {
                        Console.WriteLine(String.Format("Key: {0}, Value: {1}", key, (String)rule.PropertyBag[key]));
                    }
                }
            }
        }