Fixes test error caused by groovy type coercion (#509)

* Fixes test error caused by groovy type coercion

A long was being treated as a float

* Fixes test error caused by groovy type coercion

A long was being treated as a float
This commit is contained in:
Brian Demers 2019-10-03 12:09:23 -04:00 committed by GitHub
parent 56db77ed7e
commit 7eb9c514b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 24 deletions

View File

@ -0,0 +1,16 @@
package io.jsonwebtoken
final class DateTestUtils {
/**
* Date util method for lopping truncate the millis from a date.
* @param date input date
* @return The date time in millis with the precision of seconds
*/
static long truncateMillis(Date date) {
Calendar cal = Calendar.getInstance()
cal.setTime(date)
cal.set(Calendar.MILLISECOND, 0)
return cal.getTimeInMillis()
}
}

View File

@ -28,6 +28,7 @@ import java.security.SecureRandom
import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE
import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE
import static org.junit.Assert.* import static org.junit.Assert.*
import static io.jsonwebtoken.DateTestUtils.truncateMillis
class DeprecatedJwtParserTest { class DeprecatedJwtParserTest {
@ -972,10 +973,7 @@ class DeprecatedJwtParserTest {
requireIssuedAt(issuedAt). requireIssuedAt(issuedAt).
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getIssuedAt().getTime(), truncateMillis(issuedAt)
def issuedAtMillis = ((long) issuedAt.getTime() / 1000) * 1000
assertEquals jwt.getBody().getIssuedAt().getTime(), issuedAtMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)
@ -1280,10 +1278,7 @@ class DeprecatedJwtParserTest {
requireExpiration(expiration). requireExpiration(expiration).
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getExpiration().getTime(), truncateMillis(expiration)
def expirationMillis = ((long) expiration.getTime() / 1000) * 1000
assertEquals jwt.getBody().getExpiration().getTime(), expirationMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)
@ -1332,10 +1327,7 @@ class DeprecatedJwtParserTest {
requireNotBefore(notBefore). requireNotBefore(notBefore).
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getNotBefore().getTime(), truncateMillis(notBefore)
def notBeforeMillis = ((long) notBefore.getTime() / 1000) * 1000
assertEquals jwt.getBody().getNotBefore().getTime(), notBeforeMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)
@ -1564,4 +1556,16 @@ class DeprecatedJwtParserTest {
assertEquals 'JWT string has a digest/signature, but the header does not reference a valid signature algorithm.', se.message assertEquals 'JWT string has a digest/signature, but the header does not reference a valid signature algorithm.', se.message
} }
} }
/**
* Date util method for lopping truncate the millis from a date.
* @param date input date
* @return The date time in millis with the precision of seconds
*/
private long truncateMillis(Date date) {
Calendar cal = Calendar.getInstance()
cal.setTime(date)
cal.set(Calendar.MILLISECOND, 0)
return cal.getTimeInMillis()
}
} }

View File

@ -27,6 +27,7 @@ import java.security.SecureRandom
import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE
import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE
import static io.jsonwebtoken.DateTestUtils.truncateMillis
import static org.junit.Assert.* import static org.junit.Assert.*
class JwtParserTest { class JwtParserTest {
@ -985,10 +986,7 @@ class JwtParserTest {
build(). build().
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getIssuedAt().getTime(), truncateMillis(issuedAt), 0
def issuedAtMillis = ((long) issuedAt.getTime() / 1000) * 1000
assertEquals jwt.getBody().getIssuedAt().getTime(), issuedAtMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)
@ -1308,10 +1306,7 @@ class JwtParserTest {
build(). build().
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getExpiration().getTime(), truncateMillis(expiration)
def expirationMillis = ((long) expiration.getTime() / 1000) * 1000
assertEquals jwt.getBody().getExpiration().getTime(), expirationMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)
@ -1363,10 +1358,7 @@ class JwtParserTest {
build(). build().
parseClaimsJws(compact) parseClaimsJws(compact)
// system converts to seconds (lopping off millis precision), then returns millis assertEquals jwt.getBody().getNotBefore().getTime(), truncateMillis(notBefore)
def notBeforeMillis = ((long) notBefore.getTime() / 1000) * 1000
assertEquals jwt.getBody().getNotBefore().getTime(), notBeforeMillis, 0
} }
@Test(expected = IncorrectClaimException) @Test(expected = IncorrectClaimException)