Ejemplo 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));
        }
        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)));
        }