mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-06 06:01:31 -05:00
feat(api): add update privacy terms endpoint (#50300)
* feat: move update privacy terms route * test: update privacy terms route tests * feat: add /settings prefix to settings route * fix: updated routes in tests * Revert "fix: updated routes in tests" This reverts commit4a1305e135. * Revert "feat: add /settings prefix to settings route" This reverts commit2a0d4566fe.
This commit is contained in:
@@ -20,7 +20,6 @@ import cors from './plugins/cors';
|
||||
import jwtAuthz from './plugins/fastify-jwt-authz';
|
||||
import security from './plugins/security';
|
||||
import sessionAuth from './plugins/session-auth';
|
||||
import { testRoutes } from './routes/test';
|
||||
import { settingRoutes } from './routes/settings';
|
||||
import { auth0Routes, devLoginCallback } from './routes/auth';
|
||||
import { testMiddleware } from './middleware';
|
||||
@@ -117,7 +116,6 @@ export const build = async (
|
||||
|
||||
void fastify.register(prismaPlugin);
|
||||
|
||||
void fastify.register(testRoutes);
|
||||
void fastify.register(auth0Routes, { prefix: '/auth' });
|
||||
if (FCC_ENABLE_DEV_LOGIN_MODE) {
|
||||
void fastify.register(devLoginCallback, { prefix: '/auth' });
|
||||
|
||||
@@ -210,6 +210,36 @@ describe('settingRoutes', () => {
|
||||
expect(response?.statusCode).toEqual(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('/update-privacy-terms', () => {
|
||||
test('PUT returns 200 status code with "success" message', async () => {
|
||||
const response = await request(fastify?.server)
|
||||
.put('/update-privacy-terms')
|
||||
.set('Cookie', cookies)
|
||||
.send({ quincyEmails: true });
|
||||
|
||||
expect(response?.statusCode).toEqual(200);
|
||||
|
||||
expect(response?.body).toEqual({
|
||||
message: 'flash.privacy-updated',
|
||||
type: 'success'
|
||||
});
|
||||
});
|
||||
|
||||
test('PUT returns 400 status code with non-boolean data', async () => {
|
||||
const response = await request(fastify?.server)
|
||||
.put('/update-privacy-terms')
|
||||
.set('Cookie', cookies)
|
||||
.send({ quincyEmails: '123' });
|
||||
|
||||
expect(response?.statusCode).toEqual(400);
|
||||
expect(response?.body).toEqual({
|
||||
error: 'Bad Request',
|
||||
message: 'body/quincyEmails must be boolean',
|
||||
statusCode: 400
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Unauthenticated User', () => {
|
||||
@@ -226,5 +256,13 @@ describe('settingRoutes', () => {
|
||||
|
||||
expect(response?.statusCode).toEqual(401);
|
||||
});
|
||||
|
||||
test('PUT /update-privacy-terms returns 401 status code for un-authenticated users', async () => {
|
||||
const response = await request(fastify?.server).put(
|
||||
'/update-privacy-terms'
|
||||
);
|
||||
|
||||
expect(response?.statusCode).toEqual(401);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -232,5 +232,47 @@ export const settingRoutes: FastifyPluginCallbackTypebox = (
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
fastify.put(
|
||||
'/update-privacy-terms',
|
||||
{
|
||||
schema: {
|
||||
body: Type.Object({
|
||||
quincyEmails: Type.Boolean()
|
||||
}),
|
||||
response: {
|
||||
200: Type.Object({
|
||||
message: Type.Literal('flash.privacy-updated'),
|
||||
type: Type.Literal('success')
|
||||
}),
|
||||
500: Type.Object({
|
||||
message: Type.Literal('flash.wrong-updating'),
|
||||
type: Type.Literal('danger')
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
async (req, reply) => {
|
||||
try {
|
||||
await fastify.prisma.user.update({
|
||||
where: { id: req.session.user.id },
|
||||
data: {
|
||||
acceptedPrivacyTerms: true,
|
||||
sendQuincyEmail: req.body.quincyEmails
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
message: 'flash.privacy-updated',
|
||||
type: 'success'
|
||||
} as const;
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
void reply.code(500);
|
||||
return { message: 'flash.wrong-updating', type: 'danger' } as const;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import { FastifyPluginCallback } from 'fastify';
|
||||
|
||||
export const testRoutes: FastifyPluginCallback = (fastify, _options, done) => {
|
||||
fastify.addHook('onRequest', fastify.authenticateSession);
|
||||
|
||||
fastify.put<{ Body: { quincyEmails: boolean } }>(
|
||||
'/update-privacy-terms',
|
||||
{
|
||||
schema: {
|
||||
body: {
|
||||
type: 'object',
|
||||
required: ['quincyEmails'],
|
||||
properties: {
|
||||
quincyEmails: { type: 'boolean' }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
async req => {
|
||||
const {
|
||||
body: { quincyEmails }
|
||||
} = req;
|
||||
|
||||
try {
|
||||
await fastify.prisma.user.update({
|
||||
where: { id: req.session.user.id },
|
||||
data: { acceptedPrivacyTerms: true, sendQuincyEmail: quincyEmails }
|
||||
});
|
||||
return { msg: 'Successfully updated' };
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
throw { msg: 'Something went wrong' };
|
||||
}
|
||||
}
|
||||
);
|
||||
done();
|
||||
};
|
||||
Reference in New Issue
Block a user