Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code in src/upgrades/1.1.0/user_post_count_per_tid.js #29

Open
wants to merge 5 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/upgrades/1.1.0/user_post_count_per_tid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
1 change: 1 addition & 0 deletions src/user/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions test/user/reset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';


const assert = require('assert');
const async = require('async');

Expand All @@ -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');

Expand Down Expand Up @@ -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);
});
});