fix: update JwtService to handle default expiration and add tests for token generation
All checks were successful
Build And Publish Production Image / Build And Publish Production Image (push) Successful in 39s

This commit is contained in:
2026-03-28 03:40:03 -03:00
parent 8f508034d5
commit 2e2e75fe87
5 changed files with 34 additions and 4 deletions

View File

@@ -40,7 +40,7 @@ class AuthServiceTest {
fun should_returnValidClaims_when_jwtTokenParsed() {
val realJwtService = JwtService(
secret = "test-secret-key-for-testing-only-must-be-at-least-32-characters",
expirationMs = 86400000L
expirationMsRaw = "86400000"
)
val token = realJwtService.generateToken()
@@ -51,7 +51,7 @@ class AuthServiceTest {
fun should_returnFalse_when_expiredTokenValidated() {
val realJwtService = JwtService(
secret = "test-secret-key-for-testing-only-must-be-at-least-32-characters",
expirationMs = 1L
expirationMsRaw = "1"
)
val token = realJwtService.generateToken()

View File

@@ -0,0 +1,26 @@
package com.condado.newsletter.service
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.security.Keys
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
class JwtServiceTest {
private val secret = "12345678901234567890123456789012"
@Test
fun should_generate_token_when_expiration_is_empty() {
val jwtService = JwtService(secret, "")
val token = jwtService.generateToken()
val claims = Jwts.parser()
.verifyWith(Keys.hmacShaKeyFor(secret.toByteArray(Charsets.UTF_8)))
.build()
.parseSignedClaims(token)
.payload
assertTrue(claims.expiration.after(claims.issuedAt))
}
}