feat: i18n support for backend

This commit is contained in:
cihatata authored and cihat committed 2025-02-11 07:43:36 +03:00
1 parent 0eceac1587
commit 10713428b9
34 files changed
+1106 -838

No files matched your search

+23 -21
View File
@@ -43,7 +43,7 @@ const createQueryChain = (finalResult, comparePasswordResult = false) => ({
save: sinon.stub().resolves(),
});
describe("recoveryModule", function() {
describe("recoveryModule", function () {
let deleteManyStub,
saveStub,
findOneStub,
@@ -52,9 +52,10 @@ describe("recoveryModule", function() {
userFindOneStub;
let req, res;
beforeEach(function() {
beforeEach(function () {
req = {
body: { email: "test@test.com" },
language: 'en',
};
deleteManyStub = sinon.stub(RecoveryToken, "deleteMany");
saveStub = sinon.stub(RecoveryToken.prototype, "save");
@@ -64,19 +65,19 @@ describe("recoveryModule", function() {
userFindOneStub = sinon.stub().resolves();
});
afterEach(function() {
afterEach(function () {
sinon.restore();
});
describe("requestRecoveryToken", function() {
it("should return a recovery token", async function() {
describe("requestRecoveryToken", function () {
it("should return a recovery token", async function () {
deleteManyStub.resolves();
saveStub.resolves(mockRecoveryToken);
const result = await requestRecoveryToken(req, res);
expect(result.email).to.equal(mockRecoveryToken.email);
});
it("should handle an error", async function() {
it("should handle an error", async function () {
const err = new Error("Test error");
deleteManyStub.rejects(err);
try {
@@ -88,24 +89,24 @@ describe("recoveryModule", function() {
});
});
describe("validateRecoveryToken", function() {
it("should return a recovery token if found", async function() {
describe("validateRecoveryToken", function () {
it("should return a recovery token if found", async function () {
findOneStub.resolves(mockRecoveryToken);
const result = await validateRecoveryToken(req, res);
expect(result).to.deep.equal(mockRecoveryToken);
});
it("should thrown an error if a token is not found", async function() {
it("should thrown an error if a token is not found", async function () {
findOneStub.resolves(null);
try {
await validateRecoveryToken(req, res);
} catch (error) {
expect(error).to.exist;
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND(req.language));
}
});
it("should handle DB errors", async function() {
it("should handle DB errors", async function () {
const err = new Error("Test error");
findOneStub.rejects(err);
try {
@@ -117,40 +118,41 @@ describe("recoveryModule", function() {
});
});
describe("resetPassword", function() {
beforeEach(function() {
describe("resetPassword", function () {
beforeEach(function () {
req.body = {
password: "test",
newPassword: "test1",
};
req.language = 'en';
});
afterEach(function() {
afterEach(function () {
sinon.restore();
});
it("should thrown an error if a recovery token is not found", async function() {
it("should thrown an error if a recovery token is not found", async function () {
findOneStub.resolves(null);
try {
await resetPassword(req, res);
} catch (error) {
expect(error).to.exist;
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND(req.language));
}
});
it("should throw an error if a user is not found", async function() {
it("should throw an error if a user is not found", async function () {
findOneStub.resolves(mockRecoveryToken);
userFindOneStub = sinon.stub(User, "findOne").resolves(null);
try {
await resetPassword(req, res);
} catch (error) {
expect(error).to.exist;
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND(req.language));
}
});
it("should throw an error if the passwords match", async function() {
it("should throw an error if the passwords match", async function () {
findOneStub.resolves(mockRecoveryToken);
saveStub.resolves();
userFindOneStub = sinon
@@ -160,11 +162,11 @@ describe("recoveryModule", function() {
await resetPassword(req, res);
} catch (error) {
expect(error).to.exist;
expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH);
expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH(req.language));
}
});
it("should return a user without password if successful", async function() {
it("should return a user without password if successful", async function () {
findOneStub.resolves(mockRecoveryToken);
saveStub.resolves();
userFindOneStub = sinon