Skip to content

Commit

Permalink
P4ADEV-1341 add JWTUtilsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
antocalo committed Nov 22, 2024
1 parent 717efe0 commit 3020f16
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.gov.pagopa.payhub.pdnd.utils;

import static org.junit.jupiter.api.Assertions.*;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import java.util.Date;
import org.junit.jupiter.api.Test;

class JWTUtilsTest {

@Test
public void givenValidTokenWhenIsJWTExpiredThenTokenNotExpired() {
// Given
Date futureDate = new Date(System.currentTimeMillis() + 3600 * 1000); // 1 hour from now
String token = JWT.create()
.withExpiresAt(futureDate)
.sign(com.auth0.jwt.algorithms.Algorithm.HMAC256("secret"));

Check failure

Code scanning / CodeQL

Hard-coded credential in API call Critical test

Hard-coded value flows to
sensitive API call
.

// Then
assertFalse(JWTUtils.isJWTExpired(token));
}

@Test
public void givenExpiredTokenWhenIsJWTExpiredThenTokenExpired() {
// Given
Date pastDate = new Date(System.currentTimeMillis() - 3600 * 1000); // 1 hour ago
String token = JWT.create()
.withExpiresAt(pastDate)
.sign(com.auth0.jwt.algorithms.Algorithm.HMAC256("secret"));

Check failure

Code scanning / CodeQL

Hard-coded credential in API call Critical test

Hard-coded value flows to
sensitive API call
.
// Then
assertTrue(JWTUtils.isJWTExpired(token));
}

@Test
public void givenInvalidTokenWhenIsJWTExpiredThenException() {
// Given
String invalidtoken = "INVALIDTOKEN";
// Then
assertThrows(JWTDecodeException.class, () -> JWTUtils.isJWTExpired(invalidtoken));
}
}

0 comments on commit 3020f16

Please sign in to comment.