public void Validate_Should_Not_Add_User_With_Empty_Id() { UserCommand command = UserCommandHandlerTestHelper.GetCommand(""); string expectedInvalid = MessagesModel.Required; UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidId); }
public void Validate_Should_Not_Add_User_With_Invalid_Id_Length() { UserCommand command = UserCommandHandlerTestHelper.GetCommand("a".PadLeft(51, 'a')); string expectedInvalid = string.Format(MessagesModel.MaxLength, "50"); UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidId); }
public void Validate_Should_Not_Add_User_With_Invalid_Email_Format() { UserCommand command = UserCommandHandlerTestHelper.GetCommand(null, null, null, "testatabc.comx"); string expectedInvalid = MessagesModel.InvalidEmail; UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidEmail); }
public void Validate_Should_Not_Add_User_With_Invalid_Email_Length() { UserCommand command = UserCommandHandlerTestHelper.GetCommand(null, null, null, "a".PadLeft(256, 'a') + "@abc.comx"); string expectedInvalid = string.Format(MessagesModel.MaxLength, "255"); UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidEmail); }
public void Execute_Should_Not_Add_Invalid_User() { AddUserCommandHandler sut = GetCommandHandler(); AddUserCommand command = UserCommandHandlerTestHelper.GetAddCommand("a".PadLeft(101, 'a')); string expectedInvalid = string.Format(MessagesModel.MaxLength, "100"); UserCommandResult result = sut.Execute(command); var calls = sut.WriteRepository.ReceivedCalls().Count(); Assert.AreEqual(0, calls); }
public void Validate_Should_Not_Add_User_With_Duplicate_Id() { UserCommand command = UserCommandHandlerTestHelper.GetCommand(); User user = new User(command.Id, command.DateAdded, command.Email, command.FirstName, command.LastName, command.Roles, command.Groups, command.IsEnabled); read.GetById("id1").Returns(user); string expectedInvalid = string.Format("A user already exists with the username {0}", command.Id); UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidId); }
public UserCommandResult Execute(AddUserCommand command) { UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.WriteRepository, this.ReadRepository); if (!result.Valid) { return(result); } User user = new User(command.Id, command.DateAdded, command.Email, command.FirstName, command.LastName, command.Roles, command.Groups, command.IsEnabled); this.WriteRepository.Insert(user); return(result); }
//TODO: Decide what happens if we happen to have a duplicate key here. public UserVm EditUser(UserVm vm) { if (vm == null) { throw new ArgumentNullException(string.Format(MessagesModel.NullValueError, "UserEditVm view")); } EditUserCommand command = new EditUserCommand(vm.Id, vm.DateAdded, vm.FirstName, vm.LastName, vm.Email, vm.Roles, vm.Groups, vm.IsEnabled); UserCommandResult result = this.commandDispatcher.Dispatch <EditUserCommand, UserCommandResult, User>(command); vm = UserModelHelper.CommandResultToUserVm(result); return(vm); }
public void Validate_Should_Not_Add_User_With_Duplicate_Email() { UserCommand command = UserCommandHandlerTestHelper.GetCommand(); IList <User> users = Substitute.For <IList <User> >(); users.Count.Returns(1); read.Where(x => x.Email == "*****@*****.**").ReturnsForAnyArgs(users); string expectedInvalid = string.Format("A user already exists with the email {0}", command.Email); UserCommandResult result = UserCommandHandlerHelper.Validate(command, this.write, this.read); Assert.IsFalse(result.Valid); Assert.AreEqual(expectedInvalid, result.InvalidEmail); }
public static UserVm CommandResultToUserVm(UserCommandResult result) { if (result == null) { throw new ArgumentNullException(string.Format(MessagesModel.NullValueError, "UserCommandResult result")); } UserVm vm = new UserVm(); vm.Valid = result.Valid; vm.Message = MessagesModel.FormError; if (!string.IsNullOrWhiteSpace(result.Message)) { vm.Message = result.Message; } vm.MessageStyle = MessagesModel.MessageStyles.Error; if (!string.IsNullOrWhiteSpace(result.InvalidFirstName)) { vm.FirstNameMessage.Message = MessagesModel.ItemMessage + result.InvalidFirstName; vm.FirstNameMessage.Error = MessagesModel.ItemError; vm.FirstNameMessage.GroupError = MessagesModel.ItemGroupError; } if (!string.IsNullOrWhiteSpace(result.InvalidLastName)) { vm.LastNameMessage.Message = MessagesModel.ItemMessage + result.InvalidLastName; vm.LastNameMessage.Error = MessagesModel.ItemError; vm.LastNameMessage.GroupError = MessagesModel.ItemGroupError; } if (!string.IsNullOrWhiteSpace(result.InvalidEmail)) { vm.EmailMessage.Message = MessagesModel.ItemMessage + result.InvalidEmail; vm.EmailMessage.Error = MessagesModel.ItemError; vm.EmailMessage.GroupError = MessagesModel.ItemGroupError; } return(vm); }
public static UserCommandResult Validate(UserCommand command, IWriteRepository <User> write, IReadRepository <User> read) { if (write == null) { throw new Exception("WriteRespository can not be a null value;"); } if (read == null) { throw new Exception("ReadRespository can not be a null value;"); } if (command == null) { throw new ArgumentNullException("command"); } UserCommandResult result = new UserCommandResult(); // TODO: Research how to handle concurrency in MongoDb. // I am still a MongoDb newbie so there is probably a better way to handle concurrency issues. // For now, if a duplicate is found we will throw an exception. if (UserCommandHandlerHelper.IsDuplicateId(command.Id, read)) { result.Valid = false; result.InvalidId = string.Format("A user already exists with the username {0}", command.Id); } if (string.IsNullOrWhiteSpace(command.Id)) { result.Valid = false; result.InvalidId = MessagesModel.Required; } if (command.Id != null && command.Id.Length > 50) { result.Valid = false; result.InvalidId = string.Format(MessagesModel.MaxLength, "50"); } if (string.IsNullOrWhiteSpace(command.FirstName)) { result.Valid = false; result.InvalidFirstName = MessagesModel.Required; } if (command.FirstName != null && command.FirstName.Length > 100) { result.Valid = false; result.InvalidFirstName = string.Format(MessagesModel.MaxLength, "100"); } if (string.IsNullOrWhiteSpace(command.LastName)) { result.Valid = false; result.InvalidLastName = MessagesModel.Required; } if (command.LastName != null && command.LastName.Length > 100) { result.Valid = false; result.InvalidLastName = string.Format(MessagesModel.MaxLength, "100"); } if (IsDuplicateEmail(command.Email, read)) { result.Valid = false; result.InvalidEmail = string.Format("A user already exists with the email {0}", command.Email); } if (string.IsNullOrWhiteSpace(command.Email)) { result.Valid = false; result.InvalidEmail = MessagesModel.Required; } if (command.Email != null && command.Email.Length > 255) { result.Valid = false; result.InvalidEmail = string.Format(MessagesModel.MaxLength, "255"); } if (!Validator.IsEmail(command.Email)) { result.Valid = false; result.InvalidEmail = MessagesModel.InvalidEmail; } return(result); }