[FIX] users.info endpoint not handling the error if the user does not exist (#16495)

* Prevent user to be undefined

* Move findById to Base model

Co-authored-by: Diego Sampaio <chinello@gmail.com>
pull/16618/head
Marcos Spessatto Defendi 6 years ago committed by GitHub
parent f69130e9fd
commit af75981fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/api/server/v1/users.js
  2. 3
      app/lib/server/functions/getFullUserData.js
  3. 6
      app/models/server/models/Users.js
  4. 8
      app/models/server/models/_Base.js
  5. 4
      app/models/server/models/_BaseDb.js
  6. 15
      tests/end-to-end/api/01-users.js

@ -166,7 +166,7 @@ API.v1.addRoute('users.info', { authRequired: true }, {
: getFullUserData(params);
if (!result || result.count() !== 1) {
return API.v1.failure(`Failed to get the user data for the userId of "${ this.userId }".`);
return API.v1.failure('User not found.');
}
const [user] = result.fetch();
const myself = user._id === this.userId;

@ -78,6 +78,9 @@ export function getFullUserDataById({ userId, filterId }) {
export const getFullUserData = function({ userId, filter, limit: l }) {
const username = s.trim(filter);
const userToRetrieveFullUserData = username && Users.findOneByUsername(username, { fields: { username: 1 } });
if (!userToRetrieveFullUserData) {
return;
}
const isMyOwnInfo = userToRetrieveFullUserData && userToRetrieveFullUserData._id === userId;
const viewFullOtherUserInfo = hasPermission(userId, 'view-full-other-user-info');

@ -493,12 +493,6 @@ export class Users extends Base {
}
// FIND
findById(userId) {
const query = { _id: userId };
return this.find(query);
}
findByIds(users, options) {
const query = { _id: { $in: users } };
return this.find(query, options);

@ -119,6 +119,14 @@ export class Base {
}
}
findById(...args) {
try {
return this[this.origin].findById(...args);
} catch (e) {
console.error('Exception on find', e, ...args);
}
}
findOne(...args) {
try {
return this[this.origin].findOne(...args);

@ -130,6 +130,10 @@ export class BaseDb extends EventEmitter {
return this.model.find(...args);
}
findById(_id, options) {
return this.find({ _id }, options);
}
findOne(...args) {
this._doNotMixInclusionAndExclusionFields(args[1]);
return this.model.findOne(...args);

@ -198,6 +198,21 @@ describe('[Users]', function() {
describe('[/users.info]', () => {
after(() => updatePermission('view-other-user-channels', ['admin']));
it('should return an error when the user does not exist', (done) => {
request.get(api('users.info'))
.set(credentials)
.query({
username: 'invalid-username',
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error').and.to.be.equal('User not found.');
})
.end(done);
});
it('should query information about a user by userId', (done) => {
request.get(api('users.info'))
.set(credentials)

Loading…
Cancel
Save