v3 fix GET /groups: return an error only if an invalid type is supplied not when there are 0 results (#7203)

This commit is contained in:
Matteo Pagliazzi
2016-05-13 19:08:38 +02:00
parent 199732539f
commit 468a312357
3 changed files with 20 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import {
generateUser, generateUser,
resetHabiticaDB, resetHabiticaDB,
generateGroup, generateGroup,
translate as t,
} from '../../../../helpers/api-v3-integration.helper'; } from '../../../../helpers/api-v3-integration.helper';
import { import {
TAVERN_ID, TAVERN_ID,
@@ -70,6 +71,15 @@ describe('GET /groups', () => {
}); });
}); });
it('returns error when an invalid ?type query is passed', async () => {
await expect(user.get('/groups?type=invalid'))
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('groupTypesRequired'),
});
});
it('returns only the tavern when tavern passed in as query', async () => { it('returns only the tavern when tavern passed in as query', async () => {
await expect(user.get('/groups?type=tavern')) await expect(user.get('/groups?type=tavern'))
.to.eventually.have.a.lengthOf(1) .to.eventually.have.a.lengthOf(1)

View File

@@ -100,10 +100,6 @@ api.getGroups = {
let sort = '-memberCount'; let sort = '-memberCount';
let results = await Group.getGroups({user, types, groupFields, sort}); let results = await Group.getGroups({user, types, groupFields, sort});
// If no valid value for type was supplied, return an error
if (results.length === 0) throw new BadRequest(res.t('groupTypesRequired'));
res.respond(200, results); res.respond(200, results);
}, },
}; };

View File

@@ -8,7 +8,10 @@ import _ from 'lodash';
import { model as Challenge} from './challenge'; import { model as Challenge} from './challenge';
import validator from 'validator'; import validator from 'validator';
import { removeFromArray } from '../libs/api-v3/collectionManipulators'; import { removeFromArray } from '../libs/api-v3/collectionManipulators';
import { InternalServerError } from '../libs/api-v3/errors'; import {
InternalServerError,
BadRequest,
} from '../libs/api-v3/errors';
import * as firebase from '../libs/api-v2/firebase'; import * as firebase from '../libs/api-v2/firebase';
import baseModel from '../libs/api-v3/baseModel'; import baseModel from '../libs/api-v3/baseModel';
import { sendTxn as sendTxnEmail } from '../libs/api-v3/email'; import { sendTxn as sendTxnEmail } from '../libs/api-v3/email';
@@ -139,10 +142,16 @@ schema.statics.getGroup = async function getGroup (options = {}) {
return group; return group;
}; };
export const VALID_QUERY_TYPES = ['party', 'guilds', 'privateGuilds', 'publicGuilds', 'tavern'];
schema.statics.getGroups = async function getGroups (options = {}) { schema.statics.getGroups = async function getGroups (options = {}) {
let {user, types, groupFields = basicFields, sort = '-memberCount', populateLeader = false} = options; let {user, types, groupFields = basicFields, sort = '-memberCount', populateLeader = false} = options;
let queries = []; let queries = [];
// Throw error if an invalid type is supplied
let areValidTypes = types.every(type => VALID_QUERY_TYPES.indexOf(type) !== -1);
if (!areValidTypes) throw new BadRequest(shared.i18n.t('groupTypesRequired'));
types.forEach(type => { types.forEach(type => {
switch (type) { switch (type) {
case 'party': { case 'party': {