mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 10:00:39 -04:00
fix(api): consistent responses from certificate/showCert (#54426)
This commit is contained in:
committed by
GitHub
parent
1183f0813a
commit
28f45b51dd
@@ -627,6 +627,25 @@ describe('certificate routes', () => {
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
test('should return cert-not-found if there is no cert with that slug', async () => {
|
||||
const response = await superRequest(
|
||||
'/certificate/showCert/foobar/not-a-valid-cert-slug',
|
||||
{
|
||||
method: 'GET'
|
||||
}
|
||||
);
|
||||
expect(response.body).toEqual({
|
||||
messages: [
|
||||
{
|
||||
type: 'info',
|
||||
message: 'flash.cert-not-found',
|
||||
variables: { certSlug: 'not-a-valid-cert-slug' }
|
||||
}
|
||||
]
|
||||
});
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,7 +19,6 @@ import {
|
||||
} from '../../../shared/config/certification-settings';
|
||||
import { normalizeChallenges, removeNulls } from '../utils/normalize';
|
||||
import { SHOW_UPCOMING_CHANGES } from '../utils/env';
|
||||
import { formatCertificationValidation } from '../utils/error-formatting';
|
||||
|
||||
const {
|
||||
legacyFrontEndChallengeId,
|
||||
@@ -287,15 +286,7 @@ export const unprotectedCertificateRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.get(
|
||||
'/certificate/showCert/:username/:certSlug',
|
||||
{
|
||||
schema: schemas.certSlug,
|
||||
errorHandler(error, request, reply) {
|
||||
if (error.validation) {
|
||||
void reply.code(400);
|
||||
return formatCertificationValidation(error.validation);
|
||||
} else {
|
||||
fastify.errorHandler(error, request, reply);
|
||||
}
|
||||
}
|
||||
schema: schemas.certSlug
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@@ -307,9 +298,13 @@ export const unprotectedCertificateRoutes: FastifyPluginCallbackTypebox = (
|
||||
if (!isKnownCertSlug(certSlug)) {
|
||||
void reply.code(404);
|
||||
return reply.send({
|
||||
type: 'info',
|
||||
message: 'flash.cert-not-found',
|
||||
variables: { certSlug }
|
||||
messages: [
|
||||
{
|
||||
type: 'info',
|
||||
message: 'flash.cert-not-found',
|
||||
variables: { certSlug }
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -108,16 +108,16 @@ export const certSlug = {
|
||||
)
|
||||
})
|
||||
]),
|
||||
400: Type.Object({
|
||||
type: Type.Literal('error'),
|
||||
message: Type.String()
|
||||
}),
|
||||
404: Type.Object({
|
||||
message: Type.Literal('flash.cert-not-found'),
|
||||
type: Type.Literal('info'),
|
||||
variables: Type.Object({
|
||||
certSlug: Type.String()
|
||||
})
|
||||
messages: Type.Array(
|
||||
Type.Object({
|
||||
message: Type.Literal('flash.cert-not-found'),
|
||||
type: Type.Literal('info'),
|
||||
variables: Type.Object({
|
||||
certSlug: Type.String()
|
||||
})
|
||||
})
|
||||
)
|
||||
}),
|
||||
500: Type.Object({
|
||||
type: Type.Literal('danger'),
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { ErrorObject } from 'ajv';
|
||||
import { certTypes } from '../../../shared/config/certification-settings';
|
||||
|
||||
type CertLogs = (typeof certTypes)[keyof typeof certTypes];
|
||||
|
||||
type FormattedError = {
|
||||
type: 'error';
|
||||
@@ -50,30 +47,6 @@ export const formatProjectCompletedValidation = (
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Format validation errors for /project-completed.
|
||||
*
|
||||
* @param errors An array of validation errors.
|
||||
* @returns Formatted errors that can be used in the response.
|
||||
*/
|
||||
export const formatCertificationValidation = (
|
||||
errors: ErrorObject[]
|
||||
): FormattedError => {
|
||||
const error = getError(errors);
|
||||
|
||||
return error.instancePath === '' &&
|
||||
Object.values(certTypes).includes(error.params.missingProperty as CertLogs)
|
||||
? ({
|
||||
type: 'error',
|
||||
message:
|
||||
'You have not provided the valid param for us to display the certification.'
|
||||
} as const)
|
||||
: ({
|
||||
type: 'error',
|
||||
message: 'That does not appear to be a valid certification request.'
|
||||
} as const);
|
||||
};
|
||||
|
||||
/**
|
||||
* Format validation errors for /coderoad-challenge-completed.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user