C#C
C#3y ago
Vortac

ASP .NET 7 - Testing a JWT Token Generator

I have an ASP .NET web api which calls a CreateToken() method inside a TokenService to generate a JWT when a user logs in. The public implementation looks like this:

    public string CreateToken(ApplicationUser user)
    {
        var expiration = DateTime.UtcNow.AddMinutes(ExpirationMinutes);
        var token = CreateJwtToken(
            CreateClaims(user),
            CreateSigningCredentials(),
            expiration
        );
        var tokenHandler = new JwtSecurityTokenHandler();
        
        _logger.LogInformation("JWT Token created");
        
        return tokenHandler.WriteToken(token);
    }


There's also 3 private helper methods inside TokenService which are used to help generate the token. I should only be testing the public method correct? Also, what should I be testing for here? I've done basic unit tests before (like testing an AddNumbers(int 1, int 2)), but am I supposed to generate the token in the test and then compare it to a "correctly" generated version?
Was this page helpful?