public void Handler_ReturnsFalse_WhenNotFound_ByUserName()
        {
            var userName = FakeData.String();
            var user     = new User {
                Name = userName,
            };
            var otherUser1 = new User {
                Name = FakeData.String(),
            };
            var otherUser2 = new User {
                Name = FakeData.String(),
            };
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = otherUser1,
                },
                new LocalMembership {
                    User = otherUser2,
                },
            };
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(user.Name);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeFalse();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void Handler_ReturnsTrue_WhenFound_ByUserId()
        {
            var userId           = FakeData.Id();
            var otherUserId1     = FakeData.Id(userId);
            var otherUserId2     = FakeData.Id(userId, otherUserId1);
            var user             = new ProxiedUser(userId);
            var otherUser1       = new ProxiedUser(otherUserId1);
            var otherUser2       = new ProxiedUser(otherUserId2);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = otherUser1,
                },
                new LocalMembership {
                    User = user,
                },
                new LocalMembership {
                    User = otherUser2,
                },
            };
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(user.Id);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void Handler_ReturnsFalse_WhenNotFound_ByPrincipal()
        {
            var userId           = new Random().Next(1, int.MaxValue);
            var user             = new ProxiedUser(userId);
            var differentUser1   = new ProxiedUser(userId + 1);
            var differentUser2   = new ProxiedUser(userId - 1);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = differentUser1,
                },
                new LocalMembership {
                    User = differentUser2,
                },
            };
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture)),
            });
            var principal = new GenericPrincipal(identity, null);
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(principal);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeFalse();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void Handler_ReturnsFalse_WhenNotFound_ByUserId()
        {
            var userId           = new Random().Next(3, int.MaxValue - 3);
            var user             = new ProxiedUser(userId);
            var differentUser1   = new ProxiedUser(userId + 1);
            var differentUser2   = new ProxiedUser(userId - 1);
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = differentUser1,
                },
                new LocalMembership {
                    User = differentUser2,
                },
            };
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(user.Id);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeFalse();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
        public void Query_StringCtor_SetsUserNameProperty()
        {
            var userName = FakeData.String();
            var query    = new UserHasLocalMembership(userName);

            query.UserId.ShouldBeNull();
            query.UserName.ShouldEqual(userName);
            query.Principal.ShouldBeNull();
        }
        public void Query_PrincipalCtor_SetsPrincipalProperty()
        {
            var principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var query     = new UserHasLocalMembership(principal.Object);

            query.UserId.ShouldBeNull();
            query.UserName.ShouldBeNull();
            query.Principal.ShouldEqual(principal.Object);
        }
        public void Query_IntCtor_SetsUserIdProperty()
        {
            var userId = new Random().Next(int.MinValue, int.MaxValue);
            var query  = new UserHasLocalMembership(userId);

            query.UserId.ShouldEqual(userId);
            query.UserName.ShouldBeNull();
            query.Principal.ShouldBeNull();
        }
        public void Handler_ReturnsTrue_WhenFound_ByPrincipal()
        {
            var userId       = FakeData.Id();
            var otherUserId1 = FakeData.Id(userId);
            var otherUserId2 = FakeData.Id(userId, otherUserId1);
            var user         = new ProxiedUser(userId)
            {
                Name = FakeData.String(),
            };
            var otherUser1 = new ProxiedUser(otherUserId1)
            {
                Name = FakeData.String(),
            };
            var otherUser2 = new ProxiedUser(otherUserId2)
            {
                Name = FakeData.String(),
            };
            var localMemberships = new[]
            {
                new LocalMembership {
                    User = otherUser1,
                },
                new LocalMembership {
                    User = user,
                },
                new LocalMembership {
                    User = otherUser2,
                },
            };
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture)),
            });
            var principal = new GenericPrincipal(identity, null);
            var data      = localMemberships.AsQueryable();
            var query     = new UserHasLocalMembership(principal);
            var dbSet     = new Mock <DbSet <LocalMembership> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <LocalMembership>(dbSet.Object, entities.Object);

            entities.Setup(x => x.Query <LocalMembership>()).Returns(entitySet);
            var handler = new HandleUserHasLocalMembershipQuery(entities.Object);

            bool result = handler.Handle(query).Result;

            result.ShouldBeTrue();
            entities.Verify(x => x.Query <LocalMembership>(), Times.Once);
        }
Beispiel #9
0
        public Task <bool> Handle(UserHasLocalMembership query)
        {
            var queryable = _entities.Query <LocalMembership>();

            if (query.UserId.HasValue)
            {
                return(queryable.AnyAsync(QueryLocalMemberships.ByUserId(query.UserId.Value)));
            }

            if (query.Principal != null && query.Principal.Identity.HasAppUserId())
            {
                return(queryable.AnyAsync(QueryLocalMemberships.ByUserId(query.Principal.Identity.GetUserId <int>())));
            }

            return(queryable.AnyAsync(QueryLocalMemberships.ByUserName(query.UserName)));
        }