Skip to content

Commit

Permalink
Chore/node test runner (#502)
Browse files Browse the repository at this point in the history
* chore: convert to node test runner

* fix: verifier assertion

* chore: lib

* chore: test

* chore: test runner

* chore: removed borp

* chore: removed types

* breaking: increase min version to 20

* fix: signer
  • Loading branch information
ilteoood authored Nov 5, 2024
1 parent 864931a commit 72762fd
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 618 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"scripts": {
"postpublish": "git push origin && git push origin -f --tags",
"lint": "eslint src/**/*.js test/**/*.js src/**/*.ts test/**/*.ts",
"test": "tap --reporter=spec --coverage-report=html --coverage-report=text --100 --no-browser test/*.spec.js test/**/*.spec.js && tsd",
"test:ci": "npm run lint && tap --no-color --reporter=spec --coverage-report=json --coverage-report=text --100 test/*.spec.js test/**/*.spec.js && tsd",
"test:watch": "tap --watch --reporter=spec --coverage-report=html --coverage-report=text --no-browser test/*.spec.js test/**/*.spec.js",
"test": "node --test --experimental-test-coverage && tsd",
"test:ci": "npm run lint && npm run test",
"test:watch": "node --test --watch --experimental-test-coverage",
"test:generate-keys": "node benchmarks/keys/generate-keys.js",
"test:generate-tokens": "node benchmarks/keys/generate-tokens.js",
"benchmark:sign": "node benchmarks/sign.mjs",
Expand Down Expand Up @@ -72,14 +72,13 @@
"jose": "^2.0.6",
"jsonwebtoken": "^9.0.0",
"prettier": "^3.0.0",
"tap": "^16.3.4",
"tsd": "^0.31.0",
"typescript": "^5.0.2"
},
"engines": {
"node": ">=16"
"node": ">=20"
},
"tsd": {
"directory": "test"
}
}
}
6 changes: 2 additions & 4 deletions src/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ module.exports = function createSigner(options) {
}, {})

// Return the signer
const context = {
return sign.bind(null, {
key,
algorithm,
noTimestamp,
Expand All @@ -313,7 +313,5 @@ module.exports = function createSigner(options) {
isAsync: keyType === 'function',
additionalHeader,
fixedPayload
}

return sign.bind(null, context)
})
}
16 changes: 5 additions & 11 deletions test/compatibility.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
JWK: { asKey }
} = require('jose')
const { resolve } = require('node:path')
const { test } = require('tap')
const { test } = require('node:test')

const { createSigner, createVerifier } = require('../src')
const { useNewCrypto } = require('../src/crypto')
Expand Down Expand Up @@ -44,17 +44,14 @@ for (const type of ['HS', 'ES', 'RS', 'PS']) {
const verify = createVerifier({ algorithm, key: publicKey.toString() })
const token = jsonwebtokenSign({ a: 1, b: 2, c: 3 }, privateKey.toString(), { algorithm, noTimestamp: true })

t.strictSame(verify(token), { a: 1, b: 2, c: 3 })

t.end()
t.assert.deepStrictEqual(verify(token), { a: 1, b: 2, c: 3 })
})

test(`jsonwebtoken should correcty verify tokens created by fast-jwt - ${algorithm}`, t => {
const signer = createSigner({ algorithm, key: privateKey, noTimestamp: true })
const token = signer({ a: 1, b: 2, c: 3 })

t.strictSame(jsonwebtokenVerify(token, publicKey, { algorithm }), { a: 1, b: 2, c: 3 })
t.end()
t.assert.deepStrictEqual(jsonwebtokenVerify(token, publicKey, { algorithm }), { a: 1, b: 2, c: 3 })
})
}
}
Expand All @@ -70,17 +67,14 @@ if (useNewCrypto) {
}
})

t.strictSame(verify(token), { a: 1, b: 2, c: 3 })

t.end()
t.assert.deepStrictEqual(verify(token), { a: 1, b: 2, c: 3 })
})

test(`jose should correcty verify tokens created by fast-jwt - EdDSA with ${curve}`, t => {
const signer = createSigner({ key: privateKeys[curve], noTimestamp: true })
const token = signer({ a: 1, b: 2, c: 3 })

t.strictSame(joseVerify(token, asKey(publicKeys[curve])), { a: 1, b: 2, c: 3 })
t.end()
t.assert.deepStrictEqual(joseVerify(token, asKey(publicKeys[curve])), { a: 1, b: 2, c: 3 })
})
}
}
32 changes: 11 additions & 21 deletions test/compliance.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const {
JWK: { asKey }
} = require('jose')
Expand Down Expand Up @@ -85,10 +85,8 @@ test('HS256', t => {

const verified = createVerifier({ key })(token)

t.same(verified, payload)
t.equal(token, expectedToken)

t.end()
t.assert.deepStrictEqual(verified, payload)
t.assert.equal(token, expectedToken)
})

test('RS256', t => {
Expand All @@ -104,10 +102,8 @@ test('RS256', t => {

const verified = createVerifier({ key: rsaPublicKey })(token)

t.same(verified, payload)
t.equal(token, expectedToken)

t.end()
t.assert.deepStrictEqual(verified, payload)
t.assert.equal(token, expectedToken)
})

test('PS384', t => {
Expand All @@ -123,11 +119,9 @@ test('PS384', t => {

const verified = createVerifier({ key: rsaPublicKey })(token)

t.same(verified, payload)
t.assert.deepStrictEqual(verified, payload)
// Since PS algorithm uses random data, we cannot match the signature
t.equal(token.replace(/\..+/, ''), expectedToken.replace(/\..+/, ''))

t.end()
t.assert.equal(token.replace(/\..+/, ''), expectedToken.replace(/\..+/, ''))
})

test('ES512', t => {
Expand All @@ -143,11 +137,9 @@ test('ES512', t => {

const verified = createVerifier({ key: ecPublicKey })(token)

t.same(verified, payload)
t.assert.deepStrictEqual(verified, payload)
// Since ES algorithm uses random data, we cannot match the signature
t.equal(token.replace(/\..+/, ''), expectedToken.replace(/\..+/, ''))

t.end()
t.assert.equal(token.replace(/\..+/, ''), expectedToken.replace(/\..+/, ''))
})

if (useNewCrypto) {
Expand All @@ -173,9 +165,7 @@ if (useNewCrypto) {

const verified = createVerifier({ key: ed25519PublicKey })(token)

t.same(verified, payload)
t.equal(token, expectedToken)

t.end()
t.assert.deepStrictEqual(verified, payload)
t.assert.equal(token, expectedToken)
})
}
Loading

0 comments on commit 72762fd

Please sign in to comment.