Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SUT terminology in unit tests #163

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion JWTDecode.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ Pod::Spec.new do |s|
s.watchos.deployment_target = '6.2'

s.source_files = 'JWTDecode/*.swift'

s.swift_versions = ['5.5', '5.6']
end
82 changes: 36 additions & 46 deletions JWTDecodeTests/JWTDecodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import JWTDecode
import Foundation

class JWTDecodeSpec: QuickSpec {

override func spec() {
describe("decode") {

it("should tell a jwt is expired") {
expect(expiredJWT().expired).to(beTruthy())
}
Expand Down Expand Up @@ -70,101 +68,95 @@ class JWTDecodeSpec: QuickSpec {
expect(error).to(beJWTDecodeError(.invalidPartCount(jwtString, 2)))
})
}

}

describe("jwt parts") {
let jwt = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWIifQ.xXcD7WOvUDHJ94E6aVHYgXdsJHLl2oW7ZXm4QpVvXnY")
let sut = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWIifQ.xXcD7WOvUDHJ94E6aVHYgXdsJHLl2oW7ZXm4QpVvXnY")

it("should return header") {
expect(jwt.header as? [String: String]).to(equal(["alg": "HS256", "typ": "JWT"]))
expect(sut.header as? [String: String]).to(equal(["alg": "HS256", "typ": "JWT"]))
}

it("should return body") {
expect(jwt.body as? [String: String]).to(equal(["sub": "sub"]))
expect(sut.body as? [String: String]).to(equal(["sub": "sub"]))
}

it("should return signature") {
expect(jwt.signature).to(equal("xXcD7WOvUDHJ94E6aVHYgXdsJHLl2oW7ZXm4QpVvXnY"))
expect(sut.signature).to(equal("xXcD7WOvUDHJ94E6aVHYgXdsJHLl2oW7ZXm4QpVvXnY"))
}
}

