Esempio n. 1
0
        public async Task UpdateUnsubscribesUsers()
        {
            // Arrange.
            var users         = TestUsers.ToList();
            var policyService = new TestSecurityPolicyService();
            var entitiesMock  = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(users.MockDbSet().Object);
            var controller   = new SecurityPolicyController(entitiesMock.Object, policyService);
            var subscription = policyService.Mocks.Subscription.Object;

            users.ForEach(async u => await policyService.SubscribeAsync(u, subscription));
            policyService.MockEntitiesContext.ResetCalls();

            // Act.
            var model = new List <string>
            {
                $"{{\"u\":\"A\",\"g\":\"{subscription.SubscriptionName}\",\"v\":\"false\"}}",
                $"{{\"u\":\"B\",\"g\":\"{subscription.SubscriptionName}\",\"v\":\"true\"}}",
                $"{{\"u\":\"C\",\"g\":\"{subscription.SubscriptionName}\",\"v\":\"false\"}}"
            };
            var result = await controller.Update(model);

            // Assert.
            Assert.False(policyService.IsSubscribed(users[0], subscription));
            Assert.True(policyService.IsSubscribed(users[1], subscription));
            Assert.False(policyService.IsSubscribed(users[2], subscription));

            policyService.MockEntitiesContext.Verify(c => c.SaveChangesAsync(), Times.Exactly(2));
        }
        public async Task UpdateSubscribesUsers()
        {
            // Arrange.
            var users         = TestUsers.ToList();
            var policyService = new TestSecurityPolicyService();
            var entitiesMock  = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(users.MockDbSet().Object);
            var controller   = new SecurityPolicyController(entitiesMock.Object, policyService);
            var subscription = policyService.Mocks.Subscription.Object;

            Assert.False(users.Any(u => policyService.IsSubscribed(u, subscription)));

            // Act.
            var viewModel = new SecurityPolicyViewModel()
            {
                UsersQuery        = "A,B,C",
                UserSubscriptions = new[]
                {
                    $"{{\"u\":\"A\",\"g\":\"{subscription.SubscriptionName}\"}}",
                    $"{{\"u\":\"C\",\"g\":\"{subscription.SubscriptionName}\"}}"
                }
            };
            var result = await controller.Update(viewModel);

            // Assert.
            Assert.True(policyService.IsSubscribed(users[0], subscription));
            Assert.False(policyService.IsSubscribed(users[1], subscription));
            Assert.True(policyService.IsSubscribed(users[2], subscription));

            policyService.MockEntitiesContext.Verify(c => c.SaveChangesAsync(), Times.Exactly(2));
        }
Esempio n. 3
0
        public void IndexActionReturnsSubscriptionNames()
        {
            // Arrange.
            var policyService   = new TestSecurityPolicyService();
            var entitiesContext = policyService.MockEntitiesContext.Object;
            var controller      = new SecurityPolicyController(entitiesContext, policyService);

            // Act.
            var result = controller.Index();

            // Assert
            var model = ResultAssert.IsView <SecurityPolicyViewModel>(result);

            Assert.NotNull(model);
            Assert.NotNull(model.SubscriptionNames);
            Assert.Single(model.SubscriptionNames);
        }
Esempio n. 4
0
        public void SearchFindsMatchingUsers(string query, int foundCount, int notFoundCount)
        {
            // Arrange.
            var policyService = new TestSecurityPolicyService();
            var entitiesMock  = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(TestUsers.MockDbSet().Object);
            var controller = new SecurityPolicyController(entitiesMock.Object, policyService);

            // Act.
            JsonResult result = controller.Search(query);

            // Assert
            dynamic data          = result.Data;
            var     users         = data.Users as IEnumerable <UserSecurityPolicySubscriptions>;
            var     usersNotFound = data.UsersNotFound as IEnumerable <string>;

            Assert.NotNull(users);
            Assert.Equal(foundCount, users.Count());
            Assert.Equal(notFoundCount, usersNotFound.Count());
        }
Esempio n. 5
0
        public async Task UpdateIgnoresBadUsers()
        {
            // Arrange.
            var users         = TestUsers.ToList();
            var policyService = new TestSecurityPolicyService();
            var entitiesMock  = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(users.MockDbSet().Object);
            var controller   = new SecurityPolicyController(entitiesMock.Object, policyService);
            var subscription = policyService.Mocks.Subscription.Object;

            // Act.
            var model = new List <string>
            {
                $"{{\"u\":\"D\",\"g\":\"{subscription.SubscriptionName}\",\"v\":\"false\"}}"
            };
            var result = await controller.Update(model);

            // Assert.
            policyService.MockEntitiesContext.Verify(c => c.SaveChangesAsync(), Times.Never);
        }
        public void IsSubscribedIgnoresPolicyState()
        {
            // Arrange.
            var users         = TestUsers.ToList();
            var policyService = new TestSecurityPolicyService();
            var entitiesMock  = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(users.MockDbSet().Object);
            var controller   = new SecurityPolicyController(entitiesMock.Object, policyService);
            var subscription = policyService.Mocks.UserPoliciesSubscription.Object;

            users.ForEach(async u => await policyService.SubscribeAsync(u, subscription));
            policyService.MockEntitiesContext.ResetCalls();

            // Act.
            // Simulates changes to the configurable state of all existing policy subscriptions
            users.ForEach(u =>
                          u.SecurityPolicies.Where(p => p.Subscription == subscription.SubscriptionName).ToList().ForEach(p =>
                                                                                                                          p.Value = Guid.NewGuid().ToString()));

            // Assert.
            Assert.All(users, u => Assert.True(policyService.IsSubscribed(u, subscription)));
        }
Esempio n. 7
0
        public async Task SearchReturnsUserSubscriptions()
        {
            // Arrange.
            var policyService    = new TestSecurityPolicyService();
            var dbUsers          = TestUsers.ToArray();
            var subscription     = policyService.Mocks.Subscription.Object;
            var subscriptionName = subscription.SubscriptionName;
            await policyService.SubscribeAsync(dbUsers[1], subscription);

            var entitiesMock = policyService.MockEntitiesContext;

            entitiesMock.Setup(c => c.Users).Returns(dbUsers.MockDbSet().Object);
            var controller = new SecurityPolicyController(entitiesMock.Object, policyService);

            // Act.
            JsonResult result = controller.Search("A,B,C");

            // Assert.
            dynamic data  = result.Data;
            var     users = (data.Users as IEnumerable <UserSecurityPolicySubscriptions>)?.ToList();

            Assert.NotNull(users);
            Assert.Equal(3, users.Count());

            Assert.Equal(1, users[0].Subscriptions.Count);
            Assert.True(users[0].Subscriptions.ContainsKey(subscriptionName));
            Assert.False(users[0].Subscriptions[subscriptionName]);

            Assert.Equal(1, users[1].Subscriptions.Count);
            Assert.True(users[1].Subscriptions.ContainsKey(subscriptionName));
            Assert.True(users[1].Subscriptions[subscriptionName]);

            Assert.Equal(1, users[2].Subscriptions.Count);
            Assert.True(users[2].Subscriptions.ContainsKey(subscriptionName));
            Assert.False(users[2].Subscriptions[subscriptionName]);
        }