public void Register_ValidateUsername_AlreadyExists() { var resources = new Resources(); using (resources.Context.Database.BeginTransaction()) { // Given TestHelper.RemoveUsers(resources.Context); var user = TestHelper.CreateUser(resources.Context, username: "******"); var requestModel = new ValidateUserRequestModel { Username = user.Username }; // When var result = resources.Controller.FindUsername(requestModel); // Then Assert.IsNotNull(result); Assert.IsFalse(result.IsValid); Assert.AreEqual("Username already exists", result.Messages[0].Message); var savedUser = resources.Context.Users.First(u => u.Username == requestModel.Username); Assert.IsNotNull(savedUser); } }
public ValidationResult FindUsername([FromBody] ValidateUserRequestModel requestModel) { var result = new ValidationResult(); var existingUser = Context.Users.FirstOrDefault(r => r.Username.ToUpper() == requestModel.Username.ToUpper()); if (existingUser == null) { return(result); } else { result.InValidate("", "Username already exists"); return(result); } }
public void Register_ValidateUsername_Successful() { var resources = new Resources(); using (resources.Context.Database.BeginTransaction()) { // Given var requestModel = new ValidateUserRequestModel { Username = "******" }; // When var result = resources.Controller.FindUsername(requestModel); // Then Assert.IsNotNull(result); Assert.IsTrue(result.IsValid); var savedUser = resources.Context.Users.FirstOrDefault(u => u.Username == requestModel.Username); Assert.IsNull(savedUser); } }