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, }; diff --git a/src/user/reset.js b/src/user/reset.js index 9a6d6330ff..1f27bde0e1 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 a2c1d631cd..50a28fc301 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,8 @@ const user = require('../../src/user'); const groups = require('../../src/groups'); const password = require('../../src/password'); const utils = require('../../src/utils'); +const meta = require('../../src/meta'); + const socketUser = require('../../src/socket.io/user'); @@ -161,3 +164,71 @@ describe('locks', () => { user.reset.minSecondsBetweenEmails = 60; }); }); + + +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); + }); +});