mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-08-23 04:11:00 +08:00
test: Add tests for JWT token expiration and not before checks
This commit is contained in:
@ -48,6 +48,70 @@ void main() {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('should be valid when exp is disabled', () {
|
||||
final token =
|
||||
JWT({'foo': 'bar'}).sign(hsKey, expiresIn: Duration(hours: -1));
|
||||
|
||||
expect(
|
||||
JWT.verify(token, hsKey, checkExpiresIn: false).payload,
|
||||
contains('foo'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
//----------------------//
|
||||
// Not Before (nbf) //
|
||||
//----------------------//
|
||||
group('nbf', () {
|
||||
test('should throw when token is not yet valid', () {
|
||||
final notBefore = Duration(hours: 1);
|
||||
final token = JWT({'foo': 'bar'}).sign(hsKey, notBefore: notBefore);
|
||||
|
||||
expect(
|
||||
() => JWT.verify(token, hsKey),
|
||||
throwsA(isA<JWTNotActiveException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should be valid after nbf time', () {
|
||||
final iat = DateTime(2023);
|
||||
final nbf = iat.add(Duration(minutes: 30));
|
||||
|
||||
withClock(
|
||||
Clock.fixed(DateTime(2023)),
|
||||
() {
|
||||
final token = JWT({'foo': 'bar'}).sign(
|
||||
hsKey,
|
||||
notBefore: Duration(minutes: 30),
|
||||
);
|
||||
|
||||
fakeAsync((async) {
|
||||
async.elapse(Duration(minutes: 45));
|
||||
expect(
|
||||
JWT.verify(token, hsKey).payload,
|
||||
equals({
|
||||
'foo': 'bar',
|
||||
'iat': iat.millisecondsSinceEpoch ~/ 1000,
|
||||
'nbf': nbf.millisecondsSinceEpoch ~/ 1000,
|
||||
}),
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('should be valid when nbf check is disabled', () {
|
||||
final token = JWT({'foo': 'bar'}).sign(
|
||||
hsKey,
|
||||
notBefore: Duration(hours: 1),
|
||||
);
|
||||
|
||||
expect(
|
||||
JWT.verify(token, hsKey, checkNotBefore: false).payload,
|
||||
contains('foo'),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user