Skip to content

Commit 1904724

Browse files
committed
Test jwt, fix regression, semver breaking change
2.1.0 introduced an updated version of jsonwebtoken, which contained breaking API changes. As the phrase implies, it broke stuff. This version implements tests for that and in the spirit of semver is a major version bump unlike the last.
1 parent f32043d commit 1904724

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const bodyParse = (req) => new Promise((resolve, reject) => {
9898

9999
const filterObj = (obj, ary) => {
100100
let base = {}
101-
ary.forEach((o) => { base[o] = obj[o] })
101+
ary.forEach((o) => { if (o in obj) base[o] = obj[o] })
102102
return base
103103
}
104104

@@ -284,7 +284,7 @@ function genToken (payload = {}) {
284284
}
285285

286286
function authenticate (jwt) {
287-
let key = jwtOpts.secret || jwtOpts.publicKey
287+
let key = (jwtOpts || {}).secret || (jwtOpts || {}).publicKey
288288
if (!jwtOpts || !key) return Promise.resolve()
289289
return new Promise((resolve, reject) => {
290290
jsonwebtoken.verify(jwt, key, jwtOpts, (err, d = {}) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrud",
3-
"version": "2.1.0",
3+
"version": "3.0.0",
44
"description": "Super opinionated, minimalistic, PG centric API fabric",
55
"engines": {
66
"node": ">=6.0.0"

test.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,34 @@ const postBody = {
1414
}
1515
const basePath = '/api'
1616
const port = 8092
17-
const apiCall = getScrud({host: 'localhost', port, basePath, timeout: '10s'})
17+
const apiOpts = {host: 'localhost', port, basePath, timeout: '10s'}
1818
const putBody = {zip: 37615}
1919
const logger = () => {}
20-
const opts = {port, base: basePath, namespace: 'scrud', allowOrigins, logger}
20+
const opts = {
21+
port,
22+
base: basePath,
23+
namespace: 'scrud',
24+
allowOrigins,
25+
logger,
26+
jsonwebtoken: {
27+
secret: `SomeRandomAstString`,
28+
algorithm: `HS256`,
29+
issuer: `SCRUD`,
30+
audience: `client`,
31+
expiresIn: 1800
32+
}
33+
}
2134
Object.assign(opts, require('./../_secrets/scrud/config.json'))
2235

2336
// globals
24-
let id
37+
let id, apiCall, jwt
2538

2639
// tests
2740
test.before(async () => {
2841
await scrud.register('member')
2942
await scrud.start(opts)
43+
jwt = await scrud.genToken({some: 'stuffs'})
44+
apiCall = getScrud(Object.assign({jwt}, apiOpts))
3045
})
3146

3247
test.serial('CREATE', async (assert) => {
@@ -57,7 +72,8 @@ test.serial('DELETE', async (assert) => {
5772

5873
test.serial('regession: body parses gracefully', async (assert) => {
5974
let url = `http://localhost:${port}${basePath}/member/${id}`
60-
await assert.notThrows(axios({method: 'PUT', url, data: 'u'}))
75+
let headers = {Authorization: `Bearer ${jwt}`}
76+
await assert.notThrows(axios({method: 'PUT', url, data: 'u', headers}))
6177
})
6278

6379
test.skip('close ends pg client (not really testable)', async (assert) => {

0 commit comments

Comments
 (0)