Node.js based forum software built for the modern web
refactor: get rid of some async.waterfall/series/parallel calls
Barış Soner Uşaklıe452c097d010
diff --git a/test/authentication.js b/test/authentication.jsindex 60be72e457..12afc00c15 100644--- a/test/authentication.js+++ b/test/authentication.js@@ -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;- let uid;- async.waterfall([- function (next) {- user.create({ username: 'lockme', password: '123456' }, next);- },- 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;- assert.equal(data.res.statusCode, 403);- assert.equal(data.body, '[[error:account-locked]]');- return helpers.loginUser('lockme', 'abcdef');- },- function (data, next) {- assert.equal(data.res.statusCode, 403);- assert.equal(data.body, '[[error:account-locked]]');- db.exists(`lockout:${uid}`, next);- },- function (locked, next) {- assert(locked);- next();- },- ], done);+ const uid = await user.create({ username: 'lockme', password: '123456' });+ await helpers.loginUser('lockme', 'abcdef');+ await helpers.loginUser('lockme', 'abcdef');+ await helpers.loginUser('lockme', 'abcdef');+ let data = await helpers.loginUser('lockme', 'abcdef');++ meta.config.loginAttempts = 5;+ assert.equal(data.res.statusCode, 403);+ assert.equal(data.body, '[[error:account-locked]]');+ data = await helpers.loginUser('lockme', 'abcdef');+ assert.equal(data.res.statusCode, 403);+ assert.equal(data.body, '[[error:account-locked]]');+ const locked = await db.exists(`lockout:${uid}`);+ assert(locked);});it('should clear all reset tokens upon successful login', async () => {diff --git a/test/categories.js b/test/categories.jsindex bbff6f2466..4e4920a777 100644--- a/test/categories.js+++ b/test/categories.js@@ -1,7 +1,5 @@'use strict';--const async = require('async');const assert = require('assert');const nconf = require('nconf');const request = require('request');@@ -18,20 +16,10 @@ describe('Categories', () => {let posterUid;let adminUid;- before((done) => {- async.series({- posterUid: function (next) {- User.create({ username: 'poster' }, next);- },- adminUid: function (next) {- User.create({ username: 'admin' }, next);- },- }, (err, results) => {- assert.ifError(err);- posterUid = results.posterUid;- adminUid = results.adminUid;- groups.join('administrators', adminUid, done);- });+ before(async () => {+ posterUid = await User.create({ username: 'poster' });+ adminUid = await User.create({ username: 'admin' });+ await groups.join('administrators', adminUid);});@@ -156,32 +144,22 @@ describe('Categories', () => {describe('Categories.moveRecentReplies', () => {let moveCid;let moveTid;- before((done) => {- async.parallel({- category: function (next) {- Categories.create({- name: 'Test Category 2',- description: 'Test category created by testing script',- }, next);- },- topic: function (next) {- Topics.post({- uid: posterUid,- cid: categoryObj.cid,- title: 'Test Topic Title',- content: 'The content of test topic',- }, next);- },- }, (err, results) => {- if (err) {- return done(err);- }- moveCid = results.category.cid;- moveTid = results.topic.topicData.tid;- Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid }, (err) => {- done(err);- });- });+ before(async () => {+ const [category, topic] = await Promise.all([+ Categories.create({+ name: 'Test Category 2',+ description: 'Test category created by testing script',+ }),+ Topics.post({+ uid: posterUid,+ cid: categoryObj.cid,+ title: 'Test Topic Title',+ content: 'The content of test topic',+ }),+ ]);+ moveCid = category.cid;+ moveTid = topic.topicData.tid;+ await Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid });});it('should move posts from one category to another', (done) => {@@ -526,52 +504,24 @@ describe('Categories', () => {assert(canDelete);});- it('should create category with settings from', (done) => {- let child1Cid;- let parentCid;- async.waterfall([- function (next) {- 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 create category with settings from', async () => {+ const category = await Categories.create({ name: 'copy from', description: 'copy me' });+ const parentCid = category.cid;+ const childCategory = await Categories.create({ name: 'child1', description: 'will be gone', cloneFromCid: parentCid });+ assert.equal(childCategory.description, 'copy me');});- it('should copy settings from', (done) => {- let child1Cid;- let parentCid;- async.waterfall([- function (next) {- Categories.create({ name: 'parent', description: 'copy me' }, next);- },- function (category, next) {- parentCid = category.cid;- Categories.create({ name: 'child1' }, next);- },- function (category, next) {- child1Cid = category.cid;- socketCategories.copySettingsFrom(- { uid: adminUid },- { fromCid: parentCid, toCid: child1Cid, copyParent: true },- next- );- },- function (destinationCategory, next) {- Categories.getCategoryField(child1Cid, 'description', next);- },- function (description, next) {- assert.equal(description, 'copy me');- next();- },- ], done);+ it('should copy settings from', async () => {+ const category = await Categories.create({ name: 'parent', description: 'copy me' });+ const parentCid = category.cid;+ const childCategory = await Categories.create({ name: 'child1' });+ const child1Cid = childCategory.cid;+ const destinationCategory = await socketCategories.copySettingsFrom(+ { uid: adminUid },+ { fromCid: parentCid, toCid: child1Cid, copyParent: true },+ );+ const description = await Categories.getCategoryField(child1Cid, 'description');+ assert.equal(description, 'copy me');});it('should copy privileges from another category', async () => {@@ -852,19 +802,12 @@ describe('Categories', () => {});});- it('should not fail when there are multiple groups', (done) => {- async.series([- async.apply(groups.create, { name: 'testGroup2' }),- async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup2'),- async.apply(groups.join, 'testGroup2', 1),- function (next) {- Categories.getModeratorUids([cid, 2], (err, uids) => {- assert.ifError(err);- assert(uids[0].includes('1'));- next();- });- },- ], done);+ it('should not fail when there are multiple groups', async () => {+ await groups.create({ name: 'testGroup2' });+ await groups.join('cid:1:privileges:groups:moderate', 'testGroup2');+ await groups.join('testGroup2', 1);+ const uids = await Categories.getModeratorUids([cid, 2]);+ assert(uids[0].includes('1'));});it('should not return moderators of disabled categories', async () => {@@ -875,13 +818,11 @@ describe('Categories', () => {assert(!uids[0].includes('1'));});- after((done) => {- async.series([- async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup'),- async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup2'),- async.apply(groups.destroy, 'testGroup'),- async.apply(groups.destroy, 'testGroup2'),- ], done);+ after(async () => {+ await groups.leave(`cid:${cid}:privileges:groups:moderate`, 'testGroup');+ await groups.leave(`cid:${cid}:privileges:groups:moderate`, 'testGroup2');+ await groups.destroy('testGroup');+ await groups.destroy('testGroup2');});});});diff --git a/test/notifications.js b/test/notifications.jsindex 96894e7606..0fdcc7f117 100644--- a/test/notifications.js+++ b/test/notifications.js@@ -2,18 +2,19 @@const assert = require('assert');-const async = require('async');const nconf = require('nconf');+const util = require('util');const db = require('./mocks/databasemock');const meta = require('../src/meta');const user = require('../src/user');const topics = require('../src/topics');const categories = require('../src/categories');-const groups = require('../src/groups');const notifications = require('../src/notifications');const socketNotifications = require('../src/socket.io/notifications');+const sleep = util.promisify(setTimeout);+describe('Notifications', () => {let uid;let notification;@@ -226,73 +227,39 @@ describe('Notifications', () => {});});- it('should link to the first unread post in a watched topic', (done) => {- const categories = require('../src/categories');- const topics = require('../src/topics');- 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',- description: 'Test category created by testing script',- }, next);- },- function (category, next) {- cid = category.cid;-- topics.post({- uid: watcherUid,- cid: cid,- title: 'Test Topic Title',- content: 'The content of test topic',- }, next);- },- function (topic, next) {- tid = topic.topicData.tid;-- topics.follow(tid, watcherUid, next);- },- function (next) {- topics.reply({- uid: uid,- content: 'This is the first reply.',- tid: tid,- }, next);- },- function (post, next) {- pid = post.pid;-- topics.reply({- uid: uid,- content: 'This is the second reply.',- tid: tid,- }, next);- },- function (post, next) {- // notifications are sent asynchronously with a 1 second delay.- setTimeout(next, 3000);- },- function (next) {- user.notifications.get(watcherUid, next);- },- function (notifications, next) {- 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');- next();- },- ], (err) => {- assert.ifError(err);- done();+ it('should link to the first unread post in a watched topic', async () => {+ const watcherUid = await user.create({ username: 'watcher' });+ const { cid } = await categories.create({+ name: 'Test Category',+ description: 'Test category created by testing script',});++ const { topicData } = await topics.post({+ uid: watcherUid,+ cid: cid,+ title: 'Test Topic Title',+ content: 'The content of test topic',+ });+ const { tid } = topicData;++ await topics.follow(tid, watcherUid);++ const { pid } = await topics.reply({+ uid: uid,+ content: 'This is the first reply.',+ tid: tid,+ });++ await topics.reply({+ uid: uid,+ content: 'This is the second reply.',+ tid: tid,+ });+ // notifications are sent asynchronously with a 1 second delay.+ await sleep(3000);+ const notifications = await user.notifications.get(watcherUid);+ 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');});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) => {- let followerUid;- async.waterfall([- function (next) {- user.create({ username: 'follower' }, next);- },- function (_followerUid, next) {- followerUid = _followerUid;- user.follow(followerUid, uid, next);- },- function (next) {- categories.create({- name: 'Test Category',- description: 'Test category created by testing script',- }, next);- },- function (category, next) {- topics.post({- uid: uid,- cid: category.cid,- title: 'Test Topic Title',- 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();+ it('should send notification to followers of user when he posts', async () => {+ const followerUid = await user.create({ username: 'follower' });+ await user.follow(followerUid, uid);+ const { cid } = await categories.create({+ name: 'Test Category',+ description: 'Test category created by testing script',+ });+ await topics.post({+ uid: uid,+ cid: cid,+ title: 'Test Topic Title',+ content: 'The content of test topic',});+ await sleep(1100);+ const data = await user.notifications.getAll(followerUid, '');+ assert(data);});it('should send welcome notification', (done) => {