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
@@ -11,11 +11,12 @@ import jwt from "jsonwebtoken";
|
||||
import { successMessages } from "../../utils/messages.js";
|
||||
import sinon from "sinon";
|
||||
|
||||
describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
describe("maintenanceWindowController - createMaintenanceWindows", function () {
|
||||
let req, res, next, stub;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
language: 'en',
|
||||
body: {
|
||||
monitors: ["66ff52e7c5911c61698ac724"],
|
||||
name: "window",
|
||||
@@ -41,11 +42,11 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should reject with an error if body validation fails", async function() {
|
||||
it("should reject with an error if body validation fails", async function () {
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
});
|
||||
@@ -56,14 +57,14 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should reject with an error if jwt.verify fails", async function() {
|
||||
it("should reject with an error if jwt.verify fails", async function () {
|
||||
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
|
||||
await createMaintenanceWindows(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should reject with an error DB operations fail", async function() {
|
||||
it("should reject with an error DB operations fail", async function () {
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
});
|
||||
@@ -74,7 +75,7 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should return success message if all operations are successful", async function() {
|
||||
it("should return success message if all operations are successful", async function () {
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
});
|
||||
@@ -83,13 +84,13 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
|
||||
})
|
||||
).to.be.true;
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should return success message if all operations are successful with active set to undefined", async function() {
|
||||
it("should return success message if all operations are successful with active set to undefined", async function () {
|
||||
req.body.active = undefined;
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
@@ -99,17 +100,17 @@ describe("maintenanceWindowController - createMaintenanceWindows", function() {
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE(req.language),
|
||||
})
|
||||
).to.be.true;
|
||||
stub.restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
||||
describe("maintenanceWindowController - getMaintenanceWindowById", function () {
|
||||
let req, res, next;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: {},
|
||||
params: {
|
||||
@@ -121,6 +122,7 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
||||
settingsService: {
|
||||
getSettings: sinon.stub().returns({ jwtSecret: "jwtSecret" }),
|
||||
},
|
||||
language: 'en',
|
||||
db: {
|
||||
getMaintenanceWindowById: sinon.stub(),
|
||||
},
|
||||
@@ -132,38 +134,38 @@ describe("maintenanceWindowController - getMaintenanceWindowById", function() {
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
it("should reject if param validation fails", async function() {
|
||||
it("should reject if param validation fails", async function () {
|
||||
req.params = {};
|
||||
await getMaintenanceWindowById(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject if DB operations fail", async function() {
|
||||
it("should reject if DB operations fail", async function () {
|
||||
req.db.getMaintenanceWindowById.throws(new Error("DB error"));
|
||||
await getMaintenanceWindowById(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||
});
|
||||
|
||||
it("should return success message with data if all operations are successful", async function() {
|
||||
it("should return success message with data if all operations are successful", async function () {
|
||||
req.db.getMaintenanceWindowById.returns({ id: "123" });
|
||||
await getMaintenanceWindowById(req, res, next);
|
||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID(req.language),
|
||||
data: { id: "123" },
|
||||
})
|
||||
).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function() {
|
||||
describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function () {
|
||||
let req, res, next, stub;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: {},
|
||||
params: {},
|
||||
@@ -177,6 +179,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
db: {
|
||||
getMaintenanceWindowsByTeamId: sinon.stub(),
|
||||
},
|
||||
language: 'en',
|
||||
};
|
||||
res = {
|
||||
status: sinon.stub().returnsThis(),
|
||||
@@ -185,7 +188,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
it("should reject if query validation fails", async function() {
|
||||
it("should reject if query validation fails", async function () {
|
||||
req.query = {
|
||||
invalid: 1,
|
||||
};
|
||||
@@ -194,14 +197,14 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject if jwt.verify fails", async function() {
|
||||
it("should reject if jwt.verify fails", async function () {
|
||||
stub = sinon.stub(jwt, "verify").throws(new jwt.JsonWebTokenError());
|
||||
await getMaintenanceWindowsByTeamId(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should reject with an error if DB operations fail", async function() {
|
||||
it("should reject with an error if DB operations fail", async function () {
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
});
|
||||
@@ -212,7 +215,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
stub.restore();
|
||||
});
|
||||
|
||||
it("should return success message with data if all operations are successful", async function() {
|
||||
it("should return success message with data if all operations are successful", async function () {
|
||||
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||
return { teamId: "123" };
|
||||
});
|
||||
@@ -222,7 +225,7 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM(req.language),
|
||||
data: [{ id: jwt.verify().teamId }],
|
||||
})
|
||||
).to.be.true;
|
||||
@@ -230,10 +233,10 @@ describe("maintenanceWindowController - getMaintenanceWindowsByTeamId", function
|
||||
});
|
||||
});
|
||||
|
||||
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function() {
|
||||
describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", function () {
|
||||
let req, res, next;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: {},
|
||||
params: {
|
||||
@@ -257,25 +260,25 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should reject if param validation fails", async function() {
|
||||
it("should reject if param validation fails", async function () {
|
||||
req.params = {};
|
||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject with an error if DB operations fail", async function() {
|
||||
it("should reject with an error if DB operations fail", async function () {
|
||||
req.db.getMaintenanceWindowsByMonitorId.throws(new Error("DB error"));
|
||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||
});
|
||||
|
||||
it("should return success message with data if all operations are successful", async function() {
|
||||
it("should return success message with data if all operations are successful", async function () {
|
||||
const data = [{ monitorId: "123" }];
|
||||
req.db.getMaintenanceWindowsByMonitorId.returns(data);
|
||||
await getMaintenanceWindowsByMonitorId(req, res, next);
|
||||
@@ -284,17 +287,17 @@ describe("maintenanceWindowController - getMaintenanceWindowsByMonitorId", funct
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_MONITOR(req.language),
|
||||
data: data,
|
||||
})
|
||||
).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe("maintenanceWindowController - deleteMaintenanceWindow", function() {
|
||||
describe("maintenanceWindowController - deleteMaintenanceWindow", function () {
|
||||
let req, res, next;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: {},
|
||||
params: {
|
||||
@@ -318,46 +321,47 @@ describe("maintenanceWindowController - deleteMaintenanceWindow", function() {
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should reject if param validation fails", async function() {
|
||||
it("should reject if param validation fails", async function () {
|
||||
req.params = {};
|
||||
await deleteMaintenanceWindow(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject with an error if DB operations fail", async function() {
|
||||
it("should reject with an error if DB operations fail", async function () {
|
||||
req.db.deleteMaintenanceWindowById.throws(new Error("DB error"));
|
||||
await deleteMaintenanceWindow(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||
});
|
||||
|
||||
it("should return success message if all operations are successful", async function() {
|
||||
it("should return success message if all operations are successful", async function () {
|
||||
await deleteMaintenanceWindow(req, res, next);
|
||||
expect(req.db.deleteMaintenanceWindowById.calledOnceWith(req.params.id));
|
||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_DELETE(req.language),
|
||||
})
|
||||
).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
||||
describe("maintenanceWindowController - editMaintenanceWindow", function () {
|
||||
let req, res, next;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: {
|
||||
active: true,
|
||||
name: "test",
|
||||
},
|
||||
language: 'en',
|
||||
params: {
|
||||
id: "123",
|
||||
},
|
||||
@@ -379,32 +383,32 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
||||
next = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should reject if param validation fails", async function() {
|
||||
it("should reject if param validation fails", async function () {
|
||||
req.params = {};
|
||||
await editMaintenanceWindow(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject if body validation fails", async function() {
|
||||
it("should reject if body validation fails", async function () {
|
||||
req.body = { invalid: 1 };
|
||||
await editMaintenanceWindow(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].status).to.equal(422);
|
||||
});
|
||||
|
||||
it("should reject with an error if DB operations fail", async function() {
|
||||
it("should reject with an error if DB operations fail", async function () {
|
||||
req.db.editMaintenanceWindowById.throws(new Error("DB error"));
|
||||
await editMaintenanceWindow(req, res, next);
|
||||
expect(next.firstCall.args[0]).to.be.an("error");
|
||||
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||
});
|
||||
|
||||
it("should return success message with data if all operations are successful", async function() {
|
||||
it("should return success message with data if all operations are successful", async function () {
|
||||
const data = { id: "123" };
|
||||
req.db.editMaintenanceWindowById.returns(data);
|
||||
|
||||
@@ -414,7 +418,7 @@ describe("maintenanceWindowController - editMaintenanceWindow", function() {
|
||||
expect(
|
||||
res.json.calledOnceWith({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_EDIT(req.language),
|
||||
data: data,
|
||||
})
|
||||
).to.be.true;
|
||||
|
||||
Reference in new issue
Block a user