refactor: get rid of some async.waterfall/series/parallel calls

This commit is contained in:
Barış Soner Uşaklı
2023-09-22 11:44:32 -04:00
parent 6456e04507
commit e452c097d0
3 changed files with 112 additions and 237 deletions

View File

@@ -552,36 +552,22 @@ describe('authentication', () => {
}); });
}); });
it('should lockout account on 3 failed login attempts', (done) => { it('should lockout account on 3 failed login attempts', async () => {
meta.config.loginAttempts = 3; meta.config.loginAttempts = 3;
let uid; const uid = await user.create({ username: 'lockme', password: '123456' });
async.waterfall([ await helpers.loginUser('lockme', 'abcdef');
function (next) { await helpers.loginUser('lockme', 'abcdef');
user.create({ username: 'lockme', password: '123456' }, next); await helpers.loginUser('lockme', 'abcdef');
}, let data = await helpers.loginUser('lockme', 'abcdef');
async (_uid) => {
uid = _uid;
return helpers.loginUser('lockme', 'abcdef');
},
async data => helpers.loginUser('lockme', 'abcdef'),
async data => helpers.loginUser('lockme', 'abcdef'),
async data => helpers.loginUser('lockme', 'abcdef'),
async (data) => {
meta.config.loginAttempts = 5; meta.config.loginAttempts = 5;
assert.equal(data.res.statusCode, 403); assert.equal(data.res.statusCode, 403);
assert.equal(data.body, '[[error:account-locked]]'); assert.equal(data.body, '[[error:account-locked]]');
return helpers.loginUser('lockme', 'abcdef'); data = await helpers.loginUser('lockme', 'abcdef');
},
function (data, next) {
assert.equal(data.res.statusCode, 403); assert.equal(data.res.statusCode, 403);
assert.equal(data.body, '[[error:account-locked]]'); assert.equal(data.body, '[[error:account-locked]]');
db.exists(`lockout:${uid}`, next); const locked = await db.exists(`lockout:${uid}`);
},
function (locked, next) {
assert(locked); assert(locked);
next();
},
], done);
}); });
it('should clear all reset tokens upon successful login', async () => { it('should clear all reset tokens upon successful login', async () => {

View File

@@ -1,7 +1,5 @@
'use strict'; 'use strict';
const async = require('async');
const assert = require('assert'); const assert = require('assert');
const nconf = require('nconf'); const nconf = require('nconf');
const request = require('request'); const request = require('request');
@@ -18,20 +16,10 @@ describe('Categories', () => {
let posterUid; let posterUid;
let adminUid; let adminUid;
before((done) => { before(async () => {
async.series({ posterUid = await User.create({ username: 'poster' });
posterUid: function (next) { adminUid = await User.create({ username: 'admin' });
User.create({ username: 'poster' }, next); await groups.join('administrators', adminUid);
},
adminUid: function (next) {
User.create({ username: 'admin' }, next);
},
}, (err, results) => {
assert.ifError(err);
posterUid = results.posterUid;
adminUid = results.adminUid;
groups.join('administrators', adminUid, done);
});
}); });
@@ -156,32 +144,22 @@ describe('Categories', () => {
describe('Categories.moveRecentReplies', () => { describe('Categories.moveRecentReplies', () => {
let moveCid; let moveCid;
let moveTid; let moveTid;
before((done) => { before(async () => {
async.parallel({ const [category, topic] = await Promise.all([
category: function (next) {
Categories.create({ Categories.create({
name: 'Test Category 2', name: 'Test Category 2',
description: 'Test category created by testing script', description: 'Test category created by testing script',
}, next); }),
},
topic: function (next) {
Topics.post({ Topics.post({
uid: posterUid, uid: posterUid,
cid: categoryObj.cid, cid: categoryObj.cid,
title: 'Test Topic Title', title: 'Test Topic Title',
content: 'The content of test topic', content: 'The content of test topic',
}, next); }),
}, ]);
}, (err, results) => { moveCid = category.cid;
if (err) { moveTid = topic.topicData.tid;
return done(err); await Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid });
}
moveCid = results.category.cid;
moveTid = results.topic.topicData.tid;
Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid }, (err) => {
done(err);
});
});
}); });
it('should move posts from one category to another', (done) => { it('should move posts from one category to another', (done) => {
@@ -526,52 +504,24 @@ describe('Categories', () => {
assert(canDelete); assert(canDelete);
}); });
it('should create category with settings from', (done) => { it('should create category with settings from', async () => {
let child1Cid; const category = await Categories.create({ name: 'copy from', description: 'copy me' });
let parentCid; const parentCid = category.cid;
async.waterfall([ const childCategory = await Categories.create({ name: 'child1', description: 'will be gone', cloneFromCid: parentCid });
function (next) { assert.equal(childCategory.description, 'copy me');
Categories.create({ name: 'copy from', description: 'copy me' }, next);
},
function (category, next) {
parentCid = category.cid;
Categories.create({ name: 'child1', description: 'will be gone', cloneFromCid: parentCid }, next);
},
function (category, next) {
child1Cid = category.cid;
assert.equal(category.description, 'copy me');
next();
},
], done);
}); });
it('should copy settings from', (done) => { it('should copy settings from', async () => {
let child1Cid; const category = await Categories.create({ name: 'parent', description: 'copy me' });
let parentCid; const parentCid = category.cid;
async.waterfall([ const childCategory = await Categories.create({ name: 'child1' });
function (next) { const child1Cid = childCategory.cid;
Categories.create({ name: 'parent', description: 'copy me' }, next); const destinationCategory = await socketCategories.copySettingsFrom(
},
function (category, next) {
parentCid = category.cid;
Categories.create({ name: 'child1' }, next);
},
function (category, next) {
child1Cid = category.cid;
socketCategories.copySettingsFrom(
{ uid: adminUid }, { uid: adminUid },
{ fromCid: parentCid, toCid: child1Cid, copyParent: true }, { fromCid: parentCid, toCid: child1Cid, copyParent: true },
next
); );
}, const description = await Categories.getCategoryField(child1Cid, 'description');
function (destinationCategory, next) {
Categories.getCategoryField(child1Cid, 'description', next);
},
function (description, next) {
assert.equal(description, 'copy me'); assert.equal(description, 'copy me');
next();
},
], done);
}); });
it('should copy privileges from another category', async () => { it('should copy privileges from another category', async () => {
@@ -852,19 +802,12 @@ describe('Categories', () => {
}); });
}); });
it('should not fail when there are multiple groups', (done) => { it('should not fail when there are multiple groups', async () => {
async.series([ await groups.create({ name: 'testGroup2' });
async.apply(groups.create, { name: 'testGroup2' }), await groups.join('cid:1:privileges:groups:moderate', 'testGroup2');
async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup2'), await groups.join('testGroup2', 1);
async.apply(groups.join, 'testGroup2', 1), const uids = await Categories.getModeratorUids([cid, 2]);
function (next) {
Categories.getModeratorUids([cid, 2], (err, uids) => {
assert.ifError(err);
assert(uids[0].includes('1')); assert(uids[0].includes('1'));
next();
});
},
], done);
}); });
it('should not return moderators of disabled categories', async () => { it('should not return moderators of disabled categories', async () => {
@@ -875,13 +818,11 @@ describe('Categories', () => {
assert(!uids[0].includes('1')); assert(!uids[0].includes('1'));
}); });
after((done) => { after(async () => {
async.series([ await groups.leave(`cid:${cid}:privileges:groups:moderate`, 'testGroup');
async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup'), await groups.leave(`cid:${cid}:privileges:groups:moderate`, 'testGroup2');
async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup2'), await groups.destroy('testGroup');
async.apply(groups.destroy, 'testGroup'), await groups.destroy('testGroup2');
async.apply(groups.destroy, 'testGroup2'),
], done);
}); });
}); });
}); });

View File

@@ -2,18 +2,19 @@
const assert = require('assert'); const assert = require('assert');
const async = require('async');
const nconf = require('nconf'); const nconf = require('nconf');
const util = require('util');
const db = require('./mocks/databasemock'); const db = require('./mocks/databasemock');
const meta = require('../src/meta'); const meta = require('../src/meta');
const user = require('../src/user'); const user = require('../src/user');
const topics = require('../src/topics'); const topics = require('../src/topics');
const categories = require('../src/categories'); const categories = require('../src/categories');
const groups = require('../src/groups');
const notifications = require('../src/notifications'); const notifications = require('../src/notifications');
const socketNotifications = require('../src/socket.io/notifications'); const socketNotifications = require('../src/socket.io/notifications');
const sleep = util.promisify(setTimeout);
describe('Notifications', () => { describe('Notifications', () => {
let uid; let uid;
let notification; let notification;
@@ -226,73 +227,39 @@ describe('Notifications', () => {
}); });
}); });
it('should link to the first unread post in a watched topic', (done) => { it('should link to the first unread post in a watched topic', async () => {
const categories = require('../src/categories'); const watcherUid = await user.create({ username: 'watcher' });
const topics = require('../src/topics'); const { cid } = await categories.create({
let watcherUid;
let cid;
let tid;
let pid;
async.waterfall([
function (next) {
user.create({ username: 'watcher' }, next);
},
function (_watcherUid, next) {
watcherUid = _watcherUid;
categories.create({
name: 'Test Category', name: 'Test Category',
description: 'Test category created by testing script', description: 'Test category created by testing script',
}, next); });
},
function (category, next) {
cid = category.cid;
topics.post({ const { topicData } = await topics.post({
uid: watcherUid, uid: watcherUid,
cid: cid, cid: cid,
title: 'Test Topic Title', title: 'Test Topic Title',
content: 'The content of test topic', content: 'The content of test topic',
}, next); });
}, const { tid } = topicData;
function (topic, next) {
tid = topic.topicData.tid;
topics.follow(tid, watcherUid, next); await topics.follow(tid, watcherUid);
},
function (next) { const { pid } = await topics.reply({
topics.reply({
uid: uid, uid: uid,
content: 'This is the first reply.', content: 'This is the first reply.',
tid: tid, tid: tid,
}, next); });
},
function (post, next) {
pid = post.pid;
topics.reply({ await topics.reply({
uid: uid, uid: uid,
content: 'This is the second reply.', content: 'This is the second reply.',
tid: tid, tid: tid,
}, next); });
},
function (post, next) {
// notifications are sent asynchronously with a 1 second delay. // notifications are sent asynchronously with a 1 second delay.
setTimeout(next, 3000); await sleep(3000);
}, const notifications = await user.notifications.get(watcherUid);
function (next) {
user.notifications.get(watcherUid, next);
},
function (notifications, next) {
assert.equal(notifications.unread.length, 1, 'there should be 1 unread notification'); assert.equal(notifications.unread.length, 1, 'there should be 1 unread notification');
assert.equal(`${nconf.get('relative_path')}/post/${pid}`, notifications.unread[0].path, 'the notification should link to the first unread post'); assert.equal(`${nconf.get('relative_path')}/post/${pid}`, notifications.unread[0].path, 'the notification should link to the first unread post');
next();
},
], (err) => {
assert.ifError(err);
done();
});
}); });
it('should get notification by nid', (done) => { it('should get notification by nid', (done) => {
@@ -403,41 +370,22 @@ describe('Notifications', () => {
}); });
}); });
it('should send notification to followers of user when he posts', (done) => { it('should send notification to followers of user when he posts', async () => {
let followerUid; const followerUid = await user.create({ username: 'follower' });
async.waterfall([ await user.follow(followerUid, uid);
function (next) { const { cid } = await categories.create({
user.create({ username: 'follower' }, next);
},
function (_followerUid, next) {
followerUid = _followerUid;
user.follow(followerUid, uid, next);
},
function (next) {
categories.create({
name: 'Test Category', name: 'Test Category',
description: 'Test category created by testing script', description: 'Test category created by testing script',
}, next); });
}, await topics.post({
function (category, next) {
topics.post({
uid: uid, uid: uid,
cid: category.cid, cid: cid,
title: 'Test Topic Title', title: 'Test Topic Title',
content: 'The content of test topic', content: 'The content of test topic',
}, next);
},
function (data, next) {
setTimeout(next, 1100);
},
function (next) {
user.notifications.getAll(followerUid, '', next);
},
], (err, data) => {
assert.ifError(err);
assert(data);
done();
}); });
await sleep(1100);
const data = await user.notifications.getAll(followerUid, '');
assert(data);
}); });
it('should send welcome notification', (done) => { it('should send welcome notification', (done) => {