mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: get rid of some async.waterfall/series/parallel calls
This commit is contained in:
@@ -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;
|
meta.config.loginAttempts = 5;
|
||||||
return helpers.loginUser('lockme', 'abcdef');
|
assert.equal(data.res.statusCode, 403);
|
||||||
},
|
assert.equal(data.body, '[[error:account-locked]]');
|
||||||
async data => helpers.loginUser('lockme', 'abcdef'),
|
data = await helpers.loginUser('lockme', 'abcdef');
|
||||||
async data => helpers.loginUser('lockme', 'abcdef'),
|
assert.equal(data.res.statusCode, 403);
|
||||||
async data => helpers.loginUser('lockme', 'abcdef'),
|
assert.equal(data.body, '[[error:account-locked]]');
|
||||||
async (data) => {
|
const locked = await db.exists(`lockout:${uid}`);
|
||||||
meta.config.loginAttempts = 5;
|
assert(locked);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should clear all reset tokens upon successful login', async () => {
|
it('should clear all reset tokens upon successful login', async () => {
|
||||||
|
|||||||
@@ -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);
|
Topics.post({
|
||||||
},
|
uid: posterUid,
|
||||||
topic: function (next) {
|
cid: categoryObj.cid,
|
||||||
Topics.post({
|
title: 'Test Topic Title',
|
||||||
uid: posterUid,
|
content: 'The content of test topic',
|
||||||
cid: categoryObj.cid,
|
}),
|
||||||
title: 'Test Topic Title',
|
]);
|
||||||
content: 'The content of test topic',
|
moveCid = category.cid;
|
||||||
}, next);
|
moveTid = topic.topicData.tid;
|
||||||
},
|
await Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid });
|
||||||
}, (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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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(
|
||||||
},
|
{ uid: adminUid },
|
||||||
function (category, next) {
|
{ fromCid: parentCid, toCid: child1Cid, copyParent: true },
|
||||||
parentCid = category.cid;
|
);
|
||||||
Categories.create({ name: 'child1' }, next);
|
const description = await Categories.getCategoryField(child1Cid, 'description');
|
||||||
},
|
assert.equal(description, 'copy me');
|
||||||
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 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) {
|
assert(uids[0].includes('1'));
|
||||||
Categories.getModeratorUids([cid, 2], (err, uids) => {
|
|
||||||
assert.ifError(err);
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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;
|
name: 'Test Category',
|
||||||
let cid;
|
description: 'Test category created by testing script',
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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) => {
|
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);
|
name: 'Test Category',
|
||||||
},
|
description: 'Test category created by testing script',
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
|
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) => {
|
it('should send welcome notification', (done) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user