Skip to content

Commit

Permalink
fix(test): catch rejected promises in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceanis committed Apr 26, 2020
1 parent 503cebb commit d3b2e9e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
44 changes: 22 additions & 22 deletions src/__tests__/JWT-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('createJWT()', () => {
})

it('throws an error if unsupported algorithm is passed in', async () => {
expect(createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, alg: 'BADALGO' })).rejects.toThrow(
await expect(createJWT({ requested: ['name', 'phone'] }, { issuer: did, signer, alg: 'BADALGO' })).rejects.toThrow(
'Unsupported algorithm BADALGO'
)
})
Expand All @@ -134,7 +134,7 @@ describe('createJWT()', () => {

it('creates a valid JWT', async () => {
const jwt = await createJWT({ requested: ['name', 'phone'] }, { alg, issuer: did, signer })
return expect(naclVerifyJWT(jwt)).toBeTruthy()
return await expect(naclVerifyJWT(jwt)).toBeTruthy()
})

it('creates a JWT with correct format', async () => {
Expand Down Expand Up @@ -200,49 +200,49 @@ describe('verifyJWT()', () => {
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUzMjExMzMsIm5iZiI6MTQ4NTI2MTEzMywiaXNzIjoiZGlkOmV0aHI6MHhmM2JlYWMzMGM0OThkOWUyNjg2NWYzNGZjYWE1N2RiYjkzNWIwZDc0In0.FUasGkOYqGVxQ7S-QQvh4abGO6Dwr961UjjOxtRTyUDnl6q6ElqHqAK-WMDTmOir21pFPKLYZMtLZ4LTLpm3cQ'
// const jwt = await createJWT({nbf: PAST}, {issuer:did, signer})
expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
await expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
})
it('passes when nbf is in the past and iat is in the future', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUzODExMzMsIm5iZiI6MTQ4NTI2MTEzMywiaXNzIjoiZGlkOmV0aHI6MHhmM2JlYWMzMGM0OThkOWUyNjg2NWYzNGZjYWE1N2RiYjkzNWIwZDc0In0.8BPiSG2e6UBn1osnJ6PJYbPjtPMPaCeutTA9OCp-ZzI-QvvwPCVrrWqTu2YELbzUPwDIJCQ8v8N77xCEjIYSmQ'
// const jwt = await createJWT({nbf:PAST,iat:FUTURE},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
await expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
})
it('fails when nbf is in the future', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUzMjExMzMsIm5iZiI6MTQ4NTM4MTEzMywiaXNzIjoiZGlkOnVwb3J0OjJuUXRpUUc2Q2dtMUdZVEJhYUtBZ3I3NnVZN2lTZXhVa3FYIn0.rcFuhVHtie3Y09pWxBSf1dnjaVh6FFQLHh-83N-uLty3M5ADJ-jVFFkyt_Eupl8Kr735-oPGn_D1Nj9rl4s_Kw'
// const jwt = await createJWT({nbf:FUTURE},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
})
it('fails when nbf is in the future and iat is in the past', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUyNjExMzMsIm5iZiI6MTQ4NTM4MTEzMywiaXNzIjoiZGlkOmV0aHI6MHhmM2JlYWMzMGM0OThkOWUyNjg2NWYzNGZjYWE1N2RiYjkzNWIwZDc0In0.JjEn_huxI9SsBY_3PlD0ShpXvrRgUGFDKAgxJBc1Q5GToVpUTw007-o9BTt7JNi_G2XWmcu2aXXnDn0QFsRIrg'
// const jwt = await createJWT({nbf:FUTURE,iat:PAST},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
})
it('passes when nbf is missing and iat is in the past', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUyNjExMzMsImlzcyI6ImRpZDpldGhyOjB4ZjNiZWFjMzBjNDk4ZDllMjY4NjVmMzRmY2FhNTdkYmI5MzViMGQ3NCJ9.jkzN5kIVtuRU-Fjte8w5r-ttf9OfhdN38oFJd61CWdI5WnvU1dPCvnx1_kdk2D6Xg-uPqp1VXAb7KA2ZECivmg'
// const jwt = await createJWT({iat:PAST},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
await expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
})
it('fails when nbf is missing and iat is in the future', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpYXQiOjE0ODUzODExMzMsImlzcyI6ImRpZDpldGhyOjB4ZjNiZWFjMzBjNDk4ZDllMjY4NjVmMzRmY2FhNTdkYmI5MzViMGQ3NCJ9.FJuHvf9Tby7b4I54Cm1nh8CvLg4QH2wt2K0WfyQaLqlr3NKKI5hAdLalgZksI25gLhNrZwQFnC-nzEOs9PI1SQ'
// const jwt = await createJWT({iat:FUTURE},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow()
})
it('passes when nbf and iat are both missing', async () => {
// tslint:disable-next-line: max-line-length
const jwt =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpc3MiOiJkaWQ6ZXRocjoweGYzYmVhYzMwYzQ5OGQ5ZTI2ODY1ZjM0ZmNhYTU3ZGJiOTM1YjBkNzQifQ.KgnwgMMz-QSOtpba2QMGHMWJoLvhp-H4odjjX1QKnqj4-8dkcK12y7rj7Zq24-1d-1ne86aJCdWtx5VJv3rM7w'
// const jwt = await createJWT({iat:undefined},{issuer:did,signer})
expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
await expect(verifyJWT(jwt, { resolver })).resolves.not.toThrow()
})
})

