Skip to content

Commit

Permalink
Added some integration tests for verification of dates with and witho…
Browse files Browse the repository at this point in the history
…ut leeway.
  • Loading branch information
JanBrinker committed Sep 7, 2017
1 parent 803fcea commit 78dc201
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Tests/JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,46 @@ class ValidationTests: XCTestCase {
}
}

class IntegrationTests: XCTestCase {
func testVerificationFailureWithoutLeeway() {
let token = JWT.encode(.none) { builder in
builder.issuer = "fuller.li"
builder.audience = "cocoapods"
builder.expiration = Date().addingTimeInterval(-1) // Token expired one second ago
builder.notBefore = Date().addingTimeInterval(1) // Token starts being valid in one second
builder.issuedAt = Date().addingTimeInterval(1) // Token is issued one second in the future
}

let expectation = XCTestExpectation(description: "Verification should fail.")
do {
let _ = try JWT.decode(token, algorithm: .none)
XCTFail("InvalidToken error should have been thrown.")
} catch is InvalidToken {
expectation.fulfill()
} catch {
XCTFail("Unexpected error type while verifying token.")
}
self.wait(for: [expectation], timeout: 0.5)
}

func testVerificationSuccessWithLeeway() {
let token = JWT.encode(.none) { builder in
builder.issuer = "fuller.li"
builder.audience = "cocoapods"
builder.expiration = Date().addingTimeInterval(-1) // Token expired one second ago
builder.notBefore = Date().addingTimeInterval(1) // Token starts being valid in one second
builder.issuedAt = Date().addingTimeInterval(1) // Token is issued one second in the future
}

do {
let _ = try JWT.decode(token, algorithm: .none, leeway: 2)
// Due to leeway no error gets thrown.
} catch {
XCTFail("Unexpected error type while verifying token.")
}
}
}

// MARK: Helpers

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

0 comments on commit 78dc201

Please sign in to comment.