diff --git a/test/api/v3/integration/challenges/POST-challenge_flag.test.js b/test/api/v3/integration/challenges/POST-challenge_flag.test.js index adb2f37235..702d667e6b 100644 --- a/test/api/v3/integration/challenges/POST-challenge_flag.test.js +++ b/test/api/v3/integration/challenges/POST-challenge_flag.test.js @@ -7,6 +7,7 @@ import { describe('POST /challenges/:challengeId/flag', () => { let user; + let challengeGroup; let challenge; beforeEach(async () => { @@ -20,6 +21,7 @@ describe('POST /challenges/:challengeId/flag', () => { }); user = groupLeader; + challengeGroup = group; challenge = await generateChallenge(user, group); }); @@ -59,4 +61,19 @@ describe('POST /challenges/:challengeId/flag', () => { message: t('messageChallengeFlagAlreadyReported'), }); }); + + it('returns an error when user tries to flag an official challenge', async () => { + await user.updateOne({ + permissions: { + challengeAdmin: true, + }, + }); + challenge = await generateChallenge(user, challengeGroup, { official: true }); + await expect(user.post(`/challenges/${challenge._id}/flag`)) + .to.eventually.be.rejected.and.eql({ + code: 404, + error: 'NotFound', + message: t('messageChallengeFlagOfficial'), + }); + }); }); diff --git a/test/api/v3/integration/challenges/POST-challenges.test.js b/test/api/v3/integration/challenges/POST-challenges.test.js index e471e0c5f2..f74e7c7422 100644 --- a/test/api/v3/integration/challenges/POST-challenges.test.js +++ b/test/api/v3/integration/challenges/POST-challenges.test.js @@ -331,5 +331,71 @@ describe('POST /challenges', () => { expect(updatedChallenge.summary).to.eql(summary); }); + + it('sets categories for challenges', async () => { + const testCategory = { _id: '65c1172997c0b24600371ea9', slug: 'test', name: 'Test' }; + const challenge = await groupLeader.post('/challenges', { + group: group._id, + name: 'Test Challenge', + shortName: 'TC Label', + categories: [testCategory], + }); + + const updatedChallenge = await groupLeader.get(`/challenges/${challenge._id}`); + + expect(updatedChallenge.categories).to.eql([testCategory]); + }); + + it('does not set habitica_official category for non-admins', async () => { + const testCategory = { _id: '65c1172997c0b24600371ea9', slug: 'habitica_official', name: 'habitica_official' }; + await expect(groupLeader.post('/challenges', { + group: group._id, + name: 'Test Challenge', + shortName: 'TC Label', + categories: [testCategory], + })).to.eventually.be.rejected.and.eql({ + code: 401, + error: 'NotAuthorized', + message: t('noPrivAccess'), + }); + }); + + it('sets habitica_official category for admins', async () => { + await groupLeader.updateOne({ + permissions: { + challengeAdmin: true, + }, + }); + + const testCategory = { _id: '65c1172997c0b24600371ea9', slug: 'habitica_official', name: 'habitica_official' }; + const challenge = await groupLeader.post('/challenges', { + group: group._id, + name: 'Test Challenge', + shortName: 'TC Label', + categories: [testCategory], + }); + + const updatedChallenge = await groupLeader.get(`/challenges/${challenge._id}`); + expect(updatedChallenge.categories).to.eql([testCategory]); + }); + + it('sets official if the habitica_official category is set for admins', async () => { + await groupLeader.updateOne({ + permissions: { + challengeAdmin: true, + }, + }); + + const testCategory = { _id: '65c1172997c0b24600371ea9', slug: 'habitica_official', name: 'habitica_official' }; + const challenge = await groupLeader.post('/challenges', { + group: group._id, + name: 'Test Challenge', + shortName: 'TC Label', + categories: [testCategory], + }); + + const updatedChallenge = await groupLeader.get(`/challenges/${challenge._id}`); + expect(updatedChallenge.official).to.eql(true); + }); }); }); diff --git a/website/client/src/components/challenges/challengeDetail.vue b/website/client/src/components/challenges/challengeDetail.vue index 12de0133b8..dac2c52a33 100644 --- a/website/client/src/components/challenges/challengeDetail.vue +++ b/website/client/src/components/challenges/challengeDetail.vue @@ -223,6 +223,7 @@