public List <JobApplicationUser> SelectByPersonId(int id) { List <JobApplicationUser> jaList = null; DataProvider.ExecuteCmd(GetConnection, "dbo.JobApplication_SelectByPersonId", inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@PersonId", id); } , map : delegate(IDataReader reader, short set) { switch (set) { case 0: JobApplicationUser ja = MapJobApplication(reader); if (jaList == null) { jaList = new List <JobApplicationUser>(); } jaList.Add(ja); break; case 1: JobPosting jp = MapJobPosting(reader); JobApplicationUser jobApp = jaList.Find(app => app.JobPostingId == jp.Id); jobApp.JobPosting = jp; break; } } ); return(jaList); }
public async Task GetTokenWithClaimsPrincipal_ValidUser_ReturnsTokenWithClaimPrincipals() { // Arrange var model = new LoginModel { Email = "*****@*****.**", Password = "******" }; var user = new JobApplicationUser(model.Email); _userManager.FindByEmailAsync(model.Email).Returns(Task.FromResult(user)); _userManager.CheckPasswordAsync(user, model.Password).Returns(Task.FromResult(true)); var tokenWithClaimsPrincipal = new TokenWithClaimsPrincipal(); _tokenGenerator.GetAccessTokenWithClaimsPrincipal(user.UserName, Arg.Any <IEnumerable <Claim> >()).Returns(tokenWithClaimsPrincipal); // Act var result = await _accountManagerService.GetTokenWithClaimsPrincipal(model).ConfigureAwait(false); // Assert Received.InOrder(async() => { await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false); await _userManager.CheckPasswordAsync(user, model.Password).ConfigureAwait(false); }); Assert.Equal(tokenWithClaimsPrincipal, result); }
public async Task GetJwtToken_ValidUser_AddsRolesClaim(List <string> roles) { // Arrange var model = new LoginModel { Email = "*****@*****.**", Password = "******" }; var user = new JobApplicationUser(model.Email); _userManager.FindByEmailAsync(model.Email).Returns(Task.FromResult(user)); _userManager.CheckPasswordAsync(user, model.Password).Returns(Task.FromResult(true)); _userManager.GetRolesAsync(user).Returns(roles); var claims = new List <Claim> { new Claim(JwtClaimIdentifiers.Id, user.Id) }; claims.AddRange(roles.Select(role => new Claim(JwtClaimIdentifiers.Role, role))); // Act await _accountManagerService.GetJwtToken(model).ConfigureAwait(false); // Assert _tokenGenerator.Received(1) .GetJwtToken(user.UserName, Arg.Is <IEnumerable <Claim> >(c => claims.SequenceEqual(c, new ClaimEqualityComparer()))); }
public async Task <IEnumerable <JobApplication> > GetAllApplications() { JobApplicationUser user = await _userResolver.GetCurrentUser().ConfigureAwait(false); List <JobApplicationEntity> jobApplications = await _repository.GetAll(f => f.Owner.Equals(user)).ToListAsync().ConfigureAwait(false); return(_mapper.Map <IEnumerable <JobApplication> >(jobApplications)); }
public async Task <IdentityResult> RegisterUser(RegisterModel model) { var jobApplicationUser = new JobApplicationUser(model.Email); var result = await _userManager.CreateAsync(jobApplicationUser, model.Password).ConfigureAwait(false); return(result); }
public async Task <JobApplication> AddNewApplication(JobApplication jobApplication) { JobApplicationUser user = await _userResolver.GetCurrentUser().ConfigureAwait(false); JobApplicationEntity jobApplicationEntity = _mapper.Map <JobApplication, JobApplicationEntity>(jobApplication, opt => opt.AfterMap((src, dest) => dest.Owner = user)); await _repository.Create(jobApplicationEntity).ConfigureAwait(false); return(_mapper.Map <JobApplication>(jobApplicationEntity)); }
private async Task <IEnumerable <Claim> > AddDefaultClaims(JobApplicationUser user) { var claims = new List <Claim> { new Claim(JwtClaimIdentifiers.Id, user.Id) }; IList <string> roles = await _userManager.GetRolesAsync(user).ConfigureAwait(false); claims.AddRange(roles.Select(role => new Claim(JwtClaimIdentifiers.Role, role))); return(claims); }
public JobApplication GetApplication(string companyName) { JobApplicationUser user = _userResolver.GetCurrentUser().Result; JobApplicationEntity jobApplication = _repository.GetOne(f => f.Owner.Equals(user) && f.CompanyName.Equals(companyName, StringComparison.Ordinal)); if (jobApplication != null) { return(_mapper.Map <JobApplication>(jobApplication)); } return(null); }
private static JobApplicationUser MapJobApplication(IDataReader reader) { JobApplicationUser ja = new JobApplicationUser(); int ord = 0; ja.Id = reader.GetSafeInt32(ord++); ja.PersonId = reader.GetSafeInt32(ord++); ja.JobPostingId = reader.GetSafeInt32(ord++); ja.CoverLetter = reader.GetSafeString(ord++); ja.Status = reader.GetSafeString(ord++); ja.DateCreated = reader.GetSafeDateTime(ord++); ja.DateModified = reader.GetSafeDateTime(ord++); return(ja); }
private async Task SetPasswordForUser(JobApplicationUser user, string password) { _logger.LogInformation($"Set password for user '{user.UserName}'"); var ir = await _userManager.AddPasswordAsync(user, password).ConfigureAwait(false); if (ir.Succeeded) { _logger.LogTrace($"Password set for user '{user.UserName}' successfully"); } else { var exception = new ApplicationException($"Password for the user '{user.UserName}' cannot be set"); _logger.LogError(exception, GetIdentityErrorsInCommaSeperatedList(ir)); throw exception; } }
private async Task AddRoleToUser(string role, JobApplicationUser user) { _logger.LogInformation($"Add user {user.UserName}' to role '{role}'"); var ir = await _userManager.AddToRoleAsync(user, role).ConfigureAwait(false); if (ir.Succeeded) { _logger.LogDebug($"Added the role '{role}' to default user 'admin' successfully"); } else { var exception = new ApplicationException($"The role `{role}` cannot be set for the user 'admin'"); _logger.LogError(exception, GetIdentityErrorsInCommaSeperatedList(ir)); throw exception; } }
public async Task <JobApplication> UpdateApplication(string companyName, JobApplication newApplication) { JobApplicationUser user = await _userResolver.GetCurrentUser().ConfigureAwait(false); JobApplicationEntity oldJobApplication = _repository.GetOne(f => f.Owner.Equals(user) && f.CompanyName.Equals(companyName, StringComparison.Ordinal)); if (oldJobApplication == null) { return(null); } _mapper.Map(newApplication, oldJobApplication); await _repository.Update(oldJobApplication).ConfigureAwait(false); return(_mapper.Map <JobApplication>(oldJobApplication)); }
public async Task Login_Succeeds_RedirectsToJobApplications() { // Arrange var userManager = _fixture.Services.GetRequiredService <UserManager <JobApplicationUser> >(); var credentials = new LoginModel { Email = "*****@*****.**", Password = "******" }; var stringContent = new StringContent(credentials.ToUrl(), Encoding.UTF8, "application/x-www-form-urlencoded"); var user = new JobApplicationUser(credentials.Email); await userManager.CreateAsync(user, credentials.Password).ConfigureAwait(false); // Act var response = await _fixture.WebClient.PostAsync(new Uri(_baseUri, "login"), stringContent).ConfigureAwait(false); // Assert Assert.Equal(HttpStatusCode.Found, response.StatusCode); Assert.Equal("/JobApplications", response.Headers.Location.OriginalString); }
public async Task Register_PasswordAndConfirmationDoesNotMatch_ShowsError() { // Arrange var userManager = _fixture.Services.GetRequiredService <UserManager <JobApplicationUser> >(); var credentials = new RegisterModel { Email = "*****@*****.**", Password = "******", ConfirmPassword = "******" }; var stringContent = new StringContent(credentials.ToUrl(), Encoding.UTF8, "application/x-www-form-urlencoded"); var user = new JobApplicationUser(credentials.Email); await userManager.CreateAsync(user, credentials.Password).ConfigureAwait(false); // Act var response = await _fixture.WebClient.PostAsync(_baseUri, stringContent).ConfigureAwait(false); // Assert var responseData = response.Content.ReadAsStringAsync().Result; Assert.Contains("The password and confirmation password do not match.", responseData); }
public async Task GetTokenWithClaimsPrincipal_IncorrectPassword_ReturnsNull() { // Arrange var model = new LoginModel { Email = "*****@*****.**", Password = "******" }; var user = new JobApplicationUser(model.Email); _userManager.FindByEmailAsync(model.Email).Returns(Task.FromResult(user)); _userManager.CheckPasswordAsync(user, model.Password).Returns(Task.FromResult(false)); // Act var result = await _accountManagerService.GetTokenWithClaimsPrincipal(model).ConfigureAwait(false); // Assert Received.InOrder(async() => { await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false); await _userManager.CheckPasswordAsync(user, model.Password).ConfigureAwait(false); }); Assert.Null(result); }
public JobApplicationServiceTest() { _jobApplicationRepository = Substitute.For <IRepository <JobApplicationEntity> >(); _mapper = Substitute.For <IMapper>(); _userResolver = Substitute.For <IUserResolverService>(); _user = new JobApplicationUser("*****@*****.**"); _jobApplicationService = new JobApplicationService(_jobApplicationRepository, _mapper, _userResolver); _jobApplications = new[] { new JobApplication { CompanyName = "Company 1", ApplicationDate = new DateTime(2017, 11, 13), Status = Status.Rejected }, new JobApplication { CompanyName = "Company 2", ApplicationDate = new DateTime(2017, 11, 14), Status = Status.Applied }, new JobApplication { CompanyName = "Company 3", ApplicationDate = new DateTime(2017, 11, 14), Status = Status.Interview }, new JobApplication { CompanyName = "Company 4", ApplicationDate = new DateTime(2017, 10, 9), Status = Status.Offer } }; _jobApplicationEntities = new List <JobApplicationEntity>(); foreach (var jobApplication in _jobApplications) { var jobApplicationEntity = new JobApplicationEntity { Owner = _user, CompanyName = jobApplication.CompanyName, ApplicationDate = jobApplication.ApplicationDate, CurrentStatus = jobApplication.Status }; _jobApplicationEntities.Add(jobApplicationEntity); } }
private async Task <JobApplicationUser> CreateUser(string email) { _logger.LogInformation($"Create user with email '{email}' for application"); var user = new JobApplicationUser(email); var ir = await _userManager.CreateAsync(user).ConfigureAwait(false); if (ir.Succeeded) { _logger.LogDebug($"Created user with email '{email}' successfully"); } else { var exception = new ApplicationException($"user with '{email}' cannot be created"); _logger.LogError(exception, GetIdentityErrorsInCommaSeperatedList(ir)); throw exception; } var createdUser = await _userManager.FindByNameAsync(email).ConfigureAwait(false); return(createdUser); }