mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-13 21:02:08 +08:00
feat(api): return flash.wrong-updating if invalid request (#51604)
This commit is contained in:
parent
bbebaf0f94
commit
bd460ecabf
@ -24,6 +24,11 @@ const profileUI = {
|
||||
showPortfolio: true
|
||||
};
|
||||
|
||||
const updateErrorResponse = {
|
||||
type: 'danger',
|
||||
message: 'flash.wrong-updating'
|
||||
};
|
||||
|
||||
describe('settingRoutes', () => {
|
||||
setupServer();
|
||||
|
||||
@ -146,12 +151,7 @@ describe('settingRoutes', () => {
|
||||
}
|
||||
});
|
||||
|
||||
expect(response.body).toEqual({
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
error: 'Bad Request',
|
||||
message: `body/profileUI must have required property 'showAbout'`,
|
||||
statusCode: 400
|
||||
});
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -180,6 +180,7 @@ describe('settingRoutes', () => {
|
||||
theme: 'invalid'
|
||||
});
|
||||
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -392,6 +393,7 @@ describe('settingRoutes', () => {
|
||||
setCookies
|
||||
}).send({ keyboardShortcuts: 'invalid' });
|
||||
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -426,6 +428,7 @@ describe('settingRoutes', () => {
|
||||
githubProfile: 'invalid'
|
||||
});
|
||||
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -450,6 +453,7 @@ describe('settingRoutes', () => {
|
||||
setCookies
|
||||
}).send({ sendQuincyEmail: 'invalid' });
|
||||
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -525,6 +529,7 @@ describe('settingRoutes', () => {
|
||||
setCookies
|
||||
}).send({ isHonest: false });
|
||||
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -549,12 +554,7 @@ describe('settingRoutes', () => {
|
||||
setCookies
|
||||
}).send({ quincyEmails: '123' });
|
||||
|
||||
expect(response.body).toEqual({
|
||||
code: 'FST_ERR_VALIDATION',
|
||||
error: 'Bad Request',
|
||||
message: 'body/quincyEmails must be boolean',
|
||||
statusCode: 400
|
||||
});
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
@ -581,10 +581,7 @@ describe('settingRoutes', () => {
|
||||
setCookies
|
||||
}).send({});
|
||||
|
||||
expect(response.body).toEqual({
|
||||
type: 'danger',
|
||||
message: 'flash.wrong-updating'
|
||||
});
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
|
||||
@ -599,10 +596,7 @@ describe('settingRoutes', () => {
|
||||
]
|
||||
});
|
||||
|
||||
expect(response.body).toEqual({
|
||||
type: 'danger',
|
||||
message: 'flash.wrong-updating'
|
||||
});
|
||||
expect(response.body).toEqual(updateErrorResponse);
|
||||
expect(response.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import type { FastifyError, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
|
||||
import { isProfane } from 'no-profanity';
|
||||
import { isValidUsername } from '../../../shared/utils/validate';
|
||||
@ -38,10 +39,24 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.addHook('onRequest', fastify.csrfProtection);
|
||||
fastify.addHook('onRequest', fastify.authenticateSession);
|
||||
|
||||
function updateErrorHandler(
|
||||
error: FastifyError,
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
if (error.validation) {
|
||||
void reply.code(400);
|
||||
void reply.send({ message: 'flash.wrong-updating', type: 'danger' });
|
||||
} else {
|
||||
fastify.errorHandler(error, request, reply);
|
||||
}
|
||||
}
|
||||
|
||||
fastify.put(
|
||||
'/update-my-profileui',
|
||||
{
|
||||
schema: schemas.updateMyProfileUI
|
||||
schema: schemas.updateMyProfileUI,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -79,7 +94,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-my-theme',
|
||||
{
|
||||
schema: schemas.updateMyTheme
|
||||
schema: schemas.updateMyTheme,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -105,7 +121,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-my-socials',
|
||||
{
|
||||
schema: schemas.updateMySocials
|
||||
schema: schemas.updateMySocials,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -252,7 +269,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-my-keyboard-shortcuts',
|
||||
{
|
||||
schema: schemas.updateMyKeyboardShortcuts
|
||||
schema: schemas.updateMyKeyboardShortcuts,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -278,7 +296,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-my-quincy-email',
|
||||
{
|
||||
schema: schemas.updateMyQuincyEmail
|
||||
schema: schemas.updateMyQuincyEmail,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -304,7 +323,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-my-honesty',
|
||||
{
|
||||
schema: schemas.updateMyHonesty
|
||||
schema: schemas.updateMyHonesty,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -330,7 +350,8 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
fastify.put(
|
||||
'/update-privacy-terms',
|
||||
{
|
||||
schema: schemas.updateMyPrivacyTerms
|
||||
schema: schemas.updateMyPrivacyTerms,
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
@ -358,14 +379,7 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
'/update-my-portfolio',
|
||||
{
|
||||
schema: schemas.updateMyPortfolio,
|
||||
errorHandler: (error, request, reply) => {
|
||||
if (error.validation) {
|
||||
void reply.code(400);
|
||||
void reply.send({ message: 'flash.wrong-updating', type: 'danger' });
|
||||
} else {
|
||||
fastify.errorHandler(error, request, reply);
|
||||
}
|
||||
}
|
||||
errorHandler: updateErrorHandler
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
|
||||
@ -137,6 +137,7 @@ export const schemas = {
|
||||
},
|
||||
updateMyAbout: {
|
||||
body: Type.Object({
|
||||
// TODO(Post-MVP): make these required
|
||||
about: Type.Optional(Type.String()),
|
||||
name: Type.Optional(Type.String()),
|
||||
picture: Type.Optional(Type.String()),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user