@@ -3,6 +3,8 @@ const sinon = require("sinon");
3
3
const expect = require ( "chai" ) . expect ;
4
4
const bot = require ( "../../utils/generateBotToken" ) ;
5
5
const jwt = require ( "jsonwebtoken" ) ;
6
+ const { HEADERS } = require ( "../../../constants/constants.ts" ) ;
7
+
6
8
const { BAD_TOKEN , CLOUDFLARE_WORKER , CRON_JOB_HANDLER , DISCORD_SERVICE } = require ( "../../../constants/bot" ) ;
7
9
8
10
describe ( "Middleware | Authorize Bot" , function ( ) {
@@ -118,28 +120,39 @@ describe("Middleware | Authorize Bot", function () {
118
120
} ) ;
119
121
120
122
describe ( "Check authorization of bot for discord service" , function ( ) {
121
- it ( "should return unauthorized when token is expired or malformed for discord service" , function ( ) {
123
+ let nextSpy , boomBadRequestSpy , boomUnauthorizedSpy ;
124
+
125
+ beforeEach ( function ( ) {
126
+ nextSpy = sinon . spy ( ) ;
127
+ boomBadRequestSpy = sinon . spy ( ) ;
128
+ boomUnauthorizedSpy = sinon . spy ( ) ;
129
+ } ) ;
130
+
131
+ afterEach ( function ( ) {
132
+ sinon . restore ( ) ;
133
+ } ) ;
134
+
135
+ it ( "should return unauthorized when token is malformed for discord service" , function ( ) {
122
136
const jwtStub = sinon . stub ( jwt , "verify" ) . throws ( new Error ( "invalid token" ) ) ;
123
137
124
138
const request = {
125
139
headers : {
126
140
authorization : `Bearer ${ BAD_TOKEN } ` ,
127
- "x-service-name" : DISCORD_SERVICE ,
141
+ [ HEADERS . SERVICE_NAME ] : DISCORD_SERVICE ,
128
142
} ,
129
143
} ;
130
144
131
145
const response = {
132
146
boom : {
133
- badRequest : sinon . spy ( ) ,
134
- unauthorized : sinon . spy ( ) ,
147
+ badRequest : boomBadRequestSpy ,
148
+ unauthorized : boomUnauthorizedSpy ,
135
149
} ,
136
150
} ;
137
151
138
- const nextSpy = sinon . spy ( ) ;
139
152
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
140
153
141
154
expect ( nextSpy . calledOnce ) . to . be . equal ( false ) ;
142
- expect ( response . boom . unauthorized . calledOnce ) . to . be . equal ( true ) ;
155
+ expect ( boomUnauthorizedSpy . calledOnce ) . to . be . equal ( true ) ;
143
156
144
157
jwtStub . restore ( ) ;
145
158
} ) ;
@@ -148,34 +161,32 @@ describe("Middleware | Authorize Bot", function () {
148
161
const request = {
149
162
headers : {
150
163
authorization : `Bearer BAD_TOKEN` ,
151
- "x-service-name" : DISCORD_SERVICE ,
164
+ [ HEADERS . SERVICE_NAME ] : DISCORD_SERVICE ,
152
165
} ,
153
166
} ;
154
167
155
168
const response = {
156
169
boom : {
157
- badRequest : sinon . spy ( ) ,
170
+ badRequest : boomBadRequestSpy ,
158
171
} ,
159
172
} ;
160
173
161
- const nextSpy = sinon . spy ( ) ;
162
174
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
163
175
expect ( nextSpy . calledOnce ) . to . be . equal ( false ) ;
164
- expect ( response . boom . badRequest . calledOnce ) . to . be . equal ( true ) ;
176
+ expect ( boomBadRequestSpy . calledOnce ) . to . be . equal ( true ) ;
165
177
} ) ;
166
178
167
179
it ( "should allow request propagation when token is valid for discord service" , function ( ) {
168
180
const jwtToken = bot . generateDiscordServiceToken ( { name : DISCORD_SERVICE } ) ;
169
181
const request = {
170
182
headers : {
171
183
authorization : `Bearer ${ jwtToken } ` ,
172
- "x-service-name" : DISCORD_SERVICE ,
184
+ [ HEADERS . SERVICE_NAME ] : DISCORD_SERVICE ,
173
185
} ,
174
186
} ;
175
187
176
188
const response = { } ;
177
189
178
- const nextSpy = sinon . spy ( ) ;
179
190
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
180
191
expect ( nextSpy . calledOnce ) . to . be . equal ( true ) ;
181
192
} ) ;
@@ -185,13 +196,12 @@ describe("Middleware | Authorize Bot", function () {
185
196
const request = {
186
197
headers : {
187
198
authorization : `Bearer ${ jwtToken } ` ,
188
- "x-service-name" : DISCORD_SERVICE ,
199
+ [ HEADERS . SERVICE_NAME ] : DISCORD_SERVICE ,
189
200
} ,
190
201
} ;
191
202
192
203
const response = { } ;
193
204
194
- const nextSpy = sinon . spy ( ) ;
195
205
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
196
206
expect ( nextSpy . calledOnce ) . to . be . equal ( true ) ;
197
207
} ) ;
@@ -201,13 +211,12 @@ describe("Middleware | Authorize Bot", function () {
201
211
const request = {
202
212
headers : {
203
213
authorization : `Bearer ${ jwtToken } ` ,
204
- "x-service-name" : DISCORD_SERVICE ,
214
+ [ HEADERS . SERVICE_NAME ] : DISCORD_SERVICE ,
205
215
} ,
206
216
} ;
207
217
208
218
const response = { } ;
209
219
210
- const nextSpy = sinon . spy ( ) ;
211
220
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
212
221
expect ( nextSpy . calledOnce ) . to . be . equal ( false ) ;
213
222
} ) ;
@@ -223,7 +232,6 @@ describe("Middleware | Authorize Bot", function () {
223
232
224
233
const response = { } ;
225
234
226
- const nextSpy = sinon . spy ( ) ;
227
235
authorizeBot . verifyDiscordBot ( request , response , nextSpy ) ;
228
236
expect ( nextSpy . calledOnce ) . to . be . equal ( false ) ;
229
237
} ) ;
0 commit comments