feat: i18n support for backend
This commit is contained in:
1 parent
0eceac1587
commit
10713428b9
34 files changed
+1106
-838
No files matched your search
@@ -76,7 +76,7 @@ class AuthController {
|
|||||||
// If superAdmin exists, a token should be attached to all further register requests
|
// If superAdmin exists, a token should be attached to all further register requests
|
||||||
const superAdminExists = await this.db.checkSuperadmin(req, res);
|
const superAdminExists = await this.db.checkSuperadmin(req, res);
|
||||||
if (superAdminExists) {
|
if (superAdminExists) {
|
||||||
await this.db.getInviteTokenAndDelete(inviteToken);
|
await this.db.getInviteTokenAndDelete(inviteToken, req.language);
|
||||||
} else {
|
} else {
|
||||||
// This is the first account, create JWT secret to use if one is not supplied by env
|
// This is the first account, create JWT secret to use if one is not supplied by env
|
||||||
const jwtSecret = crypto.randomBytes(64).toString("hex");
|
const jwtSecret = crypto.randomBytes(64).toString("hex");
|
||||||
@@ -85,7 +85,7 @@ class AuthController {
|
|||||||
|
|
||||||
const newUser = await this.db.insertUser({ ...req.body }, req.file);
|
const newUser = await this.db.insertUser({ ...req.body }, req.file);
|
||||||
logger.info({
|
logger.info({
|
||||||
message: successMessages.AUTH_CREATE_USER,
|
message: successMessages.AUTH_CREATE_USER(req.language),
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
details: newUser._id,
|
details: newUser._id,
|
||||||
});
|
});
|
||||||
@@ -116,7 +116,7 @@ class AuthController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
res.success({
|
res.success({
|
||||||
msg: successMessages.AUTH_CREATE_USER,
|
msg: successMessages.AUTH_CREATE_USER(req.language),
|
||||||
data: { user: newUser, token: token, refreshToken: refreshToken },
|
data: { user: newUser, token: token, refreshToken: refreshToken },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -148,12 +148,12 @@ class AuthController {
|
|||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
|
||||||
// Check if user exists
|
// Check if user exists
|
||||||
const user = await this.db.getUserByEmail(email);
|
const user = await this.db.getUserByEmail(email, req.language);
|
||||||
|
|
||||||
// Compare password
|
// Compare password
|
||||||
const match = await user.comparePassword(password);
|
const match = await user.comparePassword(password);
|
||||||
if (match !== true) {
|
if (match !== true) {
|
||||||
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
|
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD(req.language));
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
@@ -176,7 +176,7 @@ class AuthController {
|
|||||||
userWithoutPassword.avatarImage = user.avatarImage;
|
userWithoutPassword.avatarImage = user.avatarImage;
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_LOGIN_USER,
|
msg: successMessages.AUTH_LOGIN_USER(req.language),
|
||||||
data: {
|
data: {
|
||||||
user: userWithoutPassword,
|
user: userWithoutPassword,
|
||||||
token: token,
|
token: token,
|
||||||
@@ -206,7 +206,7 @@ class AuthController {
|
|||||||
|
|
||||||
if (!refreshToken) {
|
if (!refreshToken) {
|
||||||
// No refresh token provided
|
// 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.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "refreshAuthToken";
|
error.method = "refreshAuthToken";
|
||||||
@@ -243,7 +243,7 @@ class AuthController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_TOKEN_REFRESHED,
|
msg: successMessages.AUTH_TOKEN_REFRESHED(req.language),
|
||||||
data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken },
|
data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -276,7 +276,7 @@ class AuthController {
|
|||||||
|
|
||||||
// TODO is this neccessary any longer? Verify ownership middleware should handle this
|
// TODO is this neccessary any longer? Verify ownership middleware should handle this
|
||||||
if (req.params.userId !== req.user._id.toString()) {
|
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.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -294,13 +294,13 @@ class AuthController {
|
|||||||
// Add user email to body for DB operation
|
// Add user email to body for DB operation
|
||||||
req.body.email = email;
|
req.body.email = email;
|
||||||
// Get user
|
// Get user
|
||||||
const user = await this.db.getUserByEmail(email);
|
const user = await this.db.getUserByEmail(email, req.language);
|
||||||
// Compare passwords
|
// Compare passwords
|
||||||
const match = await user.comparePassword(req.body.password);
|
const match = await user.comparePassword(req.body.password);
|
||||||
// If not a match, throw a 403
|
// If not a match, throw a 403
|
||||||
// 403 instead of 401 to avoid triggering axios interceptor
|
// 403 instead of 401 to avoid triggering axios interceptor
|
||||||
if (!match) {
|
if (!match) {
|
||||||
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
|
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD(req.language));
|
||||||
error.status = 403;
|
error.status = 403;
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
@@ -311,7 +311,7 @@ class AuthController {
|
|||||||
|
|
||||||
const updatedUser = await this.db.updateUser(req, res);
|
const updatedUser = await this.db.updateUser(req, res);
|
||||||
res.success({
|
res.success({
|
||||||
msg: successMessages.AUTH_UPDATE_USER,
|
msg: successMessages.AUTH_UPDATE_USER(req.language),
|
||||||
data: updatedUser,
|
data: updatedUser,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -333,7 +333,7 @@ class AuthController {
|
|||||||
const superAdminExists = await this.db.checkSuperadmin(req, res);
|
const superAdminExists = await this.db.checkSuperadmin(req, res);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_ADMIN_EXISTS,
|
msg: successMessages.AUTH_ADMIN_EXISTS(req.language),
|
||||||
data: superAdminExists,
|
data: superAdminExists,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -362,7 +362,7 @@ class AuthController {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { email } = req.body;
|
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 recoveryToken = await this.db.requestRecoveryToken(req, res);
|
||||||
const name = user.firstName;
|
const name = user.firstName;
|
||||||
const { clientHost } = this.settingsService.getSettings();
|
const { clientHost } = this.settingsService.getSettings();
|
||||||
@@ -379,7 +379,7 @@ class AuthController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
|
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(req.language),
|
||||||
data: msgId,
|
data: msgId,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -410,7 +410,7 @@ class AuthController {
|
|||||||
await this.db.validateRecoveryToken(req, res);
|
await this.db.validateRecoveryToken(req, res);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
|
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController"));
|
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController"));
|
||||||
@@ -443,7 +443,7 @@ class AuthController {
|
|||||||
const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
|
const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_RESET_PASSWORD,
|
msg: successMessages.AUTH_RESET_PASSWORD(req.language),
|
||||||
data: { user, token },
|
data: { user, token },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -467,7 +467,7 @@ class AuthController {
|
|||||||
const { email } = decodedToken;
|
const { email } = decodedToken;
|
||||||
|
|
||||||
// Check if the user exists
|
// 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
|
// 1. Find all the monitors associated with the team ID if superadmin
|
||||||
|
|
||||||
const result = await this.db.getMonitorsByTeamId({
|
const result = await this.db.getMonitorsByTeamId({
|
||||||
@@ -494,10 +494,10 @@ class AuthController {
|
|||||||
await this.db.deleteMonitorsByUserId(user._id);
|
await this.db.deleteMonitorsByUserId(user._id);
|
||||||
}
|
}
|
||||||
// 6. Delete the user by id
|
// 6. Delete the user by id
|
||||||
await this.db.deleteUser(user._id);
|
await this.db.deleteUser(user._id, req.language);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_DELETE_USER,
|
msg: successMessages.AUTH_DELETE_USER(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteUserController"));
|
next(handleError(error, SERVICE_NAME, "deleteUserController"));
|
||||||
@@ -509,7 +509,7 @@ class AuthController {
|
|||||||
const allUsers = await this.db.getAllUsers(req, res);
|
const allUsers = await this.db.getAllUsers(req, res);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.AUTH_GET_ALL_USERS,
|
msg: successMessages.AUTH_GET_ALL_USERS(req.language),
|
||||||
data: allUsers,
|
data: allUsers,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class CheckController {
|
|||||||
const check = await this.db.createCheck(checkData);
|
const check = await this.db.createCheck(checkData);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_CREATE,
|
msg: successMessages.CHECK_CREATE(req.language),
|
||||||
data: check,
|
data: check,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -57,7 +57,7 @@ class CheckController {
|
|||||||
const result = await this.db.getChecksByMonitor(req);
|
const result = await this.db.getChecksByMonitor(req);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_GET,
|
msg: successMessages.CHECK_GET(req.language),
|
||||||
data: result,
|
data: result,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -77,7 +77,7 @@ class CheckController {
|
|||||||
const checkData = await this.db.getChecksByTeam(req);
|
const checkData = await this.db.getChecksByTeam(req);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_GET,
|
msg: successMessages.CHECK_GET(req.language),
|
||||||
data: checkData,
|
data: checkData,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -97,7 +97,7 @@ class CheckController {
|
|||||||
const deletedCount = await this.db.deleteChecks(req.params.monitorId);
|
const deletedCount = await this.db.deleteChecks(req.params.monitorId);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_DELETE,
|
msg: successMessages.CHECK_DELETE(req.language),
|
||||||
data: { deletedCount },
|
data: { deletedCount },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -117,7 +117,7 @@ class CheckController {
|
|||||||
const deletedCount = await this.db.deleteChecksByTeamId(req.params.teamId);
|
const deletedCount = await this.db.deleteChecksByTeamId(req.params.teamId);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_DELETE,
|
msg: successMessages.CHECK_DELETE(req.language),
|
||||||
data: { deletedCount },
|
data: { deletedCount },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -144,7 +144,7 @@ class CheckController {
|
|||||||
await this.db.updateChecksTTL(teamId, ttl);
|
await this.db.updateChecksTTL(teamId, ttl);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.CHECK_UPDATE_TTL,
|
msg: successMessages.CHECK_UPDATE_TTL(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "updateTTL"));
|
next(handleError(error, SERVICE_NAME, "updateTTL"));
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class InviteController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.INVITE_ISSUED,
|
msg: successMessages.INVITE_ISSUED(req.language),
|
||||||
data: inviteToken,
|
data: inviteToken,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -83,10 +83,10 @@ class InviteController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const invite = await this.db.getInviteToken(req.body.token);
|
const invite = await this.db.getInviteToken(req.body.token, req.language);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.INVITE_VERIFIED,
|
msg: successMessages.INVITE_VERIFIED(req.language),
|
||||||
data: invite,
|
data: invite,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class MaintenanceWindowController {
|
|||||||
await Promise.all(dbTransactions);
|
await Promise.all(dbTransactions);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "createMaintenanceWindow"));
|
next(handleError(error, SERVICE_NAME, "createMaintenanceWindow"));
|
||||||
@@ -63,7 +63,7 @@ class MaintenanceWindowController {
|
|||||||
const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id);
|
const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language),
|
||||||
data: maintenanceWindow,
|
data: maintenanceWindow,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -89,7 +89,7 @@ class MaintenanceWindowController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language),
|
||||||
data: maintenanceWindows,
|
data: maintenanceWindows,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -111,7 +111,7 @@ class MaintenanceWindowController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER(req.language),
|
||||||
data: maintenanceWindows,
|
data: maintenanceWindows,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -129,7 +129,7 @@ class MaintenanceWindowController {
|
|||||||
try {
|
try {
|
||||||
await this.db.deleteMaintenanceWindowById(req.params.id);
|
await this.db.deleteMaintenanceWindowById(req.params.id);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
|
msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow"));
|
next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow"));
|
||||||
@@ -150,7 +150,7 @@ class MaintenanceWindowController {
|
|||||||
req.body
|
req.body
|
||||||
);
|
);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
|
msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language),
|
||||||
data: editedMaintenanceWindow,
|
data: editedMaintenanceWindow,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitors = await this.db.getAllMonitors();
|
const monitors = await this.db.getAllMonitors();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_ALL,
|
msg: successMessages.MONITOR_GET_ALL(req.language),
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -64,7 +64,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitors = await this.db.getAllMonitorsWithUptimeStats();
|
const monitors = await this.db.getAllMonitorsWithUptimeStats();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_ALL,
|
msg: successMessages.MONITOR_GET_ALL(req.language),
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -76,7 +76,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitor = await this.db.getUptimeDetailsById(req);
|
const monitor = await this.db.getUptimeDetailsById(req);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID(req.language),
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -105,7 +105,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitorStats = await this.db.getMonitorStatsById(req);
|
const monitorStats = await this.db.getMonitorStatsById(req);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_STATS_BY_ID,
|
msg: successMessages.MONITOR_STATS_BY_ID(req.language),
|
||||||
data: monitorStats,
|
data: monitorStats,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -133,7 +133,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitor = await this.db.getHardwareDetailsById(req);
|
const monitor = await this.db.getHardwareDetailsById(req);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID(req.language),
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -154,7 +154,7 @@ class MonitorController {
|
|||||||
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_CERTIFICATE,
|
msg: successMessages.MONITOR_CERTIFICATE(req.language),
|
||||||
data: {
|
data: {
|
||||||
certificateDate: new Date(certificate.validTo),
|
certificateDate: new Date(certificate.validTo),
|
||||||
},
|
},
|
||||||
@@ -187,7 +187,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitor = await this.db.getMonitorById(req.params.monitorId);
|
const monitor = await this.db.getMonitorById(req.params.monitorId);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_BY_ID,
|
msg: successMessages.MONITOR_GET_BY_ID(req.language),
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -231,7 +231,7 @@ class MonitorController {
|
|||||||
// Add monitor to job queue
|
// Add monitor to job queue
|
||||||
this.jobQueue.addJob(monitor._id, monitor);
|
this.jobQueue.addJob(monitor._id, monitor);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_CREATE,
|
msg: successMessages.MONITOR_CREATE(req.language),
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -309,7 +309,7 @@ class MonitorController {
|
|||||||
stack: error.stack,
|
stack: error.stack,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return res.success({ msg: successMessages.MONITOR_DELETE });
|
return res.success({ msg: successMessages.MONITOR_DELETE(req.language) });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
next(handleError(error, SERVICE_NAME, "deleteMonitor"));
|
||||||
}
|
}
|
||||||
@@ -390,10 +390,10 @@ class MonitorController {
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
notifications &&
|
notifications &&
|
||||||
notifications.map(async (notification) => {
|
notifications.map(async (notification) => {
|
||||||
notification.monitorId = editedMonitor._id;
|
notification.monitorId = editedMonitor._id;
|
||||||
await this.db.createNotification(notification);
|
await this.db.createNotification(notification);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Delete the old job(editedMonitor has the same ID as the old monitor)
|
// 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
|
// Add the new job back to the queue
|
||||||
await this.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
await this.jobQueue.addJob(editedMonitor._id, editedMonitor);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_EDIT,
|
msg: successMessages.MONITOR_EDIT(req.language),
|
||||||
data: editedMonitor,
|
data: editedMonitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -438,8 +438,8 @@ class MonitorController {
|
|||||||
monitor.save();
|
monitor.save();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: monitor.isActive
|
msg: monitor.isActive
|
||||||
? successMessages.MONITOR_RESUME
|
? successMessages.MONITOR_RESUME(req.language)
|
||||||
: successMessages.MONITOR_PAUSE,
|
: successMessages.MONITOR_PAUSE(req.language),
|
||||||
data: monitor,
|
data: monitor,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -469,7 +469,7 @@ class MonitorController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_DEMO_ADDED,
|
msg: successMessages.MONITOR_DEMO_ADDED(req.language),
|
||||||
data: demoMonitors.length,
|
data: demoMonitors.length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -488,7 +488,7 @@ class MonitorController {
|
|||||||
try {
|
try {
|
||||||
const monitors = await this.db.getMonitorsByTeamId(req);
|
const monitors = await this.db.getMonitorsByTeamId(req);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.MONITOR_GET_BY_TEAM_ID,
|
msg: successMessages.MONITOR_GET_BY_TEAM_ID(req.language),
|
||||||
data: monitors,
|
data: monitors,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class JobQueueController {
|
|||||||
try {
|
try {
|
||||||
const metrics = await this.jobQueue.getMetrics();
|
const metrics = await this.jobQueue.getMetrics();
|
||||||
res.success({
|
res.success({
|
||||||
msg: successMessages.QUEUE_GET_METRICS,
|
msg: successMessages.QUEUE_GET_METRICS(req.language),
|
||||||
data: metrics,
|
data: metrics,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -25,7 +25,7 @@ class JobQueueController {
|
|||||||
try {
|
try {
|
||||||
const jobs = await this.jobQueue.getJobStats();
|
const jobs = await this.jobQueue.getJobStats();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.QUEUE_GET_METRICS,
|
msg: successMessages.QUEUE_GET_METRICS(req.language),
|
||||||
data: jobs,
|
data: jobs,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -38,7 +38,7 @@ class JobQueueController {
|
|||||||
try {
|
try {
|
||||||
await this.jobQueue.addJob(Math.random().toString(36).substring(7));
|
await this.jobQueue.addJob(Math.random().toString(36).substring(7));
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.QUEUE_ADD_JOB,
|
msg: successMessages.QUEUE_ADD_JOB(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "addJob"));
|
next(handleError(error, SERVICE_NAME, "addJob"));
|
||||||
@@ -50,7 +50,7 @@ class JobQueueController {
|
|||||||
try {
|
try {
|
||||||
await this.jobQueue.obliterate();
|
await this.jobQueue.obliterate();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.QUEUE_OBLITERATE,
|
msg: successMessages.QUEUE_OBLITERATE(req.language),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "obliterateQueue"));
|
next(handleError(error, SERVICE_NAME, "obliterateQueue"));
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class SettingsController {
|
|||||||
const settings = { ...(await this.settingsService.getSettings()) };
|
const settings = { ...(await this.settingsService.getSettings()) };
|
||||||
delete settings.jwtSecret;
|
delete settings.jwtSecret;
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.GET_APP_SETTINGS,
|
msg: successMessages.GET_APP_SETTINGS(req.language),
|
||||||
data: settings,
|
data: settings,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -35,7 +35,7 @@ class SettingsController {
|
|||||||
const updatedSettings = { ...(await this.settingsService.reloadSettings()) };
|
const updatedSettings = { ...(await this.settingsService.reloadSettings()) };
|
||||||
delete updatedSettings.jwtSecret;
|
delete updatedSettings.jwtSecret;
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.UPDATE_APP_SETTINGS,
|
msg: successMessages.UPDATE_APP_SETTINGS(req.language),
|
||||||
data: updatedSettings,
|
data: updatedSettings,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class StatusPageController {
|
|||||||
try {
|
try {
|
||||||
const statusPage = await this.db.createStatusPage(req.body, req.file);
|
const statusPage = await this.db.createStatusPage(req.body, req.file);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.STATUS_PAGE_CREATE,
|
msg: successMessages.STATUS_PAGE_CREATE(req.language),
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -63,7 +63,7 @@ class StatusPageController {
|
|||||||
try {
|
try {
|
||||||
const statusPage = await this.db.getStatusPage();
|
const statusPage = await this.db.getStatusPage();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.STATUS_PAGE,
|
msg: successMessages.STATUS_PAGE_BY_URL(req.language),
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -41,13 +41,13 @@ const requestInviteToken = async (userData) => {
|
|||||||
* @returns {Promise<InviteToken>} The invite token data.
|
* @returns {Promise<InviteToken>} The invite token data.
|
||||||
* @throws {Error} If the invite token is not found or there is another error.
|
* @throws {Error} If the invite token is not found or there is another error.
|
||||||
*/
|
*/
|
||||||
const getInviteToken = async (token) => {
|
const getInviteToken = async (token, language) => {
|
||||||
try {
|
try {
|
||||||
const invite = await InviteToken.findOne({
|
const invite = await InviteToken.findOne({
|
||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
if (invite === null) {
|
if (invite === null) {
|
||||||
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND);
|
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND(language));
|
||||||
}
|
}
|
||||||
return invite;
|
return invite;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -67,13 +67,13 @@ const getInviteToken = async (token) => {
|
|||||||
* @returns {Promise<InviteToken>} The invite token data.
|
* @returns {Promise<InviteToken>} The invite token data.
|
||||||
* @throws {Error} If the invite token is not found or there is another error.
|
* @throws {Error} If the invite token is not found or there is another error.
|
||||||
*/
|
*/
|
||||||
const getInviteTokenAndDelete = async (token) => {
|
const getInviteTokenAndDelete = async (token, language) => {
|
||||||
try {
|
try {
|
||||||
const invite = await InviteToken.findOneAndDelete({
|
const invite = await InviteToken.findOneAndDelete({
|
||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
if (invite === null) {
|
if (invite === null) {
|
||||||
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND);
|
throw new Error(errorMessages.AUTH_INVITE_NOT_FOUND(language));
|
||||||
}
|
}
|
||||||
return invite;
|
return invite;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ const calculateGroupStats = (group) => {
|
|||||||
avgResponseTime:
|
avgResponseTime:
|
||||||
checksWithResponseTime.length > 0
|
checksWithResponseTime.length > 0
|
||||||
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
|
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
|
||||||
checksWithResponseTime.length
|
checksWithResponseTime.length
|
||||||
: 0,
|
: 0,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -330,7 +330,7 @@ const getUptimeDetailsById = async (req) => {
|
|||||||
const { monitorId } = req.params;
|
const { monitorId } = req.params;
|
||||||
const monitor = await Monitor.findById(monitorId);
|
const monitor = await Monitor.findById(monitorId);
|
||||||
if (monitor === null || monitor === undefined) {
|
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;
|
const { dateRange, normalize } = req.query;
|
||||||
@@ -601,78 +601,78 @@ const getMonitorsByTeamId = async (req) => {
|
|||||||
filteredMonitors: [
|
filteredMonitors: [
|
||||||
...(filter !== undefined
|
...(filter !== undefined
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$or: [
|
$or: [
|
||||||
{ name: { $regex: filter, $options: "i" } },
|
{ name: { $regex: filter, $options: "i" } },
|
||||||
{ url: { $regex: filter, $options: "i" } },
|
{ url: { $regex: filter, $options: "i" } },
|
||||||
],
|
],
|
||||||
},
|
|
||||||
},
|
},
|
||||||
]
|
},
|
||||||
|
]
|
||||||
: []),
|
: []),
|
||||||
{ $sort: sort },
|
{ $sort: sort },
|
||||||
{ $skip: skip },
|
{ $skip: skip },
|
||||||
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
|
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
|
||||||
...(limit
|
...(limit
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "checks",
|
from: "checks",
|
||||||
let: { monitorId: "$_id" },
|
let: { monitorId: "$_id" },
|
||||||
pipeline: [
|
pipeline: [
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{ $sort: { createdAt: -1 } },
|
},
|
||||||
...(limit ? [{ $limit: limit }] : []),
|
{ $sort: { createdAt: -1 } },
|
||||||
],
|
...(limit ? [{ $limit: limit }] : []),
|
||||||
as: "standardchecks",
|
],
|
||||||
},
|
as: "standardchecks",
|
||||||
},
|
},
|
||||||
]
|
},
|
||||||
|
]
|
||||||
: []),
|
: []),
|
||||||
...(limit
|
...(limit
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "pagespeedchecks",
|
from: "pagespeedchecks",
|
||||||
let: { monitorId: "$_id" },
|
let: { monitorId: "$_id" },
|
||||||
pipeline: [
|
pipeline: [
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{ $sort: { createdAt: -1 } },
|
},
|
||||||
...(limit ? [{ $limit: limit }] : []),
|
{ $sort: { createdAt: -1 } },
|
||||||
],
|
...(limit ? [{ $limit: limit }] : []),
|
||||||
as: "pagespeedchecks",
|
],
|
||||||
},
|
as: "pagespeedchecks",
|
||||||
},
|
},
|
||||||
]
|
},
|
||||||
|
]
|
||||||
: []),
|
: []),
|
||||||
...(limit
|
...(limit
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: "hardwarechecks",
|
from: "hardwarechecks",
|
||||||
let: { monitorId: "$_id" },
|
let: { monitorId: "$_id" },
|
||||||
pipeline: [
|
pipeline: [
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{ $sort: { createdAt: -1 } },
|
},
|
||||||
...(limit ? [{ $limit: limit }] : []),
|
{ $sort: { createdAt: -1 } },
|
||||||
],
|
...(limit ? [{ $limit: limit }] : []),
|
||||||
as: "hardwarechecks",
|
],
|
||||||
},
|
as: "hardwarechecks",
|
||||||
},
|
},
|
||||||
]
|
},
|
||||||
|
]
|
||||||
: []),
|
: []),
|
||||||
...(limit
|
...(limit
|
||||||
? [
|
? [
|
||||||
@@ -787,7 +787,7 @@ const deleteMonitor = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
||||||
if (!monitor) {
|
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;
|
return monitor;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ const validateRecoveryToken = async (req, res) => {
|
|||||||
if (recoveryToken !== null) {
|
if (recoveryToken !== null) {
|
||||||
return recoveryToken;
|
return recoveryToken;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(errorMessages.DB_TOKEN_NOT_FOUND);
|
throw new Error(errorMessages.DB_TOKEN_NOT_FOUND(req.language));
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -57,12 +57,12 @@ const resetPassword = async (req, res) => {
|
|||||||
const user = await UserModel.findOne({ email: recoveryToken.email });
|
const user = await UserModel.findOne({ email: recoveryToken.email });
|
||||||
|
|
||||||
if (user === null) {
|
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);
|
const match = await user.comparePassword(newPassword);
|
||||||
if (match === true) {
|
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;
|
user.password = newPassword;
|
||||||
|
|||||||
@@ -69,13 +69,13 @@ const insertUser = async (
|
|||||||
* @returns {Promise<UserModel>}
|
* @returns {Promise<UserModel>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const getUserByEmail = async (email) => {
|
const getUserByEmail = async (email, language) => {
|
||||||
try {
|
try {
|
||||||
// Need the password to be able to compare, removed .select()
|
// Need the password to be able to compare, removed .select()
|
||||||
// We can strip the hash before returning the user
|
// We can strip the hash before returning the user
|
||||||
const user = await UserModel.findOne({ email: email }).select("-profileImage");
|
const user = await UserModel.findOne({ email: email }).select("-profileImage");
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error(errorMessages.DB_USER_NOT_FOUND);
|
throw new Error(errorMessages.DB_USER_NOT_FOUND(language));
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -149,11 +149,11 @@ const updateUser = async (
|
|||||||
* @returns {Promise<UserModel>}
|
* @returns {Promise<UserModel>}
|
||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
const deleteUser = async (userId) => {
|
const deleteUser = async (userId, language) => {
|
||||||
try {
|
try {
|
||||||
const deletedUser = await UserModel.findByIdAndDelete(userId);
|
const deletedUser = await UserModel.findByIdAndDelete(userId);
|
||||||
if (!deletedUser) {
|
if (!deletedUser) {
|
||||||
throw new Error(errorMessages.DB_USER_NOT_FOUND);
|
throw new Error(errorMessages.DB_USER_NOT_FOUND(language));
|
||||||
}
|
}
|
||||||
return deletedUser;
|
return deletedUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
+11
-4
@@ -74,6 +74,9 @@ import MongoDB from "./db/mongo/MongoDB.js";
|
|||||||
|
|
||||||
import IORedis from "ioredis";
|
import IORedis from "ioredis";
|
||||||
|
|
||||||
|
import TranslationService from './service/translationService.js';
|
||||||
|
import languageMiddleware from './middleware/languageMiddleware.js';
|
||||||
|
|
||||||
const SERVICE_NAME = "Server";
|
const SERVICE_NAME = "Server";
|
||||||
const SHUTDOWN_TIMEOUT = 1000;
|
const SHUTDOWN_TIMEOUT = 1000;
|
||||||
let isShuttingDown = false;
|
let isShuttingDown = false;
|
||||||
@@ -175,6 +178,7 @@ const startApp = async () => {
|
|||||||
const networkService = new NetworkService(axios, ping, logger, http, Docker, net);
|
const networkService = new NetworkService(axios, ping, logger, http, Docker, net);
|
||||||
const statusService = new StatusService(db, logger);
|
const statusService = new StatusService(db, logger);
|
||||||
const notificationService = new NotificationService(emailService, db, logger);
|
const notificationService = new NotificationService(emailService, db, logger);
|
||||||
|
const translationService = new TranslationService(logger);
|
||||||
|
|
||||||
const jobQueue = new JobQueue(
|
const jobQueue = new JobQueue(
|
||||||
db,
|
db,
|
||||||
@@ -195,6 +199,11 @@ const startApp = async () => {
|
|||||||
ServiceRegistry.register(NetworkService.SERVICE_NAME, networkService);
|
ServiceRegistry.register(NetworkService.SERVICE_NAME, networkService);
|
||||||
ServiceRegistry.register(StatusService.SERVICE_NAME, statusService);
|
ServiceRegistry.register(StatusService.SERVICE_NAME, statusService);
|
||||||
ServiceRegistry.register(NotificationService.SERVICE_NAME, notificationService);
|
ServiceRegistry.register(NotificationService.SERVICE_NAME, notificationService);
|
||||||
|
ServiceRegistry.register(TranslationService.SERVICE_NAME, translationService);
|
||||||
|
|
||||||
|
|
||||||
|
await translationService.initialize();
|
||||||
|
|
||||||
server = app.listen(PORT, () => {
|
server = app.listen(PORT, () => {
|
||||||
logger.info({ message: `server started on port:${PORT}` });
|
logger.info({ message: `server started on port:${PORT}` });
|
||||||
});
|
});
|
||||||
@@ -267,12 +276,10 @@ const startApp = async () => {
|
|||||||
// Init job queue
|
// Init job queue
|
||||||
await jobQueue.initJobQueue();
|
await jobQueue.initJobQueue();
|
||||||
// Middleware
|
// Middleware
|
||||||
app.use(
|
app.use(cors());
|
||||||
cors()
|
|
||||||
//We will add configuration later
|
|
||||||
);
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
|
app.use(languageMiddleware);
|
||||||
// Swagger UI
|
// Swagger UI
|
||||||
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openApiSpec));
|
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openApiSpec));
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const isAllowed = (allowedRoles) => {
|
|||||||
|
|
||||||
// If no token is pressent, return an error
|
// If no token is pressent, return an error
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const error = new Error(errorMessages.NO_AUTH_TOKEN);
|
const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language));
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -21,7 +21,7 @@ const isAllowed = (allowedRoles) => {
|
|||||||
|
|
||||||
// If the token is improperly formatted, return an error
|
// If the token is improperly formatted, return an error
|
||||||
if (!token.startsWith(TOKEN_PREFIX)) {
|
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.status = 400;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -41,7 +41,7 @@ const isAllowed = (allowedRoles) => {
|
|||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const error = new Error(errorMessages.INSUFFICIENT_PERMISSIONS);
|
const error = new Error(errorMessages.INSUFFICIENT_PERMISSIONS(req.language));
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -17,7 +17,7 @@ const verifyJWT = (req, res, next) => {
|
|||||||
const token = req.headers["authorization"];
|
const token = req.headers["authorization"];
|
||||||
// Make sure a token is provided
|
// Make sure a token is provided
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const error = new Error(errorMessages.NO_AUTH_TOKEN);
|
const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language));
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -25,7 +25,7 @@ const verifyJWT = (req, res, next) => {
|
|||||||
}
|
}
|
||||||
// Make sure it is properly formatted
|
// Make sure it is properly formatted
|
||||||
if (!token.startsWith(TOKEN_PREFIX)) {
|
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.status = 400;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "verifyJWT";
|
error.method = "verifyJWT";
|
||||||
@@ -43,7 +43,7 @@ const verifyJWT = (req, res, next) => {
|
|||||||
handleExpiredJwtToken(req, res, next);
|
handleExpiredJwtToken(req, res, next);
|
||||||
} else {
|
} else {
|
||||||
// Invalid token (signature or token altered or other issue)
|
// 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 });
|
return res.status(401).json({ success: false, msg: errorMessage });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -60,7 +60,7 @@ function handleExpiredJwtToken(req, res, next) {
|
|||||||
|
|
||||||
if (!refreshToken) {
|
if (!refreshToken) {
|
||||||
// No refresh token provided
|
// 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.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "handleExpiredJwtToken";
|
error.method = "handleExpiredJwtToken";
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const verifyOwnership = (Model, paramName) => {
|
|||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "verifyOwnership",
|
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;
|
error.status = 404;
|
||||||
throw error;
|
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
|
// Special case for User model, as it will not have a `userId` field as other docs will
|
||||||
if (Model.modelName === "User") {
|
if (Model.modelName === "User") {
|
||||||
if (userId.toString() !== doc._id.toString()) {
|
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;
|
error.status = 403;
|
||||||
throw error;
|
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 the userID does not match the document's userID, return a 403 error
|
||||||
if (userId.toString() !== doc.userId.toString()) {
|
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;
|
error.status = 403;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const verifySuperAdmin = (req, res, next) => {
|
|||||||
const token = req.headers["authorization"];
|
const token = req.headers["authorization"];
|
||||||
// Make sure a token is provided
|
// Make sure a token is provided
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const error = new Error(errorMessages.NO_AUTH_TOKEN);
|
const error = new Error(errorMessages.NO_AUTH_TOKEN(req.language));
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -25,7 +25,7 @@ const verifySuperAdmin = (req, res, next) => {
|
|||||||
}
|
}
|
||||||
// Make sure it is properly formatted
|
// Make sure it is properly formatted
|
||||||
if (!token.startsWith(TOKEN_PREFIX)) {
|
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.status = 400;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "verifySuperAdmin";
|
error.method = "verifySuperAdmin";
|
||||||
|
|||||||
@@ -455,7 +455,7 @@ class NewJobQueue {
|
|||||||
|
|
||||||
if (wasDeleted === true) {
|
if (wasDeleted === true) {
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
message: successMessages.JOB_QUEUE_DELETE_JOB,
|
message: successMessages.JOB_QUEUE_DELETE_JOB('en'),
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "deleteJob",
|
method: "deleteJob",
|
||||||
details: `Deleted job ${monitor._id}`,
|
details: `Deleted job ${monitor._id}`,
|
||||||
@@ -587,7 +587,7 @@ class NewJobQueue {
|
|||||||
|
|
||||||
const metrics = await this.getMetrics();
|
const metrics = await this.getMetrics();
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
message: successMessages.JOB_QUEUE_OBLITERATE,
|
message: successMessages.JOB_QUEUE_OBLITERATE('en'),
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "obliterate",
|
method: "obliterate",
|
||||||
details: metrics,
|
details: metrics,
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class NetworkService {
|
|||||||
|
|
||||||
pingResponse.code = 200;
|
pingResponse.code = 200;
|
||||||
pingResponse.status = response.alive;
|
pingResponse.status = response.alive;
|
||||||
pingResponse.message = successMessages.PING_SUCCESS;
|
pingResponse.message = successMessages.PING_SUCCESS('en');
|
||||||
return pingResponse;
|
return pingResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = this.SERVICE_NAME;
|
error.service = this.SERVICE_NAME;
|
||||||
@@ -240,7 +240,7 @@ class NetworkService {
|
|||||||
const containers = await docker.listContainers({ all: true });
|
const containers = await docker.listContainers({ all: true });
|
||||||
const containerExists = containers.some((c) => c.Id.startsWith(job.data.url));
|
const containerExists = containers.some((c) => c.Id.startsWith(job.data.url));
|
||||||
if (!containerExists) {
|
if (!containerExists) {
|
||||||
throw new Error(errorMessages.DOCKER_NOT_FOUND);
|
throw new Error(errorMessages.DOCKER_NOT_FOUND('en'));
|
||||||
}
|
}
|
||||||
const container = docker.getContainer(job.data.url);
|
const container = docker.getContainer(job.data.url);
|
||||||
|
|
||||||
@@ -262,7 +262,7 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
dockerResponse.status = response?.State?.Status === "running" ? true : false;
|
dockerResponse.status = response?.State?.Status === "running" ? true : false;
|
||||||
dockerResponse.code = 200;
|
dockerResponse.code = 200;
|
||||||
dockerResponse.message = successMessages.DOCKER_SUCCESS;
|
dockerResponse.message = successMessages.DOCKER_SUCCESS('en');
|
||||||
return dockerResponse;
|
return dockerResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = this.SERVICE_NAME;
|
error.service = this.SERVICE_NAME;
|
||||||
@@ -316,7 +316,7 @@ class NetworkService {
|
|||||||
|
|
||||||
portResponse.status = response.success;
|
portResponse.status = response.success;
|
||||||
portResponse.code = 200;
|
portResponse.code = 200;
|
||||||
portResponse.message = successMessages.PORT_SUCCESS;
|
portResponse.message = successMessages.PORT_SUCCESS('en');
|
||||||
return portResponse;
|
return portResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = this.SERVICE_NAME;
|
error.service = this.SERVICE_NAME;
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -18,14 +18,16 @@ import { getTokenFromHeaders, tokenType } from "../../utils/utils.js";
|
|||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
import e from "cors";
|
import e from "cors";
|
||||||
|
|
||||||
describe("Auth Controller - issueToken", function() {
|
const mockLanguage = 'en';
|
||||||
|
|
||||||
|
describe("Auth Controller - issueToken", function () {
|
||||||
let stub;
|
let stub;
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore(); // Restore stubs after each test
|
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");
|
const error = new Error("jwt.sign error");
|
||||||
stub = sinon.stub(jwt, "sign").throws(error);
|
stub = sinon.stub(jwt, "sign").throws(error);
|
||||||
const payload = { id: "123" };
|
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 payload = { id: "123" };
|
||||||
const appSettings = { jwtSecret: "my_secret" };
|
const appSettings = { jwtSecret: "my_secret" };
|
||||||
const expectedToken = "mockToken";
|
const expectedToken = "mockToken";
|
||||||
@@ -45,7 +47,7 @@ describe("Auth Controller - issueToken", function() {
|
|||||||
expect(token).to.equal(expectedToken);
|
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 payload = { id: "123" };
|
||||||
const appSettings = { jwtSecret: "my_secret", jwtTTL: "1s" };
|
const appSettings = { jwtSecret: "my_secret", jwtTTL: "1s" };
|
||||||
const expectedToken = "mockToken";
|
const expectedToken = "mockToken";
|
||||||
@@ -55,7 +57,7 @@ describe("Auth Controller - issueToken", function() {
|
|||||||
expect(token).to.equal(expectedToken);
|
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 payload = {};
|
||||||
const appSettings = { refreshTokenSecret: "my_refresh_secret" };
|
const appSettings = { refreshTokenSecret: "my_refresh_secret" };
|
||||||
const expectedToken = "mockRefreshToken";
|
const expectedToken = "mockRefreshToken";
|
||||||
@@ -65,7 +67,7 @@ describe("Auth Controller - issueToken", function() {
|
|||||||
expect(token).to.equal(expectedToken);
|
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 payload = {};
|
||||||
const appSettings = {
|
const appSettings = {
|
||||||
refreshTokenSecret: "my_refresh_secret",
|
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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {
|
body: {
|
||||||
firstName: "firstname",
|
firstName: "firstname",
|
||||||
@@ -118,25 +120,25 @@ describe("Auth Controller - registerUser", function() {
|
|||||||
sinon.stub(logger, "error");
|
sinon.stub(logger, "error");
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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 = {};
|
req.body = {};
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.checkSuperadmin.throws(new Error("checkSuperadmin error"));
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin 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.checkSuperadmin.returns(true);
|
||||||
req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error"));
|
req.db.getInviteTokenAndDelete.throws(new Error("getInviteTokenAndDelete error"));
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
@@ -144,7 +146,7 @@ describe("Auth Controller - registerUser", function() {
|
|||||||
expect(next.firstCall.args[0].message).to.equal("getInviteTokenAndDelete error");
|
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.checkSuperadmin.returns(false);
|
||||||
req.db.updateAppSettings.throws(new Error("updateAppSettings error"));
|
req.db.updateAppSettings.throws(new Error("updateAppSettings error"));
|
||||||
await registerUser(req, res, next);
|
await registerUser(req, res, next);
|
||||||
@@ -152,7 +154,7 @@ describe("Auth Controller - registerUser", function() {
|
|||||||
expect(next.firstCall.args[0].message).to.equal("updateAppSettings error");
|
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.checkSuperadmin.resolves(false);
|
||||||
req.db.updateAppSettings.resolves();
|
req.db.updateAppSettings.resolves();
|
||||||
req.db.insertUser.rejects(new Error("insertUser error"));
|
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");
|
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.checkSuperadmin.resolves(false);
|
||||||
req.db.updateAppSettings.resolves();
|
req.db.updateAppSettings.resolves();
|
||||||
req.db.insertUser.resolves({ _id: "123" });
|
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");
|
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.checkSuperadmin.resolves(false);
|
||||||
req.db.updateAppSettings.resolves();
|
req.db.updateAppSettings.resolves();
|
||||||
req.db.insertUser.returns({ _id: "123" });
|
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");
|
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" };
|
const user = { _id: "123" };
|
||||||
req.db.checkSuperadmin.resolves(false);
|
req.db.checkSuperadmin.resolves(false);
|
||||||
req.db.updateAppSettings.resolves();
|
req.db.updateAppSettings.resolves();
|
||||||
@@ -202,14 +204,14 @@ describe("Auth Controller - registerUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_USER,
|
msg: successMessages.AUTH_CREATE_USER(mockLanguage),
|
||||||
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
|
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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" };
|
const user = { _id: "123" };
|
||||||
req.db.checkSuperadmin.resolves(true);
|
req.db.checkSuperadmin.resolves(true);
|
||||||
req.db.updateAppSettings.resolves();
|
req.db.updateAppSettings.resolves();
|
||||||
@@ -224,7 +226,7 @@ describe("Auth Controller - registerUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_USER,
|
msg: successMessages.AUTH_CREATE_USER(mockLanguage),
|
||||||
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
|
data: { user, token: sinon.match.string, refreshToken: sinon.match.string },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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;
|
let req, res, next, user;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: { email: "test@example.com", password: "Password123!" },
|
body: { email: "test@example.com", password: "Password123!" },
|
||||||
db: {
|
db: {
|
||||||
getUserByEmail: sinon.stub(),
|
getUserByEmail: sinon.stub(),
|
||||||
},
|
},
|
||||||
|
language: 'en',
|
||||||
settingsService: {
|
settingsService: {
|
||||||
getSettings: sinon.stub().resolves({
|
getSettings: sinon.stub().resolves({
|
||||||
jwtSecret: "my_secret",
|
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 = {};
|
req.body = {};
|
||||||
await loginUser(req, res, next);
|
await loginUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
|
||||||
await loginUser(req, res, next);
|
await loginUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("getUserByEmail 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);
|
req.db.getUserByEmail.resolves(user);
|
||||||
user.comparePassword.resolves(true);
|
user.comparePassword.resolves(true);
|
||||||
await loginUser(req, res, next);
|
await loginUser(req, res, next);
|
||||||
@@ -283,7 +286,7 @@ describe("Auth Controller - loginUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_LOGIN_USER,
|
msg: successMessages.AUTH_LOGIN_USER(mockLanguage),
|
||||||
data: {
|
data: {
|
||||||
user: {
|
user: {
|
||||||
email: "test@example.com",
|
email: "test@example.com",
|
||||||
@@ -297,7 +300,7 @@ describe("Auth Controller - loginUser", function() {
|
|||||||
expect(next.notCalled).to.be.true;
|
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 = {
|
req.body = {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
password: "Password123!",
|
password: "Password123!",
|
||||||
@@ -307,15 +310,15 @@ describe("Auth Controller - loginUser", function() {
|
|||||||
await loginUser(req, res, next);
|
await loginUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
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;
|
let req, res, next, issueTokenStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
headers: {
|
headers: {
|
||||||
"x-refresh-token": "valid_refresh_token",
|
"x-refresh-token": "valid_refresh_token",
|
||||||
@@ -339,39 +342,39 @@ describe("Auth Controller - refreshAuthToken", function() {
|
|||||||
sinon.replace({ issueToken }, "issueToken", issueTokenStub);
|
sinon.replace({ issueToken }, "issueToken", issueTokenStub);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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"];
|
delete req.headers["x-refresh-token"];
|
||||||
await refreshAuthToken(req, res, next);
|
await refreshAuthToken(req, res, next);
|
||||||
|
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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);
|
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"));
|
jwt.verify.yields(new Error("invalid token"));
|
||||||
await refreshAuthToken(req, res, next);
|
await refreshAuthToken(req, res, next);
|
||||||
|
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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);
|
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");
|
const error = new Error("Token expired");
|
||||||
error.name = "TokenExpiredError";
|
error.name = "TokenExpiredError";
|
||||||
jwt.verify.yields(error);
|
jwt.verify.yields(error);
|
||||||
await refreshAuthToken(req, res, next);
|
await refreshAuthToken(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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);
|
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(
|
req.settingsService.getSettings.rejects(
|
||||||
new Error("settingsService.getSettings error")
|
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");
|
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" };
|
const decodedPayload = { expiresIn: "60" };
|
||||||
jwt.verify.callsFake(() => {
|
jwt.verify.callsFake(() => {
|
||||||
return decodedPayload;
|
return decodedPayload;
|
||||||
@@ -392,7 +395,7 @@ describe("Auth Controller - refreshAuthToken", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_TOKEN_REFRESHED,
|
msg: successMessages.AUTH_TOKEN_REFRESHED(mockLanguage),
|
||||||
data: {
|
data: {
|
||||||
user: decodedPayload,
|
user: decodedPayload,
|
||||||
token: sinon.match.string,
|
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;
|
let req, res, next, stub, user;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: { userId: "123" },
|
params: { userId: "123" },
|
||||||
body: { password: "Password1!", newPassword: "Password2!" },
|
body: { password: "Password1!", newPassword: "Password2!" },
|
||||||
@@ -428,40 +431,40 @@ describe("Auth Controller - editUser", function() {
|
|||||||
stub = sinon.stub(jwt, "verify").returns({ email: "test@example.com" });
|
stub = sinon.stub(jwt, "verify").returns({ email: "test@example.com" });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
stub.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 = {};
|
req.params = {};
|
||||||
await editUser(req, res, next);
|
await editUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 };
|
req.body = { invalid: 1 };
|
||||||
await editUser(req, res, next);
|
await editUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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" };
|
req.params = { userId: "456" };
|
||||||
await editUser(req, res, next);
|
await editUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(401);
|
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"));
|
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
|
||||||
await editUser(req, res, next);
|
await editUser(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("getUserByEmail 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({
|
req.db.getUserByEmail.returns({
|
||||||
comparePassword: sinon.stub().rejects(new Error("Bad Password Match")),
|
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");
|
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({
|
req.db.getUserByEmail.returns({
|
||||||
comparePassword: sinon.stub().returns(false),
|
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]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(401);
|
expect(next.firstCall.args[0].status).to.equal(401);
|
||||||
expect(next.firstCall.args[0].message).to.equal(
|
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 = {
|
const user = {
|
||||||
comparePassword: sinon.stub().resolves(true),
|
comparePassword: sinon.stub().resolves(true),
|
||||||
};
|
};
|
||||||
@@ -497,14 +500,14 @@ describe("Auth Controller - editUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_UPDATE_USER,
|
msg: successMessages.AUTH_UPDATE_USER(mockLanguage),
|
||||||
data: { email: "test@example.com" },
|
data: { email: "test@example.com" },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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.password = undefined;
|
||||||
req.body.newPassword = undefined;
|
req.body.newPassword = undefined;
|
||||||
req.db.getUserByEmail.resolves(user);
|
req.db.getUserByEmail.resolves(user);
|
||||||
@@ -515,14 +518,14 @@ describe("Auth Controller - editUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_UPDATE_USER,
|
msg: successMessages.AUTH_UPDATE_USER(mockLanguage),
|
||||||
data: { email: "test@example.com" },
|
data: { email: "test@example.com" },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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" };
|
req.body = { password: "bad_password", newPassword: "bad_password" };
|
||||||
const user = {
|
const user = {
|
||||||
comparePassword: sinon.stub().resolves(true),
|
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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
db: {
|
db: {
|
||||||
checkSuperadmin: sinon.stub(),
|
checkSuperadmin: sinon.stub(),
|
||||||
@@ -552,35 +555,35 @@ describe("Auth Controller - checkSuperadminExists", function() {
|
|||||||
next = sinon.stub();
|
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"));
|
req.db.checkSuperadmin.rejects(new Error("checkSuperadmin error"));
|
||||||
await checkSuperadminExists(req, res, next);
|
await checkSuperadminExists(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("checkSuperadmin 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);
|
req.db.checkSuperadmin.resolves(true);
|
||||||
await checkSuperadminExists(req, res, next);
|
await checkSuperadminExists(req, res, next);
|
||||||
expect(res.status.calledWith(200)).to.be.true;
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_SUPERADMIN_EXISTS,
|
msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage),
|
||||||
data: true,
|
data: true,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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);
|
req.db.checkSuperadmin.resolves(false);
|
||||||
await checkSuperadminExists(req, res, next);
|
await checkSuperadminExists(req, res, next);
|
||||||
expect(res.status.calledWith(200)).to.be.true;
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_SUPERADMIN_EXISTS,
|
msg: successMessages.AUTH_SUPERADMIN_EXISTS(mockLanguage),
|
||||||
data: false,
|
data: false,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: { email: "test@test.com" },
|
body: { email: "test@test.com" },
|
||||||
db: {
|
db: {
|
||||||
@@ -612,21 +615,21 @@ describe("Auth Controller - requestRecovery", function() {
|
|||||||
next = sinon.stub();
|
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 = {};
|
req.body = {};
|
||||||
await requestRecovery(req, res, next);
|
await requestRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.getUserByEmail.rejects(new Error("getUserByEmail error"));
|
||||||
await requestRecovery(req, res, next);
|
await requestRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("getUserByEmail 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);
|
req.db.getUserByEmail.resolves(null);
|
||||||
await requestRecovery(req, res, next);
|
await requestRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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 = {};
|
req.body = {};
|
||||||
await requestRecovery(req, res, next);
|
await requestRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 user = { firstName: "John" };
|
||||||
const recoveryToken = { token: "recovery-token" };
|
const recoveryToken = { token: "recovery-token" };
|
||||||
const msgId = "message-id";
|
const msgId = "message-id";
|
||||||
@@ -668,7 +671,7 @@ describe("Auth Controller - requestRecovery", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
|
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(mockLanguage),
|
||||||
data: msgId,
|
data: msgId,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: { recoveryToken: "recovery-token" },
|
body: { recoveryToken: "recovery-token" },
|
||||||
db: {
|
db: {
|
||||||
@@ -693,38 +696,38 @@ describe("Auth Controller - validateRecovery", function() {
|
|||||||
next = sinon.stub();
|
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 = {};
|
req.body = {};
|
||||||
await validateRecovery(req, res, next);
|
await validateRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.validateRecoveryToken.rejects(new Error("validateRecoveryToken error"));
|
||||||
await validateRecovery(req, res, next);
|
await validateRecovery(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("validateRecoveryToken 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();
|
req.db.validateRecoveryToken.resolves();
|
||||||
await validateRecovery(req, res, next);
|
await validateRecovery(req, res, next);
|
||||||
expect(res.status.calledOnceWith(200)).to.be.true;
|
expect(res.status.calledOnceWith(200)).to.be.true;
|
||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
|
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN(mockLanguage),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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;
|
let req, res, next, newPasswordValidation, handleValidationError, handleError;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {
|
body: {
|
||||||
recoveryToken: "recovery-token",
|
recoveryToken: "recovery-token",
|
||||||
@@ -749,14 +752,14 @@ describe("Auth Controller - resetPassword", function() {
|
|||||||
handleError = sinon.stub();
|
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" };
|
req.body = { password: "bad_password" };
|
||||||
await resetPassword(req, res, next);
|
await resetPassword(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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");
|
const error = new Error("resetPassword error");
|
||||||
newPasswordValidation.validateAsync.resolves();
|
newPasswordValidation.validateAsync.resolves();
|
||||||
req.db.resetPassword.rejects(error);
|
req.db.resetPassword.rejects(error);
|
||||||
@@ -765,7 +768,7 @@ describe("Auth Controller - resetPassword", function() {
|
|||||||
expect(next.firstCall.args[0].message).to.equal("resetPassword error");
|
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 user = { _doc: {} };
|
||||||
const appSettings = { jwtSecret: "my_secret" };
|
const appSettings = { jwtSecret: "my_secret" };
|
||||||
const token = "token";
|
const token = "token";
|
||||||
@@ -782,7 +785,7 @@ describe("Auth Controller - resetPassword", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_RESET_PASSWORD,
|
msg: successMessages.AUTH_RESET_PASSWORD(mockLanguage),
|
||||||
data: { user: sinon.match.object, token: sinon.match.string },
|
data: { user: sinon.match.object, token: sinon.match.string },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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;
|
let req, res, next, handleError;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
headers: {
|
headers: {
|
||||||
authorization: "Bearer token",
|
authorization: "Bearer token",
|
||||||
@@ -825,24 +828,24 @@ describe("Auth Controller - deleteUser", function() {
|
|||||||
handleError = sinon.stub();
|
handleError = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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" });
|
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);
|
await deleteUser(req, res, next);
|
||||||
|
|
||||||
expect(req.db.getUserByEmail.calledOnceWith("test@example.com")).to.be.true;
|
expect(req.db.getUserByEmail.calledOnceWith("test@example.com")).to.be.true;
|
||||||
expect(next.calledOnce).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.status.notCalled).to.be.true;
|
||||||
expect(res.json.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 = {
|
const user = {
|
||||||
_id: "user_id",
|
_id: "user_id",
|
||||||
email: "test@example.com",
|
email: "test@example.com",
|
||||||
@@ -876,13 +879,13 @@ describe("Auth Controller - deleteUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_DELETE_USER,
|
msg: successMessages.AUTH_DELETE_USER(mockLanguage),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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 = {
|
const user = {
|
||||||
_id: "user_id",
|
_id: "user_id",
|
||||||
email: "test@example.com",
|
email: "test@example.com",
|
||||||
@@ -906,13 +909,13 @@ describe("Auth Controller - deleteUser", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_DELETE_USER,
|
msg: successMessages.AUTH_DELETE_USER(mockLanguage),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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 error = new Error("Something went wrong");
|
||||||
const SERVICE_NAME = "AuthController";
|
const SERVICE_NAME = "AuthController";
|
||||||
jwt.decode.returns({ email: "test@example.com" });
|
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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
db: {
|
db: {
|
||||||
getAllUsers: sinon.stub(),
|
getAllUsers: sinon.stub(),
|
||||||
@@ -941,11 +944,11 @@ describe("Auth Controller - getAllUsers", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore(); // Restore the original methods after each test
|
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" }];
|
const allUsers = [{ id: 1, name: "John Doe" }];
|
||||||
req.db.getAllUsers.resolves(allUsers);
|
req.db.getAllUsers.resolves(allUsers);
|
||||||
|
|
||||||
@@ -963,7 +966,7 @@ describe("Auth Controller - getAllUsers", function() {
|
|||||||
expect(next.notCalled).to.be.true;
|
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");
|
const error = new Error("Something went wrong");
|
||||||
req.db.getAllUsers.rejects(error);
|
req.db.getAllUsers.rejects(error);
|
||||||
await getAllUsers(req, res, next);
|
await getAllUsers(req, res, next);
|
||||||
|
|||||||
@@ -9,11 +9,12 @@ import {
|
|||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { errorMessages, successMessages } from "../../utils/messages.js";
|
import { errorMessages, successMessages } from "../../utils/messages.js";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
describe("Check Controller - createCheck", function() {
|
describe("Check Controller - createCheck", function () {
|
||||||
let req, res, next, handleError;
|
let req, res, next, handleError;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
|
language: 'en',
|
||||||
params: {},
|
params: {},
|
||||||
body: {},
|
body: {},
|
||||||
db: {
|
db: {
|
||||||
@@ -28,17 +29,17 @@ describe("Check Controller - createCheck", function() {
|
|||||||
handleError = sinon.stub();
|
handleError = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore(); // Restore the original methods after each test
|
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);
|
await createCheck(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 = {
|
req.params = {
|
||||||
monitorId: "monitorId",
|
monitorId: "monitorId",
|
||||||
};
|
};
|
||||||
@@ -47,7 +48,7 @@ describe("Check Controller - createCheck", function() {
|
|||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 = {
|
req.params = {
|
||||||
monitorId: "monitorId",
|
monitorId: "monitorId",
|
||||||
};
|
};
|
||||||
@@ -63,7 +64,7 @@ describe("Check Controller - createCheck", function() {
|
|||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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 = {
|
req.params = {
|
||||||
monitorId: "monitorId",
|
monitorId: "monitorId",
|
||||||
};
|
};
|
||||||
@@ -80,7 +81,7 @@ describe("Check Controller - createCheck", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_CREATE,
|
msg: successMessages.CHECK_CREATE(req.language),
|
||||||
data: { id: "123" },
|
data: { id: "123" },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: {},
|
params: {},
|
||||||
query: {},
|
query: {},
|
||||||
@@ -107,17 +108,17 @@ describe("Check Controller - getChecks", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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);
|
await getChecks(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 = {
|
req.params = {
|
||||||
monitorId: "monitorId",
|
monitorId: "monitorId",
|
||||||
};
|
};
|
||||||
@@ -128,14 +129,14 @@ describe("Check Controller - getChecks", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_GET,
|
msg: successMessages.CHECK_GET(req.language),
|
||||||
data: { checksCount: 1, checks: [{ id: "123" }] },
|
data: { checksCount: 1, checks: [{ id: "123" }] },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.notCalled).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 = {
|
req.params = {
|
||||||
monitorId: "monitorId",
|
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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: {},
|
params: {},
|
||||||
query: {},
|
query: {},
|
||||||
@@ -163,17 +164,17 @@ describe("Check Controller - getTeamChecks", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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);
|
await getTeamChecks(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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" };
|
req.params = { teamId: "1" };
|
||||||
const checkData = [{ id: 1, name: "Check 1" }];
|
const checkData = [{ id: 1, name: "Check 1" }];
|
||||||
req.db.getTeamChecks.resolves(checkData);
|
req.db.getTeamChecks.resolves(checkData);
|
||||||
@@ -184,13 +185,13 @@ describe("Check Controller - getTeamChecks", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_GET,
|
msg: successMessages.CHECK_GET(req.language),
|
||||||
data: checkData,
|
data: checkData,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).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.params = { teamId: "1" };
|
||||||
req.db.getTeamChecks.rejects(new Error("Retrieval Error"));
|
req.db.getTeamChecks.rejects(new Error("Retrieval Error"));
|
||||||
await getTeamChecks(req, res, next);
|
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;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: {},
|
params: {},
|
||||||
db: {
|
db: {
|
||||||
@@ -218,17 +219,17 @@ describe("Check Controller - deleteChecks", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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);
|
await deleteChecks(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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.params = { monitorId: "1" };
|
||||||
req.db.deleteChecks.rejects(new Error("Deletion Error"));
|
req.db.deleteChecks.rejects(new Error("Deletion Error"));
|
||||||
await deleteChecks(req, res, next);
|
await deleteChecks(req, res, next);
|
||||||
@@ -238,7 +239,7 @@ describe("Check Controller - deleteChecks", function() {
|
|||||||
expect(res.json.notCalled).to.be.true;
|
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.params = { monitorId: "123" };
|
||||||
req.db.deleteChecks.resolves(1);
|
req.db.deleteChecks.resolves(1);
|
||||||
await deleteChecks(req, res, next);
|
await deleteChecks(req, res, next);
|
||||||
@@ -247,17 +248,17 @@ describe("Check Controller - deleteChecks", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_DELETE,
|
msg: successMessages.CHECK_DELETE(req.language),
|
||||||
data: { deletedCount: 1 },
|
data: { deletedCount: 1 },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Check Controller - deleteChecksByTeamId", function() {
|
describe("Check Controller - deleteChecksByTeamId", function () {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: {},
|
params: {},
|
||||||
db: {
|
db: {
|
||||||
@@ -271,17 +272,17 @@ describe("Check Controller - deleteChecksByTeamId", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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);
|
await deleteChecksByTeamId(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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.params = { teamId: "1" };
|
||||||
req.db.deleteChecksByTeamId.rejects(new Error("Deletion Error"));
|
req.db.deleteChecksByTeamId.rejects(new Error("Deletion Error"));
|
||||||
await deleteChecksByTeamId(req, res, next);
|
await deleteChecksByTeamId(req, res, next);
|
||||||
@@ -291,7 +292,7 @@ describe("Check Controller - deleteChecksByTeamId", function() {
|
|||||||
expect(res.json.notCalled).to.be.true;
|
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.params = { teamId: "123" };
|
||||||
req.db.deleteChecksByTeamId.resolves(1);
|
req.db.deleteChecksByTeamId.resolves(1);
|
||||||
await deleteChecksByTeamId(req, res, next);
|
await deleteChecksByTeamId(req, res, next);
|
||||||
@@ -300,17 +301,17 @@ describe("Check Controller - deleteChecksByTeamId", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_DELETE,
|
msg: successMessages.CHECK_DELETE(req.language),
|
||||||
data: { deletedCount: 1 },
|
data: { deletedCount: 1 },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Check Controller - updateCheckTTL", function() {
|
describe("Check Controller - updateCheckTTL", function () {
|
||||||
let stub, req, res, next;
|
let stub, req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -332,18 +333,18 @@ describe("Check Controller - updateCheckTTL", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
stub.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);
|
await updateChecksTTL(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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();
|
stub.restore();
|
||||||
req.body = {
|
req.body = {
|
||||||
ttl: 1,
|
ttl: 1,
|
||||||
@@ -352,7 +353,7 @@ describe("Check Controller - updateCheckTTL", function() {
|
|||||||
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
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 = {
|
req.body = {
|
||||||
ttl: 1,
|
ttl: 1,
|
||||||
};
|
};
|
||||||
@@ -361,7 +362,7 @@ describe("Check Controller - updateCheckTTL", function() {
|
|||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
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 = {
|
req.body = {
|
||||||
ttl: 1,
|
ttl: 1,
|
||||||
};
|
};
|
||||||
@@ -372,7 +373,7 @@ describe("Check Controller - updateCheckTTL", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.CHECK_UPDATE_TTL,
|
msg: successMessages.CHECK_UPDATE_TTL(req.language),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,11 +11,12 @@ import jwt from "jsonwebtoken";
|
|||||||
import { successMessages } from "../../utils/messages.js";
|
import { successMessages } from "../../utils/messages.js";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
|
|
||||||
describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
describe("maintenanceWindowController - createMaintenanceWindows", function () {
|
||||||
let req, res, next, stub;
|
let req, res, next, stub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
|
language: 'en',
|
||||||
body: {
|
body: {
|
||||||
monitors: ["66ff52e7c5911c61698ac724"],
|
monitors: ["66ff52e7c5911c61698ac724"],
|
||||||
name: "window",
|
name: "window",
|
||||||
@@ -41,11 +42,11 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -56,14 +57,14 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
|||||||
stub.restore();
|
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());
|
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
|
||||||
await createMaintenanceWindows(req, res, next);
|
await createMaintenanceWindows(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
||||||
stub.restore();
|
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(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -74,7 +75,7 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
|||||||
stub.restore();
|
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(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -83,13 +84,13 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
stub.restore();
|
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;
|
req.body.active = undefined;
|
||||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
@@ -99,17 +100,17 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
stub.restore();
|
stub.restore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
describe("maintenanceWindowController - getMaintenanceWindowById", function () {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {},
|
body: {},
|
||||||
params: {
|
params: {
|
||||||
@@ -121,6 +122,7 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
|||||||
settingsService: {
|
settingsService: {
|
||||||
getSettings: sinon.stub().returns({ jwtSecret: "jwtSecret" }),
|
getSettings: sinon.stub().returns({ jwtSecret: "jwtSecret" }),
|
||||||
},
|
},
|
||||||
|
language: 'en',
|
||||||
db: {
|
db: {
|
||||||
getMaintenanceWindowById: sinon.stub(),
|
getMaintenanceWindowById: sinon.stub(),
|
||||||
},
|
},
|
||||||
@@ -132,38 +134,38 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject if param validation fails", async function() {
|
it("should reject if param validation fails", async function () {
|
||||||
req.params = {};
|
req.params = {};
|
||||||
await getMaintenanceWindowById(req, res, next);
|
await getMaintenanceWindowById(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.getMaintenanceWindowById.throws(new Error("DB error"));
|
||||||
await getMaintenanceWindowById(req, res, next);
|
await getMaintenanceWindowById(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("DB 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" });
|
req.db.getMaintenanceWindowById.returns({ id: "123" });
|
||||||
await getMaintenanceWindowById(req, res, next);
|
await getMaintenanceWindowById(req, res, next);
|
||||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language),
|
||||||
data: { id: "123" },
|
data: { id: "123" },
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function() {
|
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function () {
|
||||||
let req, res, next, stub;
|
let req, res, next, stub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {},
|
body: {},
|
||||||
params: {},
|
params: {},
|
||||||
@@ -177,6 +179,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
db: {
|
db: {
|
||||||
getMaintenanceWindowsByTeamId: sinon.stub(),
|
getMaintenanceWindowsByTeamId: sinon.stub(),
|
||||||
},
|
},
|
||||||
|
language: 'en',
|
||||||
};
|
};
|
||||||
res = {
|
res = {
|
||||||
status: sinon.stub().returnsThis(),
|
status: sinon.stub().returnsThis(),
|
||||||
@@ -185,7 +188,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject if query validation fails", async function() {
|
it("should reject if query validation fails", async function () {
|
||||||
req.query = {
|
req.query = {
|
||||||
invalid: 1,
|
invalid: 1,
|
||||||
};
|
};
|
||||||
@@ -194,14 +197,14 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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());
|
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
|
||||||
await getMaintenanceWindowsByTeamId(req, res, next);
|
await getMaintenanceWindowsByTeamId(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
||||||
stub.restore();
|
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(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -212,7 +215,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
stub.restore();
|
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(() => {
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
return { teamId: "123" };
|
return { teamId: "123" };
|
||||||
});
|
});
|
||||||
@@ -222,7 +225,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language),
|
||||||
data: [{ id: jwt.verify().teamId }],
|
data: [{ id: jwt.verify().teamId }],
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
@@ -230,10 +233,10 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function() {
|
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function () {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {},
|
body: {},
|
||||||
params: {
|
params: {
|
||||||
@@ -257,25 +260,25 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject if param validation fails", async function() {
|
it("should reject if param validation fails", async function () {
|
||||||
req.params = {};
|
req.params = {};
|
||||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.getMaintenanceWindowsByMonitorId.throws(new Error("DB error"));
|
||||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("DB 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" }];
|
const data = [{ monitorId: "123" }];
|
||||||
req.db.getMaintenanceWindowsByMonitorId.returns(data);
|
req.db.getMaintenanceWindowsByMonitorId.returns(data);
|
||||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||||
@@ -284,17 +287,17 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR,
|
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR(req.language),
|
||||||
data: data,
|
data: data,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maintenanceWindowController - deleteMaintenanceWindow", function() {
|
describe("maintenanceWindowController - deleteMaintenanceWindow", function () {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {},
|
body: {},
|
||||||
params: {
|
params: {
|
||||||
@@ -318,46 +321,47 @@ describe("maintenanceWindowController - deleteMaintenanceWindow", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject if param validation fails", async function() {
|
it("should reject if param validation fails", async function () {
|
||||||
req.params = {};
|
req.params = {};
|
||||||
await deleteMaintenanceWindow(req, res, next);
|
await deleteMaintenanceWindow(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.deleteMaintenanceWindowById.throws(new Error("DB error"));
|
||||||
await deleteMaintenanceWindow(req, res, next);
|
await deleteMaintenanceWindow(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("DB 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);
|
await deleteMaintenanceWindow(req, res, next);
|
||||||
expect(req.db.deleteMaintenanceWindowById.calledOnceWith(req.params.id));
|
expect(req.db.deleteMaintenanceWindowById.calledOnceWith(req.params.id));
|
||||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
|
msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language),
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
describe("maintenanceWindowController - editMaintenanceWindow", function () {
|
||||||
let req, res, next;
|
let req, res, next;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: {
|
body: {
|
||||||
active: true,
|
active: true,
|
||||||
name: "test",
|
name: "test",
|
||||||
},
|
},
|
||||||
|
language: 'en',
|
||||||
params: {
|
params: {
|
||||||
id: "123",
|
id: "123",
|
||||||
},
|
},
|
||||||
@@ -379,32 +383,32 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
|||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject if param validation fails", async function() {
|
it("should reject if param validation fails", async function () {
|
||||||
req.params = {};
|
req.params = {};
|
||||||
await editMaintenanceWindow(req, res, next);
|
await editMaintenanceWindow(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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 };
|
req.body = { invalid: 1 };
|
||||||
await editMaintenanceWindow(req, res, next);
|
await editMaintenanceWindow(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].status).to.equal(422);
|
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"));
|
req.db.editMaintenanceWindowById.throws(new Error("DB error"));
|
||||||
await editMaintenanceWindow(req, res, next);
|
await editMaintenanceWindow(req, res, next);
|
||||||
expect(next.firstCall.args[0]).to.be.an("error");
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(next.firstCall.args[0].message).to.equal("DB 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" };
|
const data = { id: "123" };
|
||||||
req.db.editMaintenanceWindowById.returns(data);
|
req.db.editMaintenanceWindowById.returns(data);
|
||||||
|
|
||||||
@@ -414,7 +418,7 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
|||||||
expect(
|
expect(
|
||||||
res.json.calledOnceWith({
|
res.json.calledOnceWith({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
|
msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language),
|
||||||
data: data,
|
data: data,
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -7,32 +7,33 @@ import {
|
|||||||
} from "../../db/mongo/modules/inviteModule.js";
|
} from "../../db/mongo/modules/inviteModule.js";
|
||||||
import { errorMessages } from "../../utils/messages.js";
|
import { errorMessages } from "../../utils/messages.js";
|
||||||
|
|
||||||
describe("Invite Module", function() {
|
describe("Invite Module", function () {
|
||||||
const mockUserData = {
|
const mockUserData = {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
teamId: "123",
|
teamId: "123",
|
||||||
role: ["admin"],
|
role: ["admin"],
|
||||||
token: "123",
|
token: "123",
|
||||||
};
|
};
|
||||||
|
const mockLanguage = 'en';
|
||||||
const mockInviteToken = { _id: 123, time: 123 };
|
const mockInviteToken = { _id: 123, time: 123 };
|
||||||
let inviteTokenDeleteManyStub,
|
let inviteTokenDeleteManyStub,
|
||||||
inviteTokenSaveStub,
|
inviteTokenSaveStub,
|
||||||
inviteTokenFindOneStub,
|
inviteTokenFindOneStub,
|
||||||
inviteTokenFindOneAndDeleteStub;
|
inviteTokenFindOneAndDeleteStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
inviteTokenDeleteManyStub = sinon.stub(InviteToken, "deleteMany");
|
inviteTokenDeleteManyStub = sinon.stub(InviteToken, "deleteMany");
|
||||||
inviteTokenSaveStub = sinon.stub(InviteToken.prototype, "save");
|
inviteTokenSaveStub = sinon.stub(InviteToken.prototype, "save");
|
||||||
inviteTokenFindOneStub = sinon.stub(InviteToken, "findOne");
|
inviteTokenFindOneStub = sinon.stub(InviteToken, "findOne");
|
||||||
inviteTokenFindOneAndDeleteStub = sinon.stub(InviteToken, "findOneAndDelete");
|
inviteTokenFindOneAndDeleteStub = sinon.stub(InviteToken, "findOneAndDelete");
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestInviteToken", function() {
|
describe("requestInviteToken", function () {
|
||||||
it("should return a new invite token", async function() {
|
it("should return a new invite token", async function () {
|
||||||
inviteTokenDeleteManyStub.resolves();
|
inviteTokenDeleteManyStub.resolves();
|
||||||
inviteTokenSaveStub.resolves();
|
inviteTokenSaveStub.resolves();
|
||||||
const inviteToken = await requestInviteToken(mockUserData);
|
const inviteToken = await requestInviteToken(mockUserData);
|
||||||
@@ -41,7 +42,7 @@ describe("Invite Module", function() {
|
|||||||
expect(inviteToken.token).to.exist;
|
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");
|
const err = new Error("test error");
|
||||||
inviteTokenDeleteManyStub.rejects(err);
|
inviteTokenDeleteManyStub.rejects(err);
|
||||||
try {
|
try {
|
||||||
@@ -52,23 +53,23 @@ describe("Invite Module", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getInviteToken", function() {
|
describe("getInviteToken", function () {
|
||||||
it("should return an invite token", async function() {
|
it("should return an invite token", async function () {
|
||||||
inviteTokenFindOneStub.resolves(mockInviteToken);
|
inviteTokenFindOneStub.resolves(mockInviteToken);
|
||||||
const inviteToken = await getInviteToken(mockUserData.token);
|
const inviteToken = await getInviteToken(mockUserData.token);
|
||||||
expect(inviteToken).to.deep.equal(mockInviteToken);
|
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);
|
inviteTokenFindOneStub.resolves(null);
|
||||||
try {
|
try {
|
||||||
await getInviteToken(mockUserData.token);
|
await getInviteToken(mockUserData.token);
|
||||||
} catch (error) {
|
} 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");
|
const err = new Error("test error");
|
||||||
inviteTokenFindOneStub.rejects(err);
|
inviteTokenFindOneStub.rejects(err);
|
||||||
try {
|
try {
|
||||||
@@ -80,23 +81,23 @@ describe("Invite Module", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getInviteTokenAndDelete", function() {
|
describe("getInviteTokenAndDelete", function () {
|
||||||
it("should return a deleted invite", async function() {
|
it("should return a deleted invite", async function () {
|
||||||
inviteTokenFindOneAndDeleteStub.resolves(mockInviteToken);
|
inviteTokenFindOneAndDeleteStub.resolves(mockInviteToken);
|
||||||
const deletedInvite = await getInviteTokenAndDelete(mockUserData.token);
|
const deletedInvite = await getInviteTokenAndDelete(mockUserData.token);
|
||||||
expect(deletedInvite).to.deep.equal(mockInviteToken);
|
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);
|
inviteTokenFindOneAndDeleteStub.resolves(null);
|
||||||
try {
|
try {
|
||||||
await getInviteTokenAndDelete(mockUserData.token);
|
await getInviteTokenAndDelete(mockUserData.token);
|
||||||
} catch (error) {
|
} 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");
|
const err = new Error("test error");
|
||||||
inviteTokenFindOneAndDeleteStub.rejects(err);
|
inviteTokenFindOneAndDeleteStub.rejects(err);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import {
|
|||||||
calculateGroupStats,
|
calculateGroupStats,
|
||||||
} from "../../db/mongo/modules/monitorModule.js";
|
} from "../../db/mongo/modules/monitorModule.js";
|
||||||
|
|
||||||
describe("monitorModule", function() {
|
describe("monitorModule", function () {
|
||||||
let monitorFindStub,
|
let monitorFindStub,
|
||||||
monitorFindByIdStub,
|
monitorFindByIdStub,
|
||||||
monitorFindByIdAndUpdateStub,
|
monitorFindByIdAndUpdateStub,
|
||||||
@@ -43,7 +43,7 @@ describe("monitorModule", function() {
|
|||||||
pageSpeedCheckFindStub,
|
pageSpeedCheckFindStub,
|
||||||
hardwareCheckFindStub;
|
hardwareCheckFindStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
monitorFindStub = sinon.stub(Monitor, "find");
|
monitorFindStub = sinon.stub(Monitor, "find");
|
||||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||||
monitorFindByIdAndUpdateStub = sinon.stub(Monitor, "findByIdAndUpdate");
|
monitorFindByIdAndUpdateStub = sinon.stub(Monitor, "findByIdAndUpdate");
|
||||||
@@ -63,12 +63,12 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getAllMonitors", function() {
|
describe("getAllMonitors", function () {
|
||||||
it("should return all monitors", async function() {
|
it("should return all monitors", async function () {
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{ _id: "1", name: "Monitor 1", url: "test1.com" },
|
{ _id: "1", name: "Monitor 1", url: "test1.com" },
|
||||||
{ _id: "2", name: "Monitor 2", url: "test2.com" },
|
{ _id: "2", name: "Monitor 2", url: "test2.com" },
|
||||||
@@ -81,13 +81,13 @@ describe("monitorModule", function() {
|
|||||||
expect(monitorFindStub.firstCall.args).to.deep.equal([]);
|
expect(monitorFindStub.firstCall.args).to.deep.equal([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle empty results", async function() {
|
it("should handle empty results", async function () {
|
||||||
monitorFindStub.returns([]);
|
monitorFindStub.returns([]);
|
||||||
const result = await getAllMonitors();
|
const result = await getAllMonitors();
|
||||||
expect(result).to.be.an("array").that.is.empty;
|
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
|
// Arrange
|
||||||
const error = new Error("Database error");
|
const error = new Error("Database error");
|
||||||
error.service = "MonitorModule";
|
error.service = "MonitorModule";
|
||||||
@@ -105,8 +105,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getAllMonitorsWithUptimeStats", function() {
|
describe("getAllMonitorsWithUptimeStats", function () {
|
||||||
it("should return monitors with uptime stats for different time periods", async function() {
|
it("should return monitors with uptime stats for different time periods", async function () {
|
||||||
// Mock data
|
// Mock data
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{
|
{
|
||||||
@@ -152,7 +152,7 @@ describe("monitorModule", function() {
|
|||||||
expect(monitor["90"]).to.equal(75);
|
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
|
// Mock data
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{
|
{
|
||||||
@@ -198,7 +198,7 @@ describe("monitorModule", function() {
|
|||||||
expect(monitor["90"]).to.equal(75);
|
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
|
// Mock data
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{
|
{
|
||||||
@@ -244,7 +244,7 @@ describe("monitorModule", function() {
|
|||||||
expect(monitor["90"]).to.equal(75);
|
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
|
// Setup stub to throw error
|
||||||
monitorFindStub.rejects(new Error("Database 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([]);
|
monitorFindStub.resolves([]);
|
||||||
|
|
||||||
const result = await getAllMonitorsWithUptimeStats();
|
const result = await getAllMonitorsWithUptimeStats();
|
||||||
@@ -267,7 +267,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result).to.have.lengthOf(0);
|
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 = [
|
const mockMonitors = [
|
||||||
{
|
{
|
||||||
_id: "monitor1",
|
_id: "monitor1",
|
||||||
@@ -292,28 +292,28 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("calculateUptimeDuration", function() {
|
describe("calculateUptimeDuration", function () {
|
||||||
let clock;
|
let clock;
|
||||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
// Fix the current time
|
// Fix the current time
|
||||||
clock = sinon.useFakeTimers(NOW);
|
clock = sinon.useFakeTimers(NOW);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
clock.restore();
|
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);
|
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);
|
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 = [
|
const checks = [
|
||||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" }, // Most recent
|
{ status: true, createdAt: "2024-01-01T11:00:00Z" }, // Most recent
|
||||||
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
||||||
@@ -325,7 +325,7 @@ describe("monitorModule", function() {
|
|||||||
expect(calculateUptimeDuration(checks)).to.equal(7200000);
|
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 = [
|
const checks = [
|
||||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" },
|
{ status: true, createdAt: "2024-01-01T11:00:00Z" },
|
||||||
{ status: true, createdAt: "2024-01-01T10: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;
|
let clock;
|
||||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
// Fix the current time
|
// Fix the current time
|
||||||
clock = sinon.useFakeTimers(NOW);
|
clock = sinon.useFakeTimers(NOW);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
clock.restore();
|
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);
|
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);
|
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 = [
|
const checks = [
|
||||||
{ createdAt: "2024-01-01T11:30:00Z" }, // 30 minutes ago
|
{ createdAt: "2024-01-01T11:30:00Z" }, // 30 minutes ago
|
||||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -369,7 +369,7 @@ describe("monitorModule", function() {
|
|||||||
expect(getLastChecked(checks)).to.equal(1800000);
|
expect(getLastChecked(checks)).to.equal(1800000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle checks from different days", function() {
|
it("should handle checks from different days", function () {
|
||||||
const checks = [
|
const checks = [
|
||||||
{ createdAt: "2023-12-31T12:00:00Z" }, // 24 hours ago
|
{ createdAt: "2023-12-31T12:00:00Z" }, // 24 hours ago
|
||||||
{ createdAt: "2023-12-30T12:00:00Z" },
|
{ createdAt: "2023-12-30T12:00:00Z" },
|
||||||
@@ -380,16 +380,16 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getLatestResponseTime", function() {
|
describe("getLatestResponseTime", function () {
|
||||||
it("should return 0 when checks array is empty", function() {
|
it("should return 0 when checks array is empty", function () {
|
||||||
expect(getLatestResponseTime([])).to.equal(0);
|
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);
|
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 = [
|
const checks = [
|
||||||
{ responseTime: 150, createdAt: "2024-01-01T11:30:00Z" }, // Most recent
|
{ responseTime: 150, createdAt: "2024-01-01T11:30:00Z" }, // Most recent
|
||||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -399,7 +399,7 @@ describe("monitorModule", function() {
|
|||||||
expect(getLatestResponseTime(checks)).to.equal(150);
|
expect(getLatestResponseTime(checks)).to.equal(150);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle missing responseTime in checks", function() {
|
it("should handle missing responseTime in checks", function () {
|
||||||
const checks = [
|
const checks = [
|
||||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -409,16 +409,16 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getAverageResponseTime", function() {
|
describe("getAverageResponseTime", function () {
|
||||||
it("should return 0 when checks array is empty", function() {
|
it("should return 0 when checks array is empty", function () {
|
||||||
expect(getAverageResponseTime([])).to.equal(0);
|
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);
|
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 = [
|
const checks = [
|
||||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -429,7 +429,7 @@ describe("monitorModule", function() {
|
|||||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
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 = [
|
const checks = [
|
||||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -440,7 +440,7 @@ describe("monitorModule", function() {
|
|||||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
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 = [
|
const checks = [
|
||||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||||
@@ -450,26 +450,26 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getUptimePercentage", function() {
|
describe("getUptimePercentage", function () {
|
||||||
it("should return 0 when checks array is empty", function() {
|
it("should return 0 when checks array is empty", function () {
|
||||||
expect(getUptimePercentage([])).to.equal(0);
|
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);
|
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 }];
|
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||||
expect(getUptimePercentage(checks)).to.equal(100);
|
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 }];
|
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||||
expect(getUptimePercentage(checks)).to.equal(0);
|
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 = [
|
const checks = [
|
||||||
{ status: true },
|
{ status: true },
|
||||||
{ status: false },
|
{ status: false },
|
||||||
@@ -480,33 +480,33 @@ describe("monitorModule", function() {
|
|||||||
expect(getUptimePercentage(checks)).to.equal(75);
|
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 }];
|
const checks = [{ status: true }, { status: undefined }, { status: true }];
|
||||||
// 2 up out of 3 total ≈ 66.67%
|
// 2 up out of 3 total ≈ 66.67%
|
||||||
expect(getUptimePercentage(checks)).to.equal((2 / 3) * 100);
|
expect(getUptimePercentage(checks)).to.equal((2 / 3) * 100);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getIncidents", function() {
|
describe("getIncidents", function () {
|
||||||
it("should return 0 when checks array is empty", function() {
|
it("should return 0 when checks array is empty", function () {
|
||||||
expect(getIncidents([])).to.equal(0);
|
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);
|
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 }];
|
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||||
expect(getIncidents(checks)).to.equal(0);
|
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 }];
|
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||||
expect(getIncidents(checks)).to.equal(3);
|
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 = [
|
const checks = [
|
||||||
{ status: true },
|
{ status: true },
|
||||||
{ status: false },
|
{ status: false },
|
||||||
@@ -517,7 +517,7 @@ describe("monitorModule", function() {
|
|||||||
expect(getIncidents(checks)).to.equal(2);
|
expect(getIncidents(checks)).to.equal(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle undefined status values", function() {
|
it("should handle undefined status values", function () {
|
||||||
const checks = [
|
const checks = [
|
||||||
{ status: true },
|
{ status: true },
|
||||||
{ status: undefined },
|
{ status: undefined },
|
||||||
@@ -529,10 +529,10 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorChecks", function() {
|
describe("getMonitorChecks", function () {
|
||||||
let mockModel;
|
let mockModel;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
// Create a mock model with chainable methods
|
// Create a mock model with chainable methods
|
||||||
const mockChecks = [
|
const mockChecks = [
|
||||||
{ monitorId: "123", createdAt: new Date("2024-01-01") },
|
{ monitorId: "123", createdAt: new Date("2024-01-01") },
|
||||||
@@ -546,11 +546,11 @@ describe("monitorModule", function() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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
|
// Arrange
|
||||||
const monitorId = "123";
|
const monitorId = "123";
|
||||||
const dateRange = {
|
const dateRange = {
|
||||||
@@ -579,7 +579,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle empty results", async function() {
|
it("should handle empty results", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const emptyModel = {
|
const emptyModel = {
|
||||||
find: sinon.stub().returns({
|
find: sinon.stub().returns({
|
||||||
@@ -603,7 +603,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.checksForDateRange).to.be.an("array").that.is.empty;
|
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
|
// Arrange
|
||||||
const sortedChecks = [
|
const sortedChecks = [
|
||||||
{ monitorId: "123", createdAt: new Date("2024-01-02") },
|
{ monitorId: "123", createdAt: new Date("2024-01-02") },
|
||||||
@@ -637,39 +637,39 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("processChecksForDisplay", function() {
|
describe("processChecksForDisplay", function () {
|
||||||
let normalizeStub;
|
let normalizeStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
normalizeStub = sinon.stub();
|
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 checks = [1, 2, 3, 4, 5];
|
||||||
const result = processChecksForDisplay(normalizeStub, checks);
|
const result = processChecksForDisplay(normalizeStub, checks);
|
||||||
expect(result).to.deep.equal(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 checks = [1, 2, 3];
|
||||||
const result = processChecksForDisplay(normalizeStub, checks, 5);
|
const result = processChecksForDisplay(normalizeStub, checks, 5);
|
||||||
expect(result).to.deep.equal(checks);
|
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 checks = [1, 2, 3, 4, 5, 6];
|
||||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||||
// Should return [1, 3, 5] as n = ceil(6/3) = 2
|
// Should return [1, 3, 5] as n = ceil(6/3) = 2
|
||||||
expect(result).to.deep.equal([1, 3, 5]);
|
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 checks = [];
|
||||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||||
expect(result).to.be.an("array").that.is.empty;
|
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];
|
const checks = [1, 2, 3];
|
||||||
normalizeStub.returns([10, 20, 30]);
|
normalizeStub.returns([10, 20, 30]);
|
||||||
|
|
||||||
@@ -679,7 +679,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result).to.deep.equal([10, 20, 30]);
|
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];
|
const checks = [1, 2, 3, 4, 5, 6];
|
||||||
normalizeStub.returns([10, 30, 50]);
|
normalizeStub.returns([10, 30, 50]);
|
||||||
|
|
||||||
@@ -690,7 +690,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("groupChecksByTime", function() {
|
describe("groupChecksByTime", function () {
|
||||||
const mockChecks = [
|
const mockChecks = [
|
||||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
{ createdAt: "2024-01-15T10:30:45Z" },
|
||||||
{ createdAt: "2024-01-15T10:45:15Z" },
|
{ createdAt: "2024-01-15T10:45:15Z" },
|
||||||
@@ -698,7 +698,7 @@ describe("monitorModule", function() {
|
|||||||
{ createdAt: "2024-01-16T10:30:00Z" },
|
{ 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");
|
const result = groupChecksByTime(mockChecks, "day");
|
||||||
|
|
||||||
// Get timestamps for 10:00 and 11:00 on Jan 15
|
// 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);
|
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");
|
const result = groupChecksByTime(mockChecks, "week");
|
||||||
|
|
||||||
expect(Object.keys(result)).to.have.lengthOf(2);
|
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);
|
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");
|
const result = groupChecksByTime([], "day");
|
||||||
expect(result).to.deep.equal({});
|
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 singleCheck = [{ createdAt: "2024-01-15T10:30:45Z" }];
|
||||||
const result = groupChecksByTime(singleCheck, "day");
|
const result = groupChecksByTime(singleCheck, "day");
|
||||||
|
|
||||||
@@ -735,7 +735,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result[expectedTime].checks).to.have.lengthOf(1);
|
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 = [
|
const checksWithInvalidDate = [
|
||||||
{ createdAt: "invalid-date" },
|
{ createdAt: "invalid-date" },
|
||||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
{ 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");
|
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 = [
|
const checksInSameHour = [
|
||||||
{ createdAt: "2024-01-15T10:15:00Z" },
|
{ createdAt: "2024-01-15T10:15:00Z" },
|
||||||
{ createdAt: "2024-01-15T10:45:00Z" },
|
{ createdAt: "2024-01-15T10:45:00Z" },
|
||||||
@@ -766,16 +766,16 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("calculateGroupStats", function() {
|
describe("calculateGroupStats", function () {
|
||||||
// Mock getUptimePercentage function
|
// Mock getUptimePercentage function
|
||||||
let uptimePercentageStub;
|
let uptimePercentageStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
uptimePercentageStub = sinon.stub();
|
uptimePercentageStub = sinon.stub();
|
||||||
uptimePercentageStub.returns(95); // Default return value
|
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 = {
|
const mockGroup = {
|
||||||
time: "2024-01-15",
|
time: "2024-01-15",
|
||||||
checks: [
|
checks: [
|
||||||
@@ -796,7 +796,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle empty checks array", function() {
|
it("should handle empty checks array", function () {
|
||||||
const mockGroup = {
|
const mockGroup = {
|
||||||
time: "2024-01-15",
|
time: "2024-01-15",
|
||||||
checks: [],
|
checks: [],
|
||||||
@@ -813,7 +813,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle missing responseTime values", function() {
|
it("should handle missing responseTime values", function () {
|
||||||
const mockGroup = {
|
const mockGroup = {
|
||||||
time: "2024-01-15",
|
time: "2024-01-15",
|
||||||
checks: [
|
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 = {
|
const mockGroup = {
|
||||||
time: "2024-01-15",
|
time: "2024-01-15",
|
||||||
checks: [
|
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 = {
|
const mockGroup = {
|
||||||
time: "2024-01-15",
|
time: "2024-01-15",
|
||||||
checks: [
|
checks: [
|
||||||
@@ -877,7 +877,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorStatsById", function() {
|
describe("getMonitorStatsById", function () {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const oneHourAgo = new Date(now - 3600000);
|
const oneHourAgo = new Date(now - 3600000);
|
||||||
const twoHoursAgo = new Date(now - 7200000);
|
const twoHoursAgo = new Date(now - 7200000);
|
||||||
@@ -974,18 +974,18 @@ describe("monitorModule", function() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
checkFindStub.returns({
|
checkFindStub.returns({
|
||||||
sort: () => checkDocs,
|
sort: () => checkDocs,
|
||||||
});
|
});
|
||||||
monitorFindByIdStub.returns(mockMonitor);
|
monitorFindByIdStub.returns(mockMonitor);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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";
|
req.query.sortOrder = "desc";
|
||||||
const result = await getMonitorStatsById(req);
|
const result = await getMonitorStatsById(req);
|
||||||
expect(result).to.include.keys([
|
expect(result).to.include.keys([
|
||||||
@@ -1009,7 +1009,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.aggregateData).to.be.an("array");
|
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);
|
monitorFindByIdStub.returns(mockMonitorPing);
|
||||||
req.query.sortOrder = "desc";
|
req.query.sortOrder = "desc";
|
||||||
const result = await getMonitorStatsById(req);
|
const result = await getMonitorStatsById(req);
|
||||||
@@ -1034,7 +1034,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.aggregateData).to.be.an("array");
|
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);
|
monitorFindByIdStub.returns(mockMonitorDocker);
|
||||||
req.query.sortOrder = "desc";
|
req.query.sortOrder = "desc";
|
||||||
const result = await getMonitorStatsById(req);
|
const result = await getMonitorStatsById(req);
|
||||||
@@ -1059,7 +1059,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.aggregateData).to.be.an("array");
|
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";
|
req.query.sortOrder = "asc";
|
||||||
const result = await getMonitorStatsById(req);
|
const result = await getMonitorStatsById(req);
|
||||||
expect(result).to.include.keys([
|
expect(result).to.include.keys([
|
||||||
@@ -1083,7 +1083,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.aggregateData).to.be.an("array");
|
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));
|
monitorFindByIdStub.returns(Promise.resolve(null));
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
@@ -1101,21 +1101,21 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorById", function() {
|
describe("getMonitorById", function () {
|
||||||
let notificationFindStub;
|
let notificationFindStub;
|
||||||
let monitorSaveStub;
|
let monitorSaveStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
// Create stubs
|
// Create stubs
|
||||||
notificationFindStub = sinon.stub(Notification, "find");
|
notificationFindStub = sinon.stub(Notification, "find");
|
||||||
monitorSaveStub = sinon.stub().resolves();
|
monitorSaveStub = sinon.stub().resolves();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return monitor with notifications when found", async function() {
|
it("should return monitor with notifications when found", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const monitorId = "123";
|
const monitorId = "123";
|
||||||
const mockMonitor = {
|
const mockMonitor = {
|
||||||
@@ -1139,9 +1139,10 @@ describe("monitorModule", function() {
|
|||||||
expect(monitorSaveStub.calledOnce).to.be.true;
|
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
|
// Arrange
|
||||||
const monitorId = "nonexistent";
|
const monitorId = "nonexistent";
|
||||||
|
const mockLanguage = 'en';
|
||||||
monitorFindByIdStub.resolves(null);
|
monitorFindByIdStub.resolves(null);
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
@@ -1149,14 +1150,14 @@ describe("monitorModule", function() {
|
|||||||
await getMonitorById(monitorId);
|
await getMonitorById(monitorId);
|
||||||
expect.fail("Should have thrown an error");
|
expect.fail("Should have thrown an error");
|
||||||
} catch (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.status).to.equal(404);
|
||||||
expect(error.service).to.equal("monitorModule");
|
expect(error.service).to.equal("monitorModule");
|
||||||
expect(error.method).to.equal("getMonitorById");
|
expect(error.method).to.equal("getMonitorById");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors properly", async function() {
|
it("should handle database errors properly", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const monitorId = "123";
|
const monitorId = "123";
|
||||||
const dbError = new Error("Database connection failed");
|
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
|
// Arrange
|
||||||
const monitorId = "123";
|
const monitorId = "123";
|
||||||
const mockMonitor = {
|
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
|
// Arrange
|
||||||
const monitorId = "123";
|
const monitorId = "123";
|
||||||
const mockMonitor = {
|
const mockMonitor = {
|
||||||
@@ -1222,8 +1223,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorsAndSummaryByTeamId", function() {
|
describe("getMonitorsAndSummaryByTeamId", function () {
|
||||||
it("should return monitors and correct summary counts", async function() {
|
it("should return monitors and correct summary counts", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const teamId = "team123";
|
const teamId = "team123";
|
||||||
const type = "http";
|
const type = "http";
|
||||||
@@ -1249,7 +1250,7 @@ describe("monitorModule", function() {
|
|||||||
expect(monitorFindStub.calledOnceWith({ teamId, type })).to.be.true;
|
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
|
// Arrange
|
||||||
monitorFindStub.resolves([]);
|
monitorFindStub.resolves([]);
|
||||||
|
|
||||||
@@ -1266,7 +1267,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const error = new Error("Database error");
|
const error = new Error("Database error");
|
||||||
error.service = "MonitorModule";
|
error.service = "MonitorModule";
|
||||||
@@ -1285,8 +1286,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getMonitorsByTeamId", function() {
|
describe("getMonitorsByTeamId", function () {
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
// Chain stubs for Monitor.find().skip().limit().sort()
|
// Chain stubs for Monitor.find().skip().limit().sort()
|
||||||
|
|
||||||
// Stub for CHECK_MODEL_LOOKUP model find
|
// Stub for CHECK_MODEL_LOOKUP model find
|
||||||
@@ -1297,11 +1298,11 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return monitors with basic query parameters", async function() {
|
it("should return monitors with basic query parameters", async function () {
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||||
@@ -1334,7 +1335,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result).to.have.property("monitorCount", 2);
|
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 = [
|
const mockMonitors = [
|
||||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||||
@@ -1367,7 +1368,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result).to.have.property("monitorCount", 2);
|
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 = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: {
|
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 = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: {
|
query: {
|
||||||
@@ -1420,7 +1421,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle pagination parameters", async function() {
|
it("should handle pagination parameters", async function () {
|
||||||
const req = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: {
|
query: {
|
||||||
@@ -1445,7 +1446,7 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle sorting parameters", async function() {
|
it("should handle sorting parameters", async function () {
|
||||||
const req = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: {
|
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
|
// Arrange
|
||||||
const req = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
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 = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: { normalize: "true" },
|
query: { normalize: "true" },
|
||||||
@@ -1531,7 +1532,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.monitors).to.have.lengthOf(2);
|
expect(result.monitors).to.have.lengthOf(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
const req = {
|
const req = {
|
||||||
params: { teamId: "team123" },
|
params: { teamId: "team123" },
|
||||||
query: {},
|
query: {},
|
||||||
@@ -1557,8 +1558,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createMonitor", function() {
|
describe("createMonitor", function () {
|
||||||
it("should create a monitor without notifications", async function() {
|
it("should create a monitor without notifications", async function () {
|
||||||
let monitorSaveStub = sinon.stub(Monitor.prototype, "save").resolves();
|
let monitorSaveStub = sinon.stub(Monitor.prototype, "save").resolves();
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
@@ -1583,7 +1584,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result.url).to.equal(expectedMonitor.url);
|
expect(result.url).to.equal(expectedMonitor.url);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
const req = {
|
const req = {
|
||||||
body: {
|
body: {
|
||||||
name: "Test Monitor",
|
name: "Test Monitor",
|
||||||
@@ -1600,8 +1601,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteMonitor", function() {
|
describe("deleteMonitor", function () {
|
||||||
it("should delete a monitor successfully", async function() {
|
it("should delete a monitor successfully", async function () {
|
||||||
const monitorId = "123456789";
|
const monitorId = "123456789";
|
||||||
const mockMonitor = {
|
const mockMonitor = {
|
||||||
_id: monitorId,
|
_id: monitorId,
|
||||||
@@ -1621,10 +1622,11 @@ describe("monitorModule", function() {
|
|||||||
sinon.assert.calledWith(monitorFindByIdAndDeleteStub, monitorId);
|
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 monitorId = "nonexistent123";
|
||||||
const req = {
|
const req = {
|
||||||
params: { monitorId },
|
params: { monitorId },
|
||||||
|
language: 'en',
|
||||||
};
|
};
|
||||||
|
|
||||||
monitorFindByIdAndDeleteStub.resolves(null);
|
monitorFindByIdAndDeleteStub.resolves(null);
|
||||||
@@ -1633,13 +1635,13 @@ describe("monitorModule", function() {
|
|||||||
await deleteMonitor(req);
|
await deleteMonitor(req);
|
||||||
expect.fail("Should have thrown an error");
|
expect.fail("Should have thrown an error");
|
||||||
} catch (err) {
|
} 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.service).to.equal("monitorModule");
|
||||||
expect(err.method).to.equal("deleteMonitor");
|
expect(err.method).to.equal("deleteMonitor");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
const monitorId = "123456789";
|
const monitorId = "123456789";
|
||||||
const req = {
|
const req = {
|
||||||
params: { monitorId },
|
params: { monitorId },
|
||||||
@@ -1659,8 +1661,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteAllMonitors", function() {
|
describe("deleteAllMonitors", function () {
|
||||||
it("should delete all monitors for a team successfully", async function() {
|
it("should delete all monitors for a team successfully", async function () {
|
||||||
const teamId = "team123";
|
const teamId = "team123";
|
||||||
const mockMonitors = [
|
const mockMonitors = [
|
||||||
{ _id: "1", name: "Monitor 1", teamId },
|
{ _id: "1", name: "Monitor 1", teamId },
|
||||||
@@ -1680,7 +1682,7 @@ describe("monitorModule", function() {
|
|||||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
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";
|
const teamId = "emptyTeam";
|
||||||
|
|
||||||
monitorFindStub.resolves([]);
|
monitorFindStub.resolves([]);
|
||||||
@@ -1696,7 +1698,7 @@ describe("monitorModule", function() {
|
|||||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
const teamId = "team123";
|
const teamId = "team123";
|
||||||
const dbError = new Error("Database connection error");
|
const dbError = new Error("Database connection error");
|
||||||
monitorFindStub.rejects(dbError);
|
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";
|
const teamId = "team123";
|
||||||
monitorFindStub.resolves([{ _id: "1", name: "Monitor 1" }]);
|
monitorFindStub.resolves([{ _id: "1", name: "Monitor 1" }]);
|
||||||
monitorDeleteManyStub.rejects(new Error("Delete operation failed"));
|
monitorDeleteManyStub.rejects(new Error("Delete operation failed"));
|
||||||
@@ -1725,14 +1727,14 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteMonitorsByUserId", function() {
|
describe("deleteMonitorsByUserId", function () {
|
||||||
beforeEach(function() {});
|
beforeEach(function () { });
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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
|
// Arrange
|
||||||
const userId = "user123";
|
const userId = "user123";
|
||||||
const mockResult = {
|
const mockResult = {
|
||||||
@@ -1750,7 +1752,7 @@ describe("monitorModule", function() {
|
|||||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
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
|
// Arrange
|
||||||
const userId = "nonexistentUser";
|
const userId = "nonexistentUser";
|
||||||
const mockResult = {
|
const mockResult = {
|
||||||
@@ -1768,7 +1770,7 @@ describe("monitorModule", function() {
|
|||||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const userId = "user123";
|
const userId = "user123";
|
||||||
const dbError = new Error("Database connection error");
|
const dbError = new Error("Database connection error");
|
||||||
@@ -1786,8 +1788,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("editMonitor", function() {
|
describe("editMonitor", function () {
|
||||||
it("should edit a monitor successfully", async function() {
|
it("should edit a monitor successfully", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const candidateId = "monitor123";
|
const candidateId = "monitor123";
|
||||||
const candidateMonitor = {
|
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
|
// Arrange
|
||||||
const candidateId = "nonexistent123";
|
const candidateId = "nonexistent123";
|
||||||
const candidateMonitor = {
|
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
|
// Arrange
|
||||||
const candidateId = "monitor123";
|
const candidateId = "monitor123";
|
||||||
const candidateMonitor = {
|
const candidateMonitor = {
|
||||||
@@ -1880,7 +1882,7 @@ describe("monitorModule", function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle database errors", async function() {
|
it("should handle database errors", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const candidateId = "monitor123";
|
const candidateId = "monitor123";
|
||||||
const candidateMonitor = {
|
const candidateMonitor = {
|
||||||
@@ -1902,8 +1904,8 @@ describe("monitorModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("addDemoMonitors", function() {
|
describe("addDemoMonitors", function () {
|
||||||
it("should add demo monitors successfully", async function() {
|
it("should add demo monitors successfully", async function () {
|
||||||
// Arrange
|
// Arrange
|
||||||
const userId = "user123";
|
const userId = "user123";
|
||||||
const teamId = "team123";
|
const teamId = "team123";
|
||||||
@@ -1912,7 +1914,7 @@ describe("monitorModule", function() {
|
|||||||
expect(result).to.deep.equal([{ _id: "123" }]);
|
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 userId = "user123";
|
||||||
const teamId = "team123";
|
const teamId = "team123";
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ const createQueryChain = (finalResult, comparePasswordResult = false) => ({
|
|||||||
save: sinon.stub().resolves(),
|
save: sinon.stub().resolves(),
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("recoveryModule", function() {
|
describe("recoveryModule", function () {
|
||||||
let deleteManyStub,
|
let deleteManyStub,
|
||||||
saveStub,
|
saveStub,
|
||||||
findOneStub,
|
findOneStub,
|
||||||
@@ -52,9 +52,10 @@ describe("recoveryModule", function() {
|
|||||||
userFindOneStub;
|
userFindOneStub;
|
||||||
let req, res;
|
let req, res;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
body: { email: "test@test.com" },
|
body: { email: "test@test.com" },
|
||||||
|
language: 'en',
|
||||||
};
|
};
|
||||||
deleteManyStub = sinon.stub(RecoveryToken, "deleteMany");
|
deleteManyStub = sinon.stub(RecoveryToken, "deleteMany");
|
||||||
saveStub = sinon.stub(RecoveryToken.prototype, "save");
|
saveStub = sinon.stub(RecoveryToken.prototype, "save");
|
||||||
@@ -64,19 +65,19 @@ describe("recoveryModule", function() {
|
|||||||
userFindOneStub = sinon.stub().resolves();
|
userFindOneStub = sinon.stub().resolves();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestRecoveryToken", function() {
|
describe("requestRecoveryToken", function () {
|
||||||
it("should return a recovery token", async function() {
|
it("should return a recovery token", async function () {
|
||||||
deleteManyStub.resolves();
|
deleteManyStub.resolves();
|
||||||
saveStub.resolves(mockRecoveryToken);
|
saveStub.resolves(mockRecoveryToken);
|
||||||
const result = await requestRecoveryToken(req, res);
|
const result = await requestRecoveryToken(req, res);
|
||||||
expect(result.email).to.equal(mockRecoveryToken.email);
|
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");
|
const err = new Error("Test error");
|
||||||
deleteManyStub.rejects(err);
|
deleteManyStub.rejects(err);
|
||||||
try {
|
try {
|
||||||
@@ -88,24 +89,24 @@ describe("recoveryModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("validateRecoveryToken", function() {
|
describe("validateRecoveryToken", function () {
|
||||||
it("should return a recovery token if found", async function() {
|
it("should return a recovery token if found", async function () {
|
||||||
findOneStub.resolves(mockRecoveryToken);
|
findOneStub.resolves(mockRecoveryToken);
|
||||||
const result = await validateRecoveryToken(req, res);
|
const result = await validateRecoveryToken(req, res);
|
||||||
expect(result).to.deep.equal(mockRecoveryToken);
|
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);
|
findOneStub.resolves(null);
|
||||||
try {
|
try {
|
||||||
await validateRecoveryToken(req, res);
|
await validateRecoveryToken(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error).to.exist;
|
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");
|
const err = new Error("Test error");
|
||||||
findOneStub.rejects(err);
|
findOneStub.rejects(err);
|
||||||
try {
|
try {
|
||||||
@@ -117,40 +118,41 @@ describe("recoveryModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("resetPassword", function() {
|
describe("resetPassword", function () {
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req.body = {
|
req.body = {
|
||||||
password: "test",
|
password: "test",
|
||||||
newPassword: "test1",
|
newPassword: "test1",
|
||||||
};
|
};
|
||||||
|
req.language = 'en';
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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);
|
findOneStub.resolves(null);
|
||||||
try {
|
try {
|
||||||
await resetPassword(req, res);
|
await resetPassword(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error).to.exist;
|
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);
|
findOneStub.resolves(mockRecoveryToken);
|
||||||
userFindOneStub = sinon.stub(User, "findOne").resolves(null);
|
userFindOneStub = sinon.stub(User, "findOne").resolves(null);
|
||||||
try {
|
try {
|
||||||
await resetPassword(req, res);
|
await resetPassword(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error).to.exist;
|
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);
|
findOneStub.resolves(mockRecoveryToken);
|
||||||
saveStub.resolves();
|
saveStub.resolves();
|
||||||
userFindOneStub = sinon
|
userFindOneStub = sinon
|
||||||
@@ -160,11 +162,11 @@ describe("recoveryModule", function() {
|
|||||||
await resetPassword(req, res);
|
await resetPassword(req, res);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error).to.exist;
|
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);
|
findOneStub.resolves(mockRecoveryToken);
|
||||||
saveStub.resolves();
|
saveStub.resolves();
|
||||||
userFindOneStub = sinon
|
userFindOneStub = sinon
|
||||||
|
|||||||
@@ -6,31 +6,32 @@ import {
|
|||||||
import StatusPage from "../../db/models/StatusPage.js";
|
import StatusPage from "../../db/models/StatusPage.js";
|
||||||
import { errorMessages } from "../../utils/messages.js";
|
import { errorMessages } from "../../utils/messages.js";
|
||||||
|
|
||||||
describe("statusPageModule", function() {
|
describe("statusPageModule", function () {
|
||||||
let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub;
|
let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub, mockLanguage;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
|
mockLanguage = 'en';
|
||||||
statusPageSaveStub = sinon.stub(StatusPage.prototype, "save");
|
statusPageSaveStub = sinon.stub(StatusPage.prototype, "save");
|
||||||
statusPageFindOneStub = sinon.stub(StatusPage, "findOne");
|
statusPageFindOneStub = sinon.stub(StatusPage, "findOne");
|
||||||
statusPageFindStub = sinon.stub(StatusPage, "find");
|
statusPageFindStub = sinon.stub(StatusPage, "find");
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("createStatusPage", function() {
|
describe("createStatusPage", function () {
|
||||||
it("should throw an error if a non-unique url is provided", async function() {
|
it("should throw an error if a non-unique url is provided", async function () {
|
||||||
statusPageFindOneStub.resolves(true);
|
statusPageFindOneStub.resolves(true);
|
||||||
try {
|
try {
|
||||||
await createStatusPage({ url: "test" });
|
await createStatusPage({ url: "test" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error.status).to.equal(400);
|
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");
|
const err = new Error("test");
|
||||||
err.code = 11000;
|
err.code = 11000;
|
||||||
statusPageSaveStub.rejects(err);
|
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);
|
statusPageFindOneStub.resolves(null);
|
||||||
statusPageFindStub.resolves([]);
|
statusPageFindStub.resolves([]);
|
||||||
const mockStatusPage = { url: "test" };
|
const mockStatusPage = { url: "test" };
|
||||||
@@ -51,21 +52,21 @@ describe("statusPageModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getStatusPageByUrl", function() {
|
describe("getStatusPageByUrl", function () {
|
||||||
it("should throw an error if a status page is not found", async function() {
|
it("should throw an error if a status page is not found", async function () {
|
||||||
statusPageFindOneStub.resolves(null);
|
statusPageFindOneStub.resolves(null);
|
||||||
try {
|
try {
|
||||||
await getStatusPageByUrl("test");
|
await getStatusPageByUrl("test");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error.status).to.equal(404);
|
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" };
|
const mockStatusPage = { url: "test" };
|
||||||
statusPageFindOneStub.resolves(mockStatusPage);
|
statusPageFindOneStub.resolves(mockStatusPage);
|
||||||
const statusPage = await getStatusPageByUrl(mockStatusPage.url);
|
const statusPage = await getStatusPageByUrl(mockStatusPage.url, mockLanguage);
|
||||||
expect(statusPage).to.exist;
|
expect(statusPage).to.exist;
|
||||||
expect(statusPage).to.deep.equal(mockStatusPage);
|
expect(statusPage).to.deep.equal(mockStatusPage);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ const imageFile = {
|
|||||||
image: 1,
|
image: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
describe("userModule", function() {
|
const mockLanguage = 'en';
|
||||||
|
|
||||||
|
describe("userModule", function () {
|
||||||
let teamSaveStub,
|
let teamSaveStub,
|
||||||
teamFindByIdAndDeleteStub,
|
teamFindByIdAndDeleteStub,
|
||||||
userSaveStub,
|
userSaveStub,
|
||||||
@@ -40,7 +42,7 @@ describe("userModule", function() {
|
|||||||
generateAvatarImageStub,
|
generateAvatarImageStub,
|
||||||
parseBooleanStub;
|
parseBooleanStub;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
teamSaveStub = sinon.stub(TeamModel.prototype, "save");
|
teamSaveStub = sinon.stub(TeamModel.prototype, "save");
|
||||||
teamFindByIdAndDeleteStub = sinon.stub(TeamModel, "findByIdAndDelete");
|
teamFindByIdAndDeleteStub = sinon.stub(TeamModel, "findByIdAndDelete");
|
||||||
userSaveStub = sinon.stub(UserModel.prototype, "save");
|
userSaveStub = sinon.stub(UserModel.prototype, "save");
|
||||||
@@ -54,12 +56,12 @@ describe("userModule", function() {
|
|||||||
parseBooleanStub = sinon.stub().returns(true);
|
parseBooleanStub = sinon.stub().returns(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("insertUser", function() {
|
describe("insertUser", function () {
|
||||||
it("should insert a regular user", async function() {
|
it("should insert a regular user", async function () {
|
||||||
userSaveStub.resolves(mockUser);
|
userSaveStub.resolves(mockUser);
|
||||||
userFindOneStub.returns({
|
userFindOneStub.returns({
|
||||||
select: sinon.stub().returns({
|
select: sinon.stub().returns({
|
||||||
@@ -70,7 +72,7 @@ describe("userModule", function() {
|
|||||||
expect(result).to.deep.equal(mockUser);
|
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);
|
userSaveStub.resolves(mockSuperUser);
|
||||||
userFindOneStub.returns({
|
userFindOneStub.returns({
|
||||||
select: sinon.stub().returns({
|
select: sinon.stub().returns({
|
||||||
@@ -81,7 +83,7 @@ describe("userModule", function() {
|
|||||||
expect(result).to.deep.equal(mockSuperUser);
|
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");
|
const err = new Error("test error");
|
||||||
userSaveStub.rejects(err);
|
userSaveStub.rejects(err);
|
||||||
try {
|
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");
|
const err = new Error("test error");
|
||||||
err.code = 11000;
|
err.code = 11000;
|
||||||
userSaveStub.rejects(err);
|
userSaveStub.rejects(err);
|
||||||
@@ -105,8 +107,8 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getUserByEmail", function() {
|
describe("getUserByEmail", function () {
|
||||||
it("should return a user", async function() {
|
it("should return a user", async function () {
|
||||||
userFindOneStub.returns({
|
userFindOneStub.returns({
|
||||||
select: sinon.stub().resolves(mockUser),
|
select: sinon.stub().resolves(mockUser),
|
||||||
});
|
});
|
||||||
@@ -115,23 +117,23 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getUserByEmail", function() {
|
describe("getUserByEmail", function () {
|
||||||
it("throw an error if a user is not found", async function() {
|
it("throw an error if a user is not found", async function () {
|
||||||
userFindOneStub.returns({
|
userFindOneStub.returns({
|
||||||
select: sinon.stub().resolves(null),
|
select: sinon.stub().resolves(null),
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
await getUserByEmail(mockUser.email);
|
await getUserByEmail(mockUser.email);
|
||||||
} catch (error) {
|
} 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;
|
let req, res;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
req = {
|
req = {
|
||||||
params: {
|
params: {
|
||||||
userId: "testId",
|
userId: "testId",
|
||||||
@@ -148,9 +150,9 @@ describe("userModule", function() {
|
|||||||
res = {};
|
res = {};
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {});
|
afterEach(function () { });
|
||||||
|
|
||||||
it("should update a user", async function() {
|
it("should update a user", async function () {
|
||||||
parseBooleanStub.returns(false);
|
parseBooleanStub.returns(false);
|
||||||
userFindByIdAndUpdateStub.returns({
|
userFindByIdAndUpdateStub.returns({
|
||||||
select: sinon.stub().returns({
|
select: sinon.stub().returns({
|
||||||
@@ -166,7 +168,7 @@ describe("userModule", function() {
|
|||||||
expect(result).to.deep.equal(mockUser);
|
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";
|
req.body.deleteProfileImage = "true";
|
||||||
userFindByIdAndUpdateStub.returns({
|
userFindByIdAndUpdateStub.returns({
|
||||||
select: sinon.stub().returns({
|
select: sinon.stub().returns({
|
||||||
@@ -182,7 +184,7 @@ describe("userModule", function() {
|
|||||||
expect(result).to.deep.equal(mockUser);
|
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");
|
const err = new Error("test error");
|
||||||
userFindByIdAndUpdateStub.throws(err);
|
userFindByIdAndUpdateStub.throws(err);
|
||||||
try {
|
try {
|
||||||
@@ -194,23 +196,23 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteUser", function() {
|
describe("deleteUser", function () {
|
||||||
it("should return a deleted user", async function() {
|
it("should return a deleted user", async function () {
|
||||||
userFindByIdAndDeleteStub.resolves(mockUser);
|
userFindByIdAndDeleteStub.resolves(mockUser);
|
||||||
const result = await deleteUser("testId");
|
const result = await deleteUser("testId");
|
||||||
expect(result).to.deep.equal(mockUser);
|
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 {
|
try {
|
||||||
await deleteUser("testId");
|
await deleteUser("testId");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error).to.exist;
|
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");
|
const err = new Error("test error");
|
||||||
userFindByIdAndDeleteStub.throws(err);
|
userFindByIdAndDeleteStub.throws(err);
|
||||||
try {
|
try {
|
||||||
@@ -222,14 +224,14 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteTeam", function() {
|
describe("deleteTeam", function () {
|
||||||
it("should return true if team deleted", async function() {
|
it("should return true if team deleted", async function () {
|
||||||
teamFindByIdAndDeleteStub.resolves();
|
teamFindByIdAndDeleteStub.resolves();
|
||||||
const result = await deleteTeam("testId");
|
const result = await deleteTeam("testId");
|
||||||
expect(result).to.equal(true);
|
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");
|
const err = new Error("test error");
|
||||||
teamFindByIdAndDeleteStub.throws(err);
|
teamFindByIdAndDeleteStub.throws(err);
|
||||||
try {
|
try {
|
||||||
@@ -241,14 +243,14 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteAllOtherUsers", function() {
|
describe("deleteAllOtherUsers", function () {
|
||||||
it("should return true if all other users deleted", async function() {
|
it("should return true if all other users deleted", async function () {
|
||||||
userDeleteManyStub.resolves(true);
|
userDeleteManyStub.resolves(true);
|
||||||
const result = await deleteAllOtherUsers();
|
const result = await deleteAllOtherUsers();
|
||||||
expect(result).to.equal(true);
|
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");
|
const err = new Error("test error");
|
||||||
userDeleteManyStub.throws(err);
|
userDeleteManyStub.throws(err);
|
||||||
try {
|
try {
|
||||||
@@ -260,8 +262,8 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getAllUsers", function() {
|
describe("getAllUsers", function () {
|
||||||
it("should return all users", async function() {
|
it("should return all users", async function () {
|
||||||
userFindStub.returns({
|
userFindStub.returns({
|
||||||
select: sinon.stub().returns({
|
select: sinon.stub().returns({
|
||||||
select: sinon.stub().resolves([mockUser]),
|
select: sinon.stub().resolves([mockUser]),
|
||||||
@@ -271,7 +273,7 @@ describe("userModule", function() {
|
|||||||
expect(result).to.deep.equal([mockUser]);
|
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");
|
const err = new Error("test error");
|
||||||
userFindStub.throws(err);
|
userFindStub.throws(err);
|
||||||
try {
|
try {
|
||||||
@@ -283,14 +285,14 @@ describe("userModule", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("logoutUser", function() {
|
describe("logoutUser", function () {
|
||||||
it("should return true if user logged out", async function() {
|
it("should return true if user logged out", async function () {
|
||||||
userUpdateOneStub.resolves(true);
|
userUpdateOneStub.resolves(true);
|
||||||
const result = await logoutUser("testId");
|
const result = await logoutUser("testId");
|
||||||
expect(result).to.equal(true);
|
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");
|
const err = new Error("test error");
|
||||||
userUpdateOneStub.throws(err);
|
userUpdateOneStub.throws(err);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import NetworkService from "../../service/networkService.js";
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import http from "http";
|
import http from "http";
|
||||||
import { errorMessages } from "../../utils/messages.js";
|
import { errorMessages } from "../../utils/messages.js";
|
||||||
describe("Network Service", function() {
|
describe("Network Service", function () {
|
||||||
let axios, ping, Docker, logger, networkService;
|
let axios, ping, Docker, logger, networkService;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
axios = {
|
axios = {
|
||||||
get: sinon.stub().resolves({
|
get: sinon.stub().resolves({
|
||||||
data: { foo: "bar" },
|
data: { foo: "bar" },
|
||||||
@@ -35,22 +35,22 @@ describe("Network Service", function() {
|
|||||||
networkService = new NetworkService(axios, ping, logger, http, Docker);
|
networkService = new NetworkService(axios, ping, logger, http, Docker);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("constructor", function() {
|
describe("constructor", function () {
|
||||||
it("should create a new NetworkService instance", function() {
|
it("should create a new NetworkService instance", function () {
|
||||||
const networkService = new NetworkService();
|
const networkService = new NetworkService();
|
||||||
expect(networkService).to.be.an.instanceOf(NetworkService);
|
expect(networkService).to.be.an.instanceOf(NetworkService);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("timeRequest", function() {
|
describe("timeRequest", function () {
|
||||||
it("should time an asynchronous operation", async function() {
|
it("should time an asynchronous operation", async function () {
|
||||||
const operation = sinon.stub().resolves("success");
|
const operation = sinon.stub().resolves("success");
|
||||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||||
expect(response).to.equal("success");
|
expect(response).to.equal("success");
|
||||||
expect(responseTime).to.be.a("number");
|
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 error = new Error("Test error");
|
||||||
const operation = sinon.stub().throws(error);
|
const operation = sinon.stub().throws(error);
|
||||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||||
@@ -60,8 +60,8 @@ describe("Network Service", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestPing", function() {
|
describe("requestPing", function () {
|
||||||
it("should return a response object if ping successful", async function() {
|
it("should return a response object if ping successful", async function () {
|
||||||
const pingResult = await networkService.requestPing({
|
const pingResult = await networkService.requestPing({
|
||||||
data: { url: "http://test.com", _id: "123" },
|
data: { url: "http://test.com", _id: "123" },
|
||||||
});
|
});
|
||||||
@@ -71,7 +71,7 @@ describe("Network Service", function() {
|
|||||||
expect(pingResult.status).to.be.true;
|
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");
|
const error = new Error("Test error");
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
.stub()
|
.stub()
|
||||||
@@ -86,7 +86,7 @@ describe("Network Service", function() {
|
|||||||
expect(pingResult.code).to.equal(networkService.PING_ERROR);
|
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");
|
const error = new Error("test error");
|
||||||
networkService.timeRequest = sinon.stub().throws(error);
|
networkService.timeRequest = sinon.stub().throws(error);
|
||||||
try {
|
try {
|
||||||
@@ -100,8 +100,8 @@ describe("Network Service", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestHttp", function() {
|
describe("requestHttp", function () {
|
||||||
it("should return a response object if http successful", async function() {
|
it("should return a response object if http successful", async function () {
|
||||||
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
|
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
|
||||||
const httpResult = await networkService.requestHttp(job);
|
const httpResult = await networkService.requestHttp(job);
|
||||||
expect(httpResult.monitorId).to.equal("123");
|
expect(httpResult.monitorId).to.equal("123");
|
||||||
@@ -110,7 +110,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.status).to.be.true;
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = { status: 404 };
|
error.response = { status: 404 };
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -125,7 +125,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.code).to.equal(404);
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = {};
|
error.response = {};
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -140,7 +140,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
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");
|
const error = new Error("test error");
|
||||||
networkService.timeRequest = sinon.stub().throws(error);
|
networkService.timeRequest = sinon.stub().throws(error);
|
||||||
try {
|
try {
|
||||||
@@ -154,8 +154,8 @@ describe("Network Service", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestPagespeed", function() {
|
describe("requestPagespeed", function () {
|
||||||
it("should return a response object if pagespeed successful", async function() {
|
it("should return a response object if pagespeed successful", async function () {
|
||||||
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
|
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
|
||||||
const pagespeedResult = await networkService.requestPagespeed(job);
|
const pagespeedResult = await networkService.requestPagespeed(job);
|
||||||
expect(pagespeedResult.monitorId).to.equal("123");
|
expect(pagespeedResult.monitorId).to.equal("123");
|
||||||
@@ -164,7 +164,7 @@ describe("Network Service", function() {
|
|||||||
expect(pagespeedResult.status).to.be.true;
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = { status: 404 };
|
error.response = { status: 404 };
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -179,7 +179,7 @@ describe("Network Service", function() {
|
|||||||
expect(pagespeedResult.code).to.equal(404);
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = {};
|
error.response = {};
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -194,7 +194,7 @@ describe("Network Service", function() {
|
|||||||
expect(pagespeedResult.code).to.equal(networkService.NETWORK_ERROR);
|
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");
|
const error = new Error("test error");
|
||||||
networkService.timeRequest = sinon.stub().throws(error);
|
networkService.timeRequest = sinon.stub().throws(error);
|
||||||
try {
|
try {
|
||||||
@@ -208,8 +208,8 @@ describe("Network Service", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestHardware", function() {
|
describe("requestHardware", function () {
|
||||||
it("should return a response object if hardware successful", async function() {
|
it("should return a response object if hardware successful", async function () {
|
||||||
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
|
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
|
||||||
const httpResult = await networkService.requestHardware(job);
|
const httpResult = await networkService.requestHardware(job);
|
||||||
expect(httpResult.monitorId).to.equal("123");
|
expect(httpResult.monitorId).to.equal("123");
|
||||||
@@ -218,7 +218,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.status).to.be.true;
|
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 = {
|
const job = {
|
||||||
data: {
|
data: {
|
||||||
url: "http://test.com",
|
url: "http://test.com",
|
||||||
@@ -234,7 +234,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.status).to.be.true;
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = { status: 404 };
|
error.response = { status: 404 };
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -249,7 +249,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.code).to.equal(404);
|
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");
|
const error = new Error("Test error");
|
||||||
error.response = {};
|
error.response = {};
|
||||||
networkService.timeRequest = sinon
|
networkService.timeRequest = sinon
|
||||||
@@ -264,7 +264,7 @@ describe("Network Service", function() {
|
|||||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
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");
|
const error = new Error("test error");
|
||||||
networkService.timeRequest = sinon.stub().throws(error);
|
networkService.timeRequest = sinon.stub().throws(error);
|
||||||
try {
|
try {
|
||||||
@@ -278,8 +278,8 @@ describe("Network Service", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("requestDocker", function() {
|
describe("requestDocker", function () {
|
||||||
it("should return a response object if docker successful", async function() {
|
it("should return a response object if docker successful", async function () {
|
||||||
const job = { data: { url: "http://test.com", _id: "123", type: "docker" } };
|
const job = { data: { url: "http://test.com", _id: "123", type: "docker" } };
|
||||||
const dockerResult = await networkService.requestDocker(job);
|
const dockerResult = await networkService.requestDocker(job);
|
||||||
expect(dockerResult.monitorId).to.equal("123");
|
expect(dockerResult.monitorId).to.equal("123");
|
||||||
@@ -288,7 +288,7 @@ describe("Network Service", function() {
|
|||||||
expect(dockerResult.status).to.be.true;
|
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 {
|
Docker = class {
|
||||||
listContainers = sinon.stub().resolves([
|
listContainers = sinon.stub().resolves([
|
||||||
{
|
{
|
||||||
@@ -307,7 +307,7 @@ describe("Network Service", function() {
|
|||||||
expect(dockerResult.code).to.equal(200);
|
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 {
|
Docker = class {
|
||||||
listContainers = sinon.stub().resolves([
|
listContainers = sinon.stub().resolves([
|
||||||
{
|
{
|
||||||
@@ -326,7 +326,7 @@ describe("Network Service", function() {
|
|||||||
expect(dockerResult.code).to.equal(networkService.NETWORK_ERROR);
|
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 {
|
Docker = class {
|
||||||
listContainers = sinon.stub().resolves([
|
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 {
|
Docker = class {
|
||||||
listContainers = sinon.stub().resolves([]);
|
listContainers = sinon.stub().resolves([]);
|
||||||
getContainer = sinon.stub().throws(new Error("test error"));
|
getContainer = sinon.stub().throws(new Error("test error"));
|
||||||
@@ -355,13 +355,13 @@ describe("Network Service", function() {
|
|||||||
try {
|
try {
|
||||||
await networkService.requestDocker(job);
|
await networkService.requestDocker(job);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
expect(error.message).to.equal(errorMessages.DOCKER_NOT_FOUND);
|
expect(error.message).to.equal(errorMessages.DOCKER_NOT_FOUND(mockLanguage));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getStatus", function() {
|
describe("getStatus", function () {
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
networkService.requestPing = sinon.stub();
|
networkService.requestPing = sinon.stub();
|
||||||
networkService.requestHttp = sinon.stub();
|
networkService.requestHttp = sinon.stub();
|
||||||
networkService.requestPagespeed = sinon.stub();
|
networkService.requestPagespeed = sinon.stub();
|
||||||
@@ -369,11 +369,11 @@ describe("Network Service", function() {
|
|||||||
networkService.requestDocker = sinon.stub();
|
networkService.requestDocker = sinon.stub();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function () {
|
||||||
sinon.restore();
|
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" } });
|
await networkService.getStatus({ data: { type: "ping" } });
|
||||||
expect(networkService.requestPing.calledOnce).to.be.true;
|
expect(networkService.requestPing.calledOnce).to.be.true;
|
||||||
expect(networkService.requestDocker.notCalled).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;
|
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" } });
|
await networkService.getStatus({ data: { type: "http" } });
|
||||||
expect(networkService.requestPing.notCalled).to.be.true;
|
expect(networkService.requestPing.notCalled).to.be.true;
|
||||||
expect(networkService.requestDocker.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;
|
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" } });
|
await networkService.getStatus({ data: { type: "pagespeed" } });
|
||||||
expect(networkService.requestPing.notCalled).to.be.true;
|
expect(networkService.requestPing.notCalled).to.be.true;
|
||||||
expect(networkService.requestDocker.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;
|
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" } });
|
await networkService.getStatus({ data: { type: "hardware" } });
|
||||||
expect(networkService.requestHardware.calledOnce).to.be.true;
|
expect(networkService.requestHardware.calledOnce).to.be.true;
|
||||||
expect(networkService.requestDocker.notCalled).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;
|
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" } });
|
await networkService.getStatus({ data: { type: "docker" } });
|
||||||
expect(networkService.requestDocker.calledOnce).to.be.true;
|
expect(networkService.requestDocker.calledOnce).to.be.true;
|
||||||
expect(networkService.requestHardware.notCalled).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;
|
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 {
|
try {
|
||||||
await networkService.getStatus({ data: { type: "unknown" } });
|
await networkService.getStatus({ data: { type: "unknown" } });
|
||||||
} catch (error) {
|
} 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 {
|
try {
|
||||||
await networkService.getStatus({ data: { type: undefined } });
|
await networkService.getStatus({ data: { type: undefined } });
|
||||||
} catch (error) {
|
} 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 {
|
try {
|
||||||
await networkService.getStatus({});
|
await networkService.getStatus({});
|
||||||
} catch (error) {
|
} 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 {
|
try {
|
||||||
await networkService.getStatus(null);
|
await networkService.getStatus(null);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,23 +1,25 @@
|
|||||||
import { errorMessages, successMessages } from "../../utils/messages.js";
|
import { errorMessages, successMessages } from "../../utils/messages.js";
|
||||||
describe("Messages", function() {
|
describe("Messages", function () {
|
||||||
describe("messages - errorMessages", function() {
|
describe("messages - errorMessages", function () {
|
||||||
it("should have a DB_FIND_MONITOR_BY_ID function", function() {
|
it("should have a DB_FIND_MONITOR_BY_ID function", function () {
|
||||||
const monitorId = "12345";
|
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`
|
`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";
|
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}`
|
`No checks found for monitor with id ${monitorId}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("messages - successMessages", function() {
|
describe("messages - successMessages", function () {
|
||||||
it("should have a MONITOR_GET_BY_USER_ID function", function() {
|
it("should have a MONITOR_GET_BY_USER_ID function", function () {
|
||||||
const userId = "12345";
|
const userId = "12345";
|
||||||
expect(successMessages.MONITOR_GET_BY_USER_ID(userId)).to.equal(
|
expect(successMessages.MONITOR_GET_BY_USER_ID(userId)).to.equal(
|
||||||
`Got monitor for ${userId} successfully"`
|
`Got monitor for ${userId} successfully"`
|
||||||
|
|||||||
@@ -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('');
|
||||||
|
};
|
||||||
+95
-86
@@ -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 = {
|
const errorMessages = {
|
||||||
// General Errors:
|
// General Errors:
|
||||||
FRIENDLY_ERROR: "Something went wrong...",
|
FRIENDLY_ERROR: (language) => getTranslatedMessage('FRIENDLY_ERROR', language),
|
||||||
UNKNOWN_ERROR: "An unknown error occurred",
|
UNKNOWN_ERROR: (language) => getTranslatedMessage('UNKNOWN_ERROR', language),
|
||||||
|
|
||||||
// Auth Controller
|
// Auth Controller
|
||||||
UNAUTHORIZED: "Unauthorized access",
|
UNAUTHORIZED: (language) => getTranslatedMessage('UNAUTHORIZED', language),
|
||||||
AUTH_ADMIN_EXISTS: "Admin already exists",
|
AUTH_ADMIN_EXISTS: (language) => getTranslatedMessage('AUTH_ADMIN_EXISTS', language),
|
||||||
AUTH_INVITE_NOT_FOUND: "Invite not found",
|
AUTH_INVITE_NOT_FOUND: (language) => getTranslatedMessage('AUTH_INVITE_NOT_FOUND', language),
|
||||||
|
|
||||||
//Error handling middleware
|
//Error handling middleware
|
||||||
UNKNOWN_SERVICE: "Unknown service",
|
UNKNOWN_SERVICE: (language) => getTranslatedMessage('UNKNOWN_SERVICE', language),
|
||||||
NO_AUTH_TOKEN: "No auth token provided",
|
NO_AUTH_TOKEN: (language) => getTranslatedMessage('NO_AUTH_TOKEN', language),
|
||||||
INVALID_AUTH_TOKEN: "Invalid auth token",
|
INVALID_AUTH_TOKEN: (language) => getTranslatedMessage('INVALID_AUTH_TOKEN', language),
|
||||||
EXPIRED_AUTH_TOKEN: "Token expired",
|
EXPIRED_AUTH_TOKEN: (language) => getTranslatedMessage('EXPIRED_AUTH_TOKEN', language),
|
||||||
NO_REFRESH_TOKEN: "No refresh token provided",
|
NO_REFRESH_TOKEN: (language) => getTranslatedMessage('NO_REFRESH_TOKEN', language),
|
||||||
INVALID_REFRESH_TOKEN: "Invalid refresh token",
|
INVALID_REFRESH_TOKEN: (language) => getTranslatedMessage('INVALID_REFRESH_TOKEN', language),
|
||||||
EXPIRED_REFRESH_TOKEN: "Refresh token expired",
|
EXPIRED_REFRESH_TOKEN: (language) => getTranslatedMessage('EXPIRED_REFRESH_TOKEN', language),
|
||||||
REQUEST_NEW_ACCESS_TOKEN: "Request new access token",
|
REQUEST_NEW_ACCESS_TOKEN: (language) => getTranslatedMessage('REQUEST_NEW_ACCESS_TOKEN', language),
|
||||||
|
|
||||||
//Payload
|
//Payload
|
||||||
INVALID_PAYLOAD: "Invalid payload",
|
INVALID_PAYLOAD: (language) => getTranslatedMessage('INVALID_PAYLOAD', language),
|
||||||
|
|
||||||
//Ownership Middleware
|
//Ownership Middleware
|
||||||
VERIFY_OWNER_NOT_FOUND: "Document not found",
|
VERIFY_OWNER_NOT_FOUND: (language) => getTranslatedMessage('VERIFY_OWNER_NOT_FOUND', language),
|
||||||
VERIFY_OWNER_UNAUTHORIZED: "Unauthorized access",
|
VERIFY_OWNER_UNAUTHORIZED: (language) => getTranslatedMessage('VERIFY_OWNER_UNAUTHORIZED', language),
|
||||||
|
|
||||||
//Permissions Middleware
|
//Permissions Middleware
|
||||||
INSUFFICIENT_PERMISSIONS: "Insufficient permissions",
|
INSUFFICIENT_PERMISSIONS: (language) => getTranslatedMessage('INSUFFICIENT_PERMISSIONS', language),
|
||||||
|
|
||||||
//DB Errors
|
//DB Errors
|
||||||
DB_USER_EXISTS: "User already exists",
|
DB_USER_EXISTS: (language) => getTranslatedMessage('DB_USER_EXISTS', language),
|
||||||
DB_USER_NOT_FOUND: "User not found",
|
DB_USER_NOT_FOUND: (language) => getTranslatedMessage('DB_USER_NOT_FOUND', language),
|
||||||
DB_TOKEN_NOT_FOUND: "Token not found",
|
DB_TOKEN_NOT_FOUND: (language) => getTranslatedMessage('DB_TOKEN_NOT_FOUND', language),
|
||||||
DB_RESET_PASSWORD_BAD_MATCH: "New password must be different from old password",
|
DB_RESET_PASSWORD_BAD_MATCH: (language) => getTranslatedMessage('DB_RESET_PASSWORD_BAD_MATCH', language),
|
||||||
DB_FIND_MONITOR_BY_ID: (monitorId) => `Monitor with id ${monitorId} not found`,
|
DB_FIND_MONITOR_BY_ID: (monitorId, language) => getTranslatedMessage('DB_FIND_MONITOR_BY_ID', language).replace('{monitorId}', monitorId),
|
||||||
DB_DELETE_CHECKS: (monitorId) => `No checks found for monitor with id ${monitorId}`,
|
DB_DELETE_CHECKS: (monitorId, language) => getTranslatedMessage('DB_DELETE_CHECKS', language).replace('{monitorId}', monitorId),
|
||||||
|
|
||||||
//Auth errors
|
//Auth errors
|
||||||
AUTH_INCORRECT_PASSWORD: "Incorrect password",
|
AUTH_INCORRECT_PASSWORD: (language) => getTranslatedMessage('AUTH_INCORRECT_PASSWORD', language),
|
||||||
AUTH_UNAUTHORIZED: "Unauthorized access",
|
AUTH_UNAUTHORIZED: (language) => getTranslatedMessage('AUTH_UNAUTHORIZED', language),
|
||||||
|
|
||||||
// Monitor Errors
|
// Monitor Errors
|
||||||
MONITOR_GET_BY_ID: "Monitor not found",
|
MONITOR_GET_BY_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_ID', language),
|
||||||
MONITOR_GET_BY_USER_ID: "No monitors found for user",
|
MONITOR_GET_BY_USER_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_USER_ID', language),
|
||||||
|
|
||||||
// Job Queue Errors
|
// Job Queue Errors
|
||||||
JOB_QUEUE_WORKER_CLOSE: "Error closing worker",
|
JOB_QUEUE_WORKER_CLOSE: (language) => getTranslatedMessage('JOB_QUEUE_WORKER_CLOSE', language),
|
||||||
JOB_QUEUE_DELETE_JOB: "Job not found in queue",
|
JOB_QUEUE_DELETE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_DELETE_JOB', language),
|
||||||
JOB_QUEUE_OBLITERATE: "Error obliterating queue",
|
JOB_QUEUE_OBLITERATE: (language) => getTranslatedMessage('JOB_QUEUE_OBLITERATE', language),
|
||||||
|
|
||||||
// PING Operations
|
// PING Operations
|
||||||
PING_CANNOT_RESOLVE: "No response",
|
PING_CANNOT_RESOLVE: (language) => getTranslatedMessage('PING_CANNOT_RESOLVE', language),
|
||||||
|
|
||||||
// Status Page Errors
|
// Status Page Errors
|
||||||
STATUS_PAGE_NOT_FOUND: "Status page not found",
|
STATUS_PAGE_NOT_FOUND: (language) => getTranslatedMessage('STATUS_PAGE_NOT_FOUND', language),
|
||||||
STATUS_PAGE_URL_NOT_UNIQUE: "Status page url must be unique",
|
STATUS_PAGE_URL_NOT_UNIQUE: (language) => getTranslatedMessage('STATUS_PAGE_URL_NOT_UNIQUE', language),
|
||||||
|
|
||||||
// Docker
|
// Docker
|
||||||
DOCKER_FAIL: "Failed to fetch Docker container information",
|
DOCKER_FAIL: (language) => getTranslatedMessage('DOCKER_FAIL', language),
|
||||||
DOCKER_NOT_FOUND: "Docker container not found",
|
DOCKER_NOT_FOUND: (language) => getTranslatedMessage('DOCKER_NOT_FOUND', language),
|
||||||
|
|
||||||
// Port
|
// Port
|
||||||
PORT_FAIL: "Failed to connect to port",
|
PORT_FAIL: (language) => getTranslatedMessage('PORT_FAIL', language),
|
||||||
};
|
};
|
||||||
|
|
||||||
const successMessages = {
|
const successMessages = {
|
||||||
//Alert Controller
|
//Alert Controller
|
||||||
ALERT_CREATE: "Alert created successfully",
|
ALERT_CREATE: (language) => getTranslatedMessage('ALERT_CREATE', language),
|
||||||
ALERT_GET_BY_USER: "Got alerts successfully",
|
ALERT_GET_BY_USER: (language) => getTranslatedMessage('ALERT_GET_BY_USER', language),
|
||||||
ALERT_GET_BY_MONITOR: "Got alerts by Monitor successfully",
|
ALERT_GET_BY_MONITOR: (language) => getTranslatedMessage('ALERT_GET_BY_MONITOR', language),
|
||||||
ALERT_GET_BY_ID: "Got alert by Id successfully",
|
ALERT_GET_BY_ID: (language) => getTranslatedMessage('ALERT_GET_BY_ID', language),
|
||||||
ALERT_EDIT: "Alert edited successfully",
|
ALERT_EDIT: (language) => getTranslatedMessage('ALERT_EDIT', language),
|
||||||
ALERT_DELETE: "Alert deleted successfully",
|
ALERT_DELETE: (language) => getTranslatedMessage('ALERT_DELETE', language),
|
||||||
|
|
||||||
// Auth Controller
|
// Auth Controller
|
||||||
AUTH_CREATE_USER: "User created successfully",
|
AUTH_CREATE_USER: (language) => getTranslatedMessage('AUTH_CREATE_USER', language),
|
||||||
AUTH_LOGIN_USER: "User logged in successfully",
|
AUTH_LOGIN_USER: (language) => getTranslatedMessage('AUTH_LOGIN_USER', language),
|
||||||
AUTH_LOGOUT_USER: "User logged out successfully",
|
AUTH_LOGOUT_USER: (language) => getTranslatedMessage('AUTH_LOGOUT_USER', language),
|
||||||
AUTH_UPDATE_USER: "User updated successfully",
|
AUTH_UPDATE_USER: (language) => getTranslatedMessage('AUTH_UPDATE_USER', language),
|
||||||
AUTH_CREATE_RECOVERY_TOKEN: "Recovery token created successfully",
|
AUTH_CREATE_RECOVERY_TOKEN: (language) => getTranslatedMessage('AUTH_CREATE_RECOVERY_TOKEN', language),
|
||||||
AUTH_VERIFY_RECOVERY_TOKEN: "Recovery token verified successfully",
|
AUTH_VERIFY_RECOVERY_TOKEN: (language) => getTranslatedMessage('AUTH_VERIFY_RECOVERY_TOKEN', language),
|
||||||
AUTH_RESET_PASSWORD: "Password reset successfully",
|
AUTH_RESET_PASSWORD: (language) => getTranslatedMessage('AUTH_RESET_PASSWORD', language),
|
||||||
AUTH_ADMIN_CHECK: "Admin check completed successfully",
|
AUTH_ADMIN_CHECK: (language) => getTranslatedMessage('AUTH_ADMIN_CHECK', language),
|
||||||
AUTH_DELETE_USER: "User deleted successfully",
|
AUTH_DELETE_USER: (language) => getTranslatedMessage('AUTH_DELETE_USER', language),
|
||||||
AUTH_TOKEN_REFRESHED: "Auth token is refreshed",
|
AUTH_TOKEN_REFRESHED: (language) => getTranslatedMessage('AUTH_TOKEN_REFRESHED', language),
|
||||||
AUTH_GET_ALL_USERS: "Got all users successfully",
|
AUTH_GET_ALL_USERS: (language) => getTranslatedMessage('AUTH_GET_ALL_USERS', language),
|
||||||
|
|
||||||
// Invite Controller
|
// Invite Controller
|
||||||
INVITE_ISSUED: "Invite sent successfully",
|
INVITE_ISSUED: (language) => getTranslatedMessage('INVITE_ISSUED', language),
|
||||||
INVITE_VERIFIED: "Invite verified successfully",
|
INVITE_VERIFIED: (language) => getTranslatedMessage('INVITE_VERIFIED', language),
|
||||||
|
|
||||||
// Check Controller
|
// Check Controller
|
||||||
CHECK_CREATE: "Check created successfully",
|
CHECK_CREATE: (language) => getTranslatedMessage('CHECK_CREATE', language),
|
||||||
CHECK_GET: "Got checks successfully",
|
CHECK_GET: (language) => getTranslatedMessage('CHECK_GET', language),
|
||||||
CHECK_DELETE: "Checks deleted successfully",
|
CHECK_DELETE: (language) => getTranslatedMessage('CHECK_DELETE', language),
|
||||||
CHECK_UPDATE_TTL: "Checks TTL updated successfully",
|
CHECK_UPDATE_TTL: (language) => getTranslatedMessage('CHECK_UPDATE_TTL', language),
|
||||||
|
|
||||||
//Monitor Controller
|
//Monitor Controller
|
||||||
MONITOR_GET_ALL: "Got all monitors successfully",
|
MONITOR_GET_ALL: (language) => getTranslatedMessage('MONITOR_GET_ALL', language),
|
||||||
MONITOR_STATS_BY_ID: "Got monitor stats by Id successfully",
|
MONITOR_STATS_BY_ID: (language) => getTranslatedMessage('MONITOR_STATS_BY_ID', language),
|
||||||
MONITOR_GET_BY_ID: "Got monitor by Id successfully",
|
MONITOR_GET_BY_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_ID', language),
|
||||||
MONITOR_GET_BY_TEAM_ID: "Got monitors by Team Id successfully",
|
MONITOR_GET_BY_TEAM_ID: (language) => getTranslatedMessage('MONITOR_GET_BY_TEAM_ID', language),
|
||||||
MONITOR_GET_BY_USER_ID: (userId) => `Got monitor for ${userId} successfully"`,
|
MONITOR_GET_BY_USER_ID: (userId, language) => getTranslatedMessage('MONITOR_GET_BY_USER_ID', language).replace('{userId}', userId),
|
||||||
MONITOR_CREATE: "Monitor created successfully",
|
MONITOR_CREATE: (language) => getTranslatedMessage('MONITOR_CREATE', language),
|
||||||
MONITOR_DELETE: "Monitor deleted successfully",
|
MONITOR_DELETE: (language) => getTranslatedMessage('MONITOR_DELETE', language),
|
||||||
MONITOR_EDIT: "Monitor edited successfully",
|
MONITOR_EDIT: (language) => getTranslatedMessage('MONITOR_EDIT', language),
|
||||||
MONITOR_CERTIFICATE: "Got monitor certificate successfully",
|
MONITOR_CERTIFICATE: (language) => getTranslatedMessage('MONITOR_CERTIFICATE', language),
|
||||||
MONITOR_DEMO_ADDED: "Successfully added demo monitors",
|
MONITOR_DEMO_ADDED: (language) => getTranslatedMessage('MONITOR_DEMO_ADDED', language),
|
||||||
|
|
||||||
// Queue Controller
|
// Queue Controller
|
||||||
QUEUE_GET_METRICS: "Got metrics successfully",
|
QUEUE_GET_METRICS: (language) => getTranslatedMessage('QUEUE_GET_METRICS', language),
|
||||||
QUEUE_ADD_JOB: "Job added successfully",
|
QUEUE_ADD_JOB: (language) => getTranslatedMessage('QUEUE_ADD_JOB', language),
|
||||||
QUEUE_OBLITERATE: "Queue obliterated",
|
QUEUE_OBLITERATE: (language) => getTranslatedMessage('QUEUE_OBLITERATE', language),
|
||||||
|
|
||||||
//Job Queue
|
//Job Queue
|
||||||
JOB_QUEUE_DELETE_JOB: "Job removed successfully",
|
JOB_QUEUE_DELETE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_DELETE_JOB', language),
|
||||||
JOB_QUEUE_OBLITERATE: "Queue OBLITERATED!!!",
|
JOB_QUEUE_OBLITERATE: (language) => getTranslatedMessage('JOB_QUEUE_OBLITERATE', language),
|
||||||
JOB_QUEUE_PAUSE_JOB: "Job paused successfully",
|
JOB_QUEUE_PAUSE_JOB: (language) => getTranslatedMessage('JOB_QUEUE_PAUSE_JOB', language),
|
||||||
JOB_QUEUE_RESUME_JOB: "Job resumed successfully",
|
JOB_QUEUE_RESUME_JOB: (language) => getTranslatedMessage('JOB_QUEUE_RESUME_JOB', language),
|
||||||
|
|
||||||
//Maintenance Window Controller
|
//Maintenance Window Controller
|
||||||
MAINTENANCE_WINDOW_GET_BY_ID: "Got Maintenance Window by Id successfully",
|
MAINTENANCE_WINDOW_GET_BY_ID: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_GET_BY_ID', language),
|
||||||
MAINTENANCE_WINDOW_CREATE: "Maintenance Window created successfully",
|
MAINTENANCE_WINDOW_CREATE: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_CREATE', language),
|
||||||
MAINTENANCE_WINDOW_GET_BY_TEAM: "Got Maintenance Windows by Team successfully",
|
MAINTENANCE_WINDOW_GET_BY_TEAM: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_GET_BY_TEAM', language),
|
||||||
MAINTENANCE_WINDOW_DELETE: "Maintenance Window deleted successfully",
|
MAINTENANCE_WINDOW_DELETE: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_DELETE', language),
|
||||||
MAINTENANCE_WINDOW_EDIT: "Maintenance Window edited successfully",
|
MAINTENANCE_WINDOW_EDIT: (language) => getTranslatedMessage('MAINTENANCE_WINDOW_EDIT', language),
|
||||||
|
|
||||||
//Ping Operations
|
//Ping Operations
|
||||||
PING_SUCCESS: "Success",
|
PING_SUCCESS: (language) => getTranslatedMessage('PING_SUCCESS', language),
|
||||||
|
|
||||||
// App Settings
|
// App Settings
|
||||||
GET_APP_SETTINGS: "Got app settings successfully",
|
GET_APP_SETTINGS: (language) => getTranslatedMessage('GET_APP_SETTINGS', language),
|
||||||
UPDATE_APP_SETTINGS: "Updated app settings successfully",
|
UPDATE_APP_SETTINGS: (language) => getTranslatedMessage('UPDATE_APP_SETTINGS', language),
|
||||||
|
|
||||||
// Status Page
|
// Status Page
|
||||||
STATUS_PAGE_BY_URL: "Got status page by url successfully",
|
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_UPDATE: "Status page updated successfully",
|
||||||
STATUS_PAGE_BY_TEAM_ID: "Got status pages by team id successfully",
|
STATUS_PAGE_BY_TEAM_ID: "Got status pages by team id successfully",
|
||||||
// Docker
|
// Docker
|
||||||
DOCKER_SUCCESS: "Docker container status fetched successfully",
|
DOCKER_SUCCESS: (language) => getTranslatedMessage('DOCKER_SUCCESS', language),
|
||||||
|
|
||||||
// Port
|
// Port
|
||||||
PORT_SUCCESS: "Port connected successfully",
|
PORT_SUCCESS: (language) => getTranslatedMessage('PORT_SUCCESS', language),
|
||||||
};
|
};
|
||||||
|
|
||||||
export { errorMessages, successMessages };
|
export { errorMessages, successMessages };
|
||||||
Reference in new issue
Block a user