Skip to content

Commit 50e99cd

Browse files
committed
add test for server method
1 parent 7eb70a1 commit 50e99cd

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

test/_helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function getOptions (customs) {
3535
*
3636
* @param {number} code The status code to be returned
3737
* @param {Object} data The response object to be returned
38-
* @param {boolean} isError Whether to reply with an error
38+
* @param {boolean} [isError=false] Whether to reply with an error
3939
*/
40-
function mock (code, data, isError) {
40+
function mock (code, data, isError = false) {
4141
const base = nock(fixtures.common.baseUrl)
4242
.post(`${fixtures.common.realmPath}${fixtures.common.introspectPath}`)
4343

test/index.spec.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ test.cb.serial('authentication does fail – invalid header', (t) => {
130130
})
131131
})
132132

133-
test.cb.serial('server method validates token', (t) => {
133+
test.cb.serial('server method – authentication does succeed', (t) => {
134134
helpers.mock(200, fixtures.content.userData)
135135

136136
helpers.getServer(undefined, (server) => {
@@ -143,22 +143,23 @@ test.cb.serial('server method validates token', (t) => {
143143
})
144144
})
145145

146-
test.cb.serial('server method invalidates token – validation error', (t) => {
147-
helpers.mock(400, 'an error', true)
146+
test.cb.serial('server method – authentication does succeed – cache', (t) => {
147+
helpers.mock(200, fixtures.content.userData)
148+
helpers.mock(200, fixtures.content.userData)
148149

149150
helpers.getServer(undefined, (server) => {
150-
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
151-
t.falsy(res)
152-
t.truthy(err)
153-
t.truthy(err.isBoom)
154-
t.is(err.output.statusCode, 401)
155-
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="an error"')
156-
t.end()
151+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, () => {
152+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
153+
t.falsy(err)
154+
t.truthy(res)
155+
t.truthy(res.credentials)
156+
t.end()
157+
})
157158
})
158159
})
159160
})
160161

161-
test.cb.serial('server method invalidates token – invalid', (t) => {
162+
test.cb.serial('server method – authentication does fail – invalid token', (t) => {
162163
helpers.mock(200, { active: false })
163164

164165
helpers.getServer(undefined, (server) => {
@@ -173,7 +174,7 @@ test.cb.serial('server method invalidates token – invalid', (t) => {
173174
})
174175
})
175176

176-
test.cb.serial('server method invalidates token – wrong format', (t) => {
177+
test.cb.serial('server method – authentication does fail – invalid header', (t) => {
177178
helpers.getServer(undefined, (server) => {
178179
server.kjwt.validate(fixtures.jwt.userData, (err, res) => {
179180
t.falsy(res)
@@ -185,3 +186,18 @@ test.cb.serial('server method invalidates token – wrong format', (t) => {
185186
})
186187
})
187188
})
189+
190+
test.cb.serial('server method – authentication does fail – error', (t) => {
191+
helpers.mock(400, 'an error', true)
192+
193+
helpers.getServer(undefined, (server) => {
194+
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
195+
t.falsy(res)
196+
t.truthy(err)
197+
t.truthy(err.isBoom)
198+
t.is(err.output.statusCode, 401)
199+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer error="an error"')
200+
t.end()
201+
})
202+
})
203+
})

0 commit comments

Comments
 (0)