Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12, 14, 16]
node-version: [10, 12, 14, 16]
os: [macos-latest, ubuntu-latest, windows-latest]

steps:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
},
"devDependencies": {
"@types/node": "^17.0.0",
"ava": "^4.1.0",
"ava": "^3.6.0",
"fastify": "^3.0.0",
"fastify-cookie": "^5.3.1",
"got": "^11.6.0",
"nyc": "^15.0.0",
"standard": "^16.0.1",
"tsd": "^0.19.0",
"typescript": "^4.0.2",
"undici": "^4.16.0"
"typescript": "^4.0.2"
},
"types": "types/types.d.ts",
"files": [
Expand Down
43 changes: 30 additions & 13 deletions test/secret.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,80 @@ const Fastify = require('fastify')
const fastifyCookie = require('fastify-cookie')
const fastifySession = require('..')

test('register should fail if no secret is specified', async t => {
test.cb('register should fail if no secret is specified', t => {
t.plan(1)
const fastify = Fastify()

const options = {}
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)

await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'the secret option is required!' })
fastify.ready((err) => {
t.true(err instanceof Error)
t.end()
})
})

test('register should succeed if valid secret is specified', async t => {
test.cb('register should succeed if valid secret is specified', t => {
t.plan(1)
const fastify = Fastify()

const options = { secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk' }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
t.truthy(await fastify.ready())
fastify.ready((err) => {
t.falsy(err)
t.end()
})
})

test('register should fail if the secret is too short', async t => {
test.cb('register should fail if the secret is too short', t => {
t.plan(1)
const fastify = Fastify()

const options = { secret: 'geheim' }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'the secret must have length 32 or greater' })
fastify.ready((err) => {
t.true(err instanceof Error)
t.end()
})
})

test('register should succeed if secret is short, but in an array', async t => {
test.cb('register should succeed if secret is short, but in an array', t => {
t.plan(1)
const fastify = Fastify()

const options = { secret: ['geheim'] }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
t.truthy(await fastify.ready())
fastify.ready((err) => {
t.falsy(err)
t.end()
})
})

test('register should succeed if multiple secrets are present', async t => {
test.cb('register should succeed if multiple secrets are present', t => {
t.plan(1)
const fastify = Fastify()

const options = { secret: ['geheim', 'test'] }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
t.truthy(await fastify.ready())
fastify.ready((err) => {
t.falsy(err)
t.end()
})
})

test('register should fail if no secret is present in array', async t => {
test.cb('register should fail if no secret is present in array', t => {
t.plan(1)
const fastify = Fastify()

const options = { secret: [] }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)
await t.throwsAsync(fastify.ready, { instanceOf: Error, message: 'at least one secret is required' })
fastify.ready((err) => {
t.true(err instanceof Error)
t.end()
})
})
18 changes: 10 additions & 8 deletions test/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,18 @@ test('should generate new sessionId', async (t) => {
t.is(response2.statusCode, 200)
})

test('should decorate the server with decryptSession', async t => {
test.cb('should decorate the server with decryptSession', t => {
t.plan(2)
const fastify = Fastify()

const options = { secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk' }
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)

t.truthy(await fastify.ready())
t.truthy(fastify.decryptSession)
fastify.ready((err) => {
t.falsy(err)
t.truthy(fastify.decryptSession)
t.end()
})
})

test('should decryptSession with custom request object', async (t) => {
Expand Down Expand Up @@ -371,15 +373,15 @@ test('should use custom sessionId generator if available (with request)', async
url: 'http://localhost:' + fastify.server.address().port
})
t.is(response1.statusCode, 200)
t.true(response1.headers['set-cookie'] !== undefined)
t.true(response1.headers['set-cookie'] != null)
t.true(sessionBody1.startsWith('custom-'))

const { response: response2 } = await request({
url: 'http://localhost:' + fastify.server.address().port + '/login',
headers: { Cookie: response1.headers['set-cookie'] }
})
t.is(response2.statusCode, 200)
t.true(response2.headers['set-cookie'] !== undefined)
t.true(response2.headers['set-cookie'] != null)

const { response: response3, body: sessionBody3 } = await request({
url: 'http://localhost:' + fastify.server.address().port,
Expand Down Expand Up @@ -427,15 +429,15 @@ test('should use custom sessionId generator if available (with request and rolli
url: 'http://localhost:' + fastify.server.address().port
})
t.is(response1.statusCode, 200)
t.true(response1.headers['set-cookie'] !== undefined)
t.true(response1.headers['set-cookie'] != null)
t.true(sessionBody1.startsWith('custom-'))

const { response: response2 } = await request({
url: 'http://localhost:' + fastify.server.address().port + '/login',
headers: { Cookie: response1.headers['set-cookie'] }
})
t.is(response2.statusCode, 200)
t.true(response2.headers['set-cookie'] !== undefined)
t.true(response2.headers['set-cookie'] != null)

const { response: response3, body: sessionBody3 } = await request({
url: 'http://localhost:' + fastify.server.address().port,
Expand Down
2 changes: 1 addition & 1 deletion test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('should set new session cookie if expired', async (t) => {
})

t.is(statusCode, 500)
t.is(cookie, undefined)
t.is(cookie, null)
})

test('store should be an event emitter', t => {
Expand Down
15 changes: 5 additions & 10 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Fastify = require('fastify')
const undici = require('undici')
const got = require('got')
const fastifyCookie = require('fastify-cookie')
const fastifySession = require('../lib/fastifySession')

Expand All @@ -23,19 +23,14 @@ async function testServer (handler, sessionOptions, plugin) {

async function request (options) {
let response
let body
try {
if (typeof options === 'string') {
response = await undici.request(options)
} else {
response = await undici.request(options.url, options)
}
body = await response.body.text()
response = await got(options)
} catch (err) {
response = err.response
}
const { statusCode } = response
const cookie = response.headers['set-cookie']
const { statusCode, body } = response
const cookieHeader = response.headers['set-cookie']
const cookie = cookieHeader ? cookieHeader[0] : null
return { response, body, statusCode, cookie }
}

Expand Down