-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathoptions.test.js
More file actions
147 lines (122 loc) · 4.57 KB
/
Copy pathoptions.test.js
File metadata and controls
147 lines (122 loc) · 4.57 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const { test } = require('node:test')
const Fastify = require('fastify')
const jwt = require('..')
const { AssertionError } = require('node:assert')
test('Options validation', async function (t) {
t.plan(3)
await t.test('Options are required', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt).ready(), new AssertionError({ expected: true, operator: '==', message: 'missing secret' }))
})
await t.test('Request method aliases', async function (t) {
t.plan(6)
await t.test('jwtDecode fail', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: 'sec',
jwtDecode: true
}).ready(), new AssertionError({ expected: true, operator: '==', message: 'Invalid options.jwtDecode', actual: false }))
})
await t.test('jwtDecode success', async function (t) {
const fastify = Fastify()
await fastify.register(jwt, {
secret: 'sec',
jwtDecode: 'hello'
})
})
await t.test('jwtVerify fail', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: 'sec',
jwtVerify: 123
}).ready(), new AssertionError({ expected: true, operator: '==', message: 'Invalid options.jwtVerify', actual: false }))
})
await t.test('jwtVerify success', async function (t) {
const fastify = Fastify()
await fastify.register(jwt, {
secret: 'sec',
jwtVerify: String('hello')
}).ready()
})
await t.test('jwtSign fail', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: 'sec',
jwtSign: {}
}).ready(), new AssertionError({ expected: true, operator: '==', message: 'Invalid options.jwtSign', actual: false }))
})
await t.test('jwtSign success', async function (t) {
const fastify = Fastify()
await fastify.register(jwt, {
secret: 'sec',
jwtSign: ''
}).ready()
})
})
await t.test('Secret formats', async function (t) {
t.plan(2)
await t.test('RS/ES algorithm in sign options and secret as string', async function (t) {
t.plan(2)
await t.test('RS algorithm (Must return an error)', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: 'test',
sign: {
algorithm: 'RS256',
aud: 'Some audience',
iss: 'Some issuer',
sub: 'Some subject'
}
}).ready(), new Error('RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
})
await t.test('ES algorithm (Must return an error)', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: 'test',
sign: {
algorithm: 'ES256',
aud: 'Some audience',
iss: 'Some issuer',
sub: 'Some subject'
}
}).ready(), new Error('ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
})
})
await t.test('RS/ES algorithm in sign options and secret as a Buffer', async function (t) {
t.plan(2)
await t.test('RS algorithm (Must return an error)', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: Buffer.from('some secret', 'base64'),
sign: {
algorithm: 'RS256',
aud: 'Some audience',
iss: 'Some issuer',
sub: 'Some subject'
}
}).ready(), new Error('RSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
})
await t.test('ES algorithm (Must return an error)', async function (t) {
t.plan(1)
const fastify = Fastify()
await t.assert.rejects(() => fastify.register(jwt, {
secret: Buffer.from('some secret', 'base64'),
sign: {
algorithm: 'ES256',
aud: 'Some audience',
iss: 'Some issuer',
sub: 'Some subject'
}
}).ready(), new Error('ECDSA Signatures set as Algorithm in the options require a private and public key to be set as the secret'))
})
})
})
})