From 4a3871f52f13f75a9cc83d8352c2245cb5940755 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 17 Jan 2016 10:21:35 -0600 Subject: [PATCH] tests(api): Add ApiChallenge class --- .../v2/challenges/GET-challenges_id.test.js | 34 +++++++++++++++++++ test/helpers/api-integration/api-classes.js | 8 +++++ test/helpers/api-integration/v2/index.js | 6 +--- .../api-integration/v2/object-generators.js | 21 +++++++++++- 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 test/api/v2/challenges/GET-challenges_id.test.js diff --git a/test/api/v2/challenges/GET-challenges_id.test.js b/test/api/v2/challenges/GET-challenges_id.test.js new file mode 100644 index 0000000000..dab8a71c0a --- /dev/null +++ b/test/api/v2/challenges/GET-challenges_id.test.js @@ -0,0 +1,34 @@ +import { + createAndPopulateGroup, + generateChallenge, +} from '../../../helpers/api-integration/v2'; + +describe('GET /challenges/:id', () => { + context('Member of a challenge', () => { + let leader, party, challenge; + + before(async () => { + let { + group, + groupLeader, + } = await createAndPopulateGroup(); + + party = group; + leader = groupLeader; + challenge = await generateChallenge(leader, party, { + name: 'a created challenge', + shortName: 'aCreatedChallenge', + description: 'a description for the challenge', + }); + }); + + it('returns the challenge object', async () => { + let fetchedChallenge = await leader.get(`/challenges/${challenge._id}`); + + expect(fetchedChallenge.name).to.eql(challenge.name); + expect(fetchedChallenge.shortName).to.eql(challenge.shortName); + expect(fetchedChallenge.description).to.eql(challenge.description); + expect(fetchedChallenge.members).to.have.a.lengthOf(1); + }); + }); +}); diff --git a/test/helpers/api-integration/api-classes.js b/test/helpers/api-integration/api-classes.js index 995a738283..3fe7adbca7 100644 --- a/test/helpers/api-integration/api-classes.js +++ b/test/helpers/api-integration/api-classes.js @@ -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); diff --git a/test/helpers/api-integration/v2/index.js b/test/helpers/api-integration/v2/index.js index 964aa6dc61..1d0baeea4b 100644 --- a/test/helpers/api-integration/v2/index.js +++ b/test/helpers/api-integration/v2/index.js @@ -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'; diff --git a/test/helpers/api-integration/v2/object-generators.js b/test/helpers/api-integration/v2/object-generators.js index 3b82d1c9aa..e10a4daa9a 100644 --- a/test/helpers/api-integration/v2/object-generators.js +++ b/test/helpers/api-integration/v2/object-generators.js @@ -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; +}