public async Task TestSkillsManagement() { StudentifyUser user = new StudentifyUser() { UserName = "******", }; Skill skill = new Skill() { Name = "skill", Owner = null, Rate = 50 }; var addedAccount = new StudentifyAccount { StudentifyUserId = user.Id, User = user }; await _repository.Insert.One(addedAccount); var account = await _repository.SelectByUsername("skilled-user"); await _repository.SaveSkill(account.Id, skill); var skills = await _repository.GetSkills(account.Id); Assert.IsTrue(skills.Contains(skill)); }
public async Task TestGetThreadsForSpecifiedUser() { StudentifyAccountsRepository studentifyAccountsRepository = new StudentifyAccountsRepository(_context, new SelectRepositoryBase <StudentifyAccount>(_context), new UpdateRepositoryBase <StudentifyAccount>(_context), new InsertRepositoryBase <StudentifyAccount>(_context)); StudentifyUser user = new StudentifyUser { UserName = "******" }; StudentifyAccount account = new StudentifyAccount { StudentifyUserId = user.Id, User = user }; Thread insertedThread = new Thread() { ReferencedEventId = 1, ReferencedEvent = new Info { Author = account }, UserAccount = account }; await studentifyAccountsRepository.Insert.One(account); await _repository.Insert.One(insertedThread); var threads = await _repository.SelectAllUserRelatedThreads(account.UserName); Assert.True(threads.Any()); }
public async Task <IActionResult> Register([FromBody] RegisterDto dto) { var user = new StudentifyUser() { Email = dto.Email, FirstName = dto.FirstName, LastName = dto.LastName, SecurityStamp = Guid.NewGuid().ToString(), UserName = dto.Username }; var result = await _userManager.CreateAsync(user, dto.Password); if (!result.Succeeded) { var errors = result.Errors; return(StatusCode(StatusCodes.Status500InternalServerError, errors)); } var account = new StudentifyAccount { StudentifyUserId = user.Id, User = user }; await _accountsRepository.Insert.One(account); var token = await GenerateToken(user); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token), expiration = token.ValidTo, account })); }
private async Task <JwtSecurityToken> GenerateToken(StudentifyUser user) { var userRoles = await _userManager.GetRolesAsync(user); var authClaims = new List <Claim> { new Claim(ClaimTypes.Name, user.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), }; foreach (var userRole in userRoles) { authClaims.Add(new Claim(ClaimTypes.Role, userRole)); } var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"])); var token = new JwtSecurityToken( issuer: _configuration["JWT:ValidIssuer"], audience: _configuration["JWT:ValidAudience"], expires: DateTime.Now.AddDays(3), claims: authClaims, signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256) ); return(token); }
public async Task TestSelectByUsername() { StudentifyUser user = new StudentifyUser() { UserName = "******", }; var addedAccount = new StudentifyAccount { StudentifyUserId = user.Id, User = user }; await _repository.Insert.One(addedAccount); var account = await _repository.SelectByUsername("test-user"); Assert.IsTrue(account != null); }
public async Task CreateAccountSuccess() { const int expectedNumberOfAccounts = 1; StudentifyUser user = new StudentifyUser() { UserName = "******", }; var account = new StudentifyAccount { StudentifyUserId = user.Id, User = user }; await _repository.Insert.One(account); var accounts = (await _repository.Select.All()) .Where(acc => acc.StudentifyUserId == user.Id).ToList(); Assert.AreEqual(expectedNumberOfAccounts, accounts.Count); }