From 8bc2ef227ed49ed64328176dfd56aa54d6e0fa0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 13 Sep 2023 16:23:49 -0400 Subject: [PATCH] refactor: get rid of async.waterfall/series --- test/groups.js | 128 +++++++++++++------------------------------------ 1 file changed, 32 insertions(+), 96 deletions(-) diff --git a/test/groups.js b/test/groups.js index 2fcb48d59e..10ff138639 100644 --- a/test/groups.js +++ b/test/groups.js @@ -1,7 +1,6 @@ 'use strict'; const assert = require('assert'); -const async = require('async'); const fs = require('fs'); const path = require('path'); const nconf = require('nconf'); @@ -161,33 +160,15 @@ describe('Groups', () => { }); }); - it('should return all users if no query', (done) => { - function createAndJoinGroup(username, email, callback) { - async.waterfall([ - function (next) { - User.create({ username: username, email: email }, next); - }, - function (uid, next) { - Groups.join('Test', uid, next); - }, - ], callback); + it('should return all users if no query', async () => { + async function createAndJoinGroup(username, email) { + const uid = await User.create({ username: username, email: email }); + await Groups.join('Test', uid); } - async.series([ - function (next) { - createAndJoinGroup('newuser', 'newuser@b.com', next); - }, - 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(); - }); - }); + await createAndJoinGroup('newuser', 'newuser@b.com'); + await createAndJoinGroup('bob', 'bob@b.com'); + const data = await socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' }); + assert.equal(data.users.length, 3); }); 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 cid = 1; const groupName = '1'; const uid = 1; - async.waterfall([ - function (next) { - Groups.create({ name: groupName }, next); - }, - function (groupData, next) { - privileges.categories.give(['groups:topics:create'], cid, groupName, next); - }, - function (next) { - Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create', next); - }, - 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); + await Groups.create({ name: groupName }); + await privileges.categories.give(['groups:topics:create'], cid, groupName); + let isMember = await Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create'); + assert(isMember); + await Groups.destroy(groupName); + isMember = await Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create'); + assert(!isMember); + isMember = await Groups.isMember(uid, 'registered-users'); + assert(isMember); }); }); @@ -779,36 +743,19 @@ describe('Groups', () => { }); describe('.leaveAllGroups()', () => { - it('should remove a user from all groups', (done) => { - Groups.leaveAllGroups(testUid, (err) => { - assert.ifError(err); - - const groups = ['Test', 'Hidden']; - async.every(groups, (group, next) => { - Groups.isMember(testUid, group, (err, isMember) => { - next(err, !isMember); - }); - }, (err, result) => { - assert.ifError(err); - assert(result); - - done(); - }); - }); + it('should remove a user from all groups', async () => { + await Groups.leaveAllGroups(testUid); + const groups = ['Test', 'Hidden']; + const isMembers = await Groups.isMemberOfGroups(testUid, groups); + assert(!isMembers.includes(true)); }); }); describe('.show()', () => { - it('should make a group visible', (done) => { - Groups.show('Test', function (err) { - assert.ifError(err); - assert.equal(arguments.length, 1); - db.isSortedSetMember('groups:visible:createtime', 'Test', (err, isMember) => { - assert.ifError(err); - assert.strictEqual(isMember, true); - done(); - }); - }); + it('should make a group visible', async () => { + await Groups.show('Test'); + const isMember = await db.isSortedSetMember('groups:visible:createtime', 'Test'); + assert.strictEqual(isMember, true); }); }); @@ -1262,22 +1209,11 @@ describe('Groups', () => { let regularUid; const logoPath = path.join(__dirname, '../test/files/test.png'); const imagePath = path.join(__dirname, '../test/files/groupcover.png'); - before((done) => { - User.create({ username: 'regularuser', password: '123456' }, (err, uid) => { - assert.ifError(err); - regularUid = uid; - async.series([ - function (next) { - Groups.join('Test', adminUid, next); - }, - function (next) { - Groups.join('Test', regularUid, next); - }, - function (next) { - helpers.copyFile(logoPath, imagePath, next); - }, - ], done); - }); + before(async () => { + regularUid = await User.create({ username: 'regularuser', password: '123456' }); + await Groups.join('Test', adminUid); + await Groups.join('Test', regularUid); + await helpers.copyFile(logoPath, imagePath); }); it('should fail if user is not logged in or not owner', (done) => {