forked from fastify/session
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastifySession.checkOptions.test.js
More file actions
75 lines (60 loc) · 2.31 KB
/
Copy pathfastifySession.checkOptions.test.js
File metadata and controls
75 lines (60 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict'
const test = require('tap').test
const Fastify = require('fastify')
const fastifyCookie = require('@fastify/cookie')
const fastifySession = require('..')
const crypto = require('crypto')
test('fastifySession.checkOptions: register should fail if no secret is specified', async t => {
t.plan(1)
const fastify = Fastify()
const options = {}
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
await t.rejects(fastify.ready(), new Error('the secret option is required!'))
})
test('fastifySession.checkOptions: register should succeed if secret with 32 characters is specified', async t => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifyCookie)
const secret = crypto.randomBytes(16).toString('hex')
t.equal(secret.length, 32)
fastify.register(fastifySession, { secret })
await t.resolves(fastify.ready())
})
test('fastifySession.checkOptions: register should fail if the secret is too short', async t => {
t.plan(2)
const fastify = Fastify()
const secret = crypto.randomBytes(16).toString('hex').slice(0, 31)
t.equal(secret.length, 31)
fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret })
await t.rejects(fastify.ready(), new Error('the secret must have length 32 or greater'))
})
test('fastifySession.checkOptions: register should succeed if secret is short, but in an array', async t => {
t.plan(2)
const fastify = Fastify()
const secret = crypto.randomBytes(16).toString('hex').slice(0, 31)
t.equal(secret.length, 31)
fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret: [secret] })
await t.resolves(fastify.ready())
})
test('fastifySession.checkOptions: register should succeed if multiple secrets are present', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifyCookie)
fastify.register(fastifySession, {
secret: [
crypto.randomBytes(16).toString('hex'),
crypto.randomBytes(15).toString('hex')
]
})
await t.resolves(fastify.ready())
})
test('fastifySession.checkOptions: register should fail if no secret is present in array', async t => {
t.plan(1)
const fastify = Fastify()
fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret: [] })
await t.rejects(fastify.ready(), new Error('at least one secret is required'))
})