コード例 #1
0
        public void Test_Clear()
        {
            CachingEntityAccessControlChecker  cachingEntityAccessControlChecker;
            Mock <IEntityAccessControlChecker> entityAccessControlChecker;

            EntityRef[] entities;
            EntityRef[] permissions;
            EntityRef   user;

            entities    = new EntityRef[] { 1 };
            permissions = new[] { Permissions.Read };
            user        = new EntityRef(2);

            entityAccessControlChecker = new Mock <IEntityAccessControlChecker>(MockBehavior.Strict);
            entityAccessControlChecker
            .Setup(eacc => eacc.CheckAccess(
                       It.Is <IList <EntityRef> >(e => e.SequenceEqual(entities, EntityRefComparer.Instance)),
                       It.Is <IList <EntityRef> >(p => p.SequenceEqual(permissions, EntityRefComparer.Instance)),
                       It.IsAny <EntityRef>()))
            .Returns(new Dictionary <long, bool>()
            {
                { entities.First().Id, true }
            });

            cachingEntityAccessControlChecker = new CachingEntityAccessControlChecker(entityAccessControlChecker.Object);
            cachingEntityAccessControlChecker.CheckAccess(entities, permissions, user);

            // Sanity check
            Assert.That(cachingEntityAccessControlChecker.Cache, Has.Property("Count").Positive, "Not cached");

            cachingEntityAccessControlChecker.Clear();

            Assert.That(cachingEntityAccessControlChecker.Cache, Has.Property("Count").EqualTo(0), "Not cleared");
        }
コード例 #2
0
        public void Test_CheckAccess_SimpleCached()
        {
            CachingEntityAccessControlChecker  cachingEntityAccessControlChecker;
            Mock <IEntityAccessControlChecker> entityAccessControlChecker;

            EntityRef[] entities;
            EntityRef[] permissions;
            EntityRef   user;
            IDictionary <long, bool> result;

            using (DatabaseContext.GetContext(true))
            {
                entities    = new EntityRef[] { 1 };
                permissions = new[] { Permissions.Read };
                user        = new EntityRef(2);

                entityAccessControlChecker = new Mock <IEntityAccessControlChecker>(MockBehavior.Strict);
                entityAccessControlChecker
                .Setup(eacc => eacc.CheckAccess(
                           It.Is <IList <EntityRef> >(e => e.SequenceEqual(entities, EntityRefComparer.Instance)),
                           It.Is <IList <EntityRef> >(p => p.SequenceEqual(permissions, EntityRefComparer.Instance)),
                           It.IsAny <EntityRef>()))
                .Returns(new Dictionary <long, bool>()
                {
                    { entities.First().Id, true }
                });

                cachingEntityAccessControlChecker = new CachingEntityAccessControlChecker(entityAccessControlChecker.Object);

                // Call twice
                result = cachingEntityAccessControlChecker.CheckAccess(entities, permissions, user);
                result = cachingEntityAccessControlChecker.CheckAccess(entities, permissions, user);

                // Check results
                Assert.That(result, Has.Property("Count").EqualTo(1));
                Assert.That(result, Has.Exactly(1).Property("Key").EqualTo(entities[0].Id).And.Property("Value").EqualTo(true));

                // Ensure cache contains one entry
                Assert.That(cachingEntityAccessControlChecker.Cache, Has.Count.EqualTo(1));
                Assert.That(cachingEntityAccessControlChecker.Cache,
                            Has.Exactly(1)
                            .Property("Key").EqualTo(new UserEntityPermissionTuple(user.Id, entities[0].Id, permissions.Select(x => x.Id)))
                            .And.Property("Value").True);

                // Ensure that CheckAccess was only called once for each Setup call.
                entityAccessControlChecker.VerifyAll();
            }
        }
コード例 #3
0
        public void Test_NoCachingWhenBypassSet()
        {
            CachingEntityAccessControlChecker  cachingEntityAccessControlChecker;
            Mock <IEntityAccessControlChecker> entityAccessControlChecker;

            EntityRef[] entities;
            EntityRef[] permissions;
            EntityRef   user;

            entities    = new EntityRef[] { 1 };
            permissions = new[] { Permissions.Read };
            user        = new EntityRef(2);

            entityAccessControlChecker = new Mock <IEntityAccessControlChecker>(MockBehavior.Strict);
            entityAccessControlChecker
            .Setup(eacc => eacc.CheckAccess(
                       It.Is <IList <EntityRef> >(e => e.SequenceEqual(entities, EntityRefComparer.Instance)),
                       It.Is <IList <EntityRef> >(p => p.SequenceEqual(permissions, EntityRefComparer.Instance)),
                       It.IsAny <EntityRef>()))
            .Returns(new Dictionary <long, bool>()
            {
                { entities.First().Id, true }
            });

            cachingEntityAccessControlChecker = new CachingEntityAccessControlChecker(entityAccessControlChecker.Object);

            Assert.That(cachingEntityAccessControlChecker, Has.Property("Cache").Empty,
                        "Cache not empty before test");

            using (new SecurityBypassContext())
            {
                cachingEntityAccessControlChecker.CheckAccess(entities, permissions, user);
            }

            Assert.That(cachingEntityAccessControlChecker, Has.Property("Cache").Empty,
                        "Cache not empty after test");

            entityAccessControlChecker.VerifyAll();
        }