Exemplo n.º 1
0
        public void Test_CheckAccess_MultiplePermissions()
        {
            CachingEntityAccessControlChecker  cachingEntityAccessControlChecker;
            Mock <IEntityAccessControlChecker> entityAccessControlChecker;

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

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

                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[0].Id, true }, { entities[1].Id, false }
                });

                cachingEntityAccessControlChecker = new CachingEntityAccessControlChecker(entityAccessControlChecker.Object);

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

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

                // Ensure that it is cached
                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);
                Assert.That(cachingEntityAccessControlChecker.Cache,
                            Has.Exactly(1)
                            .Property("Key").EqualTo(new UserEntityPermissionTuple(user.Id, entities[1].Id, permissions.Select(x => x.Id)))
                            .And.Property("Value").False);

                // Second call
                result = cachingEntityAccessControlChecker.CheckAccess(entities, permissions, user);

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

                // Ensure that CheckAccess was only called once for each Setup call.
                entityAccessControlChecker.VerifyAll();
            }
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 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();
        }