describe("claims") {
var token: JWT!
var sut: JWT!

describe("expiresAt claim") {

it("should handle expired jwt") {
token = expiredJWT()
expect(token.expiresAt).toNot(beNil())
expect(token.expired).to(beTruthy())
sut = expiredJWT()
expect(sut.expiresAt).toNot(beNil())
expect(sut.expired).to(beTruthy())
}

it("should handle non-expired jwt") {
token = nonExpiredJWT()
expect(token.expiresAt).toNot(beNil())
expect(token.expired).to(beFalsy())
sut = nonExpiredJWT()
expect(sut.expiresAt).toNot(beNil())
expect(sut.expired).to(beFalsy())
}

it("should handle jwt without expiresAt claim") {
token = jwt(withBody: ["sub": UUID().uuidString])
expect(token.expiresAt).to(beNil())
expect(token.expired).to(beFalsy())
sut = jwt(withBody: ["sub": UUID().uuidString])
expect(sut.expiresAt).to(beNil())
expect(sut.expired).to(beFalsy())
}
}

describe("registered claims") {

let jwt = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2fQ.LvF9wSheCB5xarpydmurWgi9NOZkdES5AbNb_UWk9Ew")

let sut = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2fQ.LvF9wSheCB5xarpydmurWgi9NOZkdES5AbNb_UWk9Ew")

it("should return issuer") {
expect(jwt.issuer).to(equal("https://samples.auth0.com"))
expect(sut.issuer).to(equal("https://samples.auth0.com"))
}

it("should return subject") {
expect(jwt.subject).to(equal("auth0|1010101010"))
expect(sut.subject).to(equal("auth0|1010101010"))
}

it("should return single audience") {
expect(jwt.audience).to(equal(["https://samples.auth0.com"]))
expect(sut.audience).to(equal(["https://samples.auth0.com"]))
}

context("multiple audiences") {

let jwt = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiaHR0cHM6Ly9zYW1wbGVzLmF1dGgwLmNvbSIsImh0dHBzOi8vYXBpLnNhbXBsZXMuYXV0aDAuY29tIl19.cfWFPuJbQ7NToa-BjHgHD1tHn3P2tOP5wTQaZc1qg6M")
let sut = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiaHR0cHM6Ly9zYW1wbGVzLmF1dGgwLmNvbSIsImh0dHBzOi8vYXBpLnNhbXBsZXMuYXV0aDAuY29tIl19.cfWFPuJbQ7NToa-BjHgHD1tHn3P2tOP5wTQaZc1qg6M")

it("should return all audiences") {
expect(jwt.audience).to(equal(["https://samples.auth0.com", "https://api.samples.auth0.com"]))
expect(sut.audience).to(equal(["https://samples.auth0.com", "https://api.samples.auth0.com"]))
}
}

it("should return issued at") {
expect(jwt.issuedAt).to(equal(Date(timeIntervalSince1970: 1372638336)))
expect(sut.issuedAt).to(equal(Date(timeIntervalSince1970: 1372638336)))
}

it("should return not before") {
expect(jwt.notBefore).to(equal(Date(timeIntervalSince1970: 1372638336)))
expect(sut.notBefore).to(equal(Date(timeIntervalSince1970: 1372638336)))
}

it("should return jwt id") {
expect(jwt.identifier).to(equal("qwerty123456"))
expect(sut.identifier).to(equal("qwerty123456"))
}
}

describe("custom claim") {

beforeEach {
token = jwt(withBody: ["sub": UUID().uuidString, "custom_string_claim": "Shawarma Friday!", "custom_integer_claim": 10, "custom_double_claim": 3.4, "custom_double_string_claim": "1.3", "custom_true_boolean_claim": true, "custom_false_boolean_claim": false])
sut = jwt(withBody: ["sub": UUID().uuidString, "custom_string_claim": "Shawarma Friday!", "custom_integer_claim": 10, "custom_double_claim": 3.4, "custom_double_string_claim": "1.3", "custom_true_boolean_claim": true, "custom_false_boolean_claim": false])
}

it("should return claim by name") {
let claim = token.claim(name: "custom_string_claim")
let claim = sut.claim(name: "custom_string_claim")
expect(claim.rawValue).toNot(beNil())
}

it("should return string claim") {
let claim = token["custom_string_claim"]
let claim = sut["custom_string_claim"]
expect(claim.string) == "Shawarma Friday!"
expect(claim.array) == ["Shawarma Friday!"]
expect(claim.integer).to(beNil())
Expand All @@ -174,7 +166,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return integer claim") {
let claim = token["custom_integer_claim"]
let claim = sut["custom_integer_claim"]
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer) == 10
Expand All @@ -184,7 +176,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return double claim") {
let claim = token["custom_double_claim"]
let claim = sut["custom_double_claim"]
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer) == 3
Expand All @@ -194,7 +186,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return double as string claim") {
let claim = token["custom_double_string_claim"]
let claim = sut["custom_double_string_claim"]
expect(claim.string) == "1.3"
expect(claim.array) == ["1.3"]
expect(claim.integer).to(beNil())
Expand All @@ -204,7 +196,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return true boolean claim") {
let claim = token["custom_true_boolean_claim"]
let claim = sut["custom_true_boolean_claim"]
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer).to(beNil())
Expand All @@ -214,7 +206,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return false boolean claim") {
let claim = token["custom_false_boolean_claim"]
let claim = sut["custom_false_boolean_claim"]
expect(claim.string).to(beNil())
expect(claim.array).to(beNil())
expect(claim.integer).to(beNil())
Expand All @@ -224,7 +216,7 @@ class JWTDecodeSpec: QuickSpec {
}

it("should return no value when claim is not present") {
let unknownClaim = token["missing_claim"]
let unknownClaim = sut["missing_claim"]
expect(unknownClaim.array).to(beNil())
expect(unknownClaim.string).to(beNil())
expect(unknownClaim.integer).to(beNil())
Expand All @@ -234,21 +226,19 @@ class JWTDecodeSpec: QuickSpec {
}

context("raw claim") {

var token: JWT!
var sut: JWT!

beforeEach {
token = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2LCJlbWFpbCI6InVzZXJAaG9zdC5jb20iLCJjdXN0b20iOlsxLDIsM119.JeMRyHLkcoiqGxd958B6PABKNvhOhIgw-kbjecmhR_E")
sut = try! decode(jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tIiwic3ViIjoiYXV0aDB8MTAxMDEwMTAxMCIsImF1ZCI6Imh0dHBzOi8vc2FtcGxlcy5hdXRoMC5jb20iLCJleHAiOjEzNzI2NzQzMzYsImlhdCI6MTM3MjYzODMzNiwianRpIjoicXdlcnR5MTIzNDU2IiwibmJmIjoxMzcyNjM4MzM2LCJlbWFpbCI6InVzZXJAaG9zdC5jb20iLCJjdXN0b20iOlsxLDIsM119.JeMRyHLkcoiqGxd958B6PABKNvhOhIgw-kbjecmhR_E")
}

it("should return email") {
expect(token["email"].string) == "user@host.com"
expect(sut["email"].string) == "user@host.com"
}

it("should return array") {
expect(token["custom"].rawValue as? [Int]).toNot(beNil())
expect(sut["custom"].rawValue as? [Int]).toNot(beNil())
}

}
}
}
Expand Down