Skip to content

Commit 64cb792

Browse files
committed
fix timezone issues on exp claim tests
1 parent 8e538b9 commit 64cb792

6 files changed

Lines changed: 52 additions & 72 deletions

File tree

‎lib/src/test/java/com/auth0/jwt/creators/AccessJwtCreatorTest.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,22 +317,19 @@ public void testAccessJwtCreatorNonStandardClaimDateValue() throws Exception {
317317

318318
@Test
319319
public void testAccessJwtCreatorExpTimeHasPassed() throws Exception {
320-
thrown.expect(TokenExpiredException.class);
321-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
320+
Calendar calendar = Calendar.getInstance();
321+
calendar.set(2014, Calendar.OCTOBER, 29);
322322

323-
String myDate = "2014/10/29";
324-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
325-
Date date = sdf.parse(myDate);
326-
long expLong = date.getTime();
327-
Date expDate = new Date(expLong);
323+
thrown.expect(TokenExpiredException.class);
324+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
328325

329326
Algorithm algorithm = Algorithm.HMAC256("secret");
330327
String token = AccessJwtCreator.build()
331328
.withIssuer("issuer")
332329
.withSubject("subject")
333330
.withAudience("audience")
334331
.withNonStandardClaim("nonStandardClaim", new Date())
335-
.withExp(expDate)
332+
.withExp(calendar.getTime())
336333
.withIat(iat)
337334
.sign(algorithm);
338335
Verification verification = AccessJWT.require(algorithm);

‎lib/src/test/java/com/auth0/jwt/creators/ExtendedJwtCreatorTest.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.junit.rules.ExpectedException;
4141

4242
import java.text.SimpleDateFormat;
43+
import java.util.Calendar;
4344
import java.util.Date;
4445
import java.util.Map;
4546

@@ -441,14 +442,11 @@ public void testExtendedJwtCreatorNonStandardClaimDateValue() throws Exception {
441442

442443
@Test
443444
public void testExtendedJwtCreatorExpTimeHasPassed() throws Exception {
444-
thrown.expect(TokenExpiredException.class);
445-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
445+
Calendar calendar = Calendar.getInstance();
446+
calendar.set(2014, Calendar.OCTOBER, 29);
446447

447-
String myDate = "2014/10/29";
448-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
449-
Date date = sdf.parse(myDate);
450-
long expLong = date.getTime();
451-
Date expDate = new Date(expLong);
448+
thrown.expect(TokenExpiredException.class);
449+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
452450

453451
Algorithm algorithm = Algorithm.HMAC256("secret");
454452
String token = ExtendedJwtCreator.build()
@@ -458,7 +456,7 @@ public void testExtendedJwtCreatorExpTimeHasPassed() throws Exception {
458456
.withIssuer("issuer")
459457
.withSubject("subject")
460458
.withAudience("audience")
461-
.withExp(expDate)
459+
.withExp(calendar.getTime())
462460
.withIat(iat)
463461
.withName(NAME)
464462
.withNonStandardClaim("nonStandardClaim", new Date())

‎lib/src/test/java/com/auth0/jwt/creators/FbJwtCreatorTest.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,15 @@ public void testFbJwtCreatorNonStandardClaimDateValue() throws Exception {
322322
}
323323
@Test
324324
public void testFbJwtCreatorExpTimeHasPassed() throws Exception {
325-
thrown.expect(TokenExpiredException.class);
326-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
325+
Calendar calendar = Calendar.getInstance();
326+
calendar.set(2014, Calendar.OCTOBER, 29);
327327

328-
String myDate = "2014/10/29";
329-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
330-
Date date = sdf.parse(myDate);
331-
long expLong = date.getTime();
332-
Date expDate = new Date(expLong);
328+
thrown.expect(TokenExpiredException.class);
329+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
333330

334331
Algorithm algorithm = Algorithm.HMAC256("secret");
335332
String token = FbJwtCreator.build()
336-
.withExp(expDate)
333+
.withExp(calendar.getTime())
337334
.withIat(iat)
338335
.withUserId(USER_ID)
339336
.withAppId(APP_ID)

‎lib/src/test/java/com/auth0/jwt/creators/GoogleJwtCreatorTest.java‎

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
package com.auth0.jwt.creators;
2121

22-
import static com.auth0.jwt.TimeUtil.generateRandomExpDateInFuture;
23-
import static com.auth0.jwt.TimeUtil.generateRandomIatDateInPast;
2422
import com.auth0.jwt.algorithms.Algorithm;
25-
import com.auth0.jwt.creators.GoogleJwtCreator;
2623
import com.auth0.jwt.exceptions.InvalidClaimException;
2724
import com.auth0.jwt.exceptions.TokenExpiredException;
2825
import com.auth0.jwt.impl.PublicClaims;
@@ -31,15 +28,18 @@
3128
import com.auth0.jwt.interfaces.GoogleVerification;
3229
import com.auth0.jwt.jwts.GoogleJWT;
3330
import com.auth0.jwt.jwts.JWT;
34-
import static java.util.Arrays.asList;
35-
import static org.junit.Assert.assertTrue;
3631
import org.junit.Rule;
3732
import org.junit.Test;
3833
import org.junit.rules.ExpectedException;
3934

35+
import java.util.Calendar;
36+
import java.util.Date;
37+
import java.util.Map;
4038

41-
import java.text.SimpleDateFormat;
42-
import java.util.*;
39+
import static com.auth0.jwt.TimeUtil.generateRandomExpDateInFuture;
40+
import static com.auth0.jwt.TimeUtil.generateRandomIatDateInPast;
41+
import static java.util.Arrays.asList;
42+
import static org.junit.Assert.assertTrue;
4343

4444
public class GoogleJwtCreatorTest {
4545

@@ -133,7 +133,7 @@ public void testGoogleJwtCreatorWhenCertainRequiredClaimIsntProvided() throws Ex
133133

134134
GoogleVerification verification = GoogleJWT.require(algorithm);
135135
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
136-
NAME, 1, 1).build();
136+
NAME, 1, 1).build();
137137
DecodedJWT jwt = verifier.decode(token);
138138
}
139139

@@ -157,7 +157,7 @@ public void testGoogleJwtCreatorNoneAlgorithmNotAllowed() throws Exception {
157157

158158
GoogleVerification verification = GoogleJWT.require(algorithm);
159159
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
160-
NAME, 1, 1).build();
160+
NAME, 1, 1).build();
161161
DecodedJWT jwt = verifier.decode(token);
162162
}
163163

@@ -180,7 +180,7 @@ public void testGoogleJwtCreatorNoneAlgorithmNotSpecifiedButStillNotAllowed() th
180180

181181
GoogleVerification verification = GoogleJWT.require(algorithm);
182182
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
183-
NAME, 1, 1).build();
183+
NAME, 1, 1).build();
184184
DecodedJWT jwt = verifier.decode(token);
185185
}
186186

