public void TryGetCurrentModeratorWithUnknownUser(ModeratorDto moderator, bool success) { "Given that the moderator has the required claim" .x(() => _httpContextAccessor .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >())) .Returns(false)); "And that the moderator identifier does not exists in its JWT" .x(() => { var mockedClaimsPrincipal = new Mock <ClaimsPrincipal>(); mockedClaimsPrincipal .Setup(_ => _.FindFirst(It.IsAny <string>())) .Returns((Claim)null); _httpContextAccessor .SetupGet(_ => _.HttpContext.User) .Returns(mockedClaimsPrincipal.Object); }); "When the system attempts to retrieve the current moderator" .x(() => success = _authenticationService.TryGetCurrentModerator(out moderator)); "Then the system should not have been able to fetch the user" .x(() => { moderator.Should().BeNull(); success.Should().BeFalse(); }); }
/// <summary> /// Generate a unique identifier for a hub given its owner /// </summary> /// <param name="hub">The <see cref="HubCreationDto" /> containing the information on the hub to be created</param> /// <param name="moderator">The <see cref="ModeratorDto" /> containing the owner's data</param> /// <returns>The Hub specific link</returns> public static string GenerateLink(HubCreationDto hub, ModeratorDto moderator) { var toHash = Encoding.UTF8.GetBytes(hub.Name + moderator.Id + moderator.Nickname); using var sha256Managed = new SHA256Managed(); return(WebEncoders.Base64UrlEncode( sha256Managed.ComputeHash(toHash))); }
public void TryGetCurrentModeratorWithBadRole(ModeratorDto moderator, bool success) { "Given that the moderator does not have the required claim" .x(() => _httpContextAccessor .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >())) .Returns(true)); "When the system attempts to retrieve the current moderator" .x(() => success = _authenticationService.TryGetCurrentModerator(out moderator)); "Then the system should not have been able to fetch the user" .x(() => { moderator.Should().BeNull(); success.Should().BeFalse(); }); }
public void TryGetCurrentModerator(ModeratorDto moderator, bool success) { "Given that the moderator has the required claim" .x(() => _httpContextAccessor .Setup(_ => _.HttpContext.User.HasClaim(It.IsAny <Predicate <Claim> >())) .Returns(false)); "And that the moderator has the NameIdentifier claim" .x(() => { var mockedClaimsPrincipal = new Mock <ClaimsPrincipal>(); mockedClaimsPrincipal .Setup(_ => _.FindFirst(It.IsAny <string>())) .Returns( new Claim(ClaimTypes.NameIdentifier, _fixture.Create <int>().ToString())); _httpContextAccessor .SetupGet(_ => _.HttpContext.User) .Returns(mockedClaimsPrincipal.Object); }); "When the system retrieves the current moderator" .x(() => { _moderatorService .Setup(_ => _.GetModerator(It.IsAny <int>())) .Returns(new ModeratorDto()); success = _authenticationService.TryGetCurrentModerator(out moderator); }); "Then the system should not have been able to fetch the user" .x(() => { moderator.Should().NotBeNull(); success.Should().BeTrue(); }); }
/// <inheritdoc cref="IJwtService.GetModeratorToken" /> public string GetModeratorToken(ModeratorDto moderator) { var claims = new List <Claim> { new Claim(ClaimTypes.Role, InTechNetRoles.Moderator), new Claim(ClaimTypes.NameIdentifier, moderator.Id.ToString()) }; var token = new JwtSecurityToken( _jwtResource.Issuer, _jwtResource.Audience, expires: _jwtResource.ValidUntil, signingCredentials: new SigningCredentials ( new SymmetricSecurityKey(_jwtResource.EncodedSecretKey), InTechNetSecurity.JwtSigningAlgorithm ), claims: claims ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public void GetAuthenticatedModerator(ModeratorDto moderator) { "Given that the authentication is successful" .x(() => _moderatorService .Setup(_ => _.AuthenticateModerator(It.IsAny <AuthenticationDto>())) .Returns(new ModeratorDto())); "When the moderator authenticate itself" .x(() => { _jwtService .Setup(_ => _.GetModeratorToken(It.IsAny <ModeratorDto>())) .Returns(_fixture.Create <string>()); moderator = _authenticationService.GetAuthenticatedModerator( _fixture.Create <AuthenticationDto>()); }); "Then the moderator should have a token" .x(() => moderator.Token.Should() .NotBeNullOrEmpty()); }