mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
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:
@@ -2,6 +2,7 @@ import {
|
||||
generateUser,
|
||||
resetHabiticaDB,
|
||||
generateGroup,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import {
|
||||
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 () => {
|
||||
await expect(user.get('/groups?type=tavern'))
|
||||
.to.eventually.have.a.lengthOf(1)
|
||||
|
||||
@@ -100,10 +100,6 @@ api.getGroups = {
|
||||
let sort = '-memberCount';
|
||||
|
||||
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);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -8,7 +8,10 @@ import _ from 'lodash';
|
||||
import { model as Challenge} from './challenge';
|
||||
import validator from 'validator';
|
||||
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 baseModel from '../libs/api-v3/baseModel';
|
||||
import { sendTxn as sendTxnEmail } from '../libs/api-v3/email';
|
||||
@@ -139,10 +142,16 @@ schema.statics.getGroup = async function getGroup (options = {}) {
|
||||
return group;
|
||||
};
|
||||
|
||||
export const VALID_QUERY_TYPES = ['party', 'guilds', 'privateGuilds', 'publicGuilds', 'tavern'];
|
||||
|
||||
schema.statics.getGroups = async function getGroups (options = {}) {
|
||||
let {user, types, groupFields = basicFields, sort = '-memberCount', populateLeader = false} = options;
|
||||
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 => {
|
||||
switch (type) {
|
||||
case 'party': {
|
||||
|
||||
Reference in New Issue
Block a user