public async Task SendEmailConfirmationForNewRegistration()
        {
            // Arrange
            var emailSenderMock = new Mock <IEmailSender>();

            // Act
            await EmailSenderExtensions.SendEmailConfirmationAsync(emailSenderMock.Object, TESTEMAIL, TESTLINK, false).ConfigureAwait(false);

            // Assert
            emailSenderMock.Verify(
                mock => mock.SendEmailAsync(
                    TESTEMAIL,
                    "Potwierdzenie założenia konta",
                    It.Is <string>(str => str.StartsWith("Prosimy o potwierdzenie założenia konta poprzez kliknięcie na link", System.StringComparison.InvariantCulture))),
                Times.Once());
        }
Exemple #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            ViewData["Companies"] = _databaseContext.Companies.ToList();
            ViewData["Cities"]    = _databaseContext.Cities.ToList();
            if (ModelState.IsValid)
            {
                Company company;

                if (model.CompanyType == "new")
                {
                    // Additional validation before creating the Company
                    var requiredFields = new[]
                    {
                        new Tuple <string, object>("Name", model.Company.Name),
                        new Tuple <string, object>("Address", model.Company.Address),
                        new Tuple <string, object>("CityId", model.Company.CityId),
                        new Tuple <string, object>("Logo", model.Company.Logo),
                        new Tuple <string, object>("Description", model.Company.Description),
                        new Tuple <string, object>("Website", model.Company.Website),
                    };

                    foreach (var field in requiredFields)
                    {
                        if (field.Item2 == null || field.Item2.Equals(""))
                        {
                            ModelState.AddModelError(string.Empty, $"{field.Item1} field is required.");
                        }
                    }
                    if (!ModelState.IsValid)
                    {
                        return(View(model));
                    }
                    // Create the company
                    company = model.Company;
                    _databaseContext.Companies.Add(company);
                }
                else
                {
                    company = _databaseContext.Companies.Find(model.CompanyId);
                }

                var user = new User
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    Name      = model.Name,
                    Surname   = model.Surname,
                    Phone     = model.Phone,
                    Published = DateTime.Now,
                    Company   = company
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await EmailSenderExtensions.SendEmailConfirmationAsync(_emailSender, model.Email, callbackUrl);

                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }