diff --git a/src/api/users.js b/src/api/users.js index cbd64e7fd5..52d738c1f3 100644 --- a/src/api/users.js +++ b/src/api/users.js @@ -312,6 +312,9 @@ async function canDeleteUids(uids) { } usersAPI.search = async function (caller, data) { + if (!data) { + throw new Error('[[error:invalid-data]]'); + } const [allowed, isPrivileged] = await Promise.all([ privileges.global.can('search:users', caller.uid), user.isPrivileged(caller.uid), diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 66a7c54820..7a26d160ca 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -22,7 +22,6 @@ const sockets = require('.'); const SocketUser = module.exports; require('./user/profile')(SocketUser); -require('./user/search')(SocketUser); require('./user/status')(SocketUser); require('./user/picture')(SocketUser); require('./user/ban')(SocketUser); diff --git a/src/socket.io/user/search.js b/src/socket.io/user/search.js deleted file mode 100644 index 8855f82c7b..0000000000 --- a/src/socket.io/user/search.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -const pagination = require('../../pagination'); -const api = require('../../api'); -const sockets = require('..'); - -module.exports = function (SocketUser) { - SocketUser.search = async function (socket, data) { - sockets.warnDeprecated(socket, 'GET /api/users'); - if (!data) { - throw new Error('[[error:invalid-data]]'); - } - const result = api.users.search(socket, data); - result.pagination = pagination.create(data.page, result.pageCount); - return result; - }; -}; diff --git a/test/user.js b/test/user.js index eb9fd91c0b..919ccf584f 100644 --- a/test/user.js +++ b/test/user.js @@ -19,8 +19,8 @@ const Password = require('../src/password'); const groups = require('../src/groups'); const helpers = require('./helpers'); const meta = require('../src/meta'); -const events = require('../src/events'); const socketUser = require('../src/socket.io/user'); +const apiUser = require('../src/api/users'); describe('User', () => { let userData; @@ -360,69 +360,73 @@ describe('User', () => { }); }); - it('should search user', (done) => { - socketUser.search({ uid: testUid }, { query: 'john' }, (err, searchData) => { - assert.ifError(err); - assert.equal(searchData.users[0].username, 'John Smith'); - done(); - }); + it('should search user', async () => { + const searchData = await apiUser.search({ uid: testUid }, { query: 'john' }); + assert.equal(searchData.users[0].username, 'John Smith'); }); - it('should error for guest', (done) => { - socketUser.search({ uid: 0 }, { query: 'john' }, (err) => { + it('should error for guest', async () => { + try { + await apiUser.search({ uid: 0 }, { query: 'john' }); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:no-privileges]]'); - done(); - }); + } }); - it('should error with invalid data', (done) => { - socketUser.search({ uid: testUid }, null, (err) => { + it('should error with invalid data', async () => { + try { + await apiUser.search({ uid: testUid }, null); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:invalid-data]]'); - done(); - }); + } }); - it('should error for unprivileged user', (done) => { - socketUser.search({ uid: testUid }, { searchBy: 'ip', query: '123' }, (err) => { + it('should error for unprivileged user', async () => { + try { + await apiUser.search({ uid: testUid }, { searchBy: 'ip', query: '123' }); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:no-privileges]]'); - done(); - }); + } }); - it('should error for unprivileged user', (done) => { - socketUser.search({ uid: testUid }, { filters: ['banned'], query: '123' }, (err) => { + it('should error for unprivileged user', async () => { + try { + await apiUser.search({ uid: testUid }, { filters: ['banned'], query: '123' }); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:no-privileges]]'); - done(); - }); + } }); - it('should error for unprivileged user', (done) => { - socketUser.search({ uid: testUid }, { filters: ['flagged'], query: '123' }, (err) => { + it('should error for unprivileged user', async () => { + try { + await apiUser.search({ uid: testUid }, { filters: ['flagged'], query: '123' }); + assert(false); + } catch (err) { assert.equal(err.message, '[[error:no-privileges]]'); - done(); - }); + } }); it('should search users by ip', async () => { const uid = await User.create({ username: 'ipsearch' }); await db.sortedSetAdd('ip:1.1.1.1:uid', [1, 1], [testUid, uid]); - const data = await socketUser.search({ uid: adminUid }, { query: '1.1.1.1', searchBy: 'ip' }); + const data = await apiUser.search({ uid: adminUid }, { query: '1.1.1.1', searchBy: 'ip' }); assert(Array.isArray(data.users)); assert.equal(data.users.length, 2); }); - it('should search users by uid', (done) => { - socketUser.search({ uid: testUid }, { query: uid, searchBy: 'uid' }, (err, data) => { - assert.ifError(err); - assert(Array.isArray(data.users)); - assert.equal(data.users[0].uid, uid); - done(); - }); + it('should search users by uid', async () => { + const data = await apiUser.search({ uid: testUid }, { query: uid, searchBy: 'uid' }); + assert(Array.isArray(data.users)); + assert.equal(data.users[0].uid, uid); }); it('should search users by fullname', async () => { const uid = await User.create({ username: 'fullnamesearch1', fullname: 'Mr. Fullname' }); - const data = await socketUser.search({ uid: adminUid }, { query: 'mr', searchBy: 'fullname' }); + const data = await apiUser.search({ uid: adminUid }, { query: 'mr', searchBy: 'fullname' }); assert(Array.isArray(data.users)); assert.equal(data.users.length, 1); assert.equal(uid, data.users[0].uid); @@ -430,38 +434,26 @@ describe('User', () => { it('should search users by fullname', async () => { const uid = await User.create({ username: 'fullnamesearch2', fullname: 'Baris:Usakli' }); - const data = await socketUser.search({ uid: adminUid }, { query: 'baris:', searchBy: 'fullname' }); + const data = await apiUser.search({ uid: adminUid }, { query: 'baris:', searchBy: 'fullname' }); assert(Array.isArray(data.users)); assert.equal(data.users.length, 1); assert.equal(uid, data.users[0].uid); }); - it('should return empty array if query is empty', (done) => { - socketUser.search({ uid: testUid }, { query: '' }, (err, data) => { - assert.ifError(err); - assert.equal(data.users.length, 0); - done(); - }); + it('should return empty array if query is empty', async () => { + const data = await apiUser.search({ uid: testUid }, { query: '' }); + assert.equal(data.users.length, 0); }); - it('should filter users', (done) => { - User.create({ username: 'ipsearch_filter' }, (err, uid) => { - assert.ifError(err); - User.bans.ban(uid, 0, '', (err) => { - assert.ifError(err); - User.setUserFields(uid, { flags: 10 }, (err) => { - assert.ifError(err); - socketUser.search({ uid: adminUid }, { - query: 'ipsearch', - filters: ['online', 'banned', 'flagged'], - }, (err, data) => { - assert.ifError(err); - assert.equal(data.users[0].username, 'ipsearch_filter'); - done(); - }); - }); - }); + it('should filter users', async () => { + const uid = await User.create({ username: 'ipsearch_filter' }); + await User.bans.ban(uid, 0, ''); + await User.setUserFields(uid, { flags: 10 }); + const data = await apiUser.search({ uid: adminUid }, { + query: 'ipsearch', + filters: ['online', 'banned', 'flagged'], }); + assert.equal(data.users[0].username, 'ipsearch_filter'); }); it('should sort results by username', (done) => {