generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/test/java/it/gov/pagopa/payhub/pdnd/utils/JWTUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Error loading related location Loading |
||
|
||
// 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 Error loading related location Loading |
||
// Then | ||
assertTrue(JWTUtils.isJWTExpired(token)); | ||
} | ||
|
||
@Test | ||
public void givenInvalidTokenWhenIsJWTExpiredThenException() { | ||
// Given | ||
String invalidtoken = "INVALIDTOKEN"; | ||
// Then | ||
assertThrows(JWTDecodeException.class, () -> JWTUtils.isJWTExpired(invalidtoken)); | ||
} | ||
} |