@@ -201,7 +201,7 @@ public void testGoogleJwtCreatorNoneAlgorithmAllowed() throws Exception {
201201

202202
GoogleVerification verification = GoogleJWT.require(algorithm);
203203
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
204-
NAME, 1, 1).build();
204+
NAME, 1, 1).build();
205205
DecodedJWT jwt = verifier.decode(token);
206206
Map<String, Claim> claims = jwt.getClaims();
207207
verifyClaims(claims, exp);
@@ -251,7 +251,7 @@ public void testGoogleJwtCreatorInvalidIssuer() throws Exception {
251251

252252
GoogleVerification verification = GoogleJWT.require(algorithm);
253253
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
254-
NAME, 1, 1).build();
254+
NAME, 1, 1).build();
255255
DecodedJWT jwt = verifier.decode(token);
256256
}
257257

@@ -363,7 +363,7 @@ public void testGoogleJwtCreatorNonStandardClaimString() throws Exception {
363363
.sign(algorithm);
364364
GoogleVerification verification = GoogleJWT.require(algorithm);
365365
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("issuer"), asList("audience"),
366-
NAME, 1, 1).build();
366+
NAME, 1, 1).build();
367367
DecodedJWT jwt = verifier.decode(token);
368368
Map<String, Claim> claims = jwt.getClaims();
369369
verifyClaims(claims, exp);
@@ -481,14 +481,11 @@ public void testGoogleJwtCreatorNonStandardClaimDate() throws Exception {
481481

482482
@Test
483483
public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
484-
thrown.expect(TokenExpiredException.class);
485-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
484+
Calendar calendar = Calendar.getInstance();
485+
calendar.set(2014, Calendar.OCTOBER, 29);
486486

487-
String myDate = "2014/10/29";
488-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
489-
Date date = sdf.parse(myDate);
490-
long expLong = date.getTime();
491-
Date expDate = new Date(expLong);
487+
thrown.expect(TokenExpiredException.class);
488+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
492489

493490
Algorithm algorithm = Algorithm.HMAC256("secret");
494491
String token = GoogleJwtCreator.build()
@@ -497,7 +494,7 @@ public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
497494
.withIssuer("issuer")
498495
.withSubject("subject")
499496
.withAudience("audience")
500-
.withExp(expDate)
497+
.withExp(calendar.getTime())
501498
.withIat(iat)
502499
.withName(NAME)
503500
.sign(algorithm);
@@ -509,14 +506,11 @@ public void testGoogleJwtCreatorExpTimeHasPassed() throws Exception {
509506

510507
@Test
511508
public void testGoogleJwtCreatorTokenCantBeUsedBefore() throws Exception {
512-
thrown.expect(InvalidClaimException.class);
513-
thrown.expectMessage("The Token can't be used before Mon Oct 29 00:00:00 PDT 2018.");
509+
Calendar calendar = Calendar.getInstance();
510+
calendar.set(2018, Calendar.OCTOBER, 29);
514511

515-
String myDate = "2018/10/29";
516-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
517-
Date date = sdf.parse(myDate);
518-
long expLong = date.getTime();
519-
Date iatDate = new Date(expLong);
512+
thrown.expect(InvalidClaimException.class);
513+
thrown.expectMessage(String.format("The Token can't be used before %s", calendar.getTime()));
520514

521515
Algorithm algorithm = Algorithm.HMAC256("secret");
522516
String token = GoogleJwtCreator.build()
@@ -526,7 +520,7 @@ public void testGoogleJwtCreatorTokenCantBeUsedBefore() throws Exception {
526520
.withSubject("subject")
527521
.withAudience("audience")
528522
.withExp(exp)
529-
.withIat(iatDate)
523+
.withIat(calendar.getTime())
530524
.withName(NAME)
531525
.sign(algorithm);
532526
GoogleVerification verification = GoogleJWT.require(algorithm);
@@ -536,14 +530,14 @@ public void testGoogleJwtCreatorTokenCantBeUsedBefore() throws Exception {
536530
}
537531

538532
@Test
539-
public void testCreateVerifierForExtended() throws Exception{
533+
public void testCreateVerifierForExtended() throws Exception {
540534
thrown.expect(UnsupportedOperationException.class);
541535
thrown.expectMessage("you shouldn't be calling this method");
542536
GoogleVerification verification = GoogleJWT.require(Algorithm.HMAC256("secret"));
543537
verification.createVerifierForExtended(null, null, null, null, null, 1L, 1L, 1L);
544538
}
545539

546-
protected static void verifyClaims(Map<String,Claim> claims, Date exp) {
540+
protected static void verifyClaims(Map<String, Claim> claims, Date exp) {
547541
assertTrue(claims.get(PICTURE).asString().equals(PICTURE));
548542
assertTrue(claims.get(EMAIL).asString().equals(EMAIL));
549543
assertTrue(claims.get(PublicClaims.ISSUER).asList(String.class).get(0).equals("issuer"));

‎lib/src/test/java/com/auth0/jwt/creators/RiscJwtCreatorTest.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,11 @@ public void testRiscJwtCreatorNonStandardClaimDateValue() throws Exception {
378378

379379
@Test
380380
public void testRiscJwtCreatorExpTimeHasPassed() throws Exception {
381-
thrown.expect(TokenExpiredException.class);
382-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
381+
Calendar calendar = Calendar.getInstance();
382+
calendar.set(2014, Calendar.OCTOBER, 29);
383383

384-
String myDate = "2014/10/29";
385-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
386-
Date date = sdf.parse(myDate);
387-
long expLong = date.getTime();
388-
Date expDate = new Date(expLong);
384+
thrown.expect(TokenExpiredException.class);
385+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
389386

390387
Algorithm algorithm = Algorithm.HMAC256("secret");
391388
String token = RiscJwtCreator.build()
@@ -394,7 +391,7 @@ public void testRiscJwtCreatorExpTimeHasPassed() throws Exception {
394391
.withSubject("subject")
395392
.withAudience("audience")
396393
.withNonStandardClaim("nonStandardClaim", new Date())
397-
.withExp(expDate)
394+
.withExp(calendar.getTime())
398395
.withIat(iat)
399396
.sign(algorithm);
400397
Verification verification = RiscJWT.require(algorithm);

‎lib/src/test/java/com/auth0/jwt/creators/ScopedJwtCreatorTest.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,11 @@ public void testScopedJwtCreatorNonStandardClaimDateValue() throws Exception {
370370

371371
@Test
372372
public void testScopedJwtCreatorExpTimeHasPassed() throws Exception {
373-
thrown.expect(TokenExpiredException.class);
374-
thrown.expectMessage("The Token has expired on Wed Oct 29 00:00:00 PDT 2014.");
373+
Calendar calendar = Calendar.getInstance();
374+
calendar.set(2014, Calendar.OCTOBER, 29);
375375

376-
String myDate = "2014/10/29";
377-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
378-
Date date = sdf.parse(myDate);
379-
long expLong = date.getTime();
380-
Date expDate = new Date(expLong);
376+
thrown.expect(TokenExpiredException.class);
377+
thrown.expectMessage(String.format("The Token has expired on %s", calendar.getTime()));
381378

382379
Algorithm algorithm = Algorithm.HMAC256("secret");
383380
String token = ScopedJwtCreator.build()
@@ -386,14 +383,14 @@ public void testScopedJwtCreatorExpTimeHasPassed() throws Exception {
386383
.withSubject("subject")
387384
.withAudience("audience")
388385
.withNonStandardClaim("nonStandardClaim", new Date())
389-
.withExp(expDate)
386+
.withExp(calendar.getTime())
390387
.withIat(iat)
391388
.sign(algorithm);
392389
Verification verification = ScopedJWT.require(algorithm);
393390
JWT verifier = verification.createVerifierForScoped("scope", asList("issuer"), asList("audience"), 1, 1).build();
394391
DecodedJWT jwt = verifier.decode(token);
395392
Map<String, Claim> claims = jwt.getClaims();
396-
verifyClaims(claims, expDate);
393+
verifyClaims(claims, calendar.getTime());
397394
}
398395

399396
private static void verifyClaims(Map<String,Claim> claims, Date exp) {

0 commit comments

Comments
 (0)