Skip to content

Commit c971b79

Browse files
authored
test: coverage 100% (#36)
1 parent 188f5a7 commit c971b79

File tree

2 files changed

+87
-28
lines changed

2 files changed

+87
-28
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function basicPlugin (fastify, opts, next) {
88
if (typeof opts.validate !== 'function') {
99
return next(new Error('Basic Auth: Missing validate function'))
1010
}
11-
const authenticateHeader = getAuthenticateHeader(opts.authenticate)
11+
const authenticateHeader = getAuthenticateHeader(opts.authenticate, next)
1212
const validate = opts.validate.bind(fastify)
1313
fastify.decorate('basicAuth', basicAuth)
1414

@@ -42,7 +42,7 @@ function basicPlugin (fastify, opts, next) {
4242
}
4343
}
4444

45-
function getAuthenticateHeader (authenticate) {
45+
function getAuthenticateHeader (authenticate, next) {
4646
if (!authenticate) return false
4747
if (authenticate === true) {
4848
return {
@@ -60,7 +60,7 @@ function getAuthenticateHeader (authenticate) {
6060
}
6161
}
6262

63-
throw Error('Basic Auth: Invalid authenticate option')
63+
next(new Error('Basic Auth: Invalid authenticate option'))
6464
}
6565

6666
module.exports = fp(basicPlugin, {

test.js

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('Basic', t => {
3939
}
4040
}, (err, res) => {
4141
t.error(err)
42-
t.strictEqual(res.statusCode, 200)
42+
t.equal(res.statusCode, 200)
4343
})
4444
})
4545

@@ -76,8 +76,8 @@ test('Basic - 401', t => {
7676
}
7777
}, (err, res) => {
7878
t.error(err)
79-
t.strictEqual(res.statusCode, 401)
80-
t.deepEqual(JSON.parse(res.payload), {
79+
t.equal(res.statusCode, 401)
80+
t.same(JSON.parse(res.payload), {
8181
error: 'Unauthorized',
8282
message: 'Winter is coming',
8383
statusCode: 401
@@ -118,7 +118,7 @@ test('Basic with promises', t => {
118118
}
119119
}, (err, res) => {
120120
t.error(err)
121-
t.strictEqual(res.statusCode, 200)
121+
t.equal(res.statusCode, 200)
122122
})
123123
})
124124

@@ -155,8 +155,8 @@ test('Basic with promises - 401', t => {
155155
}
156156
}, (err, res) => {
157157
t.error(err)
158-
t.strictEqual(res.statusCode, 401)
159-
t.deepEqual(JSON.parse(res.payload), {
158+
t.equal(res.statusCode, 401)
159+
t.same(JSON.parse(res.payload), {
160160
error: 'Unauthorized',
161161
message: 'Winter is coming',
162162
statusCode: 401
@@ -197,9 +197,9 @@ test('WWW-Authenticate (authenticate: true)', t => {
197197
authorization: basicAuthHeader('user', 'pwd')
198198
}
199199
}, (err, res) => {
200-
t.is(res.headers['www-authenticate'], 'Basic')
200+
t.equal(res.headers['www-authenticate'], 'Basic')
201201
t.error(err)
202-
t.strictEqual(res.statusCode, 200)
202+
t.equal(res.statusCode, 200)
203203
})
204204
})
205205

@@ -236,9 +236,9 @@ test('WWW-Authenticate Realm (authenticate: {realm: "example"})', t => {
236236
authorization: basicAuthHeader('user', 'pwd')
237237
}
238238
}, (err, res) => {
239-
t.is(res.headers['www-authenticate'], 'Basic realm="example"')
239+
t.equal(res.headers['www-authenticate'], 'Basic realm="example"')
240240
t.error(err)
241-
t.strictEqual(res.statusCode, 200)
241+
t.equal(res.statusCode, 200)
242242
})
243243
})
244244

@@ -249,7 +249,7 @@ test('Missing validate function', t => {
249249
fastify.register(basicAuth)
250250

251251
fastify.ready(err => {
252-
t.is(err.message, 'Basic Auth: Missing validate function')
252+
t.equal(err.message, 'Basic Auth: Missing validate function')
253253
})
254254
})
255255

@@ -287,8 +287,8 @@ test('Hook - 401', t => {
287287
}
288288
}, (err, res) => {
289289
t.error(err)
290-
t.strictEqual(res.statusCode, 401)
291-
t.deepEqual(JSON.parse(res.payload), {
290+
t.equal(res.statusCode, 401)
291+
t.same(JSON.parse(res.payload), {
292292
error: 'Unauthorized',
293293
message: 'Winter is coming',
294294
statusCode: 401
@@ -331,8 +331,8 @@ test('With fastify-auth - 401', t => {
331331
}
332332
}, (err, res) => {
333333
t.error(err)
334-
t.strictEqual(res.statusCode, 401)
335-
t.deepEqual(JSON.parse(res.payload), {
334+
t.equal(res.statusCode, 401)
335+
t.same(JSON.parse(res.payload), {
336336
error: 'Unauthorized',
337337
message: 'Winter is coming',
338338
statusCode: 401
@@ -375,8 +375,8 @@ test('Hook with fastify-auth- 401', t => {
375375
}
376376
}, (err, res) => {
377377
t.error(err)
378-
t.strictEqual(res.statusCode, 401)
379-
t.deepEqual(JSON.parse(res.payload), {
378+
t.equal(res.statusCode, 401)
379+
t.same(JSON.parse(res.payload), {
380380
error: 'Unauthorized',
381381
message: 'Winter is coming',
382382
statusCode: 401
@@ -414,8 +414,8 @@ test('Missing header', t => {
414414
method: 'GET'
415415
}, (err, res) => {
416416
t.error(err)
417-
t.strictEqual(res.statusCode, 401)
418-
t.deepEqual(JSON.parse(res.payload), {
417+
t.equal(res.statusCode, 401)
418+
t.same(JSON.parse(res.payload), {
419419
statusCode: 401,
420420
error: 'Unauthorized',
421421
message: 'Missing or bad formatted authorization header'
@@ -458,7 +458,7 @@ test('Fastify context', t => {
458458
}
459459
}, (err, res) => {
460460
t.error(err)
461-
t.strictEqual(res.statusCode, 200)
461+
t.equal(res.statusCode, 200)
462462
})
463463
})
464464

@@ -486,7 +486,7 @@ test('setErrorHandler custom error and 401', t => {
486486
})
487487

488488
fastify.setErrorHandler(function (err, req, reply) {
489-
t.strictEqual(err.statusCode, 401)
489+
t.equal(err.statusCode, 401)
490490
reply.send(err)
491491
})
492492

@@ -498,8 +498,8 @@ test('setErrorHandler custom error and 401', t => {
498498
}
499499
}, (err, res) => {
500500
t.error(err)
501-
t.strictEqual(res.statusCode, 401)
502-
t.deepEqual(JSON.parse(res.payload), {
501+
t.equal(res.statusCode, 401)
502+
t.same(JSON.parse(res.payload), {
503503
error: 'Unauthorized',
504504
message: 'Winter is coming',
505505
statusCode: 401
@@ -542,15 +542,74 @@ test('Missing header and custom error handler', t => {
542542
method: 'GET'
543543
}, (err, res) => {
544544
t.error(err)
545-
t.strictEqual(res.statusCode, 401)
546-
t.deepEqual(JSON.parse(res.payload), {
545+
t.equal(res.statusCode, 401)
546+
t.same(JSON.parse(res.payload), {
547547
statusCode: 401,
548548
error: 'Unauthorized',
549549
message: 'Missing or bad formatted authorization header'
550550
})
551551
})
552552
})
553553

554+
test('Invalid options (authenticate)', t => {
555+
t.plan(1)
556+
557+
const fastify = Fastify()
558+
fastify
559+
.register(basicAuth, { validate, authenticate: 'i am invalid' })
560+
561+
function validate (username, password, req, res, done) {
562+
if (username === 'user' && password === 'pwd') {
563+
done()
564+
} else {
565+
done(new Error('Unauthorized'))
566+
}
567+
}
568+
569+
fastify.ready(function (err) {
570+
t.equal(err.message, 'Basic Auth: Invalid authenticate option')
571+
})
572+
})
573+
574+
test('Invalid options (authenticate realm)', t => {
575+
t.plan(3)
576+
577+
const fastify = Fastify()
578+
fastify
579+
.register(basicAuth, { validate, authenticate: { realm: true } })
580+
581+
function validate (username, password, req, res, done) {
582+
if (username === 'user' && password === 'pwd') {
583+
done()
584+
} else {
585+
done(new Error('Unauthorized'))
586+
}
587+
}
588+
589+
fastify.after(() => {
590+
fastify.route({
591+
method: 'GET',
592+
url: '/',
593+
preHandler: fastify.basicAuth,
594+
handler: (req, reply) => {
595+
reply.send({ hello: 'world' })
596+
}
597+
})
598+
})
599+
600+
fastify.inject({
601+
url: '/',
602+
method: 'GET',
603+
headers: {
604+
authorization: basicAuthHeader('user', 'pwd')
605+
}
606+
}, (err, res) => {
607+
t.equal(res.headers['www-authenticate'], 'Basic')
608+
t.error(err)
609+
t.equal(res.statusCode, 200)
610+
})
611+
})
612+
554613
function basicAuthHeader (username, password) {
555614
return 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64')
556615
}

0 commit comments

Comments
 (0)