fix(api): send json messages for challenge routes (#37494)

This commit is contained in:
mrugesh
2019-10-24 17:30:23 +05:30
committed by GitHub
parent 94635dcc0b
commit f090730015
3 changed files with 19 additions and 14 deletions

View File

@ -195,17 +195,22 @@ export function isValidChallengeCompletion(req, res, next) {
body: { id, challengeType, solution } body: { id, challengeType, solution }
} = req; } = req;
const isValidChallengeCompletionErrorMsg = {
type: 'error',
message: 'That does not appear to be a valid challenge submission.'
};
if (!ObjectID.isValid(id)) { if (!ObjectID.isValid(id)) {
log('isObjectId', id, ObjectID.isValid(id)); log('isObjectId', id, ObjectID.isValid(id));
return res.sendStatus(403); return res.status(403).json(isValidChallengeCompletionErrorMsg);
} }
if ('challengeType' in req.body && !isNumeric(String(challengeType))) { if ('challengeType' in req.body && !isNumeric(String(challengeType))) {
log('challengeType', challengeType, isNumeric(challengeType)); log('challengeType', challengeType, isNumeric(challengeType));
return res.sendStatus(403); return res.status(403).json(isValidChallengeCompletionErrorMsg);
} }
if ('solution' in req.body && !isURL(solution)) { if ('solution' in req.body && !isURL(solution)) {
log('isObjectId', id, ObjectID.isValid(id)); log('isObjectId', id, ObjectID.isValid(id));
return res.sendStatus(403); return res.status(403).json(isValidChallengeCompletionErrorMsg);
} }
return next(); return next();
} }
@ -261,11 +266,11 @@ function projectCompleted(req, res, next) {
// only basejumps require github links // only basejumps require github links
(completedChallenge.challengeType === 4 && !completedChallenge.githubLink) (completedChallenge.challengeType === 4 && !completedChallenge.githubLink)
) { ) {
req.flash( return res.status(403).json({
'danger', type: 'error',
"You haven't supplied the necessary URLs for us to inspect your work." message:
); 'You have not provided the valid links for us to inspect your work.'
return res.sendStatus(403); });
} }
return user return user

View File

@ -247,8 +247,8 @@ describe('boot/challenge', () => {
isValidChallengeCompletion(req, res, next); isValidChallengeCompletion(req, res, next);
expect(res.sendStatus.called).toBe(true); expect(res.status.called).toBe(true);
expect(res.sendStatus.getCall(0).args[0]).toBe(403); expect(res.status.getCall(0).args[0]).toBe(403);
expect(next.called).toBe(false); expect(next.called).toBe(false);
}); });
@ -262,8 +262,8 @@ describe('boot/challenge', () => {
isValidChallengeCompletion(req, res, next); isValidChallengeCompletion(req, res, next);
expect(res.sendStatus.called).toBe(true); expect(res.status.called).toBe(true);
expect(res.sendStatus.getCall(0).args[0]).toBe(403); expect(res.status.getCall(0).args[0]).toBe(403);
expect(next.called).toBe(false); expect(next.called).toBe(false);
}); });
@ -281,8 +281,8 @@ describe('boot/challenge', () => {
isValidChallengeCompletion(req, res, next); isValidChallengeCompletion(req, res, next);
expect(res.sendStatus.called).toBe(true); expect(res.status.called).toBe(true);
expect(res.sendStatus.getCall(0).args[0]).toBe(403); expect(res.status.getCall(0).args[0]).toBe(403);
expect(next.called).toBe(false); expect(next.called).toBe(false);
}); });