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 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)));
        }
Ejemplo n.º 3
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]);
        }