diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 0ffa612f3..05a9b9ccb 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -76,7 +76,7 @@ class AuthController { // If superAdmin exists, a token should be attached to all further register requests const superAdminExists = await this.db.checkSuperadmin(req, res); if (superAdminExists) { - await this.db.getInviteTokenAndDelete(inviteToken); + await this.db.getInviteTokenAndDelete(inviteToken, req.language); } else { // This is the first account, create JWT secret to use if one is not supplied by env const jwtSecret = crypto.randomBytes(64).toString("hex"); @@ -85,7 +85,7 @@ class AuthController { const newUser = await this.db.insertUser({ ...req.body }, req.file); logger.info({ - message: successMessages.AUTH_CREATE_USER, + message: successMessages.AUTH_CREATE_USER(req.language), service: SERVICE_NAME, details: newUser._id, }); @@ -116,7 +116,7 @@ class AuthController { }); res.success({ - msg: successMessages.AUTH_CREATE_USER, + msg: successMessages.AUTH_CREATE_USER(req.language), data: { user: newUser, token: token, refreshToken: refreshToken }, }); } catch (error) { @@ -148,12 +148,12 @@ class AuthController { const { email, password } = req.body; // Check if user exists - const user = await this.db.getUserByEmail(email); + const user = await this.db.getUserByEmail(email, req.language); // Compare password const match = await user.comparePassword(password); if (match !== true) { - const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD); + const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD(req.language)); error.status = 401; next(error); return; @@ -176,7 +176,7 @@ class AuthController { userWithoutPassword.avatarImage = user.avatarImage; return res.success({ - msg: successMessages.AUTH_LOGIN_USER, + msg: successMessages.AUTH_LOGIN_USER(req.language), data: { user: userWithoutPassword, token: token, @@ -206,7 +206,7 @@ class AuthController { if (!refreshToken) { // No refresh token provided - const error = new Error(errorMessages.NO_REFRESH_TOKEN); + const error = new Error(errorMessages.NO_REFRESH_TOKEN(req.language)); error.status = 401; error.service = SERVICE_NAME; error.method = "refreshAuthToken"; @@ -243,7 +243,7 @@ class AuthController { ); return res.success({ - msg: successMessages.AUTH_TOKEN_REFRESHED, + msg: successMessages.AUTH_TOKEN_REFRESHED(req.language), data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken }, }); } catch (error) { @@ -276,7 +276,7 @@ class AuthController { // TODO is this neccessary any longer? Verify ownership middleware should handle this if (req.params.userId !== req.user._id.toString()) { - const error = new Error(errorMessages.AUTH_UNAUTHORIZED); + const error = new Error(errorMessages.AUTH_UNAUTHORIZED(req.language)); error.status = 401; error.service = SERVICE_NAME; next(error); @@ -294,13 +294,13 @@ class AuthController { // Add user email to body for DB operation req.body.email = email; // Get user - const user = await this.db.getUserByEmail(email); + const user = await this.db.getUserByEmail(email, req.language); // Compare passwords const match = await user.comparePassword(req.body.password); // If not a match, throw a 403 // 403 instead of 401 to avoid triggering axios interceptor if (!match) { - const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD); + const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD(req.language)); error.status = 403; next(error); return; @@ -311,7 +311,7 @@ class AuthController { const updatedUser = await this.db.updateUser(req, res); res.success({ - msg: successMessages.AUTH_UPDATE_USER, + msg: successMessages.AUTH_UPDATE_USER(req.language), data: updatedUser, }); } catch (error) { @@ -333,7 +333,7 @@ class AuthController { const superAdminExists = await this.db.checkSuperadmin(req, res); return res.success({ - msg: successMessages.AUTH_ADMIN_EXISTS, + msg: successMessages.AUTH_ADMIN_EXISTS(req.language), data: superAdminExists, }); } catch (error) { @@ -362,7 +362,7 @@ class AuthController { try { const { email } = req.body; - const user = await this.db.getUserByEmail(email); + const user = await this.db.getUserByEmail(email, req.language); const recoveryToken = await this.db.requestRecoveryToken(req, res); const name = user.firstName; const { clientHost } = this.settingsService.getSettings(); @@ -379,7 +379,7 @@ class AuthController { ); return res.success({ - msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN, + msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(req.language), data: msgId, }); } catch (error) { @@ -410,7 +410,7 @@ class AuthController { await this.db.validateRecoveryToken(req, res); return res.success({ - msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN, + msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController")); @@ -443,7 +443,7 @@ class AuthController { const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings); return res.success({ - msg: successMessages.AUTH_RESET_PASSWORD, + msg: successMessages.AUTH_RESET_PASSWORD(req.language), data: { user, token }, }); } catch (error) { @@ -467,7 +467,7 @@ class AuthController { const { email } = decodedToken; // Check if the user exists - const user = await this.db.getUserByEmail(email); + const user = await this.db.getUserByEmail(email, req.language); // 1. Find all the monitors associated with the team ID if superadmin const result = await this.db.getMonitorsByTeamId({ @@ -494,10 +494,10 @@ class AuthController { await this.db.deleteMonitorsByUserId(user._id); } // 6. Delete the user by id - await this.db.deleteUser(user._id); + await this.db.deleteUser(user._id, req.language); return res.success({ - msg: successMessages.AUTH_DELETE_USER, + msg: successMessages.AUTH_DELETE_USER(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "deleteUserController")); @@ -509,7 +509,7 @@ class AuthController { const allUsers = await this.db.getAllUsers(req, res); return res.success({ - msg: successMessages.AUTH_GET_ALL_USERS, + msg: successMessages.AUTH_GET_ALL_USERS(req.language), data: allUsers, }); } catch (error) { diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js index ae6b96dbd..0f532dea0 100644 --- a/Server/controllers/checkController.js +++ b/Server/controllers/checkController.js @@ -36,7 +36,7 @@ class CheckController { const check = await this.db.createCheck(checkData); return res.success({ - msg: successMessages.CHECK_CREATE, + msg: successMessages.CHECK_CREATE(req.language), data: check, }); } catch (error) { @@ -57,7 +57,7 @@ class CheckController { const result = await this.db.getChecksByMonitor(req); return res.success({ - msg: successMessages.CHECK_GET, + msg: successMessages.CHECK_GET(req.language), data: result, }); } catch (error) { @@ -77,7 +77,7 @@ class CheckController { const checkData = await this.db.getChecksByTeam(req); return res.success({ - msg: successMessages.CHECK_GET, + msg: successMessages.CHECK_GET(req.language), data: checkData, }); } catch (error) { @@ -97,7 +97,7 @@ class CheckController { const deletedCount = await this.db.deleteChecks(req.params.monitorId); return res.success({ - msg: successMessages.CHECK_DELETE, + msg: successMessages.CHECK_DELETE(req.language), data: { deletedCount }, }); } catch (error) { @@ -117,7 +117,7 @@ class CheckController { const deletedCount = await this.db.deleteChecksByTeamId(req.params.teamId); return res.success({ - msg: successMessages.CHECK_DELETE, + msg: successMessages.CHECK_DELETE(req.language), data: { deletedCount }, }); } catch (error) { @@ -144,7 +144,7 @@ class CheckController { await this.db.updateChecksTTL(teamId, ttl); return res.success({ - msg: successMessages.CHECK_UPDATE_TTL, + msg: successMessages.CHECK_UPDATE_TTL(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "updateTTL")); diff --git a/Server/controllers/inviteController.js b/Server/controllers/inviteController.js index a40f43a32..6c5592f69 100644 --- a/Server/controllers/inviteController.js +++ b/Server/controllers/inviteController.js @@ -66,7 +66,7 @@ class InviteController { }); return res.success({ - msg: successMessages.INVITE_ISSUED, + msg: successMessages.INVITE_ISSUED(req.language), data: inviteToken, }); } catch (error) { @@ -83,10 +83,10 @@ class InviteController { } try { - const invite = await this.db.getInviteToken(req.body.token); + const invite = await this.db.getInviteToken(req.body.token, req.language); return res.success({ - msg: successMessages.INVITE_VERIFIED, + msg: successMessages.INVITE_VERIFIED(req.language), data: invite, }); } catch (error) { diff --git a/Server/controllers/maintenanceWindowController.js b/Server/controllers/maintenanceWindowController.js index b301ba94c..f6de3d479 100644 --- a/Server/controllers/maintenanceWindowController.js +++ b/Server/controllers/maintenanceWindowController.js @@ -45,7 +45,7 @@ class MaintenanceWindowController { await Promise.all(dbTransactions); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_CREATE, + msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "createMaintenanceWindow")); @@ -63,7 +63,7 @@ class MaintenanceWindowController { const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language), data: maintenanceWindow, }); } catch (error) { @@ -89,7 +89,7 @@ class MaintenanceWindowController { ); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language), data: maintenanceWindows, }); } catch (error) { @@ -111,7 +111,7 @@ class MaintenanceWindowController { ); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER(req.language), data: maintenanceWindows, }); } catch (error) { @@ -129,7 +129,7 @@ class MaintenanceWindowController { try { await this.db.deleteMaintenanceWindowById(req.params.id); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_DELETE, + msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow")); @@ -150,7 +150,7 @@ class MaintenanceWindowController { req.body ); return res.success({ - msg: successMessages.MAINTENANCE_WINDOW_EDIT, + msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language), data: editedMaintenanceWindow, }); } catch (error) { diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index ccd8ec26f..a9d140745 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -43,7 +43,7 @@ class MonitorController { try { const monitors = await this.db.getAllMonitors(); return res.success({ - msg: successMessages.MONITOR_GET_ALL, + msg: successMessages.MONITOR_GET_ALL(req.language), data: monitors, }); } catch (error) { @@ -64,7 +64,7 @@ class MonitorController { try { const monitors = await this.db.getAllMonitorsWithUptimeStats(); return res.success({ - msg: successMessages.MONITOR_GET_ALL, + msg: successMessages.MONITOR_GET_ALL(req.language), data: monitors, }); } catch (error) { @@ -76,7 +76,7 @@ class MonitorController { try { const monitor = await this.db.getUptimeDetailsById(req); return res.success({ - msg: successMessages.MONITOR_GET_BY_ID, + msg: successMessages.MONITOR_GET_BY_ID(req.language), data: monitor, }); } catch (error) { @@ -105,7 +105,7 @@ class MonitorController { try { const monitorStats = await this.db.getMonitorStatsById(req); return res.success({ - msg: successMessages.MONITOR_STATS_BY_ID, + msg: successMessages.MONITOR_STATS_BY_ID(req.language), data: monitorStats, }); } catch (error) { @@ -133,7 +133,7 @@ class MonitorController { try { const monitor = await this.db.getHardwareDetailsById(req); return res.success({ - msg: successMessages.MONITOR_GET_BY_ID, + msg: successMessages.MONITOR_GET_BY_ID(req.language), data: monitor, }); } catch (error) { @@ -154,7 +154,7 @@ class MonitorController { const certificate = await fetchMonitorCertificate(sslChecker, monitor); return res.success({ - msg: successMessages.MONITOR_CERTIFICATE, + msg: successMessages.MONITOR_CERTIFICATE(req.language), data: { certificateDate: new Date(certificate.validTo), }, @@ -187,7 +187,7 @@ class MonitorController { try { const monitor = await this.db.getMonitorById(req.params.monitorId); return res.success({ - msg: successMessages.MONITOR_GET_BY_ID, + msg: successMessages.MONITOR_GET_BY_ID(req.language), data: monitor, }); } catch (error) { @@ -231,7 +231,7 @@ class MonitorController { // Add monitor to job queue this.jobQueue.addJob(monitor._id, monitor); return res.success({ - msg: successMessages.MONITOR_CREATE, + msg: successMessages.MONITOR_CREATE(req.language), data: monitor, }); } catch (error) { @@ -309,7 +309,7 @@ class MonitorController { stack: error.stack, }); } - return res.success({ msg: successMessages.MONITOR_DELETE }); + return res.success({ msg: successMessages.MONITOR_DELETE(req.language) }); } catch (error) { next(handleError(error, SERVICE_NAME, "deleteMonitor")); } @@ -390,10 +390,10 @@ class MonitorController { await Promise.all( notifications && - notifications.map(async (notification) => { - notification.monitorId = editedMonitor._id; - await this.db.createNotification(notification); - }) + notifications.map(async (notification) => { + notification.monitorId = editedMonitor._id; + await this.db.createNotification(notification); + }) ); // Delete the old job(editedMonitor has the same ID as the old monitor) @@ -401,7 +401,7 @@ class MonitorController { // Add the new job back to the queue await this.jobQueue.addJob(editedMonitor._id, editedMonitor); return res.success({ - msg: successMessages.MONITOR_EDIT, + msg: successMessages.MONITOR_EDIT(req.language), data: editedMonitor, }); } catch (error) { @@ -438,8 +438,8 @@ class MonitorController { monitor.save(); return res.success({ msg: monitor.isActive - ? successMessages.MONITOR_RESUME - : successMessages.MONITOR_PAUSE, + ? successMessages.MONITOR_RESUME(req.language) + : successMessages.MONITOR_PAUSE(req.language), data: monitor, }); } catch (error) { @@ -469,7 +469,7 @@ class MonitorController { ); return res.success({ - msg: successMessages.MONITOR_DEMO_ADDED, + msg: successMessages.MONITOR_DEMO_ADDED(req.language), data: demoMonitors.length, }); } catch (error) { @@ -488,7 +488,7 @@ class MonitorController { try { const monitors = await this.db.getMonitorsByTeamId(req); return res.success({ - msg: successMessages.MONITOR_GET_BY_TEAM_ID, + msg: successMessages.MONITOR_GET_BY_TEAM_ID(req.language), data: monitors, }); } catch (error) { diff --git a/Server/controllers/queueController.js b/Server/controllers/queueController.js index f62b9fdeb..f43237821 100644 --- a/Server/controllers/queueController.js +++ b/Server/controllers/queueController.js @@ -12,7 +12,7 @@ class JobQueueController { try { const metrics = await this.jobQueue.getMetrics(); res.success({ - msg: successMessages.QUEUE_GET_METRICS, + msg: successMessages.QUEUE_GET_METRICS(req.language), data: metrics, }); } catch (error) { @@ -25,7 +25,7 @@ class JobQueueController { try { const jobs = await this.jobQueue.getJobStats(); return res.success({ - msg: successMessages.QUEUE_GET_METRICS, + msg: successMessages.QUEUE_GET_METRICS(req.language), data: jobs, }); } catch (error) { @@ -38,7 +38,7 @@ class JobQueueController { try { await this.jobQueue.addJob(Math.random().toString(36).substring(7)); return res.success({ - msg: successMessages.QUEUE_ADD_JOB, + msg: successMessages.QUEUE_ADD_JOB(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "addJob")); @@ -50,7 +50,7 @@ class JobQueueController { try { await this.jobQueue.obliterate(); return res.success({ - msg: successMessages.QUEUE_OBLITERATE, + msg: successMessages.QUEUE_OBLITERATE(req.language), }); } catch (error) { next(handleError(error, SERVICE_NAME, "obliterateQueue")); diff --git a/Server/controllers/settingsController.js b/Server/controllers/settingsController.js index 496ed057a..209b43ebc 100644 --- a/Server/controllers/settingsController.js +++ b/Server/controllers/settingsController.js @@ -14,7 +14,7 @@ class SettingsController { const settings = { ...(await this.settingsService.getSettings()) }; delete settings.jwtSecret; return res.success({ - msg: successMessages.GET_APP_SETTINGS, + msg: successMessages.GET_APP_SETTINGS(req.language), data: settings, }); } catch (error) { @@ -35,7 +35,7 @@ class SettingsController { const updatedSettings = { ...(await this.settingsService.reloadSettings()) }; delete updatedSettings.jwtSecret; return res.success({ - msg: successMessages.UPDATE_APP_SETTINGS, + msg: successMessages.UPDATE_APP_SETTINGS(req.language), data: updatedSettings, }); } catch (error) { diff --git a/Server/controllers/statusPageController.js b/Server/controllers/statusPageController.js index 1a69c92ec..5be9050f1 100644 --- a/Server/controllers/statusPageController.js +++ b/Server/controllers/statusPageController.js @@ -26,7 +26,7 @@ class StatusPageController { try { const statusPage = await this.db.createStatusPage(req.body, req.file); return res.success({ - msg: successMessages.STATUS_PAGE_CREATE, + msg: successMessages.STATUS_PAGE_CREATE(req.language), data: statusPage, }); } catch (error) { @@ -63,7 +63,7 @@ class StatusPageController { try { const statusPage = await this.db.getStatusPage(); return res.success({ - msg: successMessages.STATUS_PAGE, + msg: successMessages.STATUS_PAGE_BY_URL(req.language), data: statusPage, }); } catch (error) { diff --git a/Server/db/mongo/modules/inviteModule.js b/Server/db/mongo/modules/inviteModule.js index 0cbf0ab04..2249ff1fb 100644 --- a/Server/db/mongo/modules/inviteModule.js +++ b/Server/db/mongo/modules/inviteModule.js @@ -41,13 +41,13 @@ const requestInviteToken = async (userData) => { * @returns {Promise} The invite token data. * @throws {Error} If the invite token is not found or there is another error. */ -const getInviteToken = async (token) => { +const getInviteToken = async (token, language) => { try { const invite = await InviteToken.findOne({ token, }); if (invite === null) { - throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND); + throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND(language)); } return invite; } catch (error) { @@ -67,13 +67,13 @@ const getInviteToken = async (token) => { * @returns {Promise} The invite token data. * @throws {Error} If the invite token is not found or there is another error. */ -const getInviteTokenAndDelete = async (token) => { +const getInviteTokenAndDelete = async (token, language) => { try { const invite = await InviteToken.findOneAndDelete({ token, }); if (invite === null) { - throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND); + throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND(language)); } return invite; } catch (error) { diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 9a8e98f4e..c64d41c8e 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -312,7 +312,7 @@ const calculateGroupStats = (group) => { avgResponseTime: checksWithResponseTime.length > 0 ? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) / - checksWithResponseTime.length + checksWithResponseTime.length : 0, }; }; @@ -330,7 +330,7 @@ const getUptimeDetailsById = async (req) => { const { monitorId } = req.params; const monitor = await Monitor.findById(monitorId); if (monitor === null || monitor === undefined) { - throw new Error(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId)); + throw new Error(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId, req.language)); } const { dateRange, normalize } = req.query; @@ -601,78 +601,78 @@ const getMonitorsByTeamId = async (req) => { filteredMonitors: [ ...(filter !== undefined ? [ - { - $match: { - $or: [ - { name: { $regex: filter, $options: "i" } }, - { url: { $regex: filter, $options: "i" } }, - ], - }, + { + $match: { + $or: [ + { name: { $regex: filter, $options: "i" } }, + { url: { $regex: filter, $options: "i" } }, + ], }, - ] + }, + ] : []), { $sort: sort }, { $skip: skip }, ...(rowsPerPage ? [{ $limit: rowsPerPage }] : []), ...(limit ? [ - { - $lookup: { - from: "checks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, - }, + { + $lookup: { + from: "checks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "standardchecks", - }, + }, + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "standardchecks", }, - ] + }, + ] : []), ...(limit ? [ - { - $lookup: { - from: "pagespeedchecks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, - }, + { + $lookup: { + from: "pagespeedchecks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "pagespeedchecks", - }, + }, + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "pagespeedchecks", }, - ] + }, + ] : []), ...(limit ? [ - { - $lookup: { - from: "hardwarechecks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, - }, + { + $lookup: { + from: "hardwarechecks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "hardwarechecks", - }, + }, + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "hardwarechecks", }, - ] + }, + ] : []), ...(limit ? [ @@ -787,7 +787,7 @@ const deleteMonitor = async (req, res) => { try { const monitor = await Monitor.findByIdAndDelete(monitorId); if (!monitor) { - throw new Error(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId)); + throw new Error(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId, req.language)); } return monitor; } catch (error) { diff --git a/Server/db/mongo/modules/recoveryModule.js b/Server/db/mongo/modules/recoveryModule.js index 40dd66c56..0efcca4e8 100644 --- a/Server/db/mongo/modules/recoveryModule.js +++ b/Server/db/mongo/modules/recoveryModule.js @@ -39,7 +39,7 @@ const validateRecoveryToken = async (req, res) => { if (recoveryToken !== null) { return recoveryToken; } else { - throw new Error(errorMessages.DB_TOKEN_NOT_FOUND); + throw new Error(errorMessages.DB_TOKEN_NOT_FOUND(req.language)); } } catch (error) { error.service = SERVICE_NAME; @@ -57,12 +57,12 @@ const resetPassword = async (req, res) => { const user = await UserModel.findOne({ email: recoveryToken.email }); if (user === null) { - throw new Error(errorMessages.DB_USER_NOT_FOUND); + throw new Error(errorMessages.DB_USER_NOT_FOUND(req.language)); } const match = await user.comparePassword(newPassword); if (match === true) { - throw new Error(errorMessages.DB_RESET_PASSWORD_BAD_MATCH); + throw new Error(errorMessages.DB_RESET_PASSWORD_BAD_MATCH(req.language)); } user.password = newPassword; diff --git a/Server/db/mongo/modules/userModule.js b/Server/db/mongo/modules/userModule.js index 5b951ff64..3460f7513 100644 --- a/Server/db/mongo/modules/userModule.js +++ b/Server/db/mongo/modules/userModule.js @@ -69,13 +69,13 @@ const insertUser = async ( * @returns {Promise} * @throws {Error} */ -const getUserByEmail = async (email) => { +const getUserByEmail = async (email, language) => { try { // Need the password to be able to compare, removed .select() // We can strip the hash before returning the user const user = await UserModel.findOne({ email: email }).select("-profileImage"); if (!user) { - throw new Error(errorMessages.DB_USER_NOT_FOUND); + throw new Error(errorMessages.DB_USER_NOT_FOUND(language)); } return user; } catch (error) { @@ -149,11 +149,11 @@ const updateUser = async ( * @returns {Promise} * @throws {Error} */ -const deleteUser = async (userId) => { +const deleteUser = async (userId, language) => { try { const deletedUser = await UserModel.findByIdAndDelete(userId); if (!deletedUser) { - throw new Error(errorMessages.DB_USER_NOT_FOUND); + throw new Error(errorMessages.DB_USER_NOT_FOUND(language)); } return deletedUser; } catch (error) { diff --git a/Server/index.js b/Server/index.js index 23d16856a..1b908436b 100644 --- a/Server/index.js +++ b/Server/index.js @@ -74,6 +74,9 @@ import MongoDB from "./db/mongo/MongoDB.js"; import IORedis from "ioredis"; +import TranslationService from './service/translationService.js'; +import languageMiddleware from './middleware/languageMiddleware.js'; + const SERVICE_NAME = "Server"; const SHUTDOWN_TIMEOUT = 1000; let isShuttingDown = false; @@ -175,6 +178,7 @@ const startApp = async () => { const networkService = new NetworkService(axios, ping, logger, http, Docker, net); const statusService = new StatusService(db, logger); const notificationService = new NotificationService(emailService, db, logger); + const translationService = new TranslationService(logger); const jobQueue = new JobQueue( db, @@ -195,6 +199,11 @@ const startApp = async () => { ServiceRegistry.register(NetworkService.SERVICE_NAME, networkService); ServiceRegistry.register(StatusService.SERVICE_NAME, statusService); ServiceRegistry.register(NotificationService.SERVICE_NAME, notificationService); + ServiceRegistry.register(TranslationService.SERVICE_NAME, translationService); + + + await translationService.initialize(); + server = app.listen(PORT, () => { logger.info({ message: `server started on port:${PORT}` }); }); @@ -267,12 +276,10 @@ const startApp = async () => { // Init job queue await jobQueue.initJobQueue(); // Middleware - app.use( - cors() - //We will add configuration later - ); + app.use(cors()); app.use(express.json()); app.use(helmet()); + app.use(languageMiddleware); // Swagger UI app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openApiSpec)); diff --git a/Server/middleware/isAllowed.js b/Server/middleware/isAllowed.js index bd75b00ad..a4fbcbf64 100644 --- a/Server/middleware/isAllowed.js +++ b/Server/middleware/isAllowed.js @@ -12,7 +12,7 @@ const isAllowed = (allowedRoles) => { // If no token is pressent, return an error if (!token) { - const error = new Error(errorMessages.NO_AUTH_TOKEN); + const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language)); error.status = 401; error.service = SERVICE_NAME; next(error); @@ -21,7 +21,7 @@ const isAllowed = (allowedRoles) => { // If the token is improperly formatted, return an error if (!token.startsWith(TOKEN_PREFIX)) { - const error = new Error(errorMessages.INVALID_AUTH_TOKEN); + const error = new Error(errorMessages.INVALID_AUTH_TOKEN(req.language)); error.status = 400; error.service = SERVICE_NAME; next(error); @@ -41,7 +41,7 @@ const isAllowed = (allowedRoles) => { next(); return; } else { - const error = new Error(errorMessages.INSUFFICIENT_PERMISSIONS); + const error = new Error(errorMessages.INSUFFICIENT_PERMISSIONS(req.language)); error.status = 401; error.service = SERVICE_NAME; next(error); diff --git a/Server/middleware/languageMiddleware.js b/Server/middleware/languageMiddleware.js new file mode 100644 index 000000000..52e7b9a2e --- /dev/null +++ b/Server/middleware/languageMiddleware.js @@ -0,0 +1,9 @@ +const languageMiddleware = (req, res, next) => { + const acceptLanguage = req.headers['accept-language'] || 'en'; + + req.language = acceptLanguage.split(',')[0].slice(0, 2).toLowerCase(); + + next(); +}; + +export default languageMiddleware; \ No newline at end of file diff --git a/Server/middleware/verifyJWT.js b/Server/middleware/verifyJWT.js index 87fa53bd6..08a55cd78 100644 --- a/Server/middleware/verifyJWT.js +++ b/Server/middleware/verifyJWT.js @@ -17,7 +17,7 @@ const verifyJWT = (req, res, next) => { const token = req.headers["authorization"]; // Make sure a token is provided if (!token) { - const error = new Error(errorMessages.NO_AUTH_TOKEN); + const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language)); error.status = 401; error.service = SERVICE_NAME; next(error); @@ -25,7 +25,7 @@ const verifyJWT = (req, res, next) => { } // Make sure it is properly formatted if (!token.startsWith(TOKEN_PREFIX)) { - const error = new Error(errorMessages.INVALID_AUTH_TOKEN); // Instantiate a new Error object for improperly formatted token + const error = new Error(errorMessages.INVALID_AUTH_TOKEN(req.language)); // Instantiate a new Error object for improperly formatted token error.status = 400; error.service = SERVICE_NAME; error.method = "verifyJWT"; @@ -43,7 +43,7 @@ const verifyJWT = (req, res, next) => { handleExpiredJwtToken(req, res, next); } else { // Invalid token (signature or token altered or other issue) - const errorMessage = errorMessages.INVALID_AUTH_TOKEN; + const errorMessage = errorMessages.INVALID_AUTH_TOKEN(req.language); return res.status(401).json({ success: false, msg: errorMessage }); } } else { @@ -60,7 +60,7 @@ function handleExpiredJwtToken(req, res, next) { if (!refreshToken) { // No refresh token provided - const error = new Error(errorMessages.NO_REFRESH_TOKEN); + const error = new Error(errorMessages.NO_REFRESH_TOKEN(req.language)); error.status = 401; error.service = SERVICE_NAME; error.method = "handleExpiredJwtToken"; diff --git a/Server/middleware/verifyOwnership.js b/Server/middleware/verifyOwnership.js index d812dd543..10de1f7b6 100644 --- a/Server/middleware/verifyOwnership.js +++ b/Server/middleware/verifyOwnership.js @@ -15,7 +15,7 @@ const verifyOwnership = (Model, paramName) => { service: SERVICE_NAME, method: "verifyOwnership", }); - const error = new Error(errorMessages.VERIFY_OWNER_NOT_FOUND); + const error = new Error(errorMessages.VERIFY_OWNER_NOT_FOUND(req.language)); error.status = 404; throw error; } @@ -23,7 +23,7 @@ const verifyOwnership = (Model, paramName) => { // Special case for User model, as it will not have a `userId` field as other docs will if (Model.modelName === "User") { if (userId.toString() !== doc._id.toString()) { - const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED); + const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED(req.language)); error.status = 403; throw error; } @@ -33,7 +33,7 @@ const verifyOwnership = (Model, paramName) => { // If the userID does not match the document's userID, return a 403 error if (userId.toString() !== doc.userId.toString()) { - const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED); + const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED(req.language)); error.status = 403; throw error; } diff --git a/Server/middleware/verifySuperAdmin.js b/Server/middleware/verifySuperAdmin.js index bf4c780a7..b4b764f5a 100644 --- a/Server/middleware/verifySuperAdmin.js +++ b/Server/middleware/verifySuperAdmin.js @@ -17,7 +17,7 @@ const verifySuperAdmin = (req, res, next) => { const token = req.headers["authorization"]; // Make sure a token is provided if (!token) { - const error = new Error(errorMessages.NO_AUTH_TOKEN); + const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language)); error.status = 401; error.service = SERVICE_NAME; next(error); @@ -25,7 +25,7 @@ const verifySuperAdmin = (req, res, next) => { } // Make sure it is properly formatted if (!token.startsWith(TOKEN_PREFIX)) { - const error = new Error(errorMessages.INVALID_AUTH_TOKEN); // Instantiate a new Error object for improperly formatted token + const error = new Error(errorMessages.INVALID_AUTH_TOKEN(req.language)); // Instantiate a new Error object for improperly formatted token error.status = 400; error.service = SERVICE_NAME; error.method = "verifySuperAdmin"; diff --git a/Server/service/jobQueue.js b/Server/service/jobQueue.js index f7c70f0a7..bbcca6601 100644 --- a/Server/service/jobQueue.js +++ b/Server/service/jobQueue.js @@ -455,7 +455,7 @@ class NewJobQueue { if (wasDeleted === true) { this.logger.info({ - message: successMessages.JOB_QUEUE_DELETE_JOB, + message: successMessages.JOB_QUEUE_DELETE_JOB('en'), service: SERVICE_NAME, method: "deleteJob", details: `Deleted job ${monitor._id}`, @@ -587,7 +587,7 @@ class NewJobQueue { const metrics = await this.getMetrics(); this.logger.info({ - message: successMessages.JOB_QUEUE_OBLITERATE, + message: successMessages.JOB_QUEUE_OBLITERATE('en'), service: SERVICE_NAME, method: "obliterate", details: metrics, diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 8a58d4b88..5e5f62721 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -93,7 +93,7 @@ class NetworkService { pingResponse.code = 200; pingResponse.status = response.alive; - pingResponse.message = successMessages.PING_SUCCESS; + pingResponse.message = successMessages.PING_SUCCESS('en'); return pingResponse; } catch (error) { error.service = this.SERVICE_NAME; @@ -240,7 +240,7 @@ class NetworkService { const containers = await docker.listContainers({ all: true }); const containerExists = containers.some((c) => c.Id.startsWith(job.data.url)); if (!containerExists) { - throw new Error(errorMessages.DOCKER_NOT_FOUND); + throw new Error(errorMessages.DOCKER_NOT_FOUND('en')); } const container = docker.getContainer(job.data.url); @@ -262,7 +262,7 @@ class NetworkService { } dockerResponse.status = response?.State?.Status === "running" ? true : false; dockerResponse.code = 200; - dockerResponse.message = successMessages.DOCKER_SUCCESS; + dockerResponse.message = successMessages.DOCKER_SUCCESS('en'); return dockerResponse; } catch (error) { error.service = this.SERVICE_NAME; @@ -316,7 +316,7 @@ class NetworkService { portResponse.status = response.success; portResponse.code = 200; - portResponse.message = successMessages.PORT_SUCCESS; + portResponse.message = successMessages.PORT_SUCCESS('en'); return portResponse; } catch (error) { error.service = this.SERVICE_NAME; diff --git a/Server/service/translationService.js b/Server/service/translationService.js new file mode 100644 index 000000000..11709262a --- /dev/null +++ b/Server/service/translationService.js @@ -0,0 +1,201 @@ +import axios from 'axios'; +import fs from 'fs'; +import path from 'path'; +import { formattedKey } from '../utils/formattedKey.js'; + +class TranslationService { + static SERVICE_NAME = 'TranslationService'; + + constructor(logger) { + this.logger = logger; + this.translations = {}; + this.apiToken = "ddf8d5fdbe1baa12bb3b5519b639d00a"; + this.projectId = "757606"; + this.baseUrl = 'https://api.poeditor.com/v2'; + this.localesDir = path.join(process.cwd(), 'locales'); + } + + async initialize() { + try { + // Önce dosyalardan okumayı dene + const loadedFromFiles = await this.loadFromFiles(); + + // Eğer dosyalardan yüklenemezse veya dosyalar yoksa POEditor'dan çek + if (!loadedFromFiles) { + await this.loadTranslations(); + } + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'initialize', + stack: error.stack + }); + } + } + + async loadFromFiles() { + try { + // locales klasörü yoksa false dön + if (!fs.existsSync(this.localesDir)) { + return false; + } + + // Klasördeki tüm .json dosyalarını oku + const files = fs.readdirSync(this.localesDir).filter(file => file.endsWith('.json')); + + if (files.length === 0) { + return false; + } + + // Her dosyayı oku ve translations objesine ekle + for (const file of files) { + const language = file.replace('.json', ''); + const filePath = path.join(this.localesDir, file); + const content = fs.readFileSync(filePath, 'utf8'); + this.translations[language] = JSON.parse(content); + } + + this.logger.info({ + message: 'Translations loaded from files successfully', + service: 'TranslationService', + method: 'loadFromFiles' + }); + + return true; + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'loadFromFiles', + stack: error.stack + }); + return false; + } + } + + async loadTranslations() { + try { + // Önce mevcut dilleri al + const languages = await this.getLanguages(); + + // Her dil için çevirileri indir + for (const language of languages) { + const translations = await this.exportTranslations(language); + this.translations[language] = translations; + } + + // Çevirileri dosyaya kaydet + await this.saveTranslations(); + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'loadTranslations', + stack: error.stack + }); + } + } + + async getLanguages() { + try { + const params = new URLSearchParams(); + params.append('api_token', this.apiToken); + params.append('id', this.projectId); + + const response = await axios.post(`${this.baseUrl}/languages/list`, params, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); + + return response.data.result.languages.map(lang => lang.code); + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'getLanguages', + stack: error.stack + }); + return ['en']; // Varsayılan olarak İngilizce + } + } + + async exportTranslations(language) { + try { + const params = new URLSearchParams(); + params.append('api_token', this.apiToken); + params.append('id', this.projectId); + params.append('language', language); + params.append('type', 'key_value_json'); + + // Export isteği + const exportResponse = await axios.post(`${this.baseUrl}/projects/export`, params, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); + + // İndirme URL'sini al + const { url } = exportResponse.data.result; + + // Çevirileri indir + const translationsResponse = await axios.get(url); + return translationsResponse.data; + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'exportTranslations', + stack: error.stack + }); + return {}; + } + } + + async saveTranslations() { + try { + // locales klasörü yoksa oluştur + if (!fs.existsSync(this.localesDir)) { + fs.mkdirSync(this.localesDir); + } + + // Her dil için JSON dosyası oluştur + for (const [language, translations] of Object.entries(this.translations)) { + const filePath = path.join(this.localesDir, `${language}.json`); + fs.writeFileSync(filePath, JSON.stringify(translations, null, 2)); + } + + this.logger.info({ + message: 'Translations saved to files successfully', + service: 'TranslationService', + method: 'saveTranslations' + }); + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'saveTranslations', + stack: error.stack + }); + } + } + + getTranslation(key, language = 'en') { + // Convert key from AUTH_INCORRECT_PASSWORD format to authIncorrectPassword format + const formattedKeyText = formattedKey(key); + try { + return this.translations[language]?.[formattedKeyText] || this.translations['en']?.[formattedKeyText] || formattedKeyText; + } catch (error) { + this.logger.error({ + message: error.message, + service: 'TranslationService', + method: 'getTranslation', + stack: error.stack + }); + return key; + } + } +} + +export default TranslationService; \ No newline at end of file diff --git a/Server/tests/controllers/authController.test.js b/Server/tests/controllers/authController.test.js index 3f45d1c42..c96b77da3 100644 --- a/Server/tests/controllers/authController.test.js +++ b/Server/tests/controllers/authController.test.js @@ -18,14 +18,16 @@ import { getTokenFromHeaders, tokenType } from "../../utils/utils.js"; import logger from "../../utils/logger.js"; import e from "cors"; -describe("Auth Controller - issueToken", function() { +const mockLanguage = 'en'; + +describe("Auth Controller - issueToken", function () { let stub; - afterEach(function() { + afterEach(function () { sinon.restore(); // Restore stubs after each test }); - it("should reject with an error if jwt.sign fails", function() { + it("should reject with an error if jwt.sign fails", function () { const error = new Error("jwt.sign error"); stub = sinon.stub(jwt, "sign").throws(error); const payload = { id: "123" }; @@ -35,7 +37,7 @@ describe("Auth Controller - issueToken", function() { ); }); - it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", function() { + it("should return a token if jwt.sign is successful and appSettings.jwtTTL is not defined", function () { const payload = { id: "123" }; const appSettings = { jwtSecret: "my_secret" }; const expectedToken = "mockToken"; @@ -45,7 +47,7 @@ describe("Auth Controller - issueToken", function() { expect(token).to.equal(expectedToken); }); - it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", function() { + it("should return a token if jwt.sign is successful and appSettings.jwtTTL is defined", function () { const payload = { id: "123" }; const appSettings = { jwtSecret: "my_secret", jwtTTL: "1s" }; const expectedToken = "mockToken"; @@ -55,7 +57,7 @@ describe("Auth Controller - issueToken", function() { expect(token).to.equal(expectedToken); }); - it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", function() { + it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is not defined", function () { const payload = {}; const appSettings = { refreshTokenSecret: "my_refresh_secret" }; const expectedToken = "mockRefreshToken"; @@ -65,7 +67,7 @@ describe("Auth Controller - issueToken", function() { expect(token).to.equal(expectedToken); }); - it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", function() { + it("should return a refresh token if jwt.sign is successful and appSettings.refreshTokenTTL is defined", function () { const payload = {}; const appSettings = { refreshTokenSecret: "my_refresh_secret", @@ -79,10 +81,10 @@ describe("Auth Controller - issueToken", function() { }); }); -describe("Auth Controller - registerUser", function() { +describe("Auth Controller - registerUser", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: { firstName: "firstname", @@ -118,25 +120,25 @@ describe("Auth Controller - registerUser", function() { sinon.stub(logger, "error"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if body validation fails", async function() { + it("should reject with an error if body validation fails", async function () { req.body = {}; await registerUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if checkSuperadmin fails", async function() { + it("should reject with an error if checkSuperadmin fails", async function () { req.db.checkSuperadmin.throws(new Error("checkSuperadmin error")); await registerUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error"); }); - it("should reject with an error if getInviteTokenAndDelete fails", async function() { + it("should reject with an error if getInviteTokenAndDelete fails", async function () { req.db.checkSuperadmin.returns(true); req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error")); await registerUser(req, res, next); @@ -144,7 +146,7 @@ describe("Auth Controller - registerUser", function() { expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error"); }); - it("should reject with an error if updateAppSettings fails", async function() { + it("should reject with an error if updateAppSettings fails", async function () { req.db.checkSuperadmin.returns(false); req.db.updateAppSettings.throws(new Error("updateAppSettings error")); await registerUser(req, res, next); @@ -152,7 +154,7 @@ describe("Auth Controller - registerUser", function() { expect(next.firstCall.args[0].message).to.equal("updateAppSettings error"); }); - it("should reject with an error if insertUser fails", async function() { + it("should reject with an error if insertUser fails", async function () { req.db.checkSuperadmin.resolves(false); req.db.updateAppSettings.resolves(); req.db.insertUser.rejects(new Error("insertUser error")); @@ -161,7 +163,7 @@ describe("Auth Controller - registerUser", function() { expect(next.firstCall.args[0].message).to.equal("insertUser error"); }); - it("should reject with an error if settingsService.getSettings fails", async function() { + it("should reject with an error if settingsService.getSettings fails", async function () { req.db.checkSuperadmin.resolves(false); req.db.updateAppSettings.resolves(); req.db.insertUser.resolves({ _id: "123" }); @@ -173,7 +175,7 @@ describe("Auth Controller - registerUser", function() { expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error"); }); - it("should log an error if emailService.buildAndSendEmail fails", async function() { + it("should log an error if emailService.buildAndSendEmail fails", async function () { req.db.checkSuperadmin.resolves(false); req.db.updateAppSettings.resolves(); req.db.insertUser.returns({ _id: "123" }); @@ -187,7 +189,7 @@ describe("Auth Controller - registerUser", function() { expect(logger.error.firstCall.args[0].message).to.equal("emailService error"); }); - it("should return a success message and data if all operations are successful", async function() { + it("should return a success message and data if all operations are successful", async function () { const user = { _id: "123" }; req.db.checkSuperadmin.resolves(false); req.db.updateAppSettings.resolves(); @@ -202,14 +204,14 @@ describe("Auth Controller - registerUser", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_CREATE_USER, + msg: successMessages.AUTH_CREATE_USER(mockLanguage), data: { user, token: sinon.match.string, refreshToken: sinon.match.string }, }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should return a success message and data if all operations are successful and superAdmin true", async function() { + it("should return a success message and data if all operations are successful and superAdmin true", async function () { const user = { _id: "123" }; req.db.checkSuperadmin.resolves(true); req.db.updateAppSettings.resolves(); @@ -224,7 +226,7 @@ describe("Auth Controller - registerUser", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_CREATE_USER, + msg: successMessages.AUTH_CREATE_USER(mockLanguage), data: { user, token: sinon.match.string, refreshToken: sinon.match.string }, }) ).to.be.true; @@ -232,15 +234,16 @@ describe("Auth Controller - registerUser", function() { }); }); -describe("Auth Controller - loginUser", function() { +describe("Auth Controller - loginUser", function () { let req, res, next, user; - beforeEach(function() { + beforeEach(function () { req = { body: { email: "test@example.com", password: "Password123!" }, db: { getUserByEmail: sinon.stub(), }, + language: 'en', settingsService: { getSettings: sinon.stub().resolves({ jwtSecret: "my_secret", @@ -261,21 +264,21 @@ describe("Auth Controller - loginUser", function() { }; }); - it("should reject with an error if validation fails", async function() { + it("should reject with an error if validation fails", async function () { req.body = {}; await loginUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if getUserByEmail fails", async function() { + it("should reject with an error if getUserByEmail fails", async function () { req.db.getUserByEmail.rejects(new Error("getUserByEmail error")); await loginUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("getUserByEmail error"); }); - it("should login user successfully", async function() { + it("should login user successfully", async function () { req.db.getUserByEmail.resolves(user); user.comparePassword.resolves(true); await loginUser(req, res, next); @@ -283,7 +286,7 @@ describe("Auth Controller - loginUser", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_LOGIN_USER, + msg: successMessages.AUTH_LOGIN_USER(mockLanguage), data: { user: { email: "test@example.com", @@ -297,7 +300,7 @@ describe("Auth Controller - loginUser", function() { expect(next.notCalled).to.be.true; }); - it("should reject a user with an incorrect password", async function() { + it("should reject a user with an incorrect password", async function () { req.body = { email: "test@test.com", password: "Password123!", @@ -307,15 +310,15 @@ describe("Auth Controller - loginUser", function() { await loginUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal( - errorMessages.AUTH_INCORRECT_PASSWORD + errorMessages.AUTH_INCORRECT_PASSWORD(mockLanguage) ); }); }); -describe("Auth Controller - refreshAuthToken", function() { +describe("Auth Controller - refreshAuthToken", function () { let req, res, next, issueTokenStub; - beforeEach(function() { + beforeEach(function () { req = { headers: { "x-refresh-token": "valid_refresh_token", @@ -339,39 +342,39 @@ describe("Auth Controller - refreshAuthToken", function() { sinon.replace({ issueToken }, "issueToken", issueTokenStub); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject if no refresh token is provided", async function() { + it("should reject if no refresh token is provided", async function () { delete req.headers["x-refresh-token"]; await refreshAuthToken(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); - expect(next.firstCall.args[0].message).to.equal(errorMessages.NO_REFRESH_TOKEN); + expect(next.firstCall.args[0].message).to.equal(errorMessages.NO_REFRESH_TOKEN(req.language)); expect(next.firstCall.args[0].status).to.equal(401); }); - it("should reject if the refresh token is invalid", async function() { + it("should reject if the refresh token is invalid", async function () { jwt.verify.yields(new Error("invalid token")); await refreshAuthToken(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); - expect(next.firstCall.args[0].message).to.equal(errorMessages.INVALID_REFRESH_TOKEN); + expect(next.firstCall.args[0].message).to.equal(errorMessages.INVALID_REFRESH_TOKEN(req.language)); expect(next.firstCall.args[0].status).to.equal(401); }); - it("should reject if the refresh token is expired", async function() { + it("should reject if the refresh token is expired", async function () { const error = new Error("Token expired"); error.name = "TokenExpiredError"; jwt.verify.yields(error); await refreshAuthToken(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); - expect(next.firstCall.args[0].message).to.equal(errorMessages.EXPIRED_REFRESH_TOKEN); + expect(next.firstCall.args[0].message).to.equal(errorMessages.EXPIRED_REFRESH_TOKEN(req.language)); expect(next.firstCall.args[0].status).to.equal(401); }); - it("should reject if settingsService.getSettings fails", async function() { + it("should reject if settingsService.getSettings fails", async function () { req.settingsService.getSettings.rejects( new Error("settingsService.getSettings error") ); @@ -381,7 +384,7 @@ describe("Auth Controller - refreshAuthToken", function() { expect(next.firstCall.args[0].message).to.equal("settingsService.getSettings error"); }); - it("should generate a new auth token if the refresh token is valid", async function() { + it("should generate a new auth token if the refresh token is valid", async function () { const decodedPayload = { expiresIn: "60" }; jwt.verify.callsFake(() => { return decodedPayload; @@ -392,7 +395,7 @@ describe("Auth Controller - refreshAuthToken", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_TOKEN_REFRESHED, + msg: successMessages.AUTH_TOKEN_REFRESHED(mockLanguage), data: { user: decodedPayload, token: sinon.match.string, @@ -403,10 +406,10 @@ describe("Auth Controller - refreshAuthToken", function() { }); }); -describe("Auth Controller - editUser", function() { +describe("Auth Controller - editUser", function () { let req, res, next, stub, user; - beforeEach(function() { + beforeEach(function () { req = { params: { userId: "123" }, body: { password: "Password1!", newPassword: "Password2!" }, @@ -428,40 +431,40 @@ describe("Auth Controller - editUser", function() { stub = sinon.stub(jwt, "verify").returns({ email: "test@example.com" }); }); - afterEach(function() { + afterEach(function () { sinon.restore(); stub.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await editUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if body validation fails", async function() { + it("should reject with an error if body validation fails", async function () { req.body = { invalid: 1 }; await editUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if param.userId !== req.user._id", async function() { + it("should reject with an error if param.userId !== req.user._id", async function () { req.params = { userId: "456" }; await editUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(401); }); - it("should reject with an error if !req.body.password and getUserByEmail fails", async function() { + it("should reject with an error if !req.body.password and getUserByEmail fails", async function () { req.db.getUserByEmail.rejects(new Error("getUserByEmail error")); await editUser(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("getUserByEmail error"); }); - it("should reject with an error if user.comparePassword fails", async function() { + it("should reject with an error if user.comparePassword fails", async function () { req.db.getUserByEmail.returns({ comparePassword: sinon.stub().rejects(new Error("Bad Password Match")), }); @@ -470,7 +473,7 @@ describe("Auth Controller - editUser", function() { expect(next.firstCall.args[0].message).to.equal("Bad Password Match"); }); - it("should reject with an error if user.comparePassword returns false", async function() { + it("should reject with an error if user.comparePassword returns false", async function () { req.db.getUserByEmail.returns({ comparePassword: sinon.stub().returns(false), }); @@ -478,11 +481,11 @@ describe("Auth Controller - editUser", function() { expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(401); expect(next.firstCall.args[0].message).to.equal( - errorMessages.AUTH_INCORRECT_PASSWORD + errorMessages.AUTH_INCORRECT_PASSWORD(mockLanguage) ); }); - it("should edit a user if it receives a proper request", async function() { + it("should edit a user if it receives a proper request", async function () { const user = { comparePassword: sinon.stub().resolves(true), }; @@ -497,14 +500,14 @@ describe("Auth Controller - editUser", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_UPDATE_USER, + msg: successMessages.AUTH_UPDATE_USER(mockLanguage), data: { email: "test@example.com" }, }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should edit a user if it receives a proper request and both password fields are undefined", async function() { + it("should edit a user if it receives a proper request and both password fields are undefined", async function () { req.body.password = undefined; req.body.newPassword = undefined; req.db.getUserByEmail.resolves(user); @@ -515,14 +518,14 @@ describe("Auth Controller - editUser", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_UPDATE_USER, + msg: successMessages.AUTH_UPDATE_USER(mockLanguage), data: { email: "test@example.com" }, }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should reject an edit request if password format is incorrect", async function() { + it("should reject an edit request if password format is incorrect", async function () { req.body = { password: "bad_password", newPassword: "bad_password" }; const user = { comparePassword: sinon.stub().resolves(true), @@ -536,10 +539,10 @@ describe("Auth Controller - editUser", function() { }); }); -describe("Auth Controller - checkSuperadminExists", function() { +describe("Auth Controller - checkSuperadminExists", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { db: { checkSuperadmin: sinon.stub(), @@ -552,35 +555,35 @@ describe("Auth Controller - checkSuperadminExists", function() { next = sinon.stub(); }); - it("should reject with an error if checkSuperadmin fails", async function() { + it("should reject with an error if checkSuperadmin fails", async function () { req.db.checkSuperadmin.rejects(new Error("checkSuperadmin error")); await checkSuperadminExists(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("checkSuperadmin error"); }); - it("should return true if a superadmin exists", async function() { + it("should return true if a superadmin exists", async function () { req.db.checkSuperadmin.resolves(true); await checkSuperadminExists(req, res, next); expect(res.status.calledWith(200)).to.be.true; expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_SUPERADMIN_EXISTS, + msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage), data: true, }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should return false if a superadmin does not exist", async function() { + it("should return false if a superadmin does not exist", async function () { req.db.checkSuperadmin.resolves(false); await checkSuperadminExists(req, res, next); expect(res.status.calledWith(200)).to.be.true; expect( res.json.calledWith({ success: true, - msg: successMessages.AUTH_SUPERADMIN_EXISTS, + msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage), data: false, }) ).to.be.true; @@ -588,10 +591,10 @@ describe("Auth Controller - checkSuperadminExists", function() { }); }); -describe("Auth Controller - requestRecovery", function() { +describe("Auth Controller - requestRecovery", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: { email: "test@test.com" }, db: { @@ -612,21 +615,21 @@ describe("Auth Controller - requestRecovery", function() { next = sinon.stub(); }); - it("should reject with an error if validation fails", async function() { + it("should reject with an error if validation fails", async function () { req.body = {}; await requestRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if getUserByEmail fails", async function() { + it("should reject with an error if getUserByEmail fails", async function () { req.db.getUserByEmail.rejects(new Error("getUserByEmail error")); await requestRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("getUserByEmail error"); }); - it("should throw an error if the user is not found", async function() { + it("should throw an error if the user is not found", async function () { req.db.getUserByEmail.resolves(null); await requestRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); @@ -635,14 +638,14 @@ describe("Auth Controller - requestRecovery", function() { // ); }); - it("should throw an error if the email is not provided", async function() { + it("should throw an error if the email is not provided", async function () { req.body = {}; await requestRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should return a success message if the email is provided", async function() { + it("should return a success message if the email is provided", async function () { const user = { firstName: "John" }; const recoveryToken = { token: "recovery-token" }; const msgId = "message-id"; @@ -668,7 +671,7 @@ describe("Auth Controller - requestRecovery", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN, + msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(mockLanguage), data: msgId, }) ).to.be.true; @@ -676,10 +679,10 @@ describe("Auth Controller - requestRecovery", function() { }); }); -describe("Auth Controller - validateRecovery", function() { +describe("Auth Controller - validateRecovery", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: { recoveryToken: "recovery-token" }, db: { @@ -693,38 +696,38 @@ describe("Auth Controller - validateRecovery", function() { next = sinon.stub(); }); - it("should reject with an error if validation fails", async function() { + it("should reject with an error if validation fails", async function () { req.body = {}; await validateRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if validateRecoveryToken fails", async function() { + it("should reject with an error if validateRecoveryToken fails", async function () { req.db.validateRecoveryToken.rejects(new Error("validateRecoveryToken error")); await validateRecovery(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("validateRecoveryToken error"); }); - it("should return a success message if the token is valid", async function() { + it("should return a success message if the token is valid", async function () { req.db.validateRecoveryToken.resolves(); await validateRecovery(req, res, next); expect(res.status.calledOnceWith(200)).to.be.true; expect( res.json.calledOnceWith({ success: true, - msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN, + msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN(mockLanguage), }) ).to.be.true; expect(next.notCalled).to.be.true; }); }); -describe("Auth Controller - resetPassword", function() { +describe("Auth Controller - resetPassword", function () { let req, res, next, newPasswordValidation, handleValidationError, handleError; - beforeEach(function() { + beforeEach(function () { req = { body: { recoveryToken: "recovery-token", @@ -749,14 +752,14 @@ describe("Auth Controller - resetPassword", function() { handleError = sinon.stub(); }); - it("should reject with an error if validation fails", async function() { + it("should reject with an error if validation fails", async function () { req.body = { password: "bad_password" }; await resetPassword(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if resetPassword fails", async function() { + it("should reject with an error if resetPassword fails", async function () { const error = new Error("resetPassword error"); newPasswordValidation.validateAsync.resolves(); req.db.resetPassword.rejects(error); @@ -765,7 +768,7 @@ describe("Auth Controller - resetPassword", function() { expect(next.firstCall.args[0].message).to.equal("resetPassword error"); }); - it("should reset password successfully", async function() { + it("should reset password successfully", async function () { const user = { _doc: {} }; const appSettings = { jwtSecret: "my_secret" }; const token = "token"; @@ -782,7 +785,7 @@ describe("Auth Controller - resetPassword", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.AUTH_RESET_PASSWORD, + msg: successMessages.AUTH_RESET_PASSWORD(mockLanguage), data: { user: sinon.match.object, token: sinon.match.string }, }) ).to.be.true; @@ -790,10 +793,10 @@ describe("Auth Controller - resetPassword", function() { }); }); -describe("Auth Controller - deleteUser", function() { +describe("Auth Controller - deleteUser", function () { let req, res, next, handleError; - beforeEach(function() { + beforeEach(function () { req = { headers: { authorization: "Bearer token", @@ -825,24 +828,24 @@ describe("Auth Controller - deleteUser", function() { handleError = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should throw an error if user is not found", async function() { + it("should throw an error if user is not found", async function () { jwt.decode.returns({ email: "test@example.com" }); - req.db.getUserByEmail.throws(new Error(errorMessages.DB_USER_NOT_FOUND)); + req.db.getUserByEmail.throws(new Error(errorMessages.DB_USER_NOT_FOUND(req.language))); await deleteUser(req, res, next); expect(req.db.getUserByEmail.calledOnceWith("test@example.com")).to.be.true; expect(next.calledOnce).to.be.true; - expect(next.firstCall.args[0].message).to.equal(errorMessages.DB_USER_NOT_FOUND); + expect(next.firstCall.args[0].message).to.equal(errorMessages.DB_USER_NOT_FOUND(req.language)); expect(res.status.notCalled).to.be.true; expect(res.json.notCalled).to.be.true; }); - it("should delete user and associated data if user is superadmin", async function() { + it("should delete user and associated data if user is superadmin", async function () { const user = { _id: "user_id", email: "test@example.com", @@ -876,13 +879,13 @@ describe("Auth Controller - deleteUser", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.AUTH_DELETE_USER, + msg: successMessages.AUTH_DELETE_USER(mockLanguage), }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should delete user if user is not superadmin", async function() { + it("should delete user if user is not superadmin", async function () { const user = { _id: "user_id", email: "test@example.com", @@ -906,13 +909,13 @@ describe("Auth Controller - deleteUser", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.AUTH_DELETE_USER, + msg: successMessages.AUTH_DELETE_USER(mockLanguage), }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should handle errors", async function() { + it("should handle errors", async function () { const error = new Error("Something went wrong"); const SERVICE_NAME = "AuthController"; jwt.decode.returns({ email: "test@example.com" }); @@ -925,10 +928,10 @@ describe("Auth Controller - deleteUser", function() { }); }); -describe("Auth Controller - getAllUsers", function() { +describe("Auth Controller - getAllUsers", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { db: { getAllUsers: sinon.stub(), @@ -941,11 +944,11 @@ describe("Auth Controller - getAllUsers", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); // Restore the original methods after each test }); - it("should return 200 and all users", async function() { + it("should return 200 and all users", async function () { const allUsers = [{ id: 1, name: "John Doe" }]; req.db.getAllUsers.resolves(allUsers); @@ -963,7 +966,7 @@ describe("Auth Controller - getAllUsers", function() { expect(next.notCalled).to.be.true; }); - it("should call next with error when an exception occurs", async function() { + it("should call next with error when an exception occurs", async function () { const error = new Error("Something went wrong"); req.db.getAllUsers.rejects(error); await getAllUsers(req, res, next); diff --git a/Server/tests/controllers/checkController.test.js b/Server/tests/controllers/checkController.test.js index 0c00b9487..67207bd9b 100644 --- a/Server/tests/controllers/checkController.test.js +++ b/Server/tests/controllers/checkController.test.js @@ -9,11 +9,12 @@ import { import jwt from "jsonwebtoken"; import { errorMessages, successMessages } from "../../utils/messages.js"; import sinon from "sinon"; -describe("Check Controller - createCheck", function() { +describe("Check Controller - createCheck", function () { let req, res, next, handleError; - beforeEach(function() { + beforeEach(function () { req = { + language: 'en', params: {}, body: {}, db: { @@ -28,17 +29,17 @@ describe("Check Controller - createCheck", function() { handleError = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); // Restore the original methods after each test }); - it("should reject with a validation if params are invalid", async function() { + it("should reject with a validation if params are invalid", async function () { await createCheck(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with a validation error if body is invalid", async function() { + it("should reject with a validation error if body is invalid", async function () { req.params = { monitorId: "monitorId", }; @@ -47,7 +48,7 @@ describe("Check Controller - createCheck", function() { expect(next.firstCall.args[0].status).to.equal(422); }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.params = { monitorId: "monitorId", }; @@ -63,7 +64,7 @@ describe("Check Controller - createCheck", function() { expect(next.firstCall.args[0]).to.be.an("error"); }); - it("should return a success message if check is created", async function() { + it("should return a success message if check is created", async function () { req.params = { monitorId: "monitorId", }; @@ -80,7 +81,7 @@ describe("Check Controller - createCheck", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.CHECK_CREATE, + msg: successMessages.CHECK_CREATE(req.language), data: { id: "123" }, }) ).to.be.true; @@ -88,10 +89,10 @@ describe("Check Controller - createCheck", function() { }); }); -describe("Check Controller - getChecks", function() { +describe("Check Controller - getChecks", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, query: {}, @@ -107,17 +108,17 @@ describe("Check Controller - getChecks", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with a validation error if params are invalid", async function() { + it("should reject with a validation error if params are invalid", async function () { await getChecks(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should return a success message if checks are found", async function() { + it("should return a success message if checks are found", async function () { req.params = { monitorId: "monitorId", }; @@ -128,14 +129,14 @@ describe("Check Controller - getChecks", function() { expect( res.json.calledWith({ success: true, - msg: successMessages.CHECK_GET, + msg: successMessages.CHECK_GET(req.language), data: { checksCount: 1, checks: [{ id: "123" }] }, }) ).to.be.true; expect(next.notCalled).to.be.true; }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.params = { monitorId: "monitorId", }; @@ -145,10 +146,10 @@ describe("Check Controller - getChecks", function() { }); }); -describe("Check Controller - getTeamChecks", function() { +describe("Check Controller - getTeamChecks", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, query: {}, @@ -163,17 +164,17 @@ describe("Check Controller - getTeamChecks", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with a validation error if params are invalid", async function() { + it("should reject with a validation error if params are invalid", async function () { await getTeamChecks(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should return 200 and check data on successful validation and data retrieval", async function() { + it("should return 200 and check data on successful validation and data retrieval", async function () { req.params = { teamId: "1" }; const checkData = [{ id: 1, name: "Check 1" }]; req.db.getTeamChecks.resolves(checkData); @@ -184,13 +185,13 @@ describe("Check Controller - getTeamChecks", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.CHECK_GET, + msg: successMessages.CHECK_GET(req.language), data: checkData, }) ).to.be.true; }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.params = { teamId: "1" }; req.db.getTeamChecks.rejects(new Error("Retrieval Error")); await getTeamChecks(req, res, next); @@ -201,10 +202,10 @@ describe("Check Controller - getTeamChecks", function() { }); }); -describe("Check Controller - deleteChecks", function() { +describe("Check Controller - deleteChecks", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, db: { @@ -218,17 +219,17 @@ describe("Check Controller - deleteChecks", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { await deleteChecks(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.params = { monitorId: "1" }; req.db.deleteChecks.rejects(new Error("Deletion Error")); await deleteChecks(req, res, next); @@ -238,7 +239,7 @@ describe("Check Controller - deleteChecks", function() { expect(res.json.notCalled).to.be.true; }); - it("should delete checks successfully", async function() { + it("should delete checks successfully", async function () { req.params = { monitorId: "123" }; req.db.deleteChecks.resolves(1); await deleteChecks(req, res, next); @@ -247,17 +248,17 @@ describe("Check Controller - deleteChecks", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.CHECK_DELETE, + msg: successMessages.CHECK_DELETE(req.language), data: { deletedCount: 1 }, }) ).to.be.true; }); }); -describe("Check Controller - deleteChecksByTeamId", function() { +describe("Check Controller - deleteChecksByTeamId", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, db: { @@ -271,17 +272,17 @@ describe("Check Controller - deleteChecksByTeamId", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { await deleteChecksByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.params = { teamId: "1" }; req.db.deleteChecksByTeamId.rejects(new Error("Deletion Error")); await deleteChecksByTeamId(req, res, next); @@ -291,7 +292,7 @@ describe("Check Controller - deleteChecksByTeamId", function() { expect(res.json.notCalled).to.be.true; }); - it("should delete checks successfully", async function() { + it("should delete checks successfully", async function () { req.params = { teamId: "123" }; req.db.deleteChecksByTeamId.resolves(1); await deleteChecksByTeamId(req, res, next); @@ -300,17 +301,17 @@ describe("Check Controller - deleteChecksByTeamId", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.CHECK_DELETE, + msg: successMessages.CHECK_DELETE(req.language), data: { deletedCount: 1 }, }) ).to.be.true; }); }); -describe("Check Controller - updateCheckTTL", function() { +describe("Check Controller - updateCheckTTL", function () { let stub, req, res, next; - beforeEach(function() { + beforeEach(function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -332,18 +333,18 @@ describe("Check Controller - updateCheckTTL", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); stub.restore(); }); - it("should reject if body validation fails", async function() { + it("should reject if body validation fails", async function () { await updateChecksTTL(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should throw a JwtError if verification fails", async function() { + it("should throw a JwtError if verification fails", async function () { stub.restore(); req.body = { ttl: 1, @@ -352,7 +353,7 @@ describe("Check Controller - updateCheckTTL", function() { expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError); }); - it("should call next with error if data retrieval fails", async function() { + it("should call next with error if data retrieval fails", async function () { req.body = { ttl: 1, }; @@ -361,7 +362,7 @@ describe("Check Controller - updateCheckTTL", function() { expect(next.firstCall.args[0]).to.be.an("error"); }); - it("should update TTL successfully", async function() { + it("should update TTL successfully", async function () { req.body = { ttl: 1, }; @@ -372,7 +373,7 @@ describe("Check Controller - updateCheckTTL", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.CHECK_UPDATE_TTL, + msg: successMessages.CHECK_UPDATE_TTL(req.language), }) ).to.be.true; }); diff --git a/Server/tests/controllers/maintenanceWindowController.test.js b/Server/tests/controllers/maintenanceWindowController.test.js index 974bc07be..c4b03b23b 100644 --- a/Server/tests/controllers/maintenanceWindowController.test.js +++ b/Server/tests/controllers/maintenanceWindowController.test.js @@ -11,11 +11,12 @@ import jwt from "jsonwebtoken"; import { successMessages } from "../../utils/messages.js"; import sinon from "sinon"; -describe("maintenanceWindowController - createMaintenanceWindows", function() { +describe("maintenanceWindowController - createMaintenanceWindows", function () { let req, res, next, stub; - beforeEach(function() { + beforeEach(function () { req = { + language: 'en', body: { monitors: ["66ff52e7c5911c61698ac724"], name: "window", @@ -41,11 +42,11 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if body validation fails", async function() { + it("should reject with an error if body validation fails", async function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -56,14 +57,14 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() { stub.restore(); }); - it("should reject with an error if jwt.verify fails", async function() { + it("should reject with an error if jwt.verify fails", async function () { stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError()); await createMaintenanceWindows(req, res, next); expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError); stub.restore(); }); - it("should reject with an error DB operations fail", async function() { + it("should reject with an error DB operations fail", async function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -74,7 +75,7 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() { stub.restore(); }); - it("should return success message if all operations are successful", async function() { + it("should return success message if all operations are successful", async function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -83,13 +84,13 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_CREATE, + msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language), }) ).to.be.true; stub.restore(); }); - it("should return success message if all operations are successful with active set to undefined", async function() { + it("should return success message if all operations are successful with active set to undefined", async function () { req.body.active = undefined; stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; @@ -99,17 +100,17 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_CREATE, + msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language), }) ).to.be.true; stub.restore(); }); }); -describe("maintenanceWindowController - getMaintenanceWindowById", function() { +describe("maintenanceWindowController - getMaintenanceWindowById", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: {}, params: { @@ -121,6 +122,7 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() { settingsService: { getSettings: sinon.stub().returns({ jwtSecret: "jwtSecret" }), }, + language: 'en', db: { getMaintenanceWindowById: sinon.stub(), }, @@ -132,38 +134,38 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() { next = sinon.stub(); }); - it("should reject if param validation fails", async function() { + it("should reject if param validation fails", async function () { req.params = {}; await getMaintenanceWindowById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject if DB operations fail", async function() { + it("should reject if DB operations fail", async function () { req.db.getMaintenanceWindowById.throws(new Error("DB error")); await getMaintenanceWindowById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message with data if all operations are successful", async function() { + it("should return success message with data if all operations are successful", async function () { req.db.getMaintenanceWindowById.returns({ id: "123" }); await getMaintenanceWindowById(req, res, next); expect(res.status.firstCall.args[0]).to.equal(200); expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language), data: { id: "123" }, }) ).to.be.true; }); }); -describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function() { +describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function () { let req, res, next, stub; - beforeEach(function() { + beforeEach(function () { req = { body: {}, params: {}, @@ -177,6 +179,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function db: { getMaintenanceWindowsByTeamId: sinon.stub(), }, + language: 'en', }; res = { status: sinon.stub().returnsThis(), @@ -185,7 +188,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function next = sinon.stub(); }); - it("should reject if query validation fails", async function() { + it("should reject if query validation fails", async function () { req.query = { invalid: 1, }; @@ -194,14 +197,14 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject if jwt.verify fails", async function() { + it("should reject if jwt.verify fails", async function () { stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError()); await getMaintenanceWindowsByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError); stub.restore(); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -212,7 +215,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function stub.restore(); }); - it("should return success message with data if all operations are successful", async function() { + it("should return success message with data if all operations are successful", async function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -222,7 +225,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language), data: [{ id: jwt.verify().teamId }], }) ).to.be.true; @@ -230,10 +233,10 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function }); }); -describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function() { +describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: {}, params: { @@ -257,25 +260,25 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject if param validation fails", async function() { + it("should reject if param validation fails", async function () { req.params = {}; await getMaintenanceWindowsByMonitorId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getMaintenanceWindowsByMonitorId.throws(new Error("DB error")); await getMaintenanceWindowsByMonitorId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message with data if all operations are successful", async function() { + it("should return success message with data if all operations are successful", async function () { const data = [{ monitorId: "123" }]; req.db.getMaintenanceWindowsByMonitorId.returns(data); await getMaintenanceWindowsByMonitorId(req, res, next); @@ -284,17 +287,17 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR, + msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR(req.language), data: data, }) ).to.be.true; }); }); -describe("maintenanceWindowController - deleteMaintenanceWindow", function() { +describe("maintenanceWindowController - deleteMaintenanceWindow", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: {}, params: { @@ -318,46 +321,47 @@ describe("maintenanceWindowController - deleteMaintenanceWindow", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject if param validation fails", async function() { + it("should reject if param validation fails", async function () { req.params = {}; await deleteMaintenanceWindow(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.deleteMaintenanceWindowById.throws(new Error("DB error")); await deleteMaintenanceWindow(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message if all operations are successful", async function() { + it("should return success message if all operations are successful", async function () { await deleteMaintenanceWindow(req, res, next); expect(req.db.deleteMaintenanceWindowById.calledOnceWith(req.params.id)); expect(res.status.firstCall.args[0]).to.equal(200); expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_DELETE, + msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language), }) ).to.be.true; }); }); -describe("maintenanceWindowController - editMaintenanceWindow", function() { +describe("maintenanceWindowController - editMaintenanceWindow", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { body: { active: true, name: "test", }, + language: 'en', params: { id: "123", }, @@ -379,32 +383,32 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject if param validation fails", async function() { + it("should reject if param validation fails", async function () { req.params = {}; await editMaintenanceWindow(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject if body validation fails", async function() { + it("should reject if body validation fails", async function () { req.body = { invalid: 1 }; await editMaintenanceWindow(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.editMaintenanceWindowById.throws(new Error("DB error")); await editMaintenanceWindow(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message with data if all operations are successful", async function() { + it("should return success message with data if all operations are successful", async function () { const data = { id: "123" }; req.db.editMaintenanceWindowById.returns(data); @@ -414,7 +418,7 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MAINTENANCE_WINDOW_EDIT, + msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language), data: data, }) ).to.be.true; diff --git a/Server/tests/controllers/monitorController.test.js b/Server/tests/controllers/monitorController.test.js index 33440d897..ea35abac4 100644 --- a/Server/tests/controllers/monitorController.test.js +++ b/Server/tests/controllers/monitorController.test.js @@ -19,16 +19,16 @@ import sinon from "sinon"; import { successMessages } from "../../utils/messages.js"; import logger from "../../utils/logger.js"; import axios from "axios"; -const SERVICE_NAME = "monitorController"; -describe("Monitor Controller - getAllMonitors", function() { +describe("Monitor Controller - getAllMonitors", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, query: {}, body: {}, + language: 'en', db: { getAllMonitors: sinon.stub(), }, @@ -40,18 +40,18 @@ describe("Monitor Controller - getAllMonitors", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getAllMonitors.throws(new Error("DB error")); await getAllMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = [{ monitor: "data" }]; req.db.getAllMonitors.returns(data); await getAllMonitors(req, res, next); @@ -59,20 +59,21 @@ describe("Monitor Controller - getAllMonitors", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_GET_ALL, + msg: successMessages.MONITOR_GET_ALL(req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - getAllMonitorsWithUptimeStats", function() { +describe("Monitor Controller - getAllMonitorsWithUptimeStats", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, query: {}, body: {}, + language: 'en', db: { getAllMonitorsWithUptimeStats: sinon.stub(), }, @@ -84,18 +85,18 @@ describe("Monitor Controller - getAllMonitorsWithUptimeStats", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getAllMonitorsWithUptimeStats.throws(new Error("DB error")); await getAllMonitorsWithUptimeStats(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = [{ monitor: "data" }]; req.db.getAllMonitorsWithUptimeStats.returns(data); await getAllMonitorsWithUptimeStats(req, res, next); @@ -103,23 +104,24 @@ describe("Monitor Controller - getAllMonitorsWithUptimeStats", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_GET_ALL, + msg: successMessages.MONITOR_GET_ALL(req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - getMonitorStatsById", function() { +describe("Monitor Controller - getMonitorStatsById", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: { monitorId: "123", }, query: {}, body: {}, + language: 'en', db: { getMonitorStatsById: sinon.stub(), }, @@ -131,32 +133,32 @@ describe("Monitor Controller - getMonitorStatsById", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await getMonitorStatsById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if query validation fails", async function() { + it("should reject with an error if query validation fails", async function () { req.query = { invalid: 1 }; await getMonitorStatsById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getMonitorStatsById.throws(new Error("DB error")); await getMonitorStatsById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = [{ monitorStats: "data" }]; req.db.getMonitorStatsById.returns(data); await getMonitorStatsById(req, res, next); @@ -164,23 +166,24 @@ describe("Monitor Controller - getMonitorStatsById", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_STATS_BY_ID, + msg: successMessages.MONITOR_STATS_BY_ID(req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - getMonitorCertificate", function() { +describe("Monitor Controller - getMonitorCertificate", function () { let req, res, next, fetchMonitorCertificate; - beforeEach(function() { + beforeEach(function () { req = { params: { monitorId: "123", }, query: {}, body: {}, + language: 'en', db: { getMonitorById: sinon.stub(), }, @@ -193,25 +196,25 @@ describe("Monitor Controller - getMonitorCertificate", function() { fetchMonitorCertificate = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await getMonitorCertificate(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if getMonitorById operation fails", async function() { + it("should reject with an error if getMonitorById operation fails", async function () { req.db.getMonitorById.throws(new Error("DB error")); await getMonitorCertificate(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed with a valid cert", async function() { + it("should return success message and data if all operations succeed with a valid cert", async function () { req.db.getMonitorById.returns({ url: "https://www.google.com" }); const data = { certificate: "cert", validTo: "2024/08/08" }; fetchMonitorCertificate.returns(data); @@ -220,13 +223,13 @@ describe("Monitor Controller - getMonitorCertificate", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_CERTIFICATE, + msg: successMessages.MONITOR_CERTIFICATE(req.language), data: { certificateDate: new Date(data.validTo) }, }) ).to.be.true; }); - it("should return an error if fetchMonitorCertificate fails", async function() { + it("should return an error if fetchMonitorCertificate fails", async function () { req.db.getMonitorById.returns({ url: "https://www.google.com" }); fetchMonitorCertificate.throws(new Error("Certificate error")); await getMonitorCertificate(req, res, next, fetchMonitorCertificate); @@ -235,16 +238,17 @@ describe("Monitor Controller - getMonitorCertificate", function() { }); }); -describe("Monitor Controller - getMonitorById", function() { +describe("Monitor Controller - getMonitorById", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: { monitorId: "123", }, query: {}, body: {}, + language: 'en', db: { getMonitorById: sinon.stub(), }, @@ -256,32 +260,32 @@ describe("Monitor Controller - getMonitorById", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await getMonitorById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if query param validation fails", async function() { + it("should reject with an error if query param validation fails", async function () { req.query = { invalid: 1 }; await getMonitorById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getMonitorById.throws(new Error("DB error")); await getMonitorById(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return 404 if a monitor is not found", async function() { + it("should return 404 if a monitor is not found", async function () { const error = new Error("Monitor not found"); error.status = 404; req.db.getMonitorById.throws(error); @@ -290,7 +294,7 @@ describe("Monitor Controller - getMonitorById", function() { expect(next.firstCall.args[0].status).to.equal(404); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = { monitor: "data" }; req.db.getMonitorById.returns(data); await getMonitorById(req, res, next); @@ -298,23 +302,24 @@ describe("Monitor Controller - getMonitorById", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_GET_BY_ID, + msg: successMessages.MONITOR_GET_BY_ID(req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - getMonitorsAndSummaryByTeamId", function() { +describe("Monitor Controller - getMonitorsAndSummaryByTeamId", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: { teamId: "123", }, query: {}, body: {}, + language: 'en', db: { getMonitorsAndSummaryByTeamId: sinon.stub(), }, @@ -326,32 +331,32 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await getMonitorsAndSummaryByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if query validation fails", async function() { + it("should reject with an error if query validation fails", async function () { req.query = { invalid: 1 }; await getMonitorsAndSummaryByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getMonitorsAndSummaryByTeamId.throws(new Error("DB error")); await getMonitorsAndSummaryByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = { monitors: "data", summary: "data" }; req.db.getMonitorsAndSummaryByTeamId.returns(data); await getMonitorsAndSummaryByTeamId(req, res, next); @@ -359,23 +364,24 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.teamId), + msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.teamId, req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - getMonitorsByTeamId", function() { +describe("Monitor Controller - getMonitorsByTeamId", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: { teamId: "123", }, query: {}, body: {}, + language: 'en', db: { getMonitorsByTeamId: sinon.stub(), }, @@ -387,32 +393,32 @@ describe("Monitor Controller - getMonitorsByTeamId", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await getMonitorsByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if query validation fails", async function() { + it("should reject with an error if query validation fails", async function () { req.query = { invalid: 1 }; await getMonitorsByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB operations fail", async function() { + it("should reject with an error if DB operations fail", async function () { req.db.getMonitorsByTeamId.throws(new Error("DB error")); await getMonitorsByTeamId(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const data = { monitors: "data" }; req.db.getMonitorsByTeamId.returns(data); await getMonitorsByTeamId(req, res, next); @@ -420,17 +426,17 @@ describe("Monitor Controller - getMonitorsByTeamId", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.teamId), + msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.teamId, req.language), data: data, }) ).to.be.true; }); }); -describe("Monitor Controller - createMonitor", function() { +describe("Monitor Controller - createMonitor", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: {}, query: {}, @@ -443,6 +449,7 @@ describe("Monitor Controller - createMonitor", function() { url: "https://example.com", notifications: [{ email: "example@example.com" }], }, + language: 'en', db: { createMonitor: sinon.stub(), createNotification: sinon.stub(), @@ -458,25 +465,25 @@ describe("Monitor Controller - createMonitor", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if body validation fails", async function() { + it("should reject with an error if body validation fails", async function () { req.body = {}; await createMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB createMonitor operation fail", async function() { + it("should reject with an error if DB createMonitor operation fail", async function () { req.db.createMonitor.throws(new Error("DB error")); await createMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if DB createNotification operation fail", async function() { + it("should reject with an error if DB createNotification operation fail", async function () { req.db.createNotification.throws(new Error("DB error")); req.db.createMonitor.returns({ _id: "123" }); await createMonitor(req, res, next); @@ -484,7 +491,7 @@ describe("Monitor Controller - createMonitor", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if monitor.save operation fail", async function() { + it("should reject with an error if monitor.save operation fail", async function () { req.db.createMonitor.returns({ _id: "123", save: sinon.stub().throws(new Error("Monitor save error")), @@ -494,7 +501,7 @@ describe("Monitor Controller - createMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Monitor save error"); }); - it("should throw an error if addJob operation fails", async function() { + it("should throw an error if addJob operation fails", async function () { req.db.createMonitor.returns({ _id: "123", save: sinon.stub() }); req.jobQueue.addJob.throws(new Error("Job error")); await createMonitor(req, res, next); @@ -502,7 +509,7 @@ describe("Monitor Controller - createMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Job error"); }); - it("should return success message and data if all operations succeed", async function() { + it("should return success message and data if all operations succeed", async function () { const monitor = { _id: "123", save: sinon.stub() }; req.db.createMonitor.returns(monitor); await createMonitor(req, res, next); @@ -510,28 +517,28 @@ describe("Monitor Controller - createMonitor", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_CREATE, + msg: successMessages.MONITOR_CREATE(req.language), data: monitor, }) ).to.be.true; }); }); -describe("Monitor Controller - checkEndpointResolution", function() { +describe("Monitor Controller - checkEndpointResolution", function () { let req, res, next, axiosGetStub; - beforeEach(function() { + beforeEach(function () { req = { query: { monitorURL: "https://example.com" } }; res = { status: sinon.stub().returnsThis(), json: sinon.stub() }; next = sinon.stub(); axiosGetStub = sinon.stub(axios, "get"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should resolve the URL successfully", async function() { + it("should resolve the URL successfully", async function () { axiosGetStub.resolves({ status: 200, statusText: "OK" }); await checkEndpointResolution(req, res, next); expect(res.status.calledWith(200)).to.be.true; @@ -546,7 +553,7 @@ describe("Monitor Controller - checkEndpointResolution", function() { expect(next.called).to.be.false; }); - it("should return an error if endpoint resolution fails", async function() { + it("should return an error if endpoint resolution fails", async function () { const axiosError = new Error("resolution failed"); axiosError.code = "ENOTFOUND"; axiosGetStub.rejects(axiosError); @@ -559,7 +566,7 @@ describe("Monitor Controller - checkEndpointResolution", function() { expect(errorPassedToNext.status).to.equal(500); }); - it("should reject with an error if query validation fails", async function() { + it("should reject with an error if query validation fails", async function () { req.query.monitorURL = "invalid-url"; await checkEndpointResolution(req, res, next); expect(next.calledOnce).to.be.true; @@ -570,16 +577,17 @@ describe("Monitor Controller - checkEndpointResolution", function() { }); }); -describe("Monitor Controller - deleteMonitor", function() { +describe("Monitor Controller - deleteMonitor", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { params: { monitorId: "123", }, query: {}, body: {}, + language: 'en', db: { deleteMonitor: sinon.stub(), deleteChecks: sinon.stub(), @@ -598,25 +606,25 @@ describe("Monitor Controller - deleteMonitor", function() { sinon.stub(logger, "error"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await deleteMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if DB deleteMonitor operation fail", async function() { + it("should reject with an error if DB deleteMonitor operation fail", async function () { req.db.deleteMonitor.throws(new Error("DB error")); await deleteMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should log an error if deleteJob throws an error", async function() { + it("should log an error if deleteJob throws an error", async function () { const error = new Error("Job error"); const monitor = { name: "test_monitor", _id: "123" }; req.db.deleteMonitor.returns(monitor); @@ -628,7 +636,7 @@ describe("Monitor Controller - deleteMonitor", function() { ); }); - it("should log an error if deleteChecks throws an error", async function() { + it("should log an error if deleteChecks throws an error", async function () { const error = new Error("Checks error"); const monitor = { name: "test_monitor", _id: "123" }; req.db.deleteMonitor.returns(monitor); @@ -640,7 +648,7 @@ describe("Monitor Controller - deleteMonitor", function() { ); }); - it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function() { + it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function () { const error = new Error("PageSpeed error"); const monitor = { name: "test_monitor", _id: "123" }; req.db.deleteMonitor.returns(monitor); @@ -652,7 +660,7 @@ describe("Monitor Controller - deleteMonitor", function() { ); }); - it("should log an error if deleteNotificationsByMonitorId throws an error", async function() { + it("should log an error if deleteNotificationsByMonitorId throws an error", async function () { const error = new Error("Notifications error"); const monitor = { name: "test_monitor", _id: "123" }; req.db.deleteMonitor.returns(monitor); @@ -664,7 +672,7 @@ describe("Monitor Controller - deleteMonitor", function() { ); }); - it("should return success message if all operations succeed", async function() { + it("should return success message if all operations succeed", async function () { const monitor = { name: "test_monitor", _id: "123" }; req.db.deleteMonitor.returns(monitor); await deleteMonitor(req, res, next); @@ -672,16 +680,16 @@ describe("Monitor Controller - deleteMonitor", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_DELETE, + msg: successMessages.MONITOR_DELETE(req.language), }) ).to.be.true; }); }); -describe("Monitor Controller - deleteAllMonitors", function() { +describe("Monitor Controller - deleteAllMonitors", function () { let req, res, next, stub; - beforeEach(function() { + beforeEach(function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { teamId: "123" }; }); @@ -694,6 +702,7 @@ describe("Monitor Controller - deleteAllMonitors", function() { }, query: {}, body: {}, + language: 'en', db: { deleteAllMonitors: sinon.stub(), deleteChecks: sinon.stub(), @@ -715,12 +724,12 @@ describe("Monitor Controller - deleteAllMonitors", function() { sinon.stub(logger, "error"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); stub.restore(); }); - it("should reject with an error if getTokenFromHeaders throws an error", async function() { + it("should reject with an error if getTokenFromHeaders throws an error", async function () { req.headers = {}; await deleteAllMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); @@ -728,14 +737,14 @@ describe("Monitor Controller - deleteAllMonitors", function() { expect(next.firstCall.args[0].status).to.equal(500); }); - it("should reject with an error if token validation fails", async function() { + it("should reject with an error if token validation fails", async function () { stub.restore(); req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); await deleteAllMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError); }); - it("should reject with an error if DB deleteAllMonitors operation fail", async function() { + it("should reject with an error if DB deleteAllMonitors operation fail", async function () { req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.deleteAllMonitors.throws(new Error("DB error")); await deleteAllMonitors(req, res, next); @@ -743,7 +752,7 @@ describe("Monitor Controller - deleteAllMonitors", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should log an error if deleteChecks throws an error", async function() { + it("should log an error if deleteChecks throws an error", async function () { const monitors = [{ name: "test_monitor", _id: "123" }]; req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 }); @@ -756,7 +765,7 @@ describe("Monitor Controller - deleteAllMonitors", function() { ); }); - it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function() { + it("should log an error if deletePageSpeedChecksByMonitorId throws an error", async function () { const monitors = [{ name: "test_monitor", _id: "123" }]; req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 }); @@ -769,7 +778,7 @@ describe("Monitor Controller - deleteAllMonitors", function() { ); }); - it("should log an error if deleteNotificationsByMonitorId throws an error", async function() { + it("should log an error if deleteNotificationsByMonitorId throws an error", async function () { const monitors = [{ name: "test_monitor", _id: "123" }]; req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.deleteAllMonitors.returns({ monitors, deletedCount: 1 }); @@ -782,27 +791,25 @@ describe("Monitor Controller - deleteAllMonitors", function() { ); }); - it("should return success message if all operations succeed", async function() { + it("should return success message if all operations succeed", async function () { req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); - req.db.deleteAllMonitors.returns({ - monitors: [{ name: "test_monitor", _id: "123" }], - deletedCount: 1, - }); + const { monitors, deletedCount } = { monitors: [{ name: "test_monitor", _id: "123" }], deletedCount: 1 }; + req.db.deleteAllMonitors.returns({ monitors, deletedCount }); await deleteAllMonitors(req, res, next); expect(res.status.firstCall.args[0]).to.equal(200); expect( res.json.calledOnceWith({ success: true, - msg: "Deleted 1 monitors", + msg: `Deleted ${deletedCount} monitors`, }) ).to.be.true; }); }); -describe("Monitor Controller - editMonitor", function() { +describe("Monitor Controller - editMonitor", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { headers: {}, params: { @@ -812,6 +819,7 @@ describe("Monitor Controller - editMonitor", function() { body: { notifications: [{ email: "example@example.com" }], }, + language: 'en', db: { getMonitorById: sinon.stub(), editMonitor: sinon.stub(), @@ -833,32 +841,32 @@ describe("Monitor Controller - editMonitor", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await editMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if body validation fails", async function() { + it("should reject with an error if body validation fails", async function () { req.body = { invalid: 1 }; await editMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if getMonitorById operation fails", async function() { + it("should reject with an error if getMonitorById operation fails", async function () { req.db.getMonitorById.throws(new Error("DB error")); await editMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if editMonitor operation fails", async function() { + it("should reject with an error if editMonitor operation fails", async function () { req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.throws(new Error("DB error")); await editMonitor(req, res, next); @@ -866,7 +874,7 @@ describe("Monitor Controller - editMonitor", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if deleteNotificationsByMonitorId operation fails", async function() { + it("should reject with an error if deleteNotificationsByMonitorId operation fails", async function () { req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.returns({ _id: "123" }); req.db.deleteNotificationsByMonitorId.throws(new Error("DB error")); @@ -875,7 +883,7 @@ describe("Monitor Controller - editMonitor", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if createNotification operation fails", async function() { + it("should reject with an error if createNotification operation fails", async function () { req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.returns({ _id: "123" }); req.db.createNotification.throws(new Error("DB error")); @@ -884,7 +892,7 @@ describe("Monitor Controller - editMonitor", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if deleteJob operation fails", async function() { + it("should reject with an error if deleteJob operation fails", async function () { req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.returns({ _id: "123" }); req.jobQueue.deleteJob.throws(new Error("Job error")); @@ -893,7 +901,7 @@ describe("Monitor Controller - editMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Job error"); }); - it("should reject with an error if addJob operation fails", async function() { + it("should reject with an error if addJob operation fails", async function () { req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.returns({ _id: "123" }); req.jobQueue.addJob.throws(new Error("Add Job error")); @@ -902,7 +910,7 @@ describe("Monitor Controller - editMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Add Job error"); }); - it("should return success message with data if all operations succeed", async function() { + it("should return success message with data if all operations succeed", async function () { const monitor = { _id: "123" }; req.db.getMonitorById.returns({ teamId: "123" }); req.db.editMonitor.returns(monitor); @@ -911,17 +919,17 @@ describe("Monitor Controller - editMonitor", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_EDIT, + msg: successMessages.MONITOR_EDIT(req.language), data: monitor, }) ).to.be.true; }); }); -describe("Monitor Controller - pauseMonitor", function() { +describe("Monitor Controller - pauseMonitor", function () { let req, res, next; - beforeEach(function() { + beforeEach(function () { req = { headers: {}, params: { @@ -929,6 +937,7 @@ describe("Monitor Controller - pauseMonitor", function() { }, query: {}, body: {}, + language: 'en', db: { getMonitorById: sinon.stub(), }, @@ -947,25 +956,25 @@ describe("Monitor Controller - pauseMonitor", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should reject with an error if param validation fails", async function() { + it("should reject with an error if param validation fails", async function () { req.params = {}; await pauseMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].status).to.equal(422); }); - it("should reject with an error if getMonitorById operation fails", async function() { + it("should reject with an error if getMonitorById operation fails", async function () { req.db.getMonitorById.throws(new Error("DB error")); await pauseMonitor(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if deleteJob operation fails", async function() { + it("should reject with an error if deleteJob operation fails", async function () { const monitor = { _id: req.params.monitorId, isActive: true }; req.db.getMonitorById.returns(monitor); req.jobQueue.deleteJob.throws(new Error("Delete Job error")); @@ -974,7 +983,7 @@ describe("Monitor Controller - pauseMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Delete Job error"); }); - it("should reject with an error if addJob operation fails", async function() { + it("should reject with an error if addJob operation fails", async function () { const monitor = { _id: req.params.monitorId, isActive: false }; req.db.getMonitorById.returns(monitor); req.jobQueue.addJob.throws(new Error("Add Job error")); @@ -983,7 +992,7 @@ describe("Monitor Controller - pauseMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Add Job error"); }); - it("should reject with an error if monitor.save operation fails", async function() { + it("should reject with an error if monitor.save operation fails", async function () { const monitor = { _id: req.params.monitorId, active: false, @@ -995,7 +1004,7 @@ describe("Monitor Controller - pauseMonitor", function() { expect(next.firstCall.args[0].message).to.equal("Save error"); }); - it("should return success pause message with data if all operations succeed with inactive monitor", async function() { + it("should return success pause message with data if all operations succeed with inactive monitor", async function () { const monitor = { _id: req.params.monitorId, isActive: false, @@ -1007,13 +1016,13 @@ describe("Monitor Controller - pauseMonitor", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_PAUSE, + msg: successMessages.MONITOR_PAUSE(req.language), data: monitor, }) ).to.be.true; }); - it("should return success resume message with data if all operations succeed with active monitor", async function() { + it("should return success resume message with data if all operations succeed with active monitor", async function () { const monitor = { _id: req.params.monitorId, isActive: true, @@ -1025,17 +1034,17 @@ describe("Monitor Controller - pauseMonitor", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_RESUME, + msg: successMessages.MONITOR_RESUME(req.language), data: monitor, }) ).to.be.true; }); }); -describe("Monitor Controller - addDemoMonitors", function() { +describe("Monitor Controller - addDemoMonitors", function () { let req, res, next, stub; - beforeEach(function() { + beforeEach(function () { stub = sinon.stub(jwt, "verify").callsFake(() => { return { _id: "123", teamId: "123" }; }); @@ -1046,6 +1055,7 @@ describe("Monitor Controller - addDemoMonitors", function() { params: {}, query: {}, body: {}, + language: 'en', db: { addDemoMonitors: sinon.stub(), }, @@ -1063,12 +1073,12 @@ describe("Monitor Controller - addDemoMonitors", function() { next = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); stub.restore(); }); - it("should reject with an error if getTokenFromHeaders fails", async function() { + it("should reject with an error if getTokenFromHeaders fails", async function () { req.headers = {}; await addDemoMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); @@ -1076,21 +1086,21 @@ describe("Monitor Controller - addDemoMonitors", function() { expect(next.firstCall.args[0].status).to.equal(500); }); - it("should reject with an error if getting settings fails", async function() { + it("should reject with an error if getting settings fails", async function () { req.settingsService.getSettings.throws(new Error("Settings error")); await addDemoMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); expect(next.firstCall.args[0].message).to.equal("Settings error"); }); - it("should reject with an error if JWT validation fails", async function() { + it("should reject with an error if JWT validation fails", async function () { stub.restore(); req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); await addDemoMonitors(req, res, next); expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError); }); - it("should reject with an error if addDemoMonitors operation fails", async function() { + it("should reject with an error if addDemoMonitors operation fails", async function () { req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.addDemoMonitors.throws(new Error("DB error")); await addDemoMonitors(req, res, next); @@ -1098,7 +1108,7 @@ describe("Monitor Controller - addDemoMonitors", function() { expect(next.firstCall.args[0].message).to.equal("DB error"); }); - it("should reject with an error if addJob operation fails", async function() { + it("should reject with an error if addJob operation fails", async function () { req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.addDemoMonitors.returns([{ _id: "123" }]); req.jobQueue.addJob.throws(new Error("Add Job error")); @@ -1107,7 +1117,7 @@ describe("Monitor Controller - addDemoMonitors", function() { expect(next.firstCall.args[0].message).to.equal("Add Job error"); }); - it("should return success message with data if all operations succeed", async function() { + it("should return success message with data if all operations succeed", async function () { const monitors = [{ _id: "123" }]; req.settingsService.getSettings.returns({ jwtSecret: "my_secret" }); req.db.addDemoMonitors.returns(monitors); @@ -1116,7 +1126,7 @@ describe("Monitor Controller - addDemoMonitors", function() { expect( res.json.calledOnceWith({ success: true, - msg: successMessages.MONITOR_DEMO_ADDED, + msg: successMessages.MONITOR_DEMO_ADDED(req.language), data: monitors.length, }) ).to.be.true; diff --git a/Server/tests/db/inviteModule.test.js b/Server/tests/db/inviteModule.test.js index 164840826..febaf6ab7 100644 --- a/Server/tests/db/inviteModule.test.js +++ b/Server/tests/db/inviteModule.test.js @@ -7,32 +7,33 @@ import { } from "../../db/mongo/modules/inviteModule.js"; import { errorMessages } from "../../utils/messages.js"; -describe("Invite Module", function() { +describe("Invite Module", function () { const mockUserData = { email: "test@test.com", teamId: "123", role: ["admin"], token: "123", }; + const mockLanguage = 'en'; const mockInviteToken = { _id: 123, time: 123 }; let inviteTokenDeleteManyStub, inviteTokenSaveStub, inviteTokenFindOneStub, inviteTokenFindOneAndDeleteStub; - beforeEach(function() { + beforeEach(function () { inviteTokenDeleteManyStub = sinon.stub(InviteToken, "deleteMany"); inviteTokenSaveStub = sinon.stub(InviteToken.prototype, "save"); inviteTokenFindOneStub = sinon.stub(InviteToken, "findOne"); inviteTokenFindOneAndDeleteStub = sinon.stub(InviteToken, "findOneAndDelete"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - describe("requestInviteToken", function() { - it("should return a new invite token", async function() { + describe("requestInviteToken", function () { + it("should return a new invite token", async function () { inviteTokenDeleteManyStub.resolves(); inviteTokenSaveStub.resolves(); const inviteToken = await requestInviteToken(mockUserData); @@ -41,7 +42,7 @@ describe("Invite Module", function() { expect(inviteToken.token).to.exist; }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); inviteTokenDeleteManyStub.rejects(err); try { @@ -52,23 +53,23 @@ describe("Invite Module", function() { }); }); - describe("getInviteToken", function() { - it("should return an invite token", async function() { + describe("getInviteToken", function () { + it("should return an invite token", async function () { inviteTokenFindOneStub.resolves(mockInviteToken); const inviteToken = await getInviteToken(mockUserData.token); expect(inviteToken).to.deep.equal(mockInviteToken); }); - it("should handle a token not found", async function() { + it("should handle a token not found", async function () { inviteTokenFindOneStub.resolves(null); try { await getInviteToken(mockUserData.token); } catch (error) { - expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND); + expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND(mockLanguage)); } }); - it("should handle DB errors", async function() { + it("should handle DB errors", async function () { const err = new Error("test error"); inviteTokenFindOneStub.rejects(err); try { @@ -80,23 +81,23 @@ describe("Invite Module", function() { }); }); - describe("getInviteTokenAndDelete", function() { - it("should return a deleted invite", async function() { + describe("getInviteTokenAndDelete", function () { + it("should return a deleted invite", async function () { inviteTokenFindOneAndDeleteStub.resolves(mockInviteToken); const deletedInvite = await getInviteTokenAndDelete(mockUserData.token); expect(deletedInvite).to.deep.equal(mockInviteToken); }); - it("should handle a token not found", async function() { + it("should handle a token not found", async function () { inviteTokenFindOneAndDeleteStub.resolves(null); try { await getInviteTokenAndDelete(mockUserData.token); } catch (error) { - expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND); + expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND(mockLanguage)); } }); - it("should handle DB errors", async function() { + it("should handle DB errors", async function () { const err = new Error("test error"); inviteTokenFindOneAndDeleteStub.rejects(err); try { diff --git a/Server/tests/db/monitorModule.test.js b/Server/tests/db/monitorModule.test.js index 50782494a..e60688f3f 100644 --- a/Server/tests/db/monitorModule.test.js +++ b/Server/tests/db/monitorModule.test.js @@ -31,7 +31,7 @@ import { calculateGroupStats, } from "../../db/mongo/modules/monitorModule.js"; -describe("monitorModule", function() { +describe("monitorModule", function () { let monitorFindStub, monitorFindByIdStub, monitorFindByIdAndUpdateStub, @@ -43,7 +43,7 @@ describe("monitorModule", function() { pageSpeedCheckFindStub, hardwareCheckFindStub; - beforeEach(function() { + beforeEach(function () { monitorFindStub = sinon.stub(Monitor, "find"); monitorFindByIdStub = sinon.stub(Monitor, "findById"); monitorFindByIdAndUpdateStub = sinon.stub(Monitor, "findByIdAndUpdate"); @@ -63,12 +63,12 @@ describe("monitorModule", function() { }); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - describe("getAllMonitors", function() { - it("should return all monitors", async function() { + describe("getAllMonitors", function () { + it("should return all monitors", async function () { const mockMonitors = [ { _id: "1", name: "Monitor 1", url: "test1.com" }, { _id: "2", name: "Monitor 2", url: "test2.com" }, @@ -81,13 +81,13 @@ describe("monitorModule", function() { expect(monitorFindStub.firstCall.args).to.deep.equal([]); }); - it("should handle empty results", async function() { + it("should handle empty results", async function () { monitorFindStub.returns([]); const result = await getAllMonitors(); expect(result).to.be.an("array").that.is.empty; }); - it("should throw error when database fails", async function() { + it("should throw error when database fails", async function () { // Arrange const error = new Error("Database error"); error.service = "MonitorModule"; @@ -105,8 +105,8 @@ describe("monitorModule", function() { }); }); - describe("getAllMonitorsWithUptimeStats", function() { - it("should return monitors with uptime stats for different time periods", async function() { + describe("getAllMonitorsWithUptimeStats", function () { + it("should return monitors with uptime stats for different time periods", async function () { // Mock data const mockMonitors = [ { @@ -152,7 +152,7 @@ describe("monitorModule", function() { expect(monitor["90"]).to.equal(75); }); - it("should return monitors with stats for pagespeed type", async function() { + it("should return monitors with stats for pagespeed type", async function () { // Mock data const mockMonitors = [ { @@ -198,7 +198,7 @@ describe("monitorModule", function() { expect(monitor["90"]).to.equal(75); }); - it("should return monitors with stats for hardware type", async function() { + it("should return monitors with stats for hardware type", async function () { // Mock data const mockMonitors = [ { @@ -244,7 +244,7 @@ describe("monitorModule", function() { expect(monitor["90"]).to.equal(75); }); - it("should handle errors appropriately", async function() { + it("should handle errors appropriately", async function () { // Setup stub to throw error monitorFindStub.rejects(new Error("Database error")); @@ -258,7 +258,7 @@ describe("monitorModule", function() { } }); - it("should handle empty monitor list", async function() { + it("should handle empty monitor list", async function () { monitorFindStub.resolves([]); const result = await getAllMonitorsWithUptimeStats(); @@ -267,7 +267,7 @@ describe("monitorModule", function() { expect(result).to.have.lengthOf(0); }); - it("should handle monitor with no checks", async function() { + it("should handle monitor with no checks", async function () { const mockMonitors = [ { _id: "monitor1", @@ -292,28 +292,28 @@ describe("monitorModule", function() { }); }); - describe("calculateUptimeDuration", function() { + describe("calculateUptimeDuration", function () { let clock; const NOW = new Date("2024-01-01T12:00:00Z").getTime(); - beforeEach(function() { + beforeEach(function () { // Fix the current time clock = sinon.useFakeTimers(NOW); }); - afterEach(function() { + afterEach(function () { clock.restore(); }); - it("should return 0 when checks array is empty", function() { + it("should return 0 when checks array is empty", function () { expect(calculateUptimeDuration([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(calculateUptimeDuration(null)).to.equal(0); }); - it("should calculate uptime from last down check to most recent check", function() { + it("should calculate uptime from last down check to most recent check", function () { const checks = [ { status: true, createdAt: "2024-01-01T11:00:00Z" }, // Most recent { status: true, createdAt: "2024-01-01T10:00:00Z" }, @@ -325,7 +325,7 @@ describe("monitorModule", function() { expect(calculateUptimeDuration(checks)).to.equal(7200000); }); - it("should calculate uptime from first check when no down checks exist", function() { + it("should calculate uptime from first check when no down checks exist", function () { const checks = [ { status: true, createdAt: "2024-01-01T11:00:00Z" }, { status: true, createdAt: "2024-01-01T10:00:00Z" }, @@ -337,28 +337,28 @@ describe("monitorModule", function() { }); }); - describe("getLastChecked", function() { + describe("getLastChecked", function () { let clock; const NOW = new Date("2024-01-01T12:00:00Z").getTime(); - beforeEach(function() { + beforeEach(function () { // Fix the current time clock = sinon.useFakeTimers(NOW); }); - afterEach(function() { + afterEach(function () { clock.restore(); }); - it("should return 0 when checks array is empty", function() { + it("should return 0 when checks array is empty", function () { expect(getLastChecked([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(getLastChecked(null)).to.equal(0); }); - it("should return time difference between now and most recent check", function() { + it("should return time difference between now and most recent check", function () { const checks = [ { createdAt: "2024-01-01T11:30:00Z" }, // 30 minutes ago { createdAt: "2024-01-01T11:00:00Z" }, @@ -369,7 +369,7 @@ describe("monitorModule", function() { expect(getLastChecked(checks)).to.equal(1800000); }); - it("should handle checks from different days", function() { + it("should handle checks from different days", function () { const checks = [ { createdAt: "2023-12-31T12:00:00Z" }, // 24 hours ago { createdAt: "2023-12-30T12:00:00Z" }, @@ -380,16 +380,16 @@ describe("monitorModule", function() { }); }); - describe("getLatestResponseTime", function() { - it("should return 0 when checks array is empty", function() { + describe("getLatestResponseTime", function () { + it("should return 0 when checks array is empty", function () { expect(getLatestResponseTime([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(getLatestResponseTime(null)).to.equal(0); }); - it("should return response time from most recent check", function() { + it("should return response time from most recent check", function () { const checks = [ { responseTime: 150, createdAt: "2024-01-01T11:30:00Z" }, // Most recent { responseTime: 200, createdAt: "2024-01-01T11:00:00Z" }, @@ -399,7 +399,7 @@ describe("monitorModule", function() { expect(getLatestResponseTime(checks)).to.equal(150); }); - it("should handle missing responseTime in checks", function() { + it("should handle missing responseTime in checks", function () { const checks = [ { createdAt: "2024-01-01T11:30:00Z" }, { responseTime: 200, createdAt: "2024-01-01T11:00:00Z" }, @@ -409,16 +409,16 @@ describe("monitorModule", function() { }); }); - describe("getAverageResponseTime", function() { - it("should return 0 when checks array is empty", function() { + describe("getAverageResponseTime", function () { + it("should return 0 when checks array is empty", function () { expect(getAverageResponseTime([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(getAverageResponseTime(null)).to.equal(0); }); - it("should calculate average response time from all checks", function() { + it("should calculate average response time from all checks", function () { const checks = [ { responseTime: 100, createdAt: "2024-01-01T11:30:00Z" }, { responseTime: 200, createdAt: "2024-01-01T11:00:00Z" }, @@ -429,7 +429,7 @@ describe("monitorModule", function() { expect(getAverageResponseTime(checks)).to.equal(200); }); - it("should handle missing responseTime in some checks", function() { + it("should handle missing responseTime in some checks", function () { const checks = [ { responseTime: 100, createdAt: "2024-01-01T11:30:00Z" }, { createdAt: "2024-01-01T11:00:00Z" }, @@ -440,7 +440,7 @@ describe("monitorModule", function() { expect(getAverageResponseTime(checks)).to.equal(200); }); - it("should return 0 when no checks have responseTime", function() { + it("should return 0 when no checks have responseTime", function () { const checks = [ { createdAt: "2024-01-01T11:30:00Z" }, { createdAt: "2024-01-01T11:00:00Z" }, @@ -450,26 +450,26 @@ describe("monitorModule", function() { }); }); - describe("getUptimePercentage", function() { - it("should return 0 when checks array is empty", function() { + describe("getUptimePercentage", function () { + it("should return 0 when checks array is empty", function () { expect(getUptimePercentage([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(getUptimePercentage(null)).to.equal(0); }); - it("should return 100 when all checks are up", function() { + it("should return 100 when all checks are up", function () { const checks = [{ status: true }, { status: true }, { status: true }]; expect(getUptimePercentage(checks)).to.equal(100); }); - it("should return 0 when all checks are down", function() { + it("should return 0 when all checks are down", function () { const checks = [{ status: false }, { status: false }, { status: false }]; expect(getUptimePercentage(checks)).to.equal(0); }); - it("should calculate correct percentage for mixed status checks", function() { + it("should calculate correct percentage for mixed status checks", function () { const checks = [ { status: true }, { status: false }, @@ -480,33 +480,33 @@ describe("monitorModule", function() { expect(getUptimePercentage(checks)).to.equal(75); }); - it("should handle undefined status values", function() { + it("should handle undefined status values", function () { const checks = [{ status: true }, { status: undefined }, { status: true }]; // 2 up out of 3 total ≈ 66.67% expect(getUptimePercentage(checks)).to.equal((2 / 3) * 100); }); }); - describe("getIncidents", function() { - it("should return 0 when checks array is empty", function() { + describe("getIncidents", function () { + it("should return 0 when checks array is empty", function () { expect(getIncidents([])).to.equal(0); }); - it("should return 0 when checks array is null", function() { + it("should return 0 when checks array is null", function () { expect(getIncidents(null)).to.equal(0); }); - it("should return 0 when all checks are up", function() { + it("should return 0 when all checks are up", function () { const checks = [{ status: true }, { status: true }, { status: true }]; expect(getIncidents(checks)).to.equal(0); }); - it("should count all incidents when all checks are down", function() { + it("should count all incidents when all checks are down", function () { const checks = [{ status: false }, { status: false }, { status: false }]; expect(getIncidents(checks)).to.equal(3); }); - it("should count correct number of incidents for mixed status checks", function() { + it("should count correct number of incidents for mixed status checks", function () { const checks = [ { status: true }, { status: false }, @@ -517,7 +517,7 @@ describe("monitorModule", function() { expect(getIncidents(checks)).to.equal(2); }); - it("should handle undefined status values", function() { + it("should handle undefined status values", function () { const checks = [ { status: true }, { status: undefined }, @@ -529,10 +529,10 @@ describe("monitorModule", function() { }); }); - describe("getMonitorChecks", function() { + describe("getMonitorChecks", function () { let mockModel; - beforeEach(function() { + beforeEach(function () { // Create a mock model with chainable methods const mockChecks = [ { monitorId: "123", createdAt: new Date("2024-01-01") }, @@ -546,11 +546,11 @@ describe("monitorModule", function() { }; }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should return all checks and date-ranged checks", async function() { + it("should return all checks and date-ranged checks", async function () { // Arrange const monitorId = "123"; const dateRange = { @@ -579,7 +579,7 @@ describe("monitorModule", function() { }); }); - it("should handle empty results", async function() { + it("should handle empty results", async function () { // Arrange const emptyModel = { find: sinon.stub().returns({ @@ -603,7 +603,7 @@ describe("monitorModule", function() { expect(result.checksForDateRange).to.be.an("array").that.is.empty; }); - it("should maintain sort order", async function() { + it("should maintain sort order", async function () { // Arrange const sortedChecks = [ { monitorId: "123", createdAt: new Date("2024-01-02") }, @@ -637,39 +637,39 @@ describe("monitorModule", function() { }); }); - describe("processChecksForDisplay", function() { + describe("processChecksForDisplay", function () { let normalizeStub; - beforeEach(function() { + beforeEach(function () { normalizeStub = sinon.stub(); }); - it("should return original checks when numToDisplay is not provided", function() { + it("should return original checks when numToDisplay is not provided", function () { const checks = [1, 2, 3, 4, 5]; const result = processChecksForDisplay(normalizeStub, checks); expect(result).to.deep.equal(checks); }); - it("should return original checks when numToDisplay is greater than checks length", function() { + it("should return original checks when numToDisplay is greater than checks length", function () { const checks = [1, 2, 3]; const result = processChecksForDisplay(normalizeStub, checks, 5); expect(result).to.deep.equal(checks); }); - it("should filter checks based on numToDisplay", function() { + it("should filter checks based on numToDisplay", function () { const checks = [1, 2, 3, 4, 5, 6]; const result = processChecksForDisplay(normalizeStub, checks, 3); // Should return [1, 3, 5] as n = ceil(6/3) = 2 expect(result).to.deep.equal([1, 3, 5]); }); - it("should handle empty checks array", function() { + it("should handle empty checks array", function () { const checks = []; const result = processChecksForDisplay(normalizeStub, checks, 3); expect(result).to.be.an("array").that.is.empty; }); - it("should call normalizeData when normalize is true", function() { + it("should call normalizeData when normalize is true", function () { const checks = [1, 2, 3]; normalizeStub.returns([10, 20, 30]); @@ -679,7 +679,7 @@ describe("monitorModule", function() { expect(result).to.deep.equal([10, 20, 30]); }); - it("should handle both filtering and normalization", function() { + it("should handle both filtering and normalization", function () { const checks = [1, 2, 3, 4, 5, 6]; normalizeStub.returns([10, 30, 50]); @@ -690,7 +690,7 @@ describe("monitorModule", function() { }); }); - describe("groupChecksByTime", function() { + describe("groupChecksByTime", function () { const mockChecks = [ { createdAt: "2024-01-15T10:30:45Z" }, { createdAt: "2024-01-15T10:45:15Z" }, @@ -698,7 +698,7 @@ describe("monitorModule", function() { { createdAt: "2024-01-16T10:30:00Z" }, ]; - it("should group checks by hour when dateRange is 'day'", function() { + it("should group checks by hour when dateRange is 'day'", function () { const result = groupChecksByTime(mockChecks, "day"); // Get timestamps for 10:00 and 11:00 on Jan 15 @@ -713,7 +713,7 @@ describe("monitorModule", function() { expect(result[time3].checks).to.have.lengthOf(1); }); - it("should group checks by day when dateRange is not 'day'", function() { + it("should group checks by day when dateRange is not 'day'", function () { const result = groupChecksByTime(mockChecks, "week"); expect(Object.keys(result)).to.have.lengthOf(2); @@ -721,12 +721,12 @@ describe("monitorModule", function() { expect(result["2024-01-16"].checks).to.have.lengthOf(1); }); - it("should handle empty checks array", function() { + it("should handle empty checks array", function () { const result = groupChecksByTime([], "day"); expect(result).to.deep.equal({}); }); - it("should handle single check", function() { + it("should handle single check", function () { const singleCheck = [{ createdAt: "2024-01-15T10:30:45Z" }]; const result = groupChecksByTime(singleCheck, "day"); @@ -735,7 +735,7 @@ describe("monitorModule", function() { expect(result[expectedTime].checks).to.have.lengthOf(1); }); - it("should skip invalid dates and process valid ones", function() { + it("should skip invalid dates and process valid ones", function () { const checksWithInvalidDate = [ { createdAt: "invalid-date" }, { createdAt: "2024-01-15T10:30:45Z" }, @@ -752,7 +752,7 @@ describe("monitorModule", function() { expect(result[expectedTime].checks[0].createdAt).to.equal("2024-01-15T10:30:45Z"); }); - it("should handle checks in same time group", function() { + it("should handle checks in same time group", function () { const checksInSameHour = [ { createdAt: "2024-01-15T10:15:00Z" }, { createdAt: "2024-01-15T10:45:00Z" }, @@ -766,16 +766,16 @@ describe("monitorModule", function() { }); }); - describe("calculateGroupStats", function() { + describe("calculateGroupStats", function () { // Mock getUptimePercentage function let uptimePercentageStub; - beforeEach(function() { + beforeEach(function () { uptimePercentageStub = sinon.stub(); uptimePercentageStub.returns(95); // Default return value }); - it("should calculate stats correctly for a group of checks", function() { + it("should calculate stats correctly for a group of checks", function () { const mockGroup = { time: "2024-01-15", checks: [ @@ -796,7 +796,7 @@ describe("monitorModule", function() { }); }); - it("should handle empty checks array", function() { + it("should handle empty checks array", function () { const mockGroup = { time: "2024-01-15", checks: [], @@ -813,7 +813,7 @@ describe("monitorModule", function() { }); }); - it("should handle missing responseTime values", function() { + it("should handle missing responseTime values", function () { const mockGroup = { time: "2024-01-15", checks: [ @@ -834,7 +834,7 @@ describe("monitorModule", function() { }); }); - it("should handle all checks with status false", function() { + it("should handle all checks with status false", function () { const mockGroup = { time: "2024-01-15", checks: [ @@ -855,7 +855,7 @@ describe("monitorModule", function() { }); }); - it("should handle all checks with status true", function() { + it("should handle all checks with status true", function () { const mockGroup = { time: "2024-01-15", checks: [ @@ -877,7 +877,7 @@ describe("monitorModule", function() { }); }); - describe("getMonitorStatsById", function() { + describe("getMonitorStatsById", function () { const now = new Date(); const oneHourAgo = new Date(now - 3600000); const twoHoursAgo = new Date(now - 7200000); @@ -974,18 +974,18 @@ describe("monitorModule", function() { }, }; - beforeEach(function() { + beforeEach(function () { checkFindStub.returns({ sort: () => checkDocs, }); monitorFindByIdStub.returns(mockMonitor); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should return monitor stats with calculated values, sort order desc", async function() { + it("should return monitor stats with calculated values, sort order desc", async function () { req.query.sortOrder = "desc"; const result = await getMonitorStatsById(req); expect(result).to.include.keys([ @@ -1009,7 +1009,7 @@ describe("monitorModule", function() { expect(result.aggregateData).to.be.an("array"); }); - it("should return monitor stats with calculated values, ping type", async function() { + it("should return monitor stats with calculated values, ping type", async function () { monitorFindByIdStub.returns(mockMonitorPing); req.query.sortOrder = "desc"; const result = await getMonitorStatsById(req); @@ -1034,7 +1034,7 @@ describe("monitorModule", function() { expect(result.aggregateData).to.be.an("array"); }); - it("should return monitor stats with calculated values, docker type", async function() { + it("should return monitor stats with calculated values, docker type", async function () { monitorFindByIdStub.returns(mockMonitorDocker); req.query.sortOrder = "desc"; const result = await getMonitorStatsById(req); @@ -1059,7 +1059,7 @@ describe("monitorModule", function() { expect(result.aggregateData).to.be.an("array"); }); - it("should return monitor stats with calculated values", async function() { + it("should return monitor stats with calculated values", async function () { req.query.sortOrder = "asc"; const result = await getMonitorStatsById(req); expect(result).to.include.keys([ @@ -1083,7 +1083,7 @@ describe("monitorModule", function() { expect(result.aggregateData).to.be.an("array"); }); - it("should throw error when monitor is not found", async function() { + it("should throw error when monitor is not found", async function () { monitorFindByIdStub.returns(Promise.resolve(null)); const req = { @@ -1101,21 +1101,21 @@ describe("monitorModule", function() { }); }); - describe("getMonitorById", function() { + describe("getMonitorById", function () { let notificationFindStub; let monitorSaveStub; - beforeEach(function() { + beforeEach(function () { // Create stubs notificationFindStub = sinon.stub(Notification, "find"); monitorSaveStub = sinon.stub().resolves(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should return monitor with notifications when found", async function() { + it("should return monitor with notifications when found", async function () { // Arrange const monitorId = "123"; const mockMonitor = { @@ -1139,9 +1139,10 @@ describe("monitorModule", function() { expect(monitorSaveStub.calledOnce).to.be.true; }); - it("should throw 404 error when monitor not found", async function() { + it("should throw 404 error when monitor not found", async function () { // Arrange const monitorId = "nonexistent"; + const mockLanguage = 'en'; monitorFindByIdStub.resolves(null); // Act & Assert @@ -1149,14 +1150,14 @@ describe("monitorModule", function() { await getMonitorById(monitorId); expect.fail("Should have thrown an error"); } catch (error) { - expect(error.message).to.equal(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId)); + expect(error.message).to.equal(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId, mockLanguage)); expect(error.status).to.equal(404); expect(error.service).to.equal("monitorModule"); expect(error.method).to.equal("getMonitorById"); } }); - it("should handle database errors properly", async function() { + it("should handle database errors properly", async function () { // Arrange const monitorId = "123"; const dbError = new Error("Database connection failed"); @@ -1173,7 +1174,7 @@ describe("monitorModule", function() { } }); - it("should handle notification fetch errors", async function() { + it("should handle notification fetch errors", async function () { // Arrange const monitorId = "123"; const mockMonitor = { @@ -1197,7 +1198,7 @@ describe("monitorModule", function() { } }); - it("should handle monitor save errors", async function() { + it("should handle monitor save errors", async function () { // Arrange const monitorId = "123"; const mockMonitor = { @@ -1222,8 +1223,8 @@ describe("monitorModule", function() { }); }); - describe("getMonitorsAndSummaryByTeamId", function() { - it("should return monitors and correct summary counts", async function() { + describe("getMonitorsAndSummaryByTeamId", function () { + it("should return monitors and correct summary counts", async function () { // Arrange const teamId = "team123"; const type = "http"; @@ -1249,7 +1250,7 @@ describe("monitorModule", function() { expect(monitorFindStub.calledOnceWith({ teamId, type })).to.be.true; }); - it("should return empty results for non-existent team", async function() { + it("should return empty results for non-existent team", async function () { // Arrange monitorFindStub.resolves([]); @@ -1266,7 +1267,7 @@ describe("monitorModule", function() { }); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { // Arrange const error = new Error("Database error"); error.service = "MonitorModule"; @@ -1285,8 +1286,8 @@ describe("monitorModule", function() { }); }); - describe("getMonitorsByTeamId", function() { - beforeEach(function() { + describe("getMonitorsByTeamId", function () { + beforeEach(function () { // Chain stubs for Monitor.find().skip().limit().sort() // Stub for CHECK_MODEL_LOOKUP model find @@ -1297,11 +1298,11 @@ describe("monitorModule", function() { }); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should return monitors with basic query parameters", async function() { + it("should return monitors with basic query parameters", async function () { const mockMonitors = [ { _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) }, { _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) }, @@ -1334,7 +1335,7 @@ describe("monitorModule", function() { expect(result).to.have.property("monitorCount", 2); }); - it("should return monitors with basic query parameters", async function() { + it("should return monitors with basic query parameters", async function () { const mockMonitors = [ { _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) }, { _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) }, @@ -1367,7 +1368,7 @@ describe("monitorModule", function() { expect(result).to.have.property("monitorCount", 2); }); - it("should handle type filter with array input", async function() { + it("should handle type filter with array input", async function () { const req = { params: { teamId: "team123" }, query: { @@ -1392,7 +1393,7 @@ describe("monitorModule", function() { }); }); - it("should handle text search filter", async function() { + it("should handle text search filter", async function () { const req = { params: { teamId: "team123" }, query: { @@ -1420,7 +1421,7 @@ describe("monitorModule", function() { }); }); - it("should handle pagination parameters", async function() { + it("should handle pagination parameters", async function () { const req = { params: { teamId: "team123" }, query: { @@ -1445,7 +1446,7 @@ describe("monitorModule", function() { }); }); - it("should handle sorting parameters", async function() { + it("should handle sorting parameters", async function () { const req = { params: { teamId: "team123" }, query: { @@ -1472,7 +1473,7 @@ describe("monitorModule", function() { }); }); - it("should return early when limit is -1", async function() { + it("should return early when limit is -1", async function () { // Arrange const req = { params: { teamId: "team123" }, @@ -1506,7 +1507,7 @@ describe("monitorModule", function() { }); }); - it("should normalize checks when normalize parameter is provided", async function() { + it("should normalize checks when normalize parameter is provided", async function () { const req = { params: { teamId: "team123" }, query: { normalize: "true" }, @@ -1531,7 +1532,7 @@ describe("monitorModule", function() { expect(result.monitors).to.have.lengthOf(2); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { const req = { params: { teamId: "team123" }, query: {}, @@ -1557,8 +1558,8 @@ describe("monitorModule", function() { }); }); - describe("createMonitor", function() { - it("should create a monitor without notifications", async function() { + describe("createMonitor", function () { + it("should create a monitor without notifications", async function () { let monitorSaveStub = sinon.stub(Monitor.prototype, "save").resolves(); const req = { @@ -1583,7 +1584,7 @@ describe("monitorModule", function() { expect(result.url).to.equal(expectedMonitor.url); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { const req = { body: { name: "Test Monitor", @@ -1600,8 +1601,8 @@ describe("monitorModule", function() { }); }); - describe("deleteMonitor", function() { - it("should delete a monitor successfully", async function() { + describe("deleteMonitor", function () { + it("should delete a monitor successfully", async function () { const monitorId = "123456789"; const mockMonitor = { _id: monitorId, @@ -1621,10 +1622,11 @@ describe("monitorModule", function() { sinon.assert.calledWith(monitorFindByIdAndDeleteStub, monitorId); }); - it("should throw error when monitor not found", async function() { + it("should throw error when monitor not found", async function () { const monitorId = "nonexistent123"; const req = { params: { monitorId }, + language: 'en', }; monitorFindByIdAndDeleteStub.resolves(null); @@ -1633,13 +1635,13 @@ describe("monitorModule", function() { await deleteMonitor(req); expect.fail("Should have thrown an error"); } catch (err) { - expect(err.message).to.equal(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId)); + expect(err.message).to.equal(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId, req.language)); expect(err.service).to.equal("monitorModule"); expect(err.method).to.equal("deleteMonitor"); } }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { const monitorId = "123456789"; const req = { params: { monitorId }, @@ -1659,8 +1661,8 @@ describe("monitorModule", function() { }); }); - describe("deleteAllMonitors", function() { - it("should delete all monitors for a team successfully", async function() { + describe("deleteAllMonitors", function () { + it("should delete all monitors for a team successfully", async function () { const teamId = "team123"; const mockMonitors = [ { _id: "1", name: "Monitor 1", teamId }, @@ -1680,7 +1682,7 @@ describe("monitorModule", function() { sinon.assert.calledWith(monitorDeleteManyStub, { teamId }); }); - it("should return empty array when no monitors found", async function() { + it("should return empty array when no monitors found", async function () { const teamId = "emptyTeam"; monitorFindStub.resolves([]); @@ -1696,7 +1698,7 @@ describe("monitorModule", function() { sinon.assert.calledWith(monitorDeleteManyStub, { teamId }); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { const teamId = "team123"; const dbError = new Error("Database connection error"); monitorFindStub.rejects(dbError); @@ -1710,7 +1712,7 @@ describe("monitorModule", function() { } }); - it("should handle deleteMany errors", async function() { + it("should handle deleteMany errors", async function () { const teamId = "team123"; monitorFindStub.resolves([{ _id: "1", name: "Monitor 1" }]); monitorDeleteManyStub.rejects(new Error("Delete operation failed")); @@ -1725,14 +1727,14 @@ describe("monitorModule", function() { }); }); - describe("deleteMonitorsByUserId", function() { - beforeEach(function() {}); + describe("deleteMonitorsByUserId", function () { + beforeEach(function () { }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should delete all monitors for a user successfully", async function() { + it("should delete all monitors for a user successfully", async function () { // Arrange const userId = "user123"; const mockResult = { @@ -1750,7 +1752,7 @@ describe("monitorModule", function() { sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId }); }); - it("should return zero deletedCount when no monitors found", async function() { + it("should return zero deletedCount when no monitors found", async function () { // Arrange const userId = "nonexistentUser"; const mockResult = { @@ -1768,7 +1770,7 @@ describe("monitorModule", function() { sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId }); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { // Arrange const userId = "user123"; const dbError = new Error("Database connection error"); @@ -1786,8 +1788,8 @@ describe("monitorModule", function() { }); }); - describe("editMonitor", function() { - it("should edit a monitor successfully", async function() { + describe("editMonitor", function () { + it("should edit a monitor successfully", async function () { // Arrange const candidateId = "monitor123"; const candidateMonitor = { @@ -1826,7 +1828,7 @@ describe("monitorModule", function() { ); }); - it("should return null when monitor not found", async function() { + it("should return null when monitor not found", async function () { // Arrange const candidateId = "nonexistent123"; const candidateMonitor = { @@ -1848,7 +1850,7 @@ describe("monitorModule", function() { ); }); - it("should remove notifications from update data", async function() { + it("should remove notifications from update data", async function () { // Arrange const candidateId = "monitor123"; const candidateMonitor = { @@ -1880,7 +1882,7 @@ describe("monitorModule", function() { ); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { // Arrange const candidateId = "monitor123"; const candidateMonitor = { @@ -1902,8 +1904,8 @@ describe("monitorModule", function() { }); }); - describe("addDemoMonitors", function() { - it("should add demo monitors successfully", async function() { + describe("addDemoMonitors", function () { + it("should add demo monitors successfully", async function () { // Arrange const userId = "user123"; const teamId = "team123"; @@ -1912,7 +1914,7 @@ describe("monitorModule", function() { expect(result).to.deep.equal([{ _id: "123" }]); }); - it("should handle database errors", async function() { + it("should handle database errors", async function () { const userId = "user123"; const teamId = "team123"; diff --git a/Server/tests/db/recoveryModule.test.js b/Server/tests/db/recoveryModule.test.js index 9d2978dd3..1c6d1d43c 100644 --- a/Server/tests/db/recoveryModule.test.js +++ b/Server/tests/db/recoveryModule.test.js @@ -43,7 +43,7 @@ const createQueryChain = (finalResult, comparePasswordResult = false) => ({ save: sinon.stub().resolves(), }); -describe("recoveryModule", function() { +describe("recoveryModule", function () { let deleteManyStub, saveStub, findOneStub, @@ -52,9 +52,10 @@ describe("recoveryModule", function() { userFindOneStub; let req, res; - beforeEach(function() { + beforeEach(function () { req = { body: { email: "test@test.com" }, + language: 'en', }; deleteManyStub = sinon.stub(RecoveryToken, "deleteMany"); saveStub = sinon.stub(RecoveryToken.prototype, "save"); @@ -64,19 +65,19 @@ describe("recoveryModule", function() { userFindOneStub = sinon.stub().resolves(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - describe("requestRecoveryToken", function() { - it("should return a recovery token", async function() { + describe("requestRecoveryToken", function () { + it("should return a recovery token", async function () { deleteManyStub.resolves(); saveStub.resolves(mockRecoveryToken); const result = await requestRecoveryToken(req, res); expect(result.email).to.equal(mockRecoveryToken.email); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("Test error"); deleteManyStub.rejects(err); try { @@ -88,24 +89,24 @@ describe("recoveryModule", function() { }); }); - describe("validateRecoveryToken", function() { - it("should return a recovery token if found", async function() { + describe("validateRecoveryToken", function () { + it("should return a recovery token if found", async function () { findOneStub.resolves(mockRecoveryToken); const result = await validateRecoveryToken(req, res); expect(result).to.deep.equal(mockRecoveryToken); }); - it("should thrown an error if a token is not found", async function() { + it("should thrown an error if a token is not found", async function () { findOneStub.resolves(null); try { await validateRecoveryToken(req, res); } catch (error) { expect(error).to.exist; - expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND(req.language)); } }); - it("should handle DB errors", async function() { + it("should handle DB errors", async function () { const err = new Error("Test error"); findOneStub.rejects(err); try { @@ -117,40 +118,41 @@ describe("recoveryModule", function() { }); }); - describe("resetPassword", function() { - beforeEach(function() { + describe("resetPassword", function () { + beforeEach(function () { req.body = { password: "test", newPassword: "test1", }; + req.language = 'en'; }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should thrown an error if a recovery token is not found", async function() { + it("should thrown an error if a recovery token is not found", async function () { findOneStub.resolves(null); try { await resetPassword(req, res); } catch (error) { expect(error).to.exist; - expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND(req.language)); } }); - it("should throw an error if a user is not found", async function() { + it("should throw an error if a user is not found", async function () { findOneStub.resolves(mockRecoveryToken); userFindOneStub = sinon.stub(User, "findOne").resolves(null); try { await resetPassword(req, res); } catch (error) { expect(error).to.exist; - expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND(req.language)); } }); - it("should throw an error if the passwords match", async function() { + it("should throw an error if the passwords match", async function () { findOneStub.resolves(mockRecoveryToken); saveStub.resolves(); userFindOneStub = sinon @@ -160,11 +162,11 @@ describe("recoveryModule", function() { await resetPassword(req, res); } catch (error) { expect(error).to.exist; - expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH); + expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH(req.language)); } }); - it("should return a user without password if successful", async function() { + it("should return a user without password if successful", async function () { findOneStub.resolves(mockRecoveryToken); saveStub.resolves(); userFindOneStub = sinon diff --git a/Server/tests/db/statusPageModule.test.js b/Server/tests/db/statusPageModule.test.js index 3d5d5e9d0..0793dc6b9 100644 --- a/Server/tests/db/statusPageModule.test.js +++ b/Server/tests/db/statusPageModule.test.js @@ -6,31 +6,32 @@ import { import StatusPage from "../../db/models/StatusPage.js"; import { errorMessages } from "../../utils/messages.js"; -describe("statusPageModule", function() { - let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub; +describe("statusPageModule", function () { + let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub, mockLanguage; - beforeEach(function() { + beforeEach(function () { + mockLanguage = 'en'; statusPageSaveStub = sinon.stub(StatusPage.prototype, "save"); statusPageFindOneStub = sinon.stub(StatusPage, "findOne"); statusPageFindStub = sinon.stub(StatusPage, "find"); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - describe("createStatusPage", function() { - it("should throw an error if a non-unique url is provided", async function() { + describe("createStatusPage", function () { + it("should throw an error if a non-unique url is provided", async function () { statusPageFindOneStub.resolves(true); try { await createStatusPage({ url: "test" }); } catch (error) { expect(error.status).to.equal(400); - expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE); + expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE(mockLanguage)); } }); - it("should handle duplicate URL errors", async function() { + it("should handle duplicate URL errors", async function () { const err = new Error("test"); err.code = 11000; statusPageSaveStub.rejects(err); @@ -41,7 +42,7 @@ describe("statusPageModule", function() { } }); - it("should return a status page if a unique url is provided", async function() { + it("should return a status page if a unique url is provided", async function () { statusPageFindOneStub.resolves(null); statusPageFindStub.resolves([]); const mockStatusPage = { url: "test" }; @@ -51,21 +52,21 @@ describe("statusPageModule", function() { }); }); - describe("getStatusPageByUrl", function() { - it("should throw an error if a status page is not found", async function() { + describe("getStatusPageByUrl", function () { + it("should throw an error if a status page is not found", async function () { statusPageFindOneStub.resolves(null); try { await getStatusPageByUrl("test"); } catch (error) { expect(error.status).to.equal(404); - expect(error.message).to.equal(errorMessages.STATUS_PAGE_NOT_FOUND); + expect(error.message).to.equal(errorMessages.STATUS_PAGE_NOT_FOUND(mockLanguage)); } }); - it("should return a status page if a status page is found", async function() { + it("should return a status page if a status page is found", async function () { const mockStatusPage = { url: "test" }; statusPageFindOneStub.resolves(mockStatusPage); - const statusPage = await getStatusPageByUrl(mockStatusPage.url); + const statusPage = await getStatusPageByUrl(mockStatusPage.url, mockLanguage); expect(statusPage).to.exist; expect(statusPage).to.deep.equal(mockStatusPage); }); diff --git a/Server/tests/db/userModule.test.js b/Server/tests/db/userModule.test.js index b52ba1880..0bf1fd067 100644 --- a/Server/tests/db/userModule.test.js +++ b/Server/tests/db/userModule.test.js @@ -27,7 +27,9 @@ const imageFile = { image: 1, }; -describe("userModule", function() { +const mockLanguage = 'en'; + +describe("userModule", function () { let teamSaveStub, teamFindByIdAndDeleteStub, userSaveStub, @@ -40,7 +42,7 @@ describe("userModule", function() { generateAvatarImageStub, parseBooleanStub; - beforeEach(function() { + beforeEach(function () { teamSaveStub = sinon.stub(TeamModel.prototype, "save"); teamFindByIdAndDeleteStub = sinon.stub(TeamModel, "findByIdAndDelete"); userSaveStub = sinon.stub(UserModel.prototype, "save"); @@ -54,12 +56,12 @@ describe("userModule", function() { parseBooleanStub = sinon.stub().returns(true); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - describe("insertUser", function() { - it("should insert a regular user", async function() { + describe("insertUser", function () { + it("should insert a regular user", async function () { userSaveStub.resolves(mockUser); userFindOneStub.returns({ select: sinon.stub().returns({ @@ -70,7 +72,7 @@ describe("userModule", function() { expect(result).to.deep.equal(mockUser); }); - it("should insert a superadmin user", async function() { + it("should insert a superadmin user", async function () { userSaveStub.resolves(mockSuperUser); userFindOneStub.returns({ select: sinon.stub().returns({ @@ -81,7 +83,7 @@ describe("userModule", function() { expect(result).to.deep.equal(mockSuperUser); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userSaveStub.rejects(err); try { @@ -92,7 +94,7 @@ describe("userModule", function() { } }); - it("should handle a duplicate key error", async function() { + it("should handle a duplicate key error", async function () { const err = new Error("test error"); err.code = 11000; userSaveStub.rejects(err); @@ -105,8 +107,8 @@ describe("userModule", function() { }); }); - describe("getUserByEmail", function() { - it("should return a user", async function() { + describe("getUserByEmail", function () { + it("should return a user", async function () { userFindOneStub.returns({ select: sinon.stub().resolves(mockUser), }); @@ -115,23 +117,23 @@ describe("userModule", function() { }); }); - describe("getUserByEmail", function() { - it("throw an error if a user is not found", async function() { + describe("getUserByEmail", function () { + it("throw an error if a user is not found", async function () { userFindOneStub.returns({ select: sinon.stub().resolves(null), }); try { await getUserByEmail(mockUser.email); } catch (error) { - expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND(mockLanguage)); } }); }); - describe("updateUser", function() { + describe("updateUser", function () { let req, res; - beforeEach(function() { + beforeEach(function () { req = { params: { userId: "testId", @@ -148,9 +150,9 @@ describe("userModule", function() { res = {}; }); - afterEach(function() {}); + afterEach(function () { }); - it("should update a user", async function() { + it("should update a user", async function () { parseBooleanStub.returns(false); userFindByIdAndUpdateStub.returns({ select: sinon.stub().returns({ @@ -166,7 +168,7 @@ describe("userModule", function() { expect(result).to.deep.equal(mockUser); }); - it("should delete a user profile image", async function() { + it("should delete a user profile image", async function () { req.body.deleteProfileImage = "true"; userFindByIdAndUpdateStub.returns({ select: sinon.stub().returns({ @@ -182,7 +184,7 @@ describe("userModule", function() { expect(result).to.deep.equal(mockUser); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userFindByIdAndUpdateStub.throws(err); try { @@ -194,23 +196,23 @@ describe("userModule", function() { }); }); - describe("deleteUser", function() { - it("should return a deleted user", async function() { + describe("deleteUser", function () { + it("should return a deleted user", async function () { userFindByIdAndDeleteStub.resolves(mockUser); const result = await deleteUser("testId"); expect(result).to.deep.equal(mockUser); }); - it("should throw an error if a user is not found", async function() { + it("should throw an error if a user is not found", async function () { try { await deleteUser("testId"); } catch (error) { expect(error).to.exist; - expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND(mockLanguage)); } }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userFindByIdAndDeleteStub.throws(err); try { @@ -222,14 +224,14 @@ describe("userModule", function() { }); }); - describe("deleteTeam", function() { - it("should return true if team deleted", async function() { + describe("deleteTeam", function () { + it("should return true if team deleted", async function () { teamFindByIdAndDeleteStub.resolves(); const result = await deleteTeam("testId"); expect(result).to.equal(true); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); teamFindByIdAndDeleteStub.throws(err); try { @@ -241,14 +243,14 @@ describe("userModule", function() { }); }); - describe("deleteAllOtherUsers", function() { - it("should return true if all other users deleted", async function() { + describe("deleteAllOtherUsers", function () { + it("should return true if all other users deleted", async function () { userDeleteManyStub.resolves(true); const result = await deleteAllOtherUsers(); expect(result).to.equal(true); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userDeleteManyStub.throws(err); try { @@ -260,8 +262,8 @@ describe("userModule", function() { }); }); - describe("getAllUsers", function() { - it("should return all users", async function() { + describe("getAllUsers", function () { + it("should return all users", async function () { userFindStub.returns({ select: sinon.stub().returns({ select: sinon.stub().resolves([mockUser]), @@ -271,7 +273,7 @@ describe("userModule", function() { expect(result).to.deep.equal([mockUser]); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userFindStub.throws(err); try { @@ -283,14 +285,14 @@ describe("userModule", function() { }); }); - describe("logoutUser", function() { - it("should return true if user logged out", async function() { + describe("logoutUser", function () { + it("should return true if user logged out", async function () { userUpdateOneStub.resolves(true); const result = await logoutUser("testId"); expect(result).to.equal(true); }); - it("should handle an error", async function() { + it("should handle an error", async function () { const err = new Error("test error"); userUpdateOneStub.throws(err); try { diff --git a/Server/tests/services/networkService.test.js b/Server/tests/services/networkService.test.js index a5a59c938..f20b2628d 100644 --- a/Server/tests/services/networkService.test.js +++ b/Server/tests/services/networkService.test.js @@ -3,10 +3,10 @@ import NetworkService from "../../service/networkService.js"; import { expect } from "chai"; import http from "http"; import { errorMessages } from "../../utils/messages.js"; -describe("Network Service", function() { +describe("Network Service", function () { let axios, ping, Docker, logger, networkService; - beforeEach(function() { + beforeEach(function () { axios = { get: sinon.stub().resolves({ data: { foo: "bar" }, @@ -35,22 +35,22 @@ describe("Network Service", function() { networkService = new NetworkService(axios, ping, logger, http, Docker); }); - describe("constructor", function() { - it("should create a new NetworkService instance", function() { + describe("constructor", function () { + it("should create a new NetworkService instance", function () { const networkService = new NetworkService(); expect(networkService).to.be.an.instanceOf(NetworkService); }); }); - describe("timeRequest", function() { - it("should time an asynchronous operation", async function() { + describe("timeRequest", function () { + it("should time an asynchronous operation", async function () { const operation = sinon.stub().resolves("success"); const { response, responseTime } = await networkService.timeRequest(operation); expect(response).to.equal("success"); expect(responseTime).to.be.a("number"); }); - it("should handle errors if operation throws error", async function() { + it("should handle errors if operation throws error", async function () { const error = new Error("Test error"); const operation = sinon.stub().throws(error); const { response, responseTime } = await networkService.timeRequest(operation); @@ -60,8 +60,8 @@ describe("Network Service", function() { }); }); - describe("requestPing", function() { - it("should return a response object if ping successful", async function() { + describe("requestPing", function () { + it("should return a response object if ping successful", async function () { const pingResult = await networkService.requestPing({ data: { url: "http://test.com", _id: "123" }, }); @@ -71,7 +71,7 @@ describe("Network Service", function() { expect(pingResult.status).to.be.true; }); - it("should return a response object if ping unsuccessful", async function() { + it("should return a response object if ping unsuccessful", async function () { const error = new Error("Test error"); networkService.timeRequest = sinon .stub() @@ -86,7 +86,7 @@ describe("Network Service", function() { expect(pingResult.code).to.equal(networkService.PING_ERROR); }); - it("should throw an error if ping cannot resolve", async function() { + it("should throw an error if ping cannot resolve", async function () { const error = new Error("test error"); networkService.timeRequest = sinon.stub().throws(error); try { @@ -100,8 +100,8 @@ describe("Network Service", function() { }); }); - describe("requestHttp", function() { - it("should return a response object if http successful", async function() { + describe("requestHttp", function () { + it("should return a response object if http successful", async function () { const job = { data: { url: "http://test.com", _id: "123", type: "http" } }; const httpResult = await networkService.requestHttp(job); expect(httpResult.monitorId).to.equal("123"); @@ -110,7 +110,7 @@ describe("Network Service", function() { expect(httpResult.status).to.be.true; }); - it("should return a response object if http unsuccessful", async function() { + it("should return a response object if http unsuccessful", async function () { const error = new Error("Test error"); error.response = { status: 404 }; networkService.timeRequest = sinon @@ -125,7 +125,7 @@ describe("Network Service", function() { expect(httpResult.code).to.equal(404); }); - it("should return a response object if http unsuccessful with unknown code", async function() { + it("should return a response object if http unsuccessful with unknown code", async function () { const error = new Error("Test error"); error.response = {}; networkService.timeRequest = sinon @@ -140,7 +140,7 @@ describe("Network Service", function() { expect(httpResult.code).to.equal(networkService.NETWORK_ERROR); }); - it("should throw an error if an error occurs", async function() { + it("should throw an error if an error occurs", async function () { const error = new Error("test error"); networkService.timeRequest = sinon.stub().throws(error); try { @@ -154,8 +154,8 @@ describe("Network Service", function() { }); }); - describe("requestPagespeed", function() { - it("should return a response object if pagespeed successful", async function() { + describe("requestPagespeed", function () { + it("should return a response object if pagespeed successful", async function () { const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } }; const pagespeedResult = await networkService.requestPagespeed(job); expect(pagespeedResult.monitorId).to.equal("123"); @@ -164,7 +164,7 @@ describe("Network Service", function() { expect(pagespeedResult.status).to.be.true; }); - it("should return a response object if pagespeed unsuccessful", async function() { + it("should return a response object if pagespeed unsuccessful", async function () { const error = new Error("Test error"); error.response = { status: 404 }; networkService.timeRequest = sinon @@ -179,7 +179,7 @@ describe("Network Service", function() { expect(pagespeedResult.code).to.equal(404); }); - it("should return a response object if pagespeed unsuccessful with an unknown code", async function() { + it("should return a response object if pagespeed unsuccessful with an unknown code", async function () { const error = new Error("Test error"); error.response = {}; networkService.timeRequest = sinon @@ -194,7 +194,7 @@ describe("Network Service", function() { expect(pagespeedResult.code).to.equal(networkService.NETWORK_ERROR); }); - it("should throw an error if pagespeed cannot resolve", async function() { + it("should throw an error if pagespeed cannot resolve", async function () { const error = new Error("test error"); networkService.timeRequest = sinon.stub().throws(error); try { @@ -208,8 +208,8 @@ describe("Network Service", function() { }); }); - describe("requestHardware", function() { - it("should return a response object if hardware successful", async function() { + describe("requestHardware", function () { + it("should return a response object if hardware successful", async function () { const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } }; const httpResult = await networkService.requestHardware(job); expect(httpResult.monitorId).to.equal("123"); @@ -218,7 +218,7 @@ describe("Network Service", function() { expect(httpResult.status).to.be.true; }); - it("should return a response object if hardware successful and job has a secret", async function() { + it("should return a response object if hardware successful and job has a secret", async function () { const job = { data: { url: "http://test.com", @@ -234,7 +234,7 @@ describe("Network Service", function() { expect(httpResult.status).to.be.true; }); - it("should return a response object if hardware unsuccessful", async function() { + it("should return a response object if hardware unsuccessful", async function () { const error = new Error("Test error"); error.response = { status: 404 }; networkService.timeRequest = sinon @@ -249,7 +249,7 @@ describe("Network Service", function() { expect(httpResult.code).to.equal(404); }); - it("should return a response object if hardware unsuccessful with unknown code", async function() { + it("should return a response object if hardware unsuccessful with unknown code", async function () { const error = new Error("Test error"); error.response = {}; networkService.timeRequest = sinon @@ -264,7 +264,7 @@ describe("Network Service", function() { expect(httpResult.code).to.equal(networkService.NETWORK_ERROR); }); - it("should throw an error if hardware cannot resolve", async function() { + it("should throw an error if hardware cannot resolve", async function () { const error = new Error("test error"); networkService.timeRequest = sinon.stub().throws(error); try { @@ -278,8 +278,8 @@ describe("Network Service", function() { }); }); - describe("requestDocker", function() { - it("should return a response object if docker successful", async function() { + describe("requestDocker", function () { + it("should return a response object if docker successful", async function () { const job = { data: { url: "http://test.com", _id: "123", type: "docker" } }; const dockerResult = await networkService.requestDocker(job); expect(dockerResult.monitorId).to.equal("123"); @@ -288,7 +288,7 @@ describe("Network Service", function() { expect(dockerResult.status).to.be.true; }); - it("should return a response object with status false if container not running", async function() { + it("should return a response object with status false if container not running", async function () { Docker = class { listContainers = sinon.stub().resolves([ { @@ -307,7 +307,7 @@ describe("Network Service", function() { expect(dockerResult.code).to.equal(200); }); - it("should handle an error when fetching the container", async function() { + it("should handle an error when fetching the container", async function () { Docker = class { listContainers = sinon.stub().resolves([ { @@ -326,7 +326,7 @@ describe("Network Service", function() { expect(dockerResult.code).to.equal(networkService.NETWORK_ERROR); }); - it("should throw an error if operations fail", async function() { + it("should throw an error if operations fail", async function () { Docker = class { listContainers = sinon.stub().resolves([ { @@ -345,7 +345,7 @@ describe("Network Service", function() { } }); - it("should throw an error if no matching images found", async function() { + it("should throw an error if no matching images found", async function () { Docker = class { listContainers = sinon.stub().resolves([]); getContainer = sinon.stub().throws(new Error("test error")); @@ -355,13 +355,13 @@ describe("Network Service", function() { try { await networkService.requestDocker(job); } catch (error) { - expect(error.message).to.equal(errorMessages.DOCKER_NOT_FOUND); + expect(error.message).to.equal(errorMessages.DOCKER_NOT_FOUND(mockLanguage)); } }); }); - describe("getStatus", function() { - beforeEach(function() { + describe("getStatus", function () { + beforeEach(function () { networkService.requestPing = sinon.stub(); networkService.requestHttp = sinon.stub(); networkService.requestPagespeed = sinon.stub(); @@ -369,11 +369,11 @@ describe("Network Service", function() { networkService.requestDocker = sinon.stub(); }); - afterEach(function() { + afterEach(function () { sinon.restore(); }); - it("should call requestPing if type is ping", async function() { + it("should call requestPing if type is ping", async function () { await networkService.getStatus({ data: { type: "ping" } }); expect(networkService.requestPing.calledOnce).to.be.true; expect(networkService.requestDocker.notCalled).to.be.true; @@ -381,7 +381,7 @@ describe("Network Service", function() { expect(networkService.requestPagespeed.notCalled).to.be.true; }); - it("should call requestHttp if type is http", async function() { + it("should call requestHttp if type is http", async function () { await networkService.getStatus({ data: { type: "http" } }); expect(networkService.requestPing.notCalled).to.be.true; expect(networkService.requestDocker.notCalled).to.be.true; @@ -389,7 +389,7 @@ describe("Network Service", function() { expect(networkService.requestPagespeed.notCalled).to.be.true; }); - it("should call requestPagespeed if type is pagespeed", async function() { + it("should call requestPagespeed if type is pagespeed", async function () { await networkService.getStatus({ data: { type: "pagespeed" } }); expect(networkService.requestPing.notCalled).to.be.true; expect(networkService.requestDocker.notCalled).to.be.true; @@ -397,7 +397,7 @@ describe("Network Service", function() { expect(networkService.requestPagespeed.calledOnce).to.be.true; }); - it("should call requestHardware if type is hardware", async function() { + it("should call requestHardware if type is hardware", async function () { await networkService.getStatus({ data: { type: "hardware" } }); expect(networkService.requestHardware.calledOnce).to.be.true; expect(networkService.requestDocker.notCalled).to.be.true; @@ -405,7 +405,7 @@ describe("Network Service", function() { expect(networkService.requestPagespeed.notCalled).to.be.true; }); - it("should call requestDocker if type is Docker", async function() { + it("should call requestDocker if type is Docker", async function () { await networkService.getStatus({ data: { type: "docker" } }); expect(networkService.requestDocker.calledOnce).to.be.true; expect(networkService.requestHardware.notCalled).to.be.true; @@ -413,7 +413,7 @@ describe("Network Service", function() { expect(networkService.requestPagespeed.notCalled).to.be.true; }); - it("should throw an error if an unknown type is provided", async function() { + it("should throw an error if an unknown type is provided", async function () { try { await networkService.getStatus({ data: { type: "unknown" } }); } catch (error) { @@ -423,7 +423,7 @@ describe("Network Service", function() { } }); - it("should throw an error if job type is undefined", async function() { + it("should throw an error if job type is undefined", async function () { try { await networkService.getStatus({ data: { type: undefined } }); } catch (error) { @@ -433,7 +433,7 @@ describe("Network Service", function() { } }); - it("should throw an error if job is empty", async function() { + it("should throw an error if job is empty", async function () { try { await networkService.getStatus({}); } catch (error) { @@ -442,7 +442,7 @@ describe("Network Service", function() { } }); - it("should throw an error if job is null", async function() { + it("should throw an error if job is null", async function () { try { await networkService.getStatus(null); } catch (error) { diff --git a/Server/tests/utils/messages.test.js b/Server/tests/utils/messages.test.js index 7ece332c4..a36fc2a4a 100644 --- a/Server/tests/utils/messages.test.js +++ b/Server/tests/utils/messages.test.js @@ -1,23 +1,25 @@ import { errorMessages, successMessages } from "../../utils/messages.js"; -describe("Messages", function() { - describe("messages - errorMessages", function() { - it("should have a DB_FIND_MONITOR_BY_ID function", function() { +describe("Messages", function () { + describe("messages - errorMessages", function () { + it("should have a DB_FIND_MONITOR_BY_ID function", function () { const monitorId = "12345"; - expect(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId)).to.equal( + const mockLanguage = 'en'; + expect(errorMessages.DB_FIND_MONITOR_BY_ID(monitorId, mockLanguage)).to.equal( `Monitor with id ${monitorId} not found` ); }); - it("should have a DB_DELETE_CHECKS function", function() { + it("should have a DB_DELETE_CHECKS function", function () { const monitorId = "12345"; - expect(errorMessages.DB_DELETE_CHECKS(monitorId)).to.equal( + const mockLanguage = 'en'; + expect(errorMessages.DB_DELETE_CHECKS(monitorId, mockLanguage)).to.equal( `No checks found for monitor with id ${monitorId}` ); }); }); - describe("messages - successMessages", function() { - it("should have a MONITOR_GET_BY_USER_ID function", function() { + describe("messages - successMessages", function () { + it("should have a MONITOR_GET_BY_USER_ID function", function () { const userId = "12345"; expect(successMessages.MONITOR_GET_BY_USER_ID(userId)).to.equal( `Got monitor for ${userId} successfully"` diff --git a/Server/utils/formattedKey.js b/Server/utils/formattedKey.js new file mode 100644 index 000000000..b60e6783b --- /dev/null +++ b/Server/utils/formattedKey.js @@ -0,0 +1,14 @@ +/** + * Converts a snake_case or SCREAMING_SNAKE_CASE key to camelCase + * Example: AUTH_INCORRECT_PASSWORD -> authIncorrectPassword + * @param {string} key - The key to format + * @returns {string} - The formatted key in camelCase + */ +export const formattedKey = (key) => { + return key.toLowerCase() + .split('_') + .map((word, index) => + index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1) + ) + .join(''); +}; \ No newline at end of file diff --git a/Server/utils/messages.js b/Server/utils/messages.js index 38b1bf9d1..f4568165b 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -1,137 +1,146 @@ +import ServiceRegistry from '../service/serviceRegistry.js'; +import TranslationService from '../service/translationService.js'; + +const getTranslatedMessage = (key, language = 'en') => { + console.log("getTranslatedMessage", key, language); + const translationService = ServiceRegistry.get(TranslationService.SERVICE_NAME); + return translationService.getTranslation(key, language); +}; + const errorMessages = { // General Errors: - FRIENDLY_ERROR: "Something went wrong...", - UNKNOWN_ERROR: "An unknown error occurred", + FRIENDLY_ERROR: (language) => getTranslatedMessage('FRIENDLY_ERROR', language), + UNKNOWN_ERROR: (language) => getTranslatedMessage('UNKNOWN_ERROR', language), // Auth Controller - UNAUTHORIZED: "Unauthorized access", - AUTH_ADMIN_EXISTS: "Admin already exists", - AUTH_INVITE_NOT_FOUND: "Invite not found", + UNAUTHORIZED: (language) => getTranslatedMessage('UNAUTHORIZED', language), + AUTH_ADMIN_EXISTS: (language) => getTranslatedMessage('AUTH_ADMIN_EXISTS', language), + AUTH_INVITE_NOT_FOUND: (language) => getTranslatedMessage('AUTH_INVITE_NOT_FOUND', language), //Error handling middleware - UNKNOWN_SERVICE: "Unknown service", - NO_AUTH_TOKEN: "No auth token provided", - INVALID_AUTH_TOKEN: "Invalid auth token", - EXPIRED_AUTH_TOKEN: "Token expired", - NO_REFRESH_TOKEN: "No refresh token provided", - INVALID_REFRESH_TOKEN: "Invalid refresh token", - EXPIRED_REFRESH_TOKEN: "Refresh token expired", - REQUEST_NEW_ACCESS_TOKEN: "Request new access token", + UNKNOWN_SERVICE: (language) => getTranslatedMessage('UNKNOWN_SERVICE', language), + NO_AUTH_TOKEN: (language) => getTranslatedMessage('NO_AUTH_TOKEN', language), + INVALID_AUTH_TOKEN: (language) => getTranslatedMessage('INVALID_AUTH_TOKEN', language), + EXPIRED_AUTH_TOKEN: (language) => getTranslatedMessage('EXPIRED_AUTH_TOKEN', language), + NO_REFRESH_TOKEN: (language) => getTranslatedMessage('NO_REFRESH_TOKEN', language), + INVALID_REFRESH_TOKEN: (language) => getTranslatedMessage('INVALID_REFRESH_TOKEN', language), + EXPIRED_REFRESH_TOKEN: (language) => getTranslatedMessage('EXPIRED_REFRESH_TOKEN', language), + REQUEST_NEW_ACCESS_TOKEN: (language) => getTranslatedMessage('REQUEST_NEW_ACCESS_TOKEN', language), //Payload - INVALID_PAYLOAD: "Invalid payload", + INVALID_PAYLOAD: (language) => getTranslatedMessage('INVALID_PAYLOAD', language), //Ownership Middleware - VERIFY_OWNER_NOT_FOUND: "Document not found", - VERIFY_OWNER_UNAUTHORIZED: "Unauthorized access", + VERIFY_OWNER_NOT_FOUND: (language) => getTranslatedMessage('VERIFY_OWNER_NOT_FOUND', language), + VERIFY_OWNER_UNAUTHORIZED: (language) => getTranslatedMessage('VERIFY_OWNER_UNAUTHORIZED', language), //Permissions Middleware - INSUFFICIENT_PERMISSIONS: "Insufficient permissions", + INSUFFICIENT_PERMISSIONS: (language) => getTranslatedMessage('INSUFFICIENT_PERMISSIONS', language), //DB Errors - DB_USER_EXISTS: "User already exists", - DB_USER_NOT_FOUND: "User not found", - DB_TOKEN_NOT_FOUND: "Token not found", - DB_RESET_PASSWORD_BAD_MATCH: "New password must be different from old password", - DB_FIND_MONITOR_BY_ID: (monitorId) => `Monitor with id ${monitorId} not found`, - DB_DELETE_CHECKS: (monitorId) => `No checks found for monitor with id ${monitorId}`, + DB_USER_EXISTS: (language) => getTranslatedMessage('DB_USER_EXISTS', language), + DB_USER_NOT_FOUND: (language) => getTranslatedMessage('DB_USER_NOT_FOUND', language), + DB_TOKEN_NOT_FOUND: (language) => getTranslatedMessage('DB_TOKEN_NOT_FOUND', language), + DB_RESET_PASSWORD_BAD_MATCH: (language) => getTranslatedMessage('DB_RESET_PASSWORD_BAD_MATCH', language), + DB_FIND_MONITOR_BY_ID: (monitorId, language) => getTranslatedMessage('DB_FIND_MONITOR_BY_ID', language).replace('{monitorId}', monitorId), + DB_DELETE_CHECKS: (monitorId, language) => getTranslatedMessage('DB_DELETE_CHECKS', language).replace('{monitorId}', monitorId), //Auth errors - AUTH_INCORRECT_PASSWORD: "Incorrect password", - AUTH_UNAUTHORIZED: "Unauthorized access", + AUTH_INCORRECT_PASSWORD: (language) => getTranslatedMessage('AUTH_INCORRECT_PASSWORD', language), + AUTH_UNAUTHORIZED: (language) => getTranslatedMessage('AUTH_UNAUTHORIZED', language), // Monitor Errors - MONITOR_GET_BY_ID: "Monitor not found", - MONITOR_GET_BY_USER_ID: "No monitors found for user", + MONITOR_GET_BY_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_ID', language), + MONITOR_GET_BY_USER_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_USER_ID', language), // Job Queue Errors - JOB_QUEUE_WORKER_CLOSE: "Error closing worker", - JOB_QUEUE_DELETE_JOB: "Job not found in queue", - JOB_QUEUE_OBLITERATE: "Error obliterating queue", + JOB_QUEUE_WORKER_CLOSE: (language) => getTranslatedMessage('JOB_QUEUE_WORKER_CLOSE', language), + JOB_QUEUE_DELETE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_DELETE_JOB', language), + JOB_QUEUE_OBLITERATE: (language) => getTranslatedMessage('JOB_QUEUE_OBLITERATE', language), // PING Operations - PING_CANNOT_RESOLVE: "No response", + PING_CANNOT_RESOLVE: (language) => getTranslatedMessage('PING_CANNOT_RESOLVE', language), // Status Page Errors - STATUS_PAGE_NOT_FOUND: "Status page not found", - STATUS_PAGE_URL_NOT_UNIQUE: "Status page url must be unique", + STATUS_PAGE_NOT_FOUND: (language) => getTranslatedMessage('STATUS_PAGE_NOT_FOUND', language), + STATUS_PAGE_URL_NOT_UNIQUE: (language) => getTranslatedMessage('STATUS_PAGE_URL_NOT_UNIQUE', language), // Docker - DOCKER_FAIL: "Failed to fetch Docker container information", - DOCKER_NOT_FOUND: "Docker container not found", + DOCKER_FAIL: (language) => getTranslatedMessage('DOCKER_FAIL', language), + DOCKER_NOT_FOUND: (language) => getTranslatedMessage('DOCKER_NOT_FOUND', language), // Port - PORT_FAIL: "Failed to connect to port", + PORT_FAIL: (language) => getTranslatedMessage('PORT_FAIL', language), }; const successMessages = { //Alert Controller - ALERT_CREATE: "Alert created successfully", - ALERT_GET_BY_USER: "Got alerts successfully", - ALERT_GET_BY_MONITOR: "Got alerts by Monitor successfully", - ALERT_GET_BY_ID: "Got alert by Id successfully", - ALERT_EDIT: "Alert edited successfully", - ALERT_DELETE: "Alert deleted successfully", + ALERT_CREATE: (language) => getTranslatedMessage('ALERT_CREATE', language), + ALERT_GET_BY_USER: (language) => getTranslatedMessage('ALERT_GET_BY_USER', language), + ALERT_GET_BY_MONITOR: (language) => getTranslatedMessage('ALERT_GET_BY_MONITOR', language), + ALERT_GET_BY_ID: (language) => getTranslatedMessage('ALERT_GET_BY_ID', language), + ALERT_EDIT: (language) => getTranslatedMessage('ALERT_EDIT', language), + ALERT_DELETE: (language) => getTranslatedMessage('ALERT_DELETE', language), // Auth Controller - AUTH_CREATE_USER: "User created successfully", - AUTH_LOGIN_USER: "User logged in successfully", - AUTH_LOGOUT_USER: "User logged out successfully", - AUTH_UPDATE_USER: "User updated successfully", - AUTH_CREATE_RECOVERY_TOKEN: "Recovery token created successfully", - AUTH_VERIFY_RECOVERY_TOKEN: "Recovery token verified successfully", - AUTH_RESET_PASSWORD: "Password reset successfully", - AUTH_ADMIN_CHECK: "Admin check completed successfully", - AUTH_DELETE_USER: "User deleted successfully", - AUTH_TOKEN_REFRESHED: "Auth token is refreshed", - AUTH_GET_ALL_USERS: "Got all users successfully", + AUTH_CREATE_USER: (language) => getTranslatedMessage('AUTH_CREATE_USER', language), + AUTH_LOGIN_USER: (language) => getTranslatedMessage('AUTH_LOGIN_USER', language), + AUTH_LOGOUT_USER: (language) => getTranslatedMessage('AUTH_LOGOUT_USER', language), + AUTH_UPDATE_USER: (language) => getTranslatedMessage('AUTH_UPDATE_USER', language), + AUTH_CREATE_RECOVERY_TOKEN: (language) => getTranslatedMessage('AUTH_CREATE_RECOVERY_TOKEN', language), + AUTH_VERIFY_RECOVERY_TOKEN: (language) => getTranslatedMessage('AUTH_VERIFY_RECOVERY_TOKEN', language), + AUTH_RESET_PASSWORD: (language) => getTranslatedMessage('AUTH_RESET_PASSWORD', language), + AUTH_ADMIN_CHECK: (language) => getTranslatedMessage('AUTH_ADMIN_CHECK', language), + AUTH_DELETE_USER: (language) => getTranslatedMessage('AUTH_DELETE_USER', language), + AUTH_TOKEN_REFRESHED: (language) => getTranslatedMessage('AUTH_TOKEN_REFRESHED', language), + AUTH_GET_ALL_USERS: (language) => getTranslatedMessage('AUTH_GET_ALL_USERS', language), // Invite Controller - INVITE_ISSUED: "Invite sent successfully", - INVITE_VERIFIED: "Invite verified successfully", + INVITE_ISSUED: (language) => getTranslatedMessage('INVITE_ISSUED', language), + INVITE_VERIFIED: (language) => getTranslatedMessage('INVITE_VERIFIED', language), // Check Controller - CHECK_CREATE: "Check created successfully", - CHECK_GET: "Got checks successfully", - CHECK_DELETE: "Checks deleted successfully", - CHECK_UPDATE_TTL: "Checks TTL updated successfully", + CHECK_CREATE: (language) => getTranslatedMessage('CHECK_CREATE', language), + CHECK_GET: (language) => getTranslatedMessage('CHECK_GET', language), + CHECK_DELETE: (language) => getTranslatedMessage('CHECK_DELETE', language), + CHECK_UPDATE_TTL: (language) => getTranslatedMessage('CHECK_UPDATE_TTL', language), //Monitor Controller - MONITOR_GET_ALL: "Got all monitors successfully", - MONITOR_STATS_BY_ID: "Got monitor stats by Id successfully", - MONITOR_GET_BY_ID: "Got monitor by Id successfully", - MONITOR_GET_BY_TEAM_ID: "Got monitors by Team Id successfully", - MONITOR_GET_BY_USER_ID: (userId) => `Got monitor for ${userId} successfully"`, - MONITOR_CREATE: "Monitor created successfully", - MONITOR_DELETE: "Monitor deleted successfully", - MONITOR_EDIT: "Monitor edited successfully", - MONITOR_CERTIFICATE: "Got monitor certificate successfully", - MONITOR_DEMO_ADDED: "Successfully added demo monitors", + MONITOR_GET_ALL: (language) => getTranslatedMessage('MONITOR_GET_ALL', language), + MONITOR_STATS_BY_ID: (language) => getTranslatedMessage('MONITOR_STATS_BY_ID', language), + MONITOR_GET_BY_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_ID', language), + MONITOR_GET_BY_TEAM_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_TEAM_ID', language), + MONITOR_GET_BY_USER_ID: (userId, language) => getTranslatedMessage('MONITOR_GET_BY_USER_ID', language).replace('{userId}', userId), + MONITOR_CREATE: (language) => getTranslatedMessage('MONITOR_CREATE', language), + MONITOR_DELETE: (language) => getTranslatedMessage('MONITOR_DELETE', language), + MONITOR_EDIT: (language) => getTranslatedMessage('MONITOR_EDIT', language), + MONITOR_CERTIFICATE: (language) => getTranslatedMessage('MONITOR_CERTIFICATE', language), + MONITOR_DEMO_ADDED: (language) => getTranslatedMessage('MONITOR_DEMO_ADDED', language), // Queue Controller - QUEUE_GET_METRICS: "Got metrics successfully", - QUEUE_ADD_JOB: "Job added successfully", - QUEUE_OBLITERATE: "Queue obliterated", + QUEUE_GET_METRICS: (language) => getTranslatedMessage('QUEUE_GET_METRICS', language), + QUEUE_ADD_JOB: (language) => getTranslatedMessage('QUEUE_ADD_JOB', language), + QUEUE_OBLITERATE: (language) => getTranslatedMessage('QUEUE_OBLITERATE', language), //Job Queue - JOB_QUEUE_DELETE_JOB: "Job removed successfully", - JOB_QUEUE_OBLITERATE: "Queue OBLITERATED!!!", - JOB_QUEUE_PAUSE_JOB: "Job paused successfully", - JOB_QUEUE_RESUME_JOB: "Job resumed successfully", + JOB_QUEUE_DELETE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_DELETE_JOB', language), + JOB_QUEUE_OBLITERATE: (language) => getTranslatedMessage('JOB_QUEUE_OBLITERATE', language), + JOB_QUEUE_PAUSE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_PAUSE_JOB', language), + JOB_QUEUE_RESUME_JOB: (language) => getTranslatedMessage('JOB_QUEUE_RESUME_JOB', language), //Maintenance Window Controller - MAINTENANCE_WINDOW_GET_BY_ID: "Got Maintenance Window by Id successfully", - MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully", - MAINTENANCE_WINDOW_GET_BY_TEAM: "Got Maintenance Windows by Team successfully", - MAINTENANCE_WINDOW_DELETE: "Maintenance Window deleted successfully", - MAINTENANCE_WINDOW_EDIT: "Maintenance Window edited successfully", + MAINTENANCE_WINDOW_GET_BY_ID: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_GET_BY_ID', language), + MAINTENANCE_WINDOW_CREATE: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_CREATE', language), + MAINTENANCE_WINDOW_GET_BY_TEAM: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_GET_BY_TEAM', language), + MAINTENANCE_WINDOW_DELETE: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_DELETE', language), + MAINTENANCE_WINDOW_EDIT: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_EDIT', language), //Ping Operations - PING_SUCCESS: "Success", + PING_SUCCESS: (language) => getTranslatedMessage('PING_SUCCESS', language), // App Settings - GET_APP_SETTINGS: "Got app settings successfully", - UPDATE_APP_SETTINGS: "Updated app settings successfully", + GET_APP_SETTINGS: (language) => getTranslatedMessage('GET_APP_SETTINGS', language), + UPDATE_APP_SETTINGS: (language) => getTranslatedMessage('UPDATE_APP_SETTINGS', language), // Status Page STATUS_PAGE_BY_URL: "Got status page by url successfully", @@ -141,10 +150,10 @@ const successMessages = { STATUS_PAGE_UPDATE: "Status page updated successfully", STATUS_PAGE_BY_TEAM_ID: "Got status pages by team id successfully", // Docker - DOCKER_SUCCESS: "Docker container status fetched successfully", + DOCKER_SUCCESS: (language) => getTranslatedMessage('DOCKER_SUCCESS', language), // Port - PORT_SUCCESS: "Port connected successfully", + PORT_SUCCESS: (language) => getTranslatedMessage('PORT_SUCCESS', language), }; export { errorMessages, successMessages };