public void GenerateToken_ValidCredentials_TokenShouldContainEmailAndUserId() { // Arrange var manager = new AccountManagementTest(new SecuritySettings { JwtSecret = "S3cR3+ S3cR3+ S3cR3+ S3cR3+ S3cR3+" }); var userEmail = "*****@*****.**"; var id = ObjectId.GenerateNewId(); var user = new User { AccountId = ObjectId.GenerateNewId(), Email = userEmail, Id = id, Username = "******" }; // Act var token = manager.GenerateToken(user); var claims = TokenHandler.ReadJwtToken(token).Claims.ToList(); var userClaim = ObjectId.Parse(claims.FirstOrDefault(c => c.Type == "UserId").Value); var emailClaim = claims.FirstOrDefault(c => c.Type == "Email").Value; // Assert userClaim.Should().BeEquivalentTo(id); emailClaim.Should().BeEquivalentTo(userEmail); }
public void GenerateToken_JwtSecretIsShort_ShouldThrowError() { // Arrange var manager = new AccountManagementTest(new SecuritySettings { JwtSecret = "S3cR3+" }); var userEmail = "*****@*****.**"; var id = ObjectId.GenerateNewId(); var user = new User { AccountId = ObjectId.GenerateNewId(), Email = userEmail, Id = id, Username = "******" }; Func <string> act = () => manager.GenerateToken(user, TimeSpan.FromSeconds(10)); act.Should().Throw <Exception>("Token is too short"); }