refactor: get rid of async.waterfall/series

This commit is contained in:
Barış Soner Uşaklı
2023-09-13 16:23:49 -04:00
parent 12823ba77c
commit 8bc2ef227e

View File

@@ -1,7 +1,6 @@
'use strict'; 'use strict';
const assert = require('assert'); const assert = require('assert');
const async = require('async');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const nconf = require('nconf'); const nconf = require('nconf');
@@ -161,33 +160,15 @@ describe('Groups', () => {
}); });
}); });
it('should return all users if no query', (done) => { it('should return all users if no query', async () => {
function createAndJoinGroup(username, email, callback) { async function createAndJoinGroup(username, email) {
async.waterfall([ const uid = await User.create({ username: username, email: email });
function (next) { await Groups.join('Test', uid);
User.create({ username: username, email: email }, next);
},
function (uid, next) {
Groups.join('Test', uid, next);
},
], callback);
} }
async.series([ await createAndJoinGroup('newuser', 'newuser@b.com');
function (next) { await createAndJoinGroup('bob', 'bob@b.com');
createAndJoinGroup('newuser', 'newuser@b.com', next); const data = await socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' });
}, assert.equal(data.users.length, 3);
function (next) {
createAndJoinGroup('bob', 'bob@b.com', next);
},
], (err) => {
assert.ifError(err);
socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' }, (err, data) => {
assert.ifError(err);
assert.equal(data.users.length, 3);
done();
});
});
}); });
it('should search group members', (done) => { it('should search group members', (done) => {
@@ -584,37 +565,20 @@ describe('Groups', () => {
}); });
}); });
it('should remove group from privilege groups', (done) => { it('should remove group from privilege groups', async () => {
const privileges = require('../src/privileges'); const privileges = require('../src/privileges');
const cid = 1; const cid = 1;
const groupName = '1'; const groupName = '1';
const uid = 1; const uid = 1;
async.waterfall([ await Groups.create({ name: groupName });
function (next) { await privileges.categories.give(['groups:topics:create'], cid, groupName);
Groups.create({ name: groupName }, next); let isMember = await Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create');
}, assert(isMember);
function (groupData, next) { await Groups.destroy(groupName);
privileges.categories.give(['groups:topics:create'], cid, groupName, next); isMember = await Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create');
}, assert(!isMember);
function (next) { isMember = await Groups.isMember(uid, 'registered-users');
Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create', next); assert(isMember);
},
function (isMember, next) {
assert(isMember);
Groups.destroy(groupName, next);
},
function (next) {
Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create', next);
},
function (isMember, next) {
assert(!isMember);
Groups.isMember(uid, 'registered-users', next);
},
function (isMember, next) {
assert(isMember);
next();
},
], done);
}); });
}); });
@@ -779,36 +743,19 @@ describe('Groups', () => {
}); });
describe('.leaveAllGroups()', () => { describe('.leaveAllGroups()', () => {
it('should remove a user from all groups', (done) => { it('should remove a user from all groups', async () => {
Groups.leaveAllGroups(testUid, (err) => { await Groups.leaveAllGroups(testUid);
assert.ifError(err); const groups = ['Test', 'Hidden'];
const isMembers = await Groups.isMemberOfGroups(testUid, groups);
const groups = ['Test', 'Hidden']; assert(!isMembers.includes(true));
async.every(groups, (group, next) => {
Groups.isMember(testUid, group, (err, isMember) => {
next(err, !isMember);
});
}, (err, result) => {
assert.ifError(err);
assert(result);
done();
});
});
}); });
}); });
describe('.show()', () => { describe('.show()', () => {
it('should make a group visible', (done) => { it('should make a group visible', async () => {
Groups.show('Test', function (err) { await Groups.show('Test');
assert.ifError(err); const isMember = await db.isSortedSetMember('groups:visible:createtime', 'Test');
assert.equal(arguments.length, 1); assert.strictEqual(isMember, true);
db.isSortedSetMember('groups:visible:createtime', 'Test', (err, isMember) => {
assert.ifError(err);
assert.strictEqual(isMember, true);
done();
});
});
}); });
}); });
@@ -1262,22 +1209,11 @@ describe('Groups', () => {
let regularUid; let regularUid;
const logoPath = path.join(__dirname, '../test/files/test.png'); const logoPath = path.join(__dirname, '../test/files/test.png');
const imagePath = path.join(__dirname, '../test/files/groupcover.png'); const imagePath = path.join(__dirname, '../test/files/groupcover.png');
before((done) => { before(async () => {
User.create({ username: 'regularuser', password: '123456' }, (err, uid) => { regularUid = await User.create({ username: 'regularuser', password: '123456' });
assert.ifError(err); await Groups.join('Test', adminUid);
regularUid = uid; await Groups.join('Test', regularUid);
async.series([ await helpers.copyFile(logoPath, imagePath);
function (next) {
Groups.join('Test', adminUid, next);
},
function (next) {
Groups.join('Test', regularUid, next);
},
function (next) {
helpers.copyFile(logoPath, imagePath, next);
},
], done);
});
}); });
it('should fail if user is not logged in or not owner', (done) => { it('should fail if user is not logged in or not owner', (done) => {