tests(api): Add ApiChallenge class

This commit is contained in:
Blade Barringer
2016-01-17 10:21:35 -06:00
parent 96f6bbfb0c
commit 4a3871f52f
4 changed files with 63 additions and 6 deletions

View File

@@ -61,6 +61,14 @@ export class ApiGroup extends ApiObject {
}
}
export class ApiChallenge extends ApiObject {
constructor (options) {
super(options);
this._docType = 'challenges';
}
}
function _updateLocalParameters (doc, update) {
each(update, (value, param) => {
set(doc, param, value);

View File

@@ -5,8 +5,4 @@ export { requester };
export { translate } from '../translate';
export { checkExistence, resetHabiticaDB } from '../mongo';
export {
generateUser,
generateGroup,
createAndPopulateGroup,
} from './object-generators';
export * from './object-generators';

View File

@@ -3,7 +3,7 @@ import {
} from 'lodash';
import Q from 'q';
import { v4 as generateUUID } from 'uuid';
import { ApiUser, ApiGroup } from '../api-classes';
import { ApiUser, ApiGroup, ApiChallenge } from '../api-classes';
import { requester } from '../requester';
// Creates a new user and returns it
@@ -106,3 +106,22 @@ export async function createAndPopulateGroup (settings = {}) {
invitees,
};
}
// Generates a new challenge. Requires an ApiGroup object with a
// _leader attribute (given with generateGroup method). The group
// will will become the group that owns the challenge. The group's
// leader will be the one to create the challenge. It takes a details
// argument for the initial challenge creation and an update argument
// which will update the challenge via the db
export async function generateChallenge (challengeCreator, group, details = {}, update = {}) {
details.group = group._id;
details.prize = details.prize || 0;
details.official = details.official || false;
let challenge = await challengeCreator.post('/challenges', details);
let apiChallenge = new ApiChallenge(challenge);
await apiChallenge.update(update);
return apiChallenge;
}