Ejemplo n.º 1
0
        public void Registration_RegisterNewUserToEmptyDataBase()
        {
            var(repoMock, controller, authOption) = GetDefalt();
            ApplicationUser createdUser  = null;
            string          UsedPassword = null;

            repoMock
            .Setup(R => R.CreateUserAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>()))
            .Callback <ApplicationUser, string>((U, P) => (createdUser, UsedPassword) = (U, P))
            .Returns(Task.FromResult(IdentityResult.Success));

            var registerModel = new RegisterModel.InputModel()
            {
                Email           = "*****@*****.**",
                Password        = "******",
                ConfirmPassword = "******",
                Sex             = true,
                Name            = "Тест",
                Surname         = "Тестеров",
                PhoneNumber     = "+78885553535",
                Birthday        = "25/03/1995"
            };
            var result = controller.Registration(registerModel).Result;

            Assert.AreEqual(StatusCode.OK, result.StatusCode);
            repoMock.Verify(R => R.CreateUserAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>()), Times.Once);
            Assert.AreEqual(registerModel.Email, createdUser.Email);
            Assert.AreEqual(registerModel.Password, UsedPassword);
            Assert.AreNotEqual(registerModel.Password, createdUser.PasswordHash);
            Assert.AreEqual(registerModel.Sex, createdUser.Sex);
            Assert.AreEqual(registerModel.Name, createdUser.Name);
            Assert.AreEqual(registerModel.Surname, createdUser.Surname);
            Assert.AreEqual(registerModel.PhoneNumber, createdUser.PhoneNumber);
            Assert.AreEqual(registerModel.ParsedBirthday(), createdUser.Birthday);
        }
Ejemplo n.º 2
0
        public async Task <ResponseBase> Registration([FromBody] RegisterModel.InputModel model)
        {
            if (ModelState.IsValid)
            {
                await CheckRegistrationsArgs(model);

                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    Name        = model.Name,
                    Surname     = model.Surname,
                    PhoneNumber = model.PhoneNumber,
                    Sex         = model.Sex,
                    Birthday    = model.ParsedBirthday()
                };

                var result = await repository.CreateUserAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    logger.LogInformation(3, "User created a new account with password.");
                    return(ResponseBase.OKResponse);
                }
                logger.LogWarning(string.Join(" ", result.Errors.Select(E => $"{E.Code}---{E.Description}")));
                logger.LogWarning("Model valid but error");
                logger.LogWarning(JsonConvert.SerializeObject(ModelState, Formatting.Indented));
                logger.LogWarning(JsonConvert.SerializeObject(model, Formatting.Indented));
            }
            //ModelState.
            logger.LogWarning("Model not valid");
            logger.LogWarning(JsonConvert.SerializeObject(ModelState, Formatting.Indented));
            logger.LogWarning(JsonConvert.SerializeObject(model, Formatting.Indented));
            throw new ArgumentException();
        }