Пример #1
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIdentity_HasNoNameIdentifierClaim()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.Name, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #2
0
        public void Handler_ReturnsNonNullUserView_WhenFound_ByPrincipal()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldNotBeNull();
            result.UserId.ShouldEqual(user.Id);
            result.UserName.ShouldEqual(user.Name);
            result.PrimaryEmailAddress.ShouldEqual(primaryEmail.Value);
            result.PrimaryEmailHash.ShouldEqual(primaryEmail.HashedValue);
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #3
0
        public void Handler_ReturnsNullUserView_WhenNotFound_ByPrincipal()
        {
            var userId      = FakeData.Id();
            var otherUserId = FakeData.Id(canNotBe: userId);
            var user        = new ProxiedUser(otherUserId)
            {
                Name = FakeData.String(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userId.ToString(CultureInfo.InvariantCulture)),
            };
            var identity  = new ClaimsIdentity(claims, "authenticationType");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #4
0
        public void Handler_ReturnsNonNullUserView_WhenFound_ByName()
        {
            var userId   = FakeData.Id();
            var userName = FakeData.String();
            var user     = new ProxiedUser(userId)
            {
                Name = userName,
            };
            var primaryEmail = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = FakeData.Email(),
                HashedValue = FakeData.String(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(userName);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldNotBeNull();
            result.UserId.ShouldEqual(user.Id);
            result.UserName.ShouldEqual(user.Name);
            result.PrimaryEmailAddress.ShouldEqual(primaryEmail.Value);
            result.PrimaryEmailHash.ShouldEqual(primaryEmail.HashedValue);
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #5
0
        public void Handler_ReturnsNullUserView_WhenNotFound_ByName()
        {
            var userId   = new Random().Next(1, int.MaxValue - 3);
            var userName = Guid.NewGuid().ToString();
            var user     = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var primaryEmail = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsPrimary   = true,
                IsVerified  = true,
            };
            var secondaryEmail1 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
                IsVerified  = true,
            };
            var secondaryEmail2 = new EmailAddress
            {
                Value       = string.Format("{0}@domain.tld", Guid.NewGuid()),
                HashedValue = Guid.NewGuid().ToString(),
            };

            user.EmailAddresses.Add(secondaryEmail1);
            user.EmailAddresses.Add(secondaryEmail2);
            user.EmailAddresses.Add(primaryEmail);
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(userName);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #6
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIsNull()
        {
            var userId = new Random().Next(1, int.MaxValue - 3);
            var user   = new ProxiedUser(userId)
            {
                Name = Guid.NewGuid().ToString(),
            };
            var data = new[] { user }.AsQueryable();
            var query     = new UserViewBy(null as IPrincipal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }
Пример #7
0
        public void Handler_ReturnsNullUserView_WhenPrincipalIdentity_IsNotClaimsIdentity()
        {
            var userId = FakeData.Id();
            var user   = new ProxiedUser(userId)
            {
                Name = FakeData.String(),
            };
            var data = new[] { user }.AsQueryable();
            var identity  = new GenericIdentity(user.Name, "authentication type");
            var principal = new GenericPrincipal(identity, null);
            var query     = new UserViewBy(principal);
            var dbSet     = new Mock <DbSet <User> >(MockBehavior.Strict).SetupDataAsync(data);
            var entities  = new Mock <IReadEntities>(MockBehavior.Strict);
            var entitySet = new EntitySet <User>(dbSet.Object, entities.Object);
            IQueryable <User> toBeReturned = entitySet;

            entities.Setup(x => x.Query <User>()).Returns(toBeReturned);
            var handler = new HandleUserViewByQuery(entities.Object);

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

            result.ShouldBeNull();
            entities.Verify(x => x.Query <User>(), Times.Once);
        }