コード例 #1
0
        public void WhenNewUserSurnameIsTooLong_ShouldtHaveError()
        {
            var validator = new NewUserValidator();

            #region Init variables
            string        name          = "testname";
            StringBuilder stringBuilder = new StringBuilder('x');
            for (int i = 0; i < validator.surnameMaxLength + 1; i++)
            {
                stringBuilder.Append('x');
            }
            string surname = stringBuilder.ToString();
            string address = "*****@*****.**";
            #endregion
            NewUser mailReadUpdate = new NewUser
            {
                Name    = name,
                Surname = surname,
                Address = address
            };
            var result = validator.TestValidate(mailReadUpdate);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Surname);
            #endregion
        }
コード例 #2
0
        public ActionResult Add(UserViewModel uvm)
        {
            if (uvm.NewUserDto == null)
            {
                return(Redirect("Index"));
            }

            NewUserValidator validator = new NewUserValidator();
            ValidationResult result    = validator.Validate(uvm.NewUserDto);

            if (!result.IsValid)
            {
                result.Errors.ToList().ForEach(error =>
                {
                    ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                });

                return(View(uvm));
            }
            else if (uvm.IsExist(uvm.NewUserDto.Username))
            {
                ModelState.AddModelError("UserName", "User name has existed.");
                return(View(uvm));
            }
            else
            {
                uvm.NewUserDto.Userpwd = Common.Utility.EncryptUtils.MD5Encrypt(uvm.NewUserDto.Userpwd);
                uvm.AddNewUser(uvm.NewUserDto);
                return(Redirect("Index"));
            }
        }
コード例 #3
0
        public void validator_requires_user_resource()
        {
            var l = new Web.Resources.List();
            var sut = new NewUserValidator(Session);

            Assert.Throws<ArgumentException>(() => sut.Validate(l));
        }
コード例 #4
0
 public void no_email_address_adds_error()
 {
     var user = DataGenerator.GenerateWebModelUser();
     user.Email = String.Empty;
     var sut = new NewUserValidator(Session);
     var errorList = sut.Validate(user);
     Assert.IsTrue(errorList.Any(x=>x.Field=="Email"));
     Assert.AreEqual(1, errorList.Count);
 }
コード例 #5
0
        public async Task <JsonResult> RegisterUser(
            NewUserViewModel newUserViewModel,
            NewUserValidator newUserValidator,
            Guid dataSourceId)
        {
            Throw.IfNull(newUserViewModel, nameof(newUserViewModel));
            Throw.IfNull(newUserValidator, nameof(newUserValidator));

            _logger.Debug($"{nameof(RegisterUser)} has been called with model" +
                          $" {newUserViewModel.UserName} name, {newUserViewModel.Email} email.");

            newUserValidator.SetUserNameUniqueValidation(_registrationService.IsUserNameUnique);
            newUserValidator.SetEmailUniqueValidation(_registrationService.IsEmailUnique);

            var modelState = await newUserValidator.ValidateAsync(newUserViewModel);

            var validation = new List <string>();
            var success    = false;
            var emailSent  = false;

            if (modelState.IsValid)
            {
                _logger.Debug($"{nameof(RegisterUser)} has been called with valid model.");

                if (_registrationService.CreateUser(
                        newUserViewModel.UserName,
                        newUserViewModel.Email,
                        newUserViewModel.Password))
                {
                    success = true;

                    var emailDataSource = _sitecoreService.GetItem <IWelcomingEmail>(dataSourceId);

                    if (!(emailDataSource is null))
                    {
                        _emailService.SetDataSource(emailDataSource);
                    }

                    emailSent = await _emailService.SendEmail(newUserViewModel.Email);
                }
            }
            else
            {
                _logger.Warn($"{nameof(RegisterUser)} has been called with invalid model.");

                foreach (var error in modelState.Errors)
                {
                    validation.Add(error.ErrorMessage);
                }
            }

            return(Json(new { validation, success, emailSent }));
        }
コード例 #6
0
        public void WhenNewUserAddressHaveNoAt_ShouldtHaveError()
        {
            var validator = new NewUserValidator();

            #region Init variables
            string name    = "testname";
            string surname = "testsurname";
            string address = "testaddress.com";
            #endregion
            NewUser mailReadUpdate = new NewUser
            {
                Name    = name,
                Surname = surname,
                Address = address
            };
            var result = validator.TestValidate(mailReadUpdate);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Address);
            #endregion
        }
コード例 #7
0
        public void WhenNewUserIsOk_ShouldNotHaveAnyError()
        {
            var validator = new NewUserValidator();

            #region Init variables
            string name    = "testname";
            string surname = "testsurname";
            string address = "*****@*****.**";
            #endregion
            NewUser mailReadUpdate = new NewUser
            {
                Name    = name,
                Surname = surname,
                Address = address
            };
            var result = validator.TestValidate(mailReadUpdate);
            #region Tests
            result.ShouldNotHaveAnyValidationErrors();
            #endregion
        }