Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/main_endpoints/routes/Printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ router.get('/healthCheck', async (req, res) => {

router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
const decoded = await decodeToken(req);
if (!decoded.token) {
if (decoded.status !== OK) {
logger.warn('/sendPrintRequest was requested with an invalid token');
return res.sendStatus(decoded.status);
}
Expand Down Expand Up @@ -150,8 +150,8 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
});

router.get('/status', async (req, res) => {
const decodedToken = await decodeToken(req);
if (!decodedToken || Object.keys(decodedToken) === 0) {
const decoded = await decodeToken(req);
if (decoded.status !== OK) {
logger.warn('/status was requested with an invalid token');
return res.sendStatus(UNAUTHORIZED);
}
Expand Down
4 changes: 1 addition & 3 deletions api/main_endpoints/routes/ShortcutSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const User = require('../models/User.js');
const { decodeToken } = require('../util/token-functions');
const {
OK,
UNAUTHORIZED,
FORBIDDEN,
SERVER_ERROR,
} = require('../../util/constants').STATUS_CODES;
const membershipState = require('../../util/constants').MEMBERSHIP_STATE;
Expand All @@ -21,7 +19,7 @@ const MAX_RESULT = 5;
// Search for all cleezy urls using either alias or url
router.post('/', async function(req, res) {
const decoded = await decodeToken(req, membershipState.OFFICER);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

Expand Down
21 changes: 10 additions & 11 deletions api/main_endpoints/routes/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ROWS_PER_PAGE = 20;
// Delete a member
router.post('/delete', async (req, res) => {
const decoded = await decodeToken(req);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

Expand Down Expand Up @@ -76,8 +76,7 @@ router.post('/delete', async (req, res) => {
// Search for a member
router.post('/search', async function(req, res) {
const decoded = await decodeToken(req, membershipState.OFFICER);

if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

Expand Down Expand Up @@ -119,7 +118,7 @@ router.post('/search', async function(req, res) {
// Search for all members
router.post('/users', async function(req, res) {
const decoded = await decodeToken(req, membershipState.OFFICER);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

Expand Down Expand Up @@ -162,7 +161,7 @@ router.post('/users', async function(req, res) {
// Edit/Update a member record
router.post('/edit', async (req, res) => {
const decoded = await decodeToken(req);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

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

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

router.post('/getUserById', async (req, res) => {
const decoded = await decodeToken(req);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}
let targetUserId = req.body.userID;
Expand Down Expand Up @@ -403,7 +402,7 @@ router.post('/getUserDataByEmail', (req, res) => {
// Search for all members with verified emails and subscribed
router.post('/usersSubscribedAndVerified', async function(req, res) {
const decoded = await decodeToken(req, membershipState.OFFICER);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}
User.find({ emailVerified: true, emailOptIn: true })
Expand All @@ -428,7 +427,7 @@ router.post('/usersSubscribedAndVerified', async function(req, res) {
// Search for all members with verified emails, subscribed, and not banned or pending
router.post('/usersValidVerifiedAndSubscribed', async function(req, res) {
const decoded = await decodeToken(req, membershipState.OFFICER);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}
User.find({
Expand All @@ -454,7 +453,7 @@ router.post('/usersValidVerifiedAndSubscribed', async function(req, res) {
// Generate an API key for the Messages API if the user does not have an API key; otherwise, return the existing API key
router.post('/apikey', async (req, res) => {
const decoded = await decodeToken(req);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}
let { _id } = decoded.token;
Expand Down Expand Up @@ -488,7 +487,7 @@ router.post('/apikey', async (req, res) => {
// Assumes members who have paid have been assigned an expiration date
router.get('/getNewPaidMembersThisSemester', async (req, res) => {
const decoded = await decodeToken(req, membershipState.OFFICER);
if (!decoded.token) {
if (decoded.status !== OK) {
return res.sendStatus(decoded.status);
}

Expand Down
10 changes: 6 additions & 4 deletions test/api/Advertisement.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ describe('Advertisement', () => {
expect(res).to.have.status(UNAUTHORIZED);
});

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

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

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

it('Should return 404 if ad is not found', async () => {
Expand Down
8 changes: 5 additions & 3 deletions test/api/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const {
OK,
BAD_REQUEST,
UNAUTHORIZED,
CONFLICT
CONFLICT,
FORBIDDEN
} = require('../../api/util/constants').STATUS_CODES;
const SceApiTester = require('../util/tools/SceApiTester');

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

it('Should return statusCode 401 when a token is invalid',
it('Should return statusCode 403 when a token is invalid',
async () => {
setTokenStatus(null);
const result = await test.sendPostRequestWithToken(
token, '/api/Auth/verify', {});
expect(result).to.have.status(UNAUTHORIZED);
expect(result).to.have.status(FORBIDDEN);
});

it('Should return statusCode 200 when a ' +
Expand Down
8 changes: 5 additions & 3 deletions test/api/LedSign.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
OK,
SERVER_ERROR,
UNAUTHORIZED,
FORBIDDEN
} = require('../../api/util/constants').STATUS_CODES;
const {
initializeTokenMock,
Expand Down Expand Up @@ -67,15 +68,16 @@ describe('LED Sign', () => {
});

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

it('Should return 400 when invalid token is sent', async () => {
it('Should return 403 when invalid token is sent', async () => {
setTokenStatus(null);
const result = await test.sendPostRequestWithToken(token,
'/api/LedSign/updateSignText');
expect(result).to.have.status(UNAUTHORIZED);
expect(result).to.have.status(FORBIDDEN);
});

it('Should return 500 when the ssh tunnel is down', async () => {
Expand Down
19 changes: 11 additions & 8 deletions test/api/OfficeAccessCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ describe('OfficeAccessCard', () => {
expect(result).to.have.status(UNAUTHORIZED);
});

it('Should return 401 when invalid token is sent', async () => {
it('Should return 403 when invalid token is sent', async () => {
setTokenStatus(null);
const result = await test.sendPostRequestWithToken(token,
DELETE_API_PATH);
expect(result).to.have.status(UNAUTHORIZED);
DELETE_API_PATH, { _id: VALID_ID });
expect(result).to.have.status(FORBIDDEN);
});

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

it('Should return 401 when invalid token is sent', async () => {
it('Should return 403 when invalid token is sent', async () => {
setTokenStatus(null);
const result = await test.sendPostRequestWithToken(token,
GET_ALL_CARDS_API_PATH);
expect(result).to.have.status(UNAUTHORIZED);
expect(result).to.have.status(FORBIDDEN);
});

it('Should return 200 with a successful fetch of all cards', async () => {
Expand Down Expand Up @@ -256,10 +258,11 @@ describe('OfficeAccessCard', () => {
expect(result).to.have.status(UNAUTHORIZED);
});

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

it('Should return 400 when _id is missing from request body', async () => {
Expand Down
8 changes: 5 additions & 3 deletions test/api/Printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs');
const {
OK,
UNAUTHORIZED,
FORBIDDEN,
} = require('../../api/util/constants').STATUS_CODES;

const {
Expand Down Expand Up @@ -116,14 +117,15 @@ describe('Printer', () => {

const DUMMY_CHUNK = new FormData();

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

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

it(`Should successfully process all ${TOTAL_CHUNKS} chunks sent (with valid token)`, async () => {
Expand Down
7 changes: 4 additions & 3 deletions test/api/ShortcutSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const chaiHttp = require('chai-http');
const {
OK,
UNAUTHORIZED,
FORBIDDEN,
} = require('../../api/util/constants').STATUS_CODES;
const SceApiTester = require('../util/tools/SceApiTester');

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

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

before(async () => {
Expand Down
Loading