Skip to content

Commit 0ad8878

Browse files
fix: test cases
1 parent 6fe128c commit 0ad8878

File tree

129 files changed

+775
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+775
-426
lines changed

.mocharc.js renamed to .mocharc.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Mocha configuration file
33
* Info: https://mochajs.org/#configuring-mocha-nodejs
44
*/
5-
export default {
5+
module.exports = {
66
timeout: "5000",
77
extension: ["ts", "js"],
8-
require: "ts-node/register",
8+
require: ["ts-node/register/transpile-only"]
99
};

constants/roles.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export const ROLES = {
77
INDISCORD: "in_discord",
88
};
99

10+
export const SUPERUSER = ROLES.SUPERUSER;
11+
export const APPOWNER = ROLES.APPOWNER;
12+
export const MEMBER = ROLES.MEMBER;
13+
export const ARCHIVED = ROLES.ARCHIVED;
14+
export const INDISCORD = ROLES.INDISCORD;
15+

middlewares/assignTask.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import taskModel from "../models/tasks.js";
22
import firestore from "../utils/firestore.js";
33

4-
const tasks = firestore.collection("tasks");
4+
export const tasks = firestore.collection("tasks");
55

6-
const assignTask = async function (req, res) {
6+
export const assignTask = async function (req, res) {
77
try {
88
// this hardcoded value will be removed once we have user skill
99
const { task } = await taskModel.fetchSkillLevelTask("FRONTEND", 1);

middlewares/authenticate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { retrieveUsers } from "../services/dataAccessLayer.js";
1717
* @param {Function} next - Express middleware function
1818
* @returns {Object} - Returns unauthorized object if user has been restricted.
1919
*/
20-
const checkRestricted = async (req, res, next) => {
20+
export const checkRestricted = async (req, res, next) => {
2121
const { roles } = req.userData;
2222
if (roles && roles.restricted && req.method !== "GET") {
2323
return res.boom.forbidden("You are restricted from performing this action");

middlewares/authenticateProfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const authenticateProfile = (authenticate) => {
1+
export const authenticateProfile = (authenticate) => {
22
return async (req, res, next) => {
33
if (req.query.profile === "true") {
44
return await authenticate(req, res, next);

middlewares/authorization.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* - Route requiring `superUser` role is only allowed for `super_user`.
66
* - Route requiring `appOwner` role is allowed for `superUser` and `app_owner`.
77
*/
8-
const REQUIRED_ROLES_PRIORITY = {
8+
export const REQUIRED_ROLES_PRIORITY = {
99
superUser: ["super_user"],
1010
appOwner: ["app_owner", "super_user"],
1111
default: ["default", "super_user", "app_owner"],
@@ -19,7 +19,7 @@ const REQUIRED_ROLES_PRIORITY = {
1919
* @param {Object} userRoles - Roles information of the current user.
2020
* @returns {Boolean} - Whether the current user is authorized for required role level.
2121
*/
22-
const userHasPermission = (requiredRole, userRoles) => {
22+
export const userHasPermission = (requiredRole, userRoles) => {
2323
const allowedRoles = REQUIRED_ROLES_PRIORITY[`${requiredRole}`] || ["default"];
2424
return allowedRoles.some((role) => {
2525
return Boolean(userRoles[`${role}`]);
@@ -35,7 +35,7 @@ const userHasPermission = (requiredRole, userRoles) => {
3535
* @param {String} requiredRole - The least role authority required for a route.
3636
* @returns {Function} - A middleware function that authorizes given role.
3737
*/
38-
const authorizeUser = (requiredRole) => {
38+
export const authorizeUser = (requiredRole) => {
3939
return (req, res, next) => {
4040
const { roles = {} } = req.userData;
4141
// All users should have `default` role
@@ -49,6 +49,7 @@ const authorizeUser = (requiredRole) => {
4949
};
5050

5151
export default {
52+
REQUIRED_ROLES_PRIORITY,
5253
authorizeUser,
5354
userHasPermission,
5455
};

middlewares/authorizeBot.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ export const verifyDiscordBot = async (req, res, next) => {
3535
return res.boom.badRequest("Invalid Request");
3636
}
3737
};
38+
39+
export default {
40+
verifyCronJob,
41+
verifyDiscordBot,
42+
};

middlewares/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,32 @@ export const middleware = (app) => {
3838
app.use(
3939
cors({
4040
origin: (origin, callback) => {
41-
const allowedOrigins = config.get("cors.allowedOrigins") as RegExp;
42-
if (!origin || allowedOrigins.test(origin)) {
41+
const allowedOriginsConfig = config.get("cors.allowedOrigins");
42+
43+
let allowedOrigins;
44+
try {
45+
if (allowedOriginsConfig instanceof RegExp) {
46+
allowedOrigins = allowedOriginsConfig;
47+
} else if (typeof allowedOriginsConfig === 'string') {
48+
// Handle string representation of regex
49+
const regexStr = allowedOriginsConfig.startsWith('/') && allowedOriginsConfig.endsWith('/')
50+
? allowedOriginsConfig.slice(1, -1)
51+
: allowedOriginsConfig;
52+
allowedOrigins = new RegExp(regexStr);
53+
} else {
54+
// Fallback: create from string representation
55+
allowedOrigins = new RegExp(allowedOriginsConfig.toString());
56+
}
57+
58+
if (!origin || allowedOrigins.test(origin)) {
59+
callback(null, true);
60+
} else {
61+
callback(new Error('Not allowed by CORS'));
62+
}
63+
} catch (error) {
64+
// Fallback: allow all origins if there's an error with regex
65+
console.error('CORS regex configuration error:', error);
4366
callback(null, true);
44-
} else {
45-
callback(new Error('Not allowed by CORS'));
4667
}
4768
},
4869
credentials: true,

middlewares/rateLimiting.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { TOO_MANY_REQUESTS } from "../constants/rateLimiting.js";
33
import { getRetrySeconds } from "../utils/rateLimiting.js";
44

55
// INFO: temporarily added here, will be take from env-var/config
6-
const opts = {
6+
export const opts = {
77
keyPrefix: "commonRateLimiter--login_fail_by_ip_per_minute",
88
points: 5,
99
duration: 30,
1010
blockDuration: 60 * 10,
1111
};
12-
const globalRateLimiter = new RateLimiterMemory(opts);
12+
export const globalRateLimiter = new RateLimiterMemory(opts);
1313

1414
/**
1515
* @param req object represents the HTTP request and has property for the request ip address
@@ -18,7 +18,7 @@ const globalRateLimiter = new RateLimiterMemory(opts);
1818
* @returns Promise, which:
1919
* - `resolved` with next middelware function call `next()`
2020
* - `resolved` with response status set to 429 and message `Too Many Requests` */
21-
async function commonRateLimiter(req, res, next) {
21+
export async function commonRateLimiter(req, res, next) {
2222
// INFO: get the clientIP when running behind a proxy
2323
const ipAddress = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
2424
let retrySeconds = 0;

middlewares/skipAuthorizeRolesWrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const skipAuthorizeRolesUnderFF = (authorizeMiddleware) => {
1+
export const skipAuthorizeRolesUnderFF = (authorizeMiddleware) => {
22
return (req, res, next) => {
33
const { dev } = req.query;
44
const isDev = dev === "true";

0 commit comments

Comments
 (0)