Skip to content

Commit 63cc4ea

Browse files
authored
add FORBIDDEN to token stub tests (#1982)
* does this work * yes it worked * OfficeAccessCard tests * oops this is post not get * Advertisement tests * Auth tests * import * led sign tests * printer tests + some token handling * smh * shortcut search tests and tokens * User.js tests and tokens
1 parent c31c8b8 commit 63cc4ea

File tree

11 files changed

+89
-60
lines changed

11 files changed

+89
-60
lines changed

api/main_endpoints/routes/Printer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ router.get('/healthCheck', async (req, res) => {
7474

7575
router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
7676
const decoded = await decodeToken(req);
77-
if (!decoded.token) {
77+
if (decoded.status !== OK) {
7878
logger.warn('/sendPrintRequest was requested with an invalid token');
7979
return res.sendStatus(decoded.status);
8080
}
@@ -150,8 +150,8 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
150150
});
151151

152152
router.get('/status', async (req, res) => {
153-
const decodedToken = await decodeToken(req);
154-
if (!decodedToken || Object.keys(decodedToken) === 0) {
153+
const decoded = await decodeToken(req);
154+
if (decoded.status !== OK) {
155155
logger.warn('/status was requested with an invalid token');
156156
return res.sendStatus(UNAUTHORIZED);
157157
}

api/main_endpoints/routes/ShortcutSearch.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const User = require('../models/User.js');
66
const { decodeToken } = require('../util/token-functions');
77
const {
88
OK,
9-
UNAUTHORIZED,
10-
FORBIDDEN,
119
SERVER_ERROR,
1210
} = require('../../util/constants').STATUS_CODES;
1311
const membershipState = require('../../util/constants').MEMBERSHIP_STATE;
@@ -21,7 +19,7 @@ const MAX_RESULT = 5;
2119
// Search for all cleezy urls using either alias or url
2220
router.post('/', async function(req, res) {
2321
const decoded = await decodeToken(req, membershipState.OFFICER);
24-
if (!decoded.token) {
22+
if (decoded.status !== OK) {
2523
return res.sendStatus(decoded.status);
2624
}
2725

api/main_endpoints/routes/User.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const ROWS_PER_PAGE = 20;
3333
// Delete a member
3434
router.post('/delete', async (req, res) => {
3535
const decoded = await decodeToken(req);
36-
if (!decoded.token) {
36+
if (decoded.status !== OK) {
3737
return res.sendStatus(decoded.status);
3838
}
3939

@@ -76,8 +76,7 @@ router.post('/delete', async (req, res) => {
7676
// Search for a member
7777
router.post('/search', async function(req, res) {
7878
const decoded = await decodeToken(req, membershipState.OFFICER);
79-
80-
if (!decoded.token) {
79+
if (decoded.status !== OK) {
8180
return res.sendStatus(decoded.status);
8281
}
8382

@@ -119,7 +118,7 @@ router.post('/search', async function(req, res) {
119118
// Search for all members
120119
router.post('/users', async function(req, res) {
121120
const decoded = await decodeToken(req, membershipState.OFFICER);
122-
if (!decoded.token) {
121+
if (decoded.status !== OK) {
123122
return res.sendStatus(decoded.status);
124123
}
125124

@@ -162,7 +161,7 @@ router.post('/users', async function(req, res) {
162161
// Edit/Update a member record
163162
router.post('/edit', async (req, res) => {
164163
const decoded = await decodeToken(req);
165-
if (!decoded.token) {
164+
if (decoded.status !== OK) {
166165
return res.sendStatus(decoded.status);
167166
}
168167

@@ -291,7 +290,7 @@ router.post('/edit', async (req, res) => {
291290

292291
router.post('/getPagesPrintedCount', async (req, res) => {
293292
const decoded = await decodeToken(req);
294-
if (!decoded.token) {
293+
if (decoded.status !== OK) {
295294
return res.sendStatus(decoded.status);
296295
}
297296
User.findOne({ email: req.body.email }, function(error, result) {
@@ -317,7 +316,7 @@ router.post('/getPagesPrintedCount', async (req, res) => {
317316

318317
router.post('/getUserById', async (req, res) => {
319318
const decoded = await decodeToken(req);
320-
if (!decoded.token) {
319+
if (decoded.status !== OK) {
321320
return res.sendStatus(decoded.status);
322321
}
323322
let targetUserId = req.body.userID;
@@ -403,7 +402,7 @@ router.post('/getUserDataByEmail', (req, res) => {
403402
// Search for all members with verified emails and subscribed
404403
router.post('/usersSubscribedAndVerified', async function(req, res) {
405404
const decoded = await decodeToken(req, membershipState.OFFICER);
406-
if (!decoded.token) {
405+
if (decoded.status !== OK) {
407406
return res.sendStatus(decoded.status);
408407
}
409408
User.find({ emailVerified: true, emailOptIn: true })
@@ -428,7 +427,7 @@ router.post('/usersSubscribedAndVerified', async function(req, res) {
428427
// Search for all members with verified emails, subscribed, and not banned or pending
429428
router.post('/usersValidVerifiedAndSubscribed', async function(req, res) {
430429
const decoded = await decodeToken(req, membershipState.OFFICER);
431-
if (!decoded.token) {
430+
if (decoded.status !== OK) {
432431
return res.sendStatus(decoded.status);
433432
}
434433
User.find({
@@ -454,7 +453,7 @@ router.post('/usersValidVerifiedAndSubscribed', async function(req, res) {
454453
// Generate an API key for the Messages API if the user does not have an API key; otherwise, return the existing API key
455454
router.post('/apikey', async (req, res) => {
456455
const decoded = await decodeToken(req);
457-
if (!decoded.token) {
456+
if (decoded.status !== OK) {
458457
return res.sendStatus(decoded.status);
459458
}
460459
let { _id } = decoded.token;
@@ -488,7 +487,7 @@ router.post('/apikey', async (req, res) => {
488487
// Assumes members who have paid have been assigned an expiration date
489488
router.get('/getNewPaidMembersThisSemester', async (req, res) => {
490489
const decoded = await decodeToken(req, membershipState.OFFICER);
491-
if (!decoded.token) {
490+
if (decoded.status !== OK) {
492491
return res.sendStatus(decoded.status);
493492
}
494493

test/api/Advertisement.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ describe('Advertisement', () => {
6666
expect(res).to.have.status(UNAUTHORIZED);
6767
});
6868

69-
it('Should return 401 when invalid token is sent', async () => {
69+
it('Should return 403 when invalid token is sent', async () => {
70+
setTokenStatus(null);
7071
const res = await test.sendPostRequestWithToken(token, '/api/Advertisement/createAdvertisement', VALID_ADVERTISEMENT);
71-
expect(res).to.have.status(UNAUTHORIZED);
72+
expect(res).to.have.status(FORBIDDEN);
7273
});
7374

7475
describe('audit log tests for creating ads', () => {
@@ -130,9 +131,10 @@ describe('Advertisement', () => {
130131
expect(res).to.have.status(UNAUTHORIZED);
131132
});
132133

133-
it('Should return 401 if invalid token is sent', async () => {
134+
it('Should return 403 if invalid token is sent', async () => {
135+
setTokenStatus(null);
134136
const res = await test.sendPostRequestWithToken(token, '/api/Advertisement/deleteAdvertisement', { _id: VALID_ADVERTISEMENT._id });
135-
expect(res).to.have.status(UNAUTHORIZED);
137+
expect(res).to.have.status(FORBIDDEN);
136138
});
137139

138140
it('Should return 404 if ad is not found', async () => {

test/api/Auth.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const {
1313
OK,
1414
BAD_REQUEST,
1515
UNAUTHORIZED,
16-
CONFLICT
16+
CONFLICT,
17+
FORBIDDEN
1718
} = require('../../api/util/constants').STATUS_CODES;
1819
const SceApiTester = require('../util/tools/SceApiTester');
1920

@@ -562,11 +563,12 @@ describe('Auth', () => {
562563
expect(result).to.have.status(UNAUTHORIZED);
563564
});
564565

565-
it('Should return statusCode 401 when a token is invalid',
566+
it('Should return statusCode 403 when a token is invalid',
566567
async () => {
568+
setTokenStatus(null);
567569
const result = await test.sendPostRequestWithToken(
568570
token, '/api/Auth/verify', {});
569-
expect(result).to.have.status(UNAUTHORIZED);
571+
expect(result).to.have.status(FORBIDDEN);
570572
});
571573

572574
it('Should return statusCode 200 when a ' +

test/api/LedSign.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
OK,
1010
SERVER_ERROR,
1111
UNAUTHORIZED,
12+
FORBIDDEN
1213
} = require('../../api/util/constants').STATUS_CODES;
1314
const {
1415
initializeTokenMock,
@@ -67,15 +68,16 @@ describe('LED Sign', () => {
6768
});
6869

6970
describe('/POST updateSignText', () => {
70-
it('Should return 400 when token is not sent', async () => {
71+
it('Should return 401 when token is not sent', async () => {
7172
const result = await test.sendPostRequest('/api/LedSign/updateSignText');
7273
expect(result).to.have.status(UNAUTHORIZED);
7374
});
7475

75-
it('Should return 400 when invalid token is sent', async () => {
76+
it('Should return 403 when invalid token is sent', async () => {
77+
setTokenStatus(null);
7678
const result = await test.sendPostRequestWithToken(token,
7779
'/api/LedSign/updateSignText');
78-
expect(result).to.have.status(UNAUTHORIZED);
80+
expect(result).to.have.status(FORBIDDEN);
7981
});
8082

8183
it('Should return 500 when the ssh tunnel is down', async () => {

test/api/OfficeAccessCard.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ describe('OfficeAccessCard', () => {
184184
expect(result).to.have.status(UNAUTHORIZED);
185185
});
186186

187-
it('Should return 401 when invalid token is sent', async () => {
187+
it('Should return 403 when invalid token is sent', async () => {
188+
setTokenStatus(null);
188189
const result = await test.sendPostRequestWithToken(token,
189-
DELETE_API_PATH);
190-
expect(result).to.have.status(UNAUTHORIZED);
190+
DELETE_API_PATH, { _id: VALID_ID });
191+
expect(result).to.have.status(FORBIDDEN);
191192
});
192193

193194
it('Should return 404 if the card attempted to be deleted was not found', async () => {
@@ -224,10 +225,11 @@ describe('OfficeAccessCard', () => {
224225
expect(result).to.have.status(UNAUTHORIZED);
225226
});
226227

227-
it('Should return 401 when invalid token is sent', async () => {
228+
it('Should return 403 when invalid token is sent', async () => {
229+
setTokenStatus(null);
228230
const result = await test.sendPostRequestWithToken(token,
229231
GET_ALL_CARDS_API_PATH);
230-
expect(result).to.have.status(UNAUTHORIZED);
232+
expect(result).to.have.status(FORBIDDEN);
231233
});
232234

233235
it('Should return 200 with a successful fetch of all cards', async () => {
@@ -255,10 +257,11 @@ describe('OfficeAccessCard', () => {
255257
expect(result).to.have.status(UNAUTHORIZED);
256258
});
257259

258-
it('Should return 401 when invalid token is sent', async () => {
260+
it('Should return 403 when invalid token is sent', async () => {
261+
setTokenStatus(null);
259262
const result = await test.sendPostRequestWithToken(token,
260-
EDIT_API_PATH);
261-
expect(result).to.have.status(UNAUTHORIZED);
263+
EDIT_API_PATH, { _id: testCardId, alias: NEW_ALIAS });
264+
expect(result).to.have.status(FORBIDDEN);
262265
});
263266

264267
it('Should return 400 when _id is missing from request body', async () => {

test/api/Printer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs');
77
const {
88
OK,
99
UNAUTHORIZED,
10+
FORBIDDEN,
1011
} = require('../../api/util/constants').STATUS_CODES;
1112

1213
const {
@@ -116,14 +117,15 @@ describe('Printer', () => {
116117

117118
const DUMMY_CHUNK = new FormData();
118119

119-
it('Should return 400 when token is not sent', async () => {
120+
it('Should return 401 when token is not sent', async () => {
120121
const result = await test.sendPostRequest('/api/Printer/sendPrintRequest', { DUMMY_CHUNK });
121122
expect(result).to.have.status(UNAUTHORIZED);
122123
});
123124

124-
it('Should return 400 when invalid token is sent', async () => {
125+
it('Should return 403 when invalid token is sent', async () => {
126+
setTokenStatus(null);
125127
const result = await test.sendPostRequestWithToken(token, '/api/Printer/sendPrintRequest', { DUMMY_CHUNK });
126-
expect(result).to.have.status(UNAUTHORIZED);
128+
expect(result).to.have.status(FORBIDDEN);
127129
});
128130

129131
it(`Should successfully process all ${TOTAL_CHUNKS} chunks sent (with valid token)`, async () => {

test/api/ShortcutSearch.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const chaiHttp = require('chai-http');
99
const {
1010
OK,
1111
UNAUTHORIZED,
12+
FORBIDDEN,
1213
} = require('../../api/util/constants').STATUS_CODES;
1314
const SceApiTester = require('../util/tools/SceApiTester');
1415

@@ -76,10 +77,10 @@ describe('ShortcutSearch', () => {
7677
expect(result).to.have.status(UNAUTHORIZED);
7778
});
7879

79-
it('Should return status code 401 if access level is invalid', async () => {
80-
setTokenStatus(false, { accessLevel: MEMBERSHIP_STATE.MEMBER });
80+
it('Should return status code 403 if access level is invalid', async () => {
81+
setTokenStatus(null, { accessLevel: MEMBERSHIP_STATE.MEMBER });
8182
const result = await test.sendPostRequestWithToken(token, url, queryUser);
82-
expect(result).to.have.status(UNAUTHORIZED);
83+
expect(result).to.have.status(FORBIDDEN);
8384
});
8485

8586
before(async () => {

0 commit comments

Comments
 (0)