From 61898fd752e73e2f381918891660c03168e96904 Mon Sep 17 00:00:00 2001 From: Moza Al Thani Date: Sun, 1 Sep 2024 11:20:36 +0300 Subject: [PATCH 1/5] extracted nested function out of outer function --- src/upgrades/1.1.0/user_post_count_per_tid.js | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/upgrades/1.1.0/user_post_count_per_tid.js b/src/upgrades/1.1.0/user_post_count_per_tid.js index b6e31f3307..341625d352 100644 --- a/src/upgrades/1.1.0/user_post_count_per_tid.js +++ b/src/upgrades/1.1.0/user_post_count_per_tid.js @@ -5,44 +5,46 @@ const async = require('async'); const winston = require('winston'); const db = require('../../database'); -module.exports = { - name: 'Users post count per tid', - timestamp: Date.UTC(2016, 3, 19), - method: function (callback) { - const batch = require('../../batch'); - const topics = require('../../topics'); - let count = 0; - batch.processSortedSet('topics:tid', (tids, next) => { - winston.verbose(`upgraded ${count} topics`); - count += tids.length; - async.each(tids, (tid, next) => { - db.delete(`tid:${tid}:posters`, (err) => { +const action = function (callback) { + const batch = require('../../batch'); + const topics = require('../../topics'); + let count = 0; + batch.processSortedSet('topics:tid', (tids, next) => { + winston.verbose(`upgraded ${count} topics`); + count += tids.length; + async.each(tids, (tid, next) => { + db.delete(`tid:${tid}:posters`, (err) => { + if (err) { + return next(err); + } + topics.getPids(tid, (err, pids) => { if (err) { return next(err); } - topics.getPids(tid, (err, pids) => { - if (err) { - return next(err); - } - if (!pids.length) { - return next(); - } + if (!pids.length) { + return next(); + } - async.eachSeries(pids, (pid, next) => { - db.getObjectField(`post:${pid}`, 'uid', (err, uid) => { - if (err) { - return next(err); - } - if (!parseInt(uid, 10)) { - return next(); - } - db.sortedSetIncrBy(`tid:${tid}:posters`, 1, uid, next); - }); - }, next); - }); + async.eachSeries(pids, (pid, next) => { + db.getObjectField(`post:${pid}`, 'uid', (err, uid) => { + if (err) { + return next(err); + } + if (!parseInt(uid, 10)) { + return next(); + } + db.sortedSetIncrBy(`tid:${tid}:posters`, 1, uid, next); + }); + }, next); }); - }, next); - }, {}, callback); - }, + }); + }, next); + }, {}, callback); +}; + +module.exports = { + name: 'Users post count per tid', + timestamp: Date.UTC(2016, 3, 19), + method: action, }; From 2eabe729acf1dc18f427852dde6507ab8032e575 Mon Sep 17 00:00:00 2001 From: Moza Al Thani Date: Tue, 3 Sep 2024 11:16:41 +0300 Subject: [PATCH 2/5] Increasing the test coverage of src/user/reset.js --- test/user/reset.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/test/user/reset.js b/test/user/reset.js index a2c1d631cd..ae31197f6c 100644 --- a/test/user/reset.js +++ b/test/user/reset.js @@ -1,5 +1,6 @@ 'use strict'; + const assert = require('assert'); const async = require('async'); @@ -9,6 +10,9 @@ const user = require('../../src/user'); const groups = require('../../src/groups'); const password = require('../../src/password'); const utils = require('../../src/utils'); +// const db = require('../../src/database'); +const meta = require('../../src/meta'); + const socketUser = require('../../src/socket.io/user'); @@ -161,3 +165,84 @@ describe('locks', () => { user.reset.minSecondsBetweenEmails = 60; }); }); + +it('should throw an error if the reset password is the same as the current password', async () => { + const uid = await user.create({ username: 'samepassuser', email: 'same@pass.com', password: '123456' }); + const code = await user.reset.generate(uid); + + let err; + try { + await user.reset.commit(code, '123456'); // Attempt to reset with the same password + } catch (_err) { + err = _err; + } + + assert.strictEqual(err.message, '[[error:reset-same-password]]'); +}); + +describe('UserReset.updateExpiry', () => { + let originalSetUserField; + let originalDeleteObjectField; + let originalPasswordExpiryDays; + + beforeEach(() => { + // Backup the original methods + originalSetUserField = user.setUserField; + originalDeleteObjectField = db.deleteObjectField; + originalPasswordExpiryDays = meta.config.passwordExpiryDays; + + // Mock the methods + user.setUserField = async (uid, field, value) => { + // Simple mock behavior, you could also push the call details into an array for further validation + }; + + db.deleteObjectField = async (key, field) => { + // Simple mock behavior, same as above + }; + }); + + afterEach(() => { + // Restore the original methods + user.setUserField = originalSetUserField; + db.deleteObjectField = originalDeleteObjectField; + meta.config.passwordExpiryDays = originalPasswordExpiryDays; + }); + + it('should set password expiry if expireDays is greater than 0', async () => { + const uid = 1; + const expireDays = 10; + const oneDay = 1000 * 60 * 60 * 24; + const expectedExpiry = Date.now() + (oneDay * expireDays); + + let setFieldCalled = false; + user.setUserField = async (uidArg, fieldArg, valueArg) => { + assert.strictEqual(uidArg, uid); + assert.strictEqual(fieldArg, 'passwordExpiry'); + assert.strictEqual(valueArg >= expectedExpiry - 1 && valueArg <= expectedExpiry + 1, true); + setFieldCalled = true; + }; + + meta.config.passwordExpiryDays = expireDays; + + await user.reset.updateExpiry(uid); + + assert.strictEqual(setFieldCalled, true); + }); + + it('should delete passwordExpiry if expireDays is 0 or less', async () => { + const uid = 1; + + let deleteFieldCalled = false; + db.deleteObjectField = async (keyArg, fieldArg) => { + assert.strictEqual(keyArg, `user:${uid}`); + assert.strictEqual(fieldArg, 'passwordExpiry'); + deleteFieldCalled = true; + }; + + meta.config.passwordExpiryDays = 0; + + await user.reset.updateExpiry(uid); + + assert.strictEqual(deleteFieldCalled, true); + }); +}); From 02d449c79f2f5532194cf34f79dd1dd43bcdda6e Mon Sep 17 00:00:00 2001 From: Moza Al Thani Date: Tue, 3 Sep 2024 11:27:40 +0300 Subject: [PATCH 3/5] Increasing the test coverage of src/user/reset.js --- test/user/reset.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/user/reset.js b/test/user/reset.js index ae31197f6c..6b268b8749 100644 --- a/test/user/reset.js +++ b/test/user/reset.js @@ -166,19 +166,6 @@ describe('locks', () => { }); }); -it('should throw an error if the reset password is the same as the current password', async () => { - const uid = await user.create({ username: 'samepassuser', email: 'same@pass.com', password: '123456' }); - const code = await user.reset.generate(uid); - - let err; - try { - await user.reset.commit(code, '123456'); // Attempt to reset with the same password - } catch (_err) { - err = _err; - } - - assert.strictEqual(err.message, '[[error:reset-same-password]]'); -}); describe('UserReset.updateExpiry', () => { let originalSetUserField; From 7f494e67fa38847416185435d0936da2f67830c2 Mon Sep 17 00:00:00 2001 From: Moza Al Thani Date: Wed, 4 Sep 2024 13:28:08 +0300 Subject: [PATCH 4/5] Fixing comment issue in test/user/reset.js --- src/user/reset.js | 1 + test/user/reset.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user/reset.js b/src/user/reset.js index 9a6d6330ff..56e7e62bc8 100644 --- a/src/user/reset.js +++ b/src/user/reset.js @@ -131,6 +131,7 @@ UserReset.commit = async function (code, password) { }; UserReset.updateExpiry = async function (uid) { + console.log("MOZA_ALTHANI"); const expireDays = meta.config.passwordExpiryDays; if (expireDays > 0) { const oneDay = 1000 * 60 * 60 * 24; diff --git a/test/user/reset.js b/test/user/reset.js index 6b268b8749..50a28fc301 100644 --- a/test/user/reset.js +++ b/test/user/reset.js @@ -10,7 +10,6 @@ const user = require('../../src/user'); const groups = require('../../src/groups'); const password = require('../../src/password'); const utils = require('../../src/utils'); -// const db = require('../../src/database'); const meta = require('../../src/meta'); From d5823622fd52ed954eb203900f9ff77d649646c5 Mon Sep 17 00:00:00 2001 From: Moza Al Thani Date: Wed, 4 Sep 2024 15:08:42 +0300 Subject: [PATCH 5/5] Fixing comment lint failure --- src/user/reset.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user/reset.js b/src/user/reset.js index 56e7e62bc8..1f27bde0e1 100644 --- a/src/user/reset.js +++ b/src/user/reset.js @@ -131,7 +131,7 @@ UserReset.commit = async function (code, password) { }; UserReset.updateExpiry = async function (uid) { - console.log("MOZA_ALTHANI"); + console.log('MOZA_ALTHANI'); const expireDays = meta.config.passwordExpiryDays; if (expireDays > 0) { const oneDay = 1000 * 60 * 60 * 24;