public async void ProfileController_RegisterWithInValidData()
        {
            //Arange
            var model = new RegisterViewModel { Password = "******", PasswordAgain = "short", Username = "******" };

            _userManager.Setup(x => x.CreateAsync(It.IsAny<User>(), model.Password)).ReturnsAsync(IdentityResult.Failed("Error"));
            
            //Act
            var result = await _sut.Register(model) as ViewResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.AreEqual(1, _sut.ModelState.Count);
        }
        public async void ProfileController_RegisterWithValidDataWithModelError()
        {
            //Arange
            var model = new RegisterViewModel { Password = "******", PasswordAgain = "test12345", Username = "******" };

            _sut.ModelState.AddModelError("error", "error");

            //Act
            var result = await _sut.Register(model) as ViewResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.AreEqual(model, result.Model);
            Assert.AreEqual(1, _sut.ModelState.Count);
        }
        public async void ProfileController_RegisterWithValidData()
        {
            //Arange
            var model = new RegisterViewModel { Password = "******", PasswordAgain = "tester12345", Username = "******" };

            _userManager.Setup(x => x.CreateAsync(It.IsAny<User>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success);
            
            //Act
            var result = await _sut.Register(model) as RedirectToRouteResult;

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.AreEqual(0, _sut.ModelState.Count);
            Assert.AreEqual("Start", result.RouteValues["action"]);
            Assert.AreEqual("Main", result.RouteValues["controller"]);
        }
예제 #4
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = new User() { UserName = model.Username, Email = model.EmailAdress };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, true, true);
                return RedirectToAction("Start", "Main");
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error);
                }
            }

            return View(model);
        }