Expand Down Expand Up @@ -273,7 +273,7 @@ describe('verifyJWT()', () => {

it('rejects an expired JWT', async () => {
const jwt = await createJWT({ exp: NOW - NBF_SKEW - 1 }, { issuer: did, signer })
expect(verifyJWT(jwt, { resolver })).rejects.toThrow(/JWT has expired/)
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow(/JWT has expired/)
})

it('accepts a valid audience', async () => {
Expand All @@ -290,7 +290,7 @@ describe('verifyJWT()', () => {

it('rejects invalid multiple audiences', async () => {
const jwt = await createJWT({ aud: [did, did] }, { issuer: did, signer })
expect(verifyJWT(jwt, { resolver, audience: aud })).rejects.toThrow(/JWT audience does not match your DID/)
await expect(verifyJWT(jwt, { resolver, audience: aud })).rejects.toThrow(/JWT audience does not match your DID/)
})

it('accepts a valid audience using callback_url', async () => {
Expand All @@ -304,12 +304,12 @@ describe('verifyJWT()', () => {

it('rejects invalid audience', async () => {
const jwt = await createJWT({ aud }, { issuer: did, signer })
expect(verifyJWT(jwt, { resolver, audience: did })).rejects.toThrow(/JWT audience does not match your DID/)
await expect(verifyJWT(jwt, { resolver, audience: did })).rejects.toThrow(/JWT audience does not match your DID or callback url/)
})

it('rejects an invalid audience using callback_url where callback is wrong', async () => {
const jwt = await createJWT({ aud: 'http://pututu.uport.me/unique' }, { issuer: did, signer })
expect(
await expect(
verifyJWT(jwt, {
resolver,
callbackUrl: 'http://pututu.uport.me/unique/1'
Expand All @@ -319,14 +319,14 @@ describe('verifyJWT()', () => {

it('rejects an invalid audience using callback_url where callback is missing', async () => {
const jwt = await createJWT({ aud: 'http://pututu.uport.me/unique' }, { issuer: did, signer })
expect(verifyJWT(jwt, { resolver })).rejects.toThrow(
"JWT audience matching your callback url is required but one wasn't passed in"
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow(
"JWT audience is required but your app address has not been configured"
)
})

it('rejects invalid audience as no address is present', async () => {
const jwt = await createJWT({ aud }, { issuer: did, signer })
expect(verifyJWT(jwt, { resolver })).rejects.toThrow(/JWT audience does not match your DID or callback url/)
await expect(verifyJWT(jwt, { resolver })).rejects.toThrow(/JWT audience is required but your app address has not been configured/)
})
})

Expand Down Expand Up @@ -453,7 +453,7 @@ describe('resolveAuthenticator()', () => {
})

it('errors if no suitable public keys exist', async () => {
return expect(
return await expect(
resolveAuthenticator({ resolve: jest.fn().mockReturnValue(unsupportedFormat) }, alg, did)
).rejects.toEqual(new Error(`DID document for ${did} does not have public keys for ${alg}`))
})
Expand Down Expand Up @@ -489,34 +489,34 @@ describe('resolveAuthenticator()', () => {
})

it('errors if no suitable public keys exist', async () => {
return expect(
return await expect(
resolveAuthenticator({ resolve: jest.fn().mockReturnValue(unsupportedFormat) }, alg, did)
).rejects.toEqual(new Error(`DID document for ${did} does not have public keys for ${alg}`))
})
})

it('errors if no suitable public keys exist for authentication', async () => {
return expect(
return await expect(
resolveAuthenticator({ resolve: jest.fn().mockReturnValue(singleKey) }, alg, did, true)
).rejects.toEqual(
new Error(`DID document for ${did} does not have public keys suitable for authenticationg user`)
)
})

it('errors if no public keys exist', async () => {
return expect(
return await expect(
resolveAuthenticator({ resolve: jest.fn().mockReturnValue(noPublicKey) }, alg, did)
).rejects.toEqual(new Error(`DID document for ${did} does not have public keys for ${alg}`))
})

it('errors if no DID document exists', async () => {
return expect(resolveAuthenticator({ resolve: jest.fn().mockReturnValue(null) }, alg, did)).rejects.toEqual(
return await expect(resolveAuthenticator({ resolve: jest.fn().mockReturnValue(null) }, alg, did)).rejects.toEqual(
new Error(`Unable to resolve DID document for ${did}`)
)
})

it('errors if no supported signature types exist', async () => {
return expect(
return await expect(
resolveAuthenticator({ resolve: jest.fn().mockReturnValue(singleKey) }, 'ESBAD', did)
).rejects.toEqual(new Error('No supported signature types for algorithm ESBAD'))
})
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/SignerAlgorithm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('SignerAlgorithm', () => {
describe('ES256K', () => {
const jwtSigner = SignerAlgorithm('ES256K')
it('returns correct signature', async () => {
return expect(jwtSigner('hello', signer)).resolves.toEqual(
return await expect(jwtSigner('hello', signer)).resolves.toEqual(
'MaCPcIypS76TnvKSbhbPMG01BJvjQ6ouITV-mVt7_bfTZfGkEdwooSqbzPBHAlZXGzYYvrTnH4M9lF3OZMdpRQ'
)
})
Expand All @@ -65,7 +65,7 @@ describe('ES256K', () => {
describe('ES256K-R', () => {
const jwtSigner = SignerAlgorithm('ES256K-R')
it('returns correct signature', async () => {
return expect(jwtSigner('hello', signer)).resolves.toEqual(
return await expect(jwtSigner('hello', signer)).resolves.toEqual(
'MaCPcIypS76TnvKSbhbPMG01BJvjQ6ouITV-mVt7_bfTZfGkEdwooSqbzPBHAlZXGzYYvrTnH4M9lF3OZMdpRQE'
)
})
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('ES256K-R', () => {
describe('Ed25519', () => {
const jwtSigner = SignerAlgorithm('Ed25519')
it('returns correct signature', async () => {
return expect(jwtSigner('hello', edSigner)).resolves.toEqual(
return await expect(jwtSigner('hello', edSigner)).resolves.toEqual(
'lLY_SeplJc_4tgMP1BHmjfxS0UEi-Xvonzbss4GT7yuFz--H28uCwsRjlIwXL4I0ugCrM-zQoA2gW2JdnFRkDQ'
)
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/SimpleSigner-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const privateKey = '278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25
const signer = SimpleSigner(privateKey)
it('signs data', async () => {
const plaintext = 'thequickbrownfoxjumpedoverthelazyprogrammer'
return expect(signer(plaintext)).resolves.toMatchSnapshot()
return await expect(signer(plaintext)).resolves.toMatchSnapshot()
})

0 comments on commit d3b2e9e

Please sign in to comment.