public async Task CreateClientShouldWorkWithCorrectData() { var clients = new List <Client>(); this.clientRepository.Setup(r => r.AllAsNoTracking()).Returns(() => clients.AsQueryable()); this.clientRepository.Setup(r => r.AddAsync(It.IsAny <Client>())).Callback((Client client) => clients.Add(client)); var model = new CreateClientInputModel { FullName = "Client One", PositionPlayed = PositionName.PowerForward, Email = "*****@*****.**", HasExperience = true, Phone = "088", }; var appUser = new ApplicationUser { Id = "clientuserId", Email = "*****@*****.**" }; await this.clientsService.CreateClientAsync(model, appUser); Assert.Contains(clients, x => x.Name == "Client One"); Assert.Single(clients); this.clientRepository.Verify(x => x.AllAsNoTracking(), Times.Never); }
public async Task <bool> CreateClientAsync(CreateClientInputModel input, ApplicationUser user) { var client = new Client { Name = input.FullName, HasBasketballExperience = input.HasExperience, Phone = input.Phone, PositionPlayed = input.PositionPlayed, User = user, UserId = user.Id, }; if (client != null && !string.IsNullOrEmpty(input.Phone)) { await this.clientRepository.AddAsync(client); await this.clientRepository.SaveChangesAsync(); return(true); } throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionWhileCreatingClient); }
public async Task CreateClientShouldThrowIOEWithInvalidData() { var clients = new List <Client>(); this.clientRepository.Setup(r => r.AllAsNoTracking()).Returns(() => clients.AsQueryable()); this.clientRepository.Setup(r => r.AddAsync(It.IsAny <Client>())).Callback((Client client) => clients.Add(client)); var model = new CreateClientInputModel { FullName = string.Empty, PositionPlayed = PositionName.PowerForward, Email = string.Empty, HasExperience = false, Phone = string.Empty, }; var appUser = new ApplicationUser { Id = string.Empty, Email = string.Empty }; await Assert.ThrowsAsync <InvalidOperationException>(() => this.clientsService.CreateClientAsync(model, appUser)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (Input.SelectedRole == "Coach") { var coachInputModel = new CreateCoachInputModel { FullName = Input.FullName, Email = Input.Email, Description = Input.Description, Experience = Input.Experience.HasValue ? Input.Experience.Value : 1, Phone = Input.Phone, UserImage = Input.UserImage, }; var coachUser = _userManager.FindByEmailAsync(coachInputModel.Email).Result; await _coachService.CreateCoachAsync(coachInputModel, coachUser); await _userManager.AddToRoleAsync(user, GlobalConstants.CoachRoleName); } else { var clientInputModel = new CreateClientInputModel { FullName = Input.FullName, Email = Input.Email, PositionPlayed = Input.PositionPlayed, HasExperience = Input.HasExperience, Phone = Input.Phone, }; var clientUser = _userManager.FindByEmailAsync(clientInputModel.Email).Result; await _clientService.CreateClientAsync(clientInputModel, clientUser); await _userManager.AddToRoleAsync(user, GlobalConstants.ClientRoleName); } if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }