Skip to content

Commit e12ca1c

Browse files
committed
fix: use HEADERS constant for service name in Discord bot authorization
1 parent e98ac4f commit e12ca1c

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

constants/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ const HEADERS_FOR_SSE = {
1616
"Cache-Control": "no-cache",
1717
};
1818

19+
const HEADERS = {
20+
SERVICE_NAME: "x-service-name",
21+
};
22+
1923
module.exports = {
2024
DOCUMENT_WRITE_SIZE,
2125
daysOfWeek,
2226
HEADERS_FOR_SSE,
27+
HEADERS,
2328
};

middlewares/authorizeBot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const botVerifcation = require("../services/botVerificationService");
2+
const { HEADERS } = require("../constants/constants.ts");
23
const { CLOUDFLARE_WORKER, CRON_JOB_HANDLER, DISCORD_SERVICE } = require("../constants/bot");
34

45
const verifyCronJob = async (req, res, next) => {
@@ -18,7 +19,7 @@ const verifyCronJob = async (req, res, next) => {
1819
const verifyDiscordBot = async (req, res, next) => {
1920
try {
2021
const token = req.headers.authorization.split(" ")[1];
21-
const serviceName = req.headers["x-service-name"] || "";
22+
const serviceName = req.headers[HEADERS.SERVICE_NAME] || "";
2223
if (serviceName === DISCORD_SERVICE) {
2324
const data = botVerifcation.verifyDiscordService(token);
2425
if (data.name !== DISCORD_SERVICE) {

test/unit/middlewares/authorizeBot.test.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const sinon = require("sinon");
33
const expect = require("chai").expect;
44
const bot = require("../../utils/generateBotToken");
55
const jwt = require("jsonwebtoken");
6+
const { HEADERS } = require("../../../constants/constants.ts");
7+
68
const { BAD_TOKEN, CLOUDFLARE_WORKER, CRON_JOB_HANDLER, DISCORD_SERVICE } = require("../../../constants/bot");
79

810
describe("Middleware | Authorize Bot", function () {
@@ -118,28 +120,39 @@ describe("Middleware | Authorize Bot", function () {
118120
});
119121

120122
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 () {
122136
const jwtStub = sinon.stub(jwt, "verify").throws(new Error("invalid token"));
123137

124138
const request = {
125139
headers: {
126140
authorization: `Bearer ${BAD_TOKEN}`,
127-
"x-service-name": DISCORD_SERVICE,
141+
[HEADERS.SERVICE_NAME]: DISCORD_SERVICE,
128142
},
129143
};
130144

131145
const response = {
132146
boom: {
133-
badRequest: sinon.spy(),
134-
unauthorized: sinon.spy(),
147+
badRequest: boomBadRequestSpy,
148+
unauthorized: boomUnauthorizedSpy,
135149
},
136150
};
137151

138-
const nextSpy = sinon.spy();
139152
authorizeBot.verifyDiscordBot(request, response, nextSpy);
140153

141154
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);
143156

144157
jwtStub.restore();
145158
});
@@ -148,34 +161,32 @@ describe("Middleware | Authorize Bot", function () {
148161
const request = {
149162
headers: {
150163
authorization: `Bearer BAD_TOKEN`,
151-
"x-service-name": DISCORD_SERVICE,
164+
[HEADERS.SERVICE_NAME]: DISCORD_SERVICE,
152165
},
153166
};
154167

155168
const response = {
156169
boom: {
157-
badRequest: sinon.spy(),
170+
badRequest: boomBadRequestSpy,
158171
},
159172
};
160173

161-
const nextSpy = sinon.spy();
162174
authorizeBot.verifyDiscordBot(request, response, nextSpy);
163175
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);
165177
});
166178

167179
it("should allow request propagation when token is valid for discord service", function () {
168180
const jwtToken = bot.generateDiscordServiceToken({ name: DISCORD_SERVICE });
169181
const request = {
170182
headers: {
171183
authorization: `Bearer ${jwtToken}`,
172-
"x-service-name": DISCORD_SERVICE,
184+
[HEADERS.SERVICE_NAME]: DISCORD_SERVICE,
173185
},
174186
};
175187

176188
const response = {};
177189

178-
const nextSpy = sinon.spy();
179190
authorizeBot.verifyDiscordBot(request, response, nextSpy);
180191
expect(nextSpy.calledOnce).to.be.equal(true);
181192
});
@@ -185,13 +196,12 @@ describe("Middleware | Authorize Bot", function () {
185196
const request = {
186197
headers: {
187198
authorization: `Bearer ${jwtToken}`,
188-
"x-service-name": DISCORD_SERVICE,
199+
[HEADERS.SERVICE_NAME]: DISCORD_SERVICE,
189200
},
190201
};
191202

192203
const response = {};
193204

194-
const nextSpy = sinon.spy();
195205
authorizeBot.verifyDiscordBot(request, response, nextSpy);
196206
expect(nextSpy.calledOnce).to.be.equal(true);
197207
});
@@ -201,13 +211,12 @@ describe("Middleware | Authorize Bot", function () {
201211
const request = {
202212
headers: {
203213
authorization: `Bearer ${jwtToken}`,
204-
"x-service-name": DISCORD_SERVICE,
214+
[HEADERS.SERVICE_NAME]: DISCORD_SERVICE,
205215
},
206216
};
207217

208218
const response = {};
209219

210-
const nextSpy = sinon.spy();
211220
authorizeBot.verifyDiscordBot(request, response, nextSpy);
212221
expect(nextSpy.calledOnce).to.be.equal(false);
213222
});
@@ -223,7 +232,6 @@ describe("Middleware | Authorize Bot", function () {
223232

224233
const response = {};
225234

226-
const nextSpy = sinon.spy();
227235
authorizeBot.verifyDiscordBot(request, response, nextSpy);
228236
expect(nextSpy.calledOnce).to.be.equal(false);
229237
});

0 commit comments

Comments
 (0)