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