public async Task Should_GetUser_ById() { var user = new ApplicationUser { Id = 1.ToString(), Email = "*****@*****.**", FirstName = "test3", LastName = "test3", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = true, IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 1, LanguagePreference = "tr" } }; _context.Users.Add(user); _context.Roles.Add(new ApplicationRole { Id = 1.ToString(), Name = "Admin" }); _context.UserRoles.Add(new IdentityUserRole <string> { RoleId = 1.ToString(), UserId = 1.ToString() }); _context.SaveChanges(); var getUser = await _userApiViewModelService.GetUserById(1.ToString()); Assert.Equal(user.Email, getUser.Email); Assert.True(getUser.HasAuthority); Assert.Equal(user.FirstName, getUser.FirstName); Assert.Equal(user.LastName, getUser.LastName); Assert.Equal(user.CreatedAt, getUser.CreatedAt); Assert.True(getUser.IsActive); Assert.True(getUser.IsAdmin); }
public async Task <IActionResult> GetUser(string id) { var result = await _userViewModelService.GetUserById(id); if (result != null) { return(Ok(result)); } return(NotFound()); }
public async Task <object> Post([FromBody] LoginViewModel model) { if (!ModelState.IsValid) { return(BadRequest(Errors.GetErrorList(ModelState))); } var user = await _userService.GetUserByEmail(model.Email); if (user != null) { if (!await _userManager.IsEmailConfirmedAsync(user)) { ModelState.AddModelError("", _localizer["EmailNotConfirmed"]); return(BadRequest(Errors.GetErrorList(ModelState))); } if (await _userManager.CheckPasswordAsync(user, model.Password)) { var getUserWithImage = await _userViewModelService.GetUserById(user.Id); var getRoleAdmin = await _userService.GetUserRoles(user.Id); return(Ok(new { user.FirstName, user.LastName, Token = await GenerateEncodedToken(model.Email, user), user.Email, getUserWithImage.UserImage, IsAdmin = getRoleAdmin.Any(role => role.Name == "Admin"), user.TenantId, Lang = user.UserDetail.LanguagePreference, user.Id })); } var list = new List <ErrorViewModel> { new ErrorViewModel { Title = _localizer["InvalidLoginAttempt"], Description = _localizer["InvalidUserOrPass"] } }; return(BadRequest(list)); } { var list = new List <ErrorViewModel> { new ErrorViewModel { Title = _localizer["InvalidLoginAttempt"], Description = _localizer["InvalidUserOrPass"] } }; return(BadRequest(list)); } }