Skip to content

Commit

Permalink
Write test for error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Dec 20, 2019
1 parent 552f9df commit 26b6747
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 4 deletions.
137 changes: 137 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ms": "^2.1.2"
},
"devDependencies": {
"@types/sinon": "^7.5.1",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"ava": "^2.4.0",
Expand All @@ -49,6 +50,7 @@
"integreat": "^0.7.6",
"nyc": "^14.1.1",
"prettier": "^1.19.1",
"sinon": "^7.5.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
}
Expand Down
28 changes: 26 additions & 2 deletions src/tests/commonjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import test from 'ava'
import sinon = require('sinon')

import resourcesFn = require('..')

const resources = resourcesFn()

test('should have resources', t => {
const resources = resourcesFn()
t.truthy(resources)
t.truthy(resources.authenticators)
t.truthy(resources.authenticators.jwt)
})

test('should log error', async t => {
const logger = {
info: sinon.stub(),
error: sinon.stub()
}
const resources = resourcesFn(logger)
const options = {
audience: 'waste-iq',
key: 's3cr3t',
subPath: 'params.userid'
}
const request = {
action: 'GET',
params: {},
data: null,
access: { ident: { id: 'johnf' } }
}

const ret = await resources.authenticators.jwt.authenticate(options, request)

t.is(ret.status, 'refused')
t.is(logger.error.callCount, 1)
})
28 changes: 26 additions & 2 deletions src/tests/import.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import test from 'ava'
import sinon = require('sinon')

import resourcesFn from '..'

const resources = resourcesFn()

test('should have resources', t => {
const resources = resourcesFn()
t.truthy(resources)
t.truthy(resources.authenticators)
t.truthy(resources.authenticators.jwt)
})

test('should log error', async t => {
const logger = {
info: sinon.stub(),
error: sinon.stub()
}
const resources = resourcesFn(logger)
const options = {
audience: 'waste-iq',
key: 's3cr3t',
expiresIn: 'invalid' // To make signing fail
}
const request = {
action: 'GET',
params: {},
data: null,
access: { ident: { id: 'johnf' } }
}

const ret = await resources.authenticators.jwt.authenticate(options, request)

t.is(ret.status, 'refused')
t.is(logger.error.callCount, 1)
})

0 comments on commit 26b6747

Please sign in to comment.