test: Add tests for JWT token expiration and not before checks

This commit is contained in:
Jonas Roussel
2025-02-27 11:50:08 +01:00
parent 6fd7e585aa
commit eb9c722e17

View File

@ -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'),
);
});
}); });
}); });
} }