public void HasGetSet() { const int value = 8; var entity = new EmailAddress { PersonId = value }; entity.ShouldNotBeNull(); entity.PersonId.ShouldEqual(value); }
public void ThrowsNullReferenceException_WhenPrincipalIsNull() { var query = new GetMyEmailAddressByNumberQuery { Principal = null, }; var emailAddress = new EmailAddress { Person = new Person { User = new User { Name = "", }, }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var handler = new GetMyEmailAddressByNumberHandler(entities.Object); NullReferenceException exception = null; try { handler.Handle(query); } catch (NullReferenceException ex) { exception = ex; } exception.ShouldNotBeNull(); }
public void IgnoresReturnUrl() { var entity = new EmailAddress(); var model = Mapper.Map<UpdateEmailValueForm>(entity); model.ShouldNotBeNull(); model.ReturnUrl.ShouldBeNull(); }
public void MapsNumber_ToNumber() { const int number = 2; var entity = new EmailAddress { Number = number }; var model = Mapper.Map<UpdateEmailValueForm>(entity); model.ShouldNotBeNull(); model.Number.ShouldEqual(entity.Number); }
public void MapsValue_ToOldSpelling() { const string value = "*****@*****.**"; var entity = new EmailAddress { Value = value }; var model = Mapper.Map<UpdateEmailValueForm>(entity); model.ShouldNotBeNull(); model.OldSpelling.ShouldNotBeNull(); model.OldSpelling.ShouldEqual(entity.Value); }
public static bool NumberAndPrincipalMatchesEntity(int number, IPrincipal principal, IQueryEntities entities, out EmailAddress entity) { if (entities == null) { entity = null; return false; } entity = entities.Query<EmailAddress>().ByUserNameAndNumber(principal.Identity.Name, number); // return true (valid) if there is an entity return entity != null; }
public void MapsPersonUserName_ToPersonUserName() { const string userName = "******"; var entity = new EmailAddress { Person = new Person { User = new User { Name = userName } } }; var model = Mapper.Map<UpdateEmailValueForm>(entity); model.ShouldNotBeNull(); model.PersonUserName.ShouldNotBeNull(); model.PersonUserName.ShouldEqual(entity.Person.User.Name); }
public void ExecutesQuery_ToGetUserByName() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var query = new GetMyEmailAddressByNumberQuery { Principal = principal, }; var emailAddress = new EmailAddress { Person = new Person { User = new User { Name = principal.Identity.Name, }, }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var handler = new GetMyEmailAddressByNumberHandler(entities.Object); handler.Handle(query); entities.Verify(m => m.Query<EmailAddress>(), Times.Once()); }
public void DoesNotUpdate_WhenNewValue_IsSameAsOldSpelling() { var princial = "".AsPrincipal(); const string value = "*****@*****.**"; var command = new UpdateMyEmailValueCommand { NewValue = value, Principal = princial, }; var emailAddress = new EmailAddress { Value = value, Person = new Person { User = new User { Name = princial.Identity.Name } }, }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Get<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); entities.Setup(m => m.Update(It.IsAny<EmailAddress>())); var handler = new UpdateMyEmailValueHandler(entities.Object); handler.Handle(command); entities.Verify(m => m.Update(It.IsAny<EmailAddress>()), Times.Never()); command.ChangedState.ShouldEqual(false); }
public EmailAddress AddEmail(string value) { // email may already exist var email = Emails.ByValue(value); if (email != null) return email; // create email email = new EmailAddress { // if person does not already have a default email, this is it IsDefault = (Emails.Count(a => a.IsDefault) == 0), Value = value, Person = this, Number = Emails.NextNumber(), }; // add & return email Emails.Add(email); return email; }
public EmailAddress AddEmail(string value) { // email may already exist var email = Emails.SingleOrDefault(x => x.Value.Equals(value, StringComparison.OrdinalIgnoreCase)); if (email != null) return email; // create email email = new EmailAddress { // if person does not already have a default email, this is it IsDefault = (Emails.Count(a => a.IsDefault) == 0), Value = value, Person = this, Number = Emails.NextNumber(), }; // add & return email Emails.Add(email); return email; }
public static bool IsConfirmed(EmailAddress entity) { // return true (valid) if the email is confirmed return entity != null && entity.IsConfirmed; }
public void IsValidWhen_Value_MatchesPreviousSpelling_CaseInsensitively() { var model = new UpdateEmailValueForm { Value = "*****@*****.**", PersonUserName = string.Empty }; var emailAddress = new EmailAddress { Value = "*****@*****.**", Person = new Person { User = new User { Name = model.PersonUserName } }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var validator = new UpdateEmailValueValidator(entities.Object); var results = validator.Validate(model); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldBeNull(); }
public void IsValidWhen_Number_MatchesEmail_ForPersonUserName() { const string personUserName = "******"; const string emailValue = "*****@*****.**"; var form = new UpdateEmailValueForm { PersonUserName = personUserName, Number = 13, Value = emailValue.ToUpper(), }; var emailAddress = new EmailAddress { Value = emailValue.ToLower(), Number = form.Number, Person = new Person { User = new User { Name = form.PersonUserName, } } }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var validator = new UpdateEmailValueValidator(entities.Object); var results = validator.Validate(form); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldBeNull(); }
public void IsInvalidWhen_Value_DoesNotMatchPreviousSpelling_CaseInsensitively() { var model = new UpdateEmailValueForm { Value = "*****@*****.**", PersonUserName = string.Empty }; var emailAddress = new EmailAddress { Value = "*****@*****.**", Person = new Person { User = new User { Name = model.PersonUserName }, } }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var validator = new UpdateEmailValueValidator(entities.Object); var results = validator.Validate(model); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual( UpdateEmailValueValidator.FailedBecausePreviousSpellingDoesNotMatchValueCaseInsensitively); // ReSharper restore PossibleNullReferenceException }
public void ChangesEmailSpelling_WhenNewValue_IsDifferentFromOldSpelling() { const string newValue = "*****@*****.**"; const string oldValue = "*****@*****.**"; var princial = "".AsPrincipal(); var emailAddress = new EmailAddress { Value = oldValue, Person = new Person { User = new User { Name = princial.Identity.Name, } } }; EmailAddress updatedEntity = null; var command = new UpdateMyEmailValueCommand { NewValue = newValue, Principal = princial, }; var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Get<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); entities.Setup(m => m.Update(It.Is<EmailAddress>(a => a.Value == newValue))) .Callback((Entity entity) => updatedEntity = (EmailAddress)entity); var handler = new UpdateMyEmailValueHandler(entities.Object); handler.Handle(command); entities.Verify(m => m.Update(It.Is<EmailAddress>(a => a.Value == newValue)), Times.Once()); command.ChangedState.ShouldEqual(true); updatedEntity.Value.ShouldEqual(newValue); }
public void ReturnsNull_WhenQueryForUser_ReturnsNull() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var query = new GetMyEmailAddressByNumberQuery { Principal = principal, }; var emailAddress = new EmailAddress { Person = new Person { User = new User { Name = "someone else", }, }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var handler = new GetMyEmailAddressByNumberHandler(entities.Object); var result = handler.Handle(query); result.ShouldBeNull(); }
public void IsInvalidWhen_DoesNotMatchPreviousSpelling_CaseInsensitively() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var command = new UpdateMyEmailValueCommand { Principal = principal, NewValue = "*****@*****.**", }; var emailAddress = new EmailAddress { Value = "*****@*****.**", Person = new Person { User = new User { Name = principal.Identity.Name, }, }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var validator = new UpdateMyEmailValueValidator(entities.Object); var results = validator.Validate(command); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue"); error.ShouldNotBeNull(); // ReSharper disable PossibleNullReferenceException error.ErrorMessage.ShouldEqual(string.Format( ValidateEmailAddress.FailedBecauseNewValueDidNotMatchCurrentValueCaseInvsensitively, command.NewValue)); // ReSharper restore PossibleNullReferenceException }
public void IsValidWhen_MatchesPreviousSpelling_CaseInsensitively() { var command = new UpdateMyEmailValueCommand { NewValue = "*****@*****.**", }; var emailAddress = new EmailAddress { Value = "*****@*****.**" }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); var validator = new UpdateMyEmailValueValidator(entities.Object); var results = validator.Validate(command); results.IsValid.ShouldBeFalse(); results.Errors.Count.ShouldBeInRange(1, int.MaxValue); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "NewValue"); error.ShouldBeNull(); }
public static bool IsConfirmed(EmailAddress entity) { // return true (valid) if the email is confirmed return(entity != null && entity.IsConfirmed); }
public static bool NewValueMatchesCurrentValueCaseInsensitively(string newValue, EmailAddress email) { return(email != null && email.Value.Equals(newValue, StringComparison.OrdinalIgnoreCase)); }
public static bool NewValueMatchesCurrentValueCaseInsensitively(string newValue, EmailAddress email) { return email != null && email.Value.Equals(newValue, StringComparison.OrdinalIgnoreCase); }
public void IsValidWhen_MatchesEmail_ForPrincipal() { const string principalIdentityName = "*****@*****.**"; var principal = principalIdentityName.AsPrincipal(); var command = new UpdateMyEmailValueCommand { Principal = principal, Number = 1, NewValue = principalIdentityName.ToUpper(), }; var emailAddress = new EmailAddress { Number = command.Number, Value = command.NewValue, Person = new Person { User = new User { Name = principal.Identity.Name, }, }, }; var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize(); entities.Setup(m => m.Query<EmailAddress>()).Returns(new[] { emailAddress }.AsQueryable); entities.Setup(m => m.Query<User>()).Returns(new User[] { }.AsQueryable); var validator = new UpdateMyEmailValueValidator(entities.Object); var results = validator.Validate(command); var error = results.Errors.SingleOrDefault(e => e.PropertyName == "Number"); error.ShouldBeNull(); }