Skip to content

Commit 78dc201

Browse files
committed
Added some integration tests for verification of dates with and without leeway.
1 parent 803fcea commit 78dc201

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Tests/JWTTests/JWTTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,46 @@ class ValidationTests: XCTestCase {
362362
}
363363
}
364364

365+
class IntegrationTests: XCTestCase {
366+
func testVerificationFailureWithoutLeeway() {
367+
let token = JWT.encode(.none) { builder in
368+
builder.issuer = "fuller.li"
369+
builder.audience = "cocoapods"
370+
builder.expiration = Date().addingTimeInterval(-1) // Token expired one second ago
371+
builder.notBefore = Date().addingTimeInterval(1) // Token starts being valid in one second
372+
builder.issuedAt = Date().addingTimeInterval(1) // Token is issued one second in the future
373+
}
374+
375+
let expectation = XCTestExpectation(description: "Verification should fail.")
376+
do {
377+
let _ = try JWT.decode(token, algorithm: .none)
378+
XCTFail("InvalidToken error should have been thrown.")
379+
} catch is InvalidToken {
380+
expectation.fulfill()
381+
} catch {
382+
XCTFail("Unexpected error type while verifying token.")
383+
}
384+
self.wait(for: [expectation], timeout: 0.5)
385+
}
386+
387+
func testVerificationSuccessWithLeeway() {
388+
let token = JWT.encode(.none) { builder in
389+
builder.issuer = "fuller.li"
390+
builder.audience = "cocoapods"
391+
builder.expiration = Date().addingTimeInterval(-1) // Token expired one second ago
392+
builder.notBefore = Date().addingTimeInterval(1) // Token starts being valid in one second
393+
builder.issuedAt = Date().addingTimeInterval(1) // Token is issued one second in the future
394+
}
395+
396+
do {
397+
let _ = try JWT.decode(token, algorithm: .none, leeway: 2)
398+
// Due to leeway no error gets thrown.
399+
} catch {
400+
XCTFail("Unexpected error type while verifying token.")
401+
}
402+
}
403+
}
404+
365405
// MARK: Helpers
366406

367407
func assertSuccess(_ decoder: @autoclosure () throws -> Payload, closure: ((Payload) -> Void)? = nil) {

0 commit comments

Comments
 (0)