-
Notifications
You must be signed in to change notification settings - Fork 56
/
example-async.test.js
162 lines (142 loc) · 2.94 KB
/
example-async.test.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
const t = require('tap')
const test = t.test
const { rimrafSync } = require('rimraf')
const build = require('./example-async')
let fastify = null
let token = null
t.before(() => {
rimrafSync('./authdb')
fastify = build()
})
t.teardown(async () => {
await fastify.close()
rimrafSync('./authdb')
})
test('Route without auth', t => {
t.plan(2)
fastify.inject({
method: 'GET',
url: '/no-auth'
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
})
})
test('Missing header', t => {
t.plan(2)
fastify.inject({
method: 'GET',
url: '/auth',
headers: {}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
error: 'Unauthorized',
message: 'Missing token header',
statusCode: 401
})
})
})
test('Register user', t => {
t.plan(3)
fastify.inject({
method: 'POST',
url: '/register',
payload: {
user: 'tomas',
password: 'a-very-secure-one'
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 200)
token = payload.token
t.equal(typeof payload.token, 'string')
})
})
test('Auth succesful', t => {
t.plan(2)
fastify.inject({
method: 'GET',
url: '/auth',
headers: {
auth: token
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
})
})
test('Auth succesful (multiple)', t => {
t.plan(2)
fastify.inject({
method: 'POST',
url: '/auth-multiple',
payload: {
user: 'tomas',
password: 'a-very-secure-one'
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
})
})
test('Auth not succesful', t => {
t.plan(2)
fastify.inject({
method: 'GET',
url: '/auth',
headers: {
auth: 'the winter is coming'
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
error: 'Unauthorized',
message: 'Token not valid',
statusCode: 401
})
})
})
test('Auth not succesful (multiple)', t => {
t.plan(2)
fastify.inject({
method: 'POST',
url: '/auth-multiple',
payload: {
user: 'tomas',
password: 'wrong!'
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
error: 'Unauthorized',
message: 'Password not valid',
statusCode: 401
})
})
})
test('Failure with explicit reply', t => {
t.plan(3)
fastify.inject({
method: 'POST',
url: '/auth-multiple',
payload: {
failureWithReply: true,
user: 'tomas',
password: 'wrong!'
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 401)
t.same(payload, { error: 'Unauthorized' })
})
})