Skip to content

Commit

Permalink
Added Chai Things, updated tests, updated get user challenges, and ad…
Browse files Browse the repository at this point in the history
…ded get user challenges tests
  • Loading branch information
TheHollidayInn committed Jan 30, 2016
1 parent 6716d1f commit e81a371
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"babel-eslint": "^4.1.6",
"chai": "^3.4.0",
"chai-as-promised": "^5.1.0",
"chai-things": "^0.2.0",
"coveralls": "^2.11.2",
"csv": "~0.3.6",
"deep-diff": "~0.1.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ describe('GET challenges/group/:groupId', () => {
it('should return group challenges for member', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);

expect(_.findIndex(challenges, {_id: challenge._id})).to.be.above(-1);
expect(_.findIndex(challenges, {_id: challenge2._id})).to.be.above(-1);
challenges.should.contain.a.item.with.property('_id', challenge._id);
challenges.should.contain.a.item.with.property('_id', challenge2._id);
});
});

Expand Down Expand Up @@ -75,8 +75,8 @@ describe('GET challenges/group/:groupId', () => {
it('should return group challenges for member', async () => {
let challenges = await user.get(`/challenges/groups/${privateGuild._id}`);

expect(_.findIndex(challenges, {_id: challenge._id})).to.be.above(-1);
expect(_.findIndex(challenges, {_id: challenge2._id})).to.be.above(-1);
challenges.should.contain.a.item.with.property('_id', challenge._id);
challenges.should.contain.a.item.with.property('_id', challenge2._id);
});
});
});
66 changes: 66 additions & 0 deletions test/api/v3/integration/challenges/GET-challenges_user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
generateUser,
generateChallenge,
createAndPopulateGroup,
} from '../../../../helpers/api-v3-integration.helper';

describe('GET challenges/user', () => {
let user, member, nonMember, challenge, challenge2;

before(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: {
name: 'TestGuild',
type: 'guild',
privacy: 'public',
},
members: 1,
});

user = groupLeader;

member = members[0];
nonMember = await generateUser();

challenge = await generateChallenge(user, group);
challenge2 = await generateChallenge(user, group);
});

it('should return challenges user has joined', async () => {
await nonMember.post(`/challenges/${challenge._id}/join`);

let challenges = await nonMember.get(`/challenges/user`);

challenges.should.contain.a.item.with.property('_id', challenge._id);
});

it('should return challenges user has created', async () => {
let challenges = await user.get(`/challenges/user`);

challenges.should.contain.a.item.with.property('_id', challenge._id);
challenges.should.contain.a.item.with.property('_id', challenge2._id);
});

it('should return challenges in user\'s group', async () => {
let challenges = await member.get(`/challenges/user`);

challenges.should.contain.a.item.with.property('_id', challenge._id);
challenges.should.contain.a.item.with.property('_id', challenge2._id);
});

it('should not return challenges user doesn\'t have access to', async () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestPrivateGuild',
type: 'guild',
privacy: 'private',
},
});

let privateChallenge = await generateChallenge(groupLeader, group);

let challenges = await nonMember.get(`/challenges/user`);

challenges.should.not.contain.a.item.with.property('_id', privateChallenge._id);
});
});
2 changes: 2 additions & 0 deletions test/helpers/globals.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ global._ = require('lodash');
global.chai = require('chai');
chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));
chai.should();
chai.use(require('chai-things'));
global.expect = chai.expect;
global.sinon = require('sinon');
global.sandbox = sinon.sandbox.create();
Expand Down
11 changes: 6 additions & 5 deletions website/src/controllers/api-v3/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ api.joinChallenge = {
let challenge = await Challenge.findOne({ _id: req.params.challengeId });
if (!challenge) throw new NotFound(res.t('challengeNotFound'));

if (!challenge.hasAccess(user)) throw new NotFound(res.t('challengeNotFound'));
let group = await Group.getGroup({user, groupId: challenge.groupId, fields: '_id type privacy', optionalMembership: true});
if (!challenge.hasAccess(user) && (!group || !challenge.canView(user, group))) throw new NotFound(res.t('challengeNotFound'));

if (_.contains(user.challenges, challenge._id)) throw new NotAuthorized(res.t('userAlreadyInChallenge'));

Expand All @@ -130,16 +131,16 @@ api.joinChallenge = {
};

/**
* @api {get} /challenges Get challenges for a user
* @api {get} /challenges/user Get challenges for a user
* @apiVersion 3.0.0
* @apiName GetChallenges
* @apiName GetUserChallenges
* @apiGroup Challenge
*
* @apiSuccess {Array} challenges An array of challenges
*/
api.getChallenges = {
api.getUserChallenges = {
method: 'GET',
url: '/challenges',
url: '/challenges/user',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
let user = res.locals.user;
Expand Down

0 comments on commit e81a371

Please sign in to comment.