feat(api): DELETE user/user-token (#50733)

This commit is contained in:
Oliver Eyton-Williams
2023-07-26 12:01:12 +02:00
committed by GitHub
parent ae164d7ca8
commit 7eba794749
3 changed files with 98 additions and 2 deletions
+49 -1
View File
@@ -311,10 +311,19 @@ describe('userRoutes', () => {
describe('/user/user-token', () => {
let userId: string | undefined;
beforeEach(async () => {
const user = await fastifyTestInstance.prisma.user.findFirst({
const user = await fastifyTestInstance.prisma.user.findFirstOrThrow({
where: { email: 'foo@bar.com' }
});
userId = user?.id;
await fastifyTestInstance.prisma.userToken.create({
data: {
created: new Date(),
id: '123',
ttl: 1000,
userId
}
});
});
afterEach(async () => {
@@ -393,7 +402,37 @@ describe('userRoutes', () => {
).toBeNull();
expect(await fastifyTestInstance.prisma.userToken.count()).toBe(1);
});
test('DELETE returns 200 status with null userToken', async () => {
const response = await superRequest('/user/user-token', {
method: 'DELETE',
setCookies
});
expect(response.body).toStrictEqual({ userToken: null });
expect(response.status).toBe(200);
expect(await fastifyTestInstance.prisma.userToken.count()).toBe(0);
});
test('DELETEing a missing userToken returns 404 status with an error message', async () => {
await superRequest('/user/user-token', {
method: 'DELETE',
setCookies
});
const response = await superRequest('/user/user-token', {
method: 'DELETE',
setCookies
});
expect(response.body).toStrictEqual({
type: 'info',
message: 'userToken not found'
});
expect(response.status).toBe(404);
});
});
describe('user/get-user-session', () => {
beforeEach(async () => {
await fastifyTestInstance.prisma.user.updateMany({
@@ -577,6 +616,15 @@ describe('userRoutes', () => {
});
describe('/user/user-token', () => {
test('DELETE returns 401 status code with error message', async () => {
const response = await superRequest('/user/user-token', {
method: 'DELETE',
setCookies
});
expect(response?.statusCode).toBe(401);
});
test('POST returns 401 status code with error message', async () => {
const response = await superRequest('/user/user-token', {
method: 'POST',
+32 -1
View File
@@ -1,7 +1,7 @@
import _, { isEmpty } from 'lodash';
import { ObjectId } from 'mongodb';
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
import { customAlphabet } from 'nanoid';
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
import { schemas } from '../schemas';
import {
@@ -262,5 +262,36 @@ export const userRoutes: FastifyPluginCallbackTypebox = (
};
});
fastify.delete(
'/user/user-token',
{
schema: schemas.deleteUserToken
},
async (req, reply) => {
try {
const { count } = await fastify.prisma.userToken.deleteMany({
where: { userId: req.session.user.id }
});
if (count === 0) {
void reply.code(404);
return {
message: 'userToken not found',
type: 'info'
} as const;
}
return { userToken: null };
} catch (err) {
fastify.log.error(err);
void reply.code(500);
return {
type: 'danger',
message:
'Oops! Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.'
} as const;
}
}
);
done();
};
+17
View File
@@ -309,6 +309,23 @@ export const schemas = {
})
}
},
deleteUserToken: {
response: {
200: Type.Object({
userToken: Type.Null()
}),
404: Type.Object({
message: Type.Literal('userToken not found'),
type: Type.Literal('info')
}),
500: Type.Object({
message: Type.Literal(
'Oops! Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.'
),
type: Type.Literal('danger')
})
}
},
// Deprecated endpoints:
deprecatedEndpoints: {
response: {