feat(scripts): add delete dead uploaded files
This commit is contained in:
@ -3,13 +3,15 @@ import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended'
|
||||
|
||||
import prisma from '../tools/database/prisma.js'
|
||||
|
||||
jest.mock('nodemailer', () => ({
|
||||
createTransport: () => {
|
||||
return {
|
||||
sendMail: jest.fn(async () => {})
|
||||
jest.mock('nodemailer', () => {
|
||||
return {
|
||||
createTransport: () => {
|
||||
return {
|
||||
sendMail: jest.fn(async () => {})
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
jest.mock('../tools/database/prisma.js', () => ({
|
||||
__esModule: true,
|
||||
|
58
src/scripts/delete-dead-uploaded-files.ts
Normal file
58
src/scripts/delete-dead-uploaded-files.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import fs from 'node:fs'
|
||||
|
||||
import prisma from '../tools/database/prisma.js'
|
||||
import { UPLOADS_URL } from '../tools/configurations/index.js'
|
||||
|
||||
const getPathStoredInDatabaseFromFile = (
|
||||
file: string,
|
||||
folderInUploadsFolder: string
|
||||
): string => {
|
||||
return `/uploads/${folderInUploadsFolder}/${file}`
|
||||
}
|
||||
|
||||
const deleteDeadUploadedFiles = async (
|
||||
folderInUploadsFolder: string,
|
||||
getElementInDatabase: (file: string) => Promise<unknown | null>
|
||||
): Promise<void> => {
|
||||
const UPLOADS_FILES_URL = new URL(`./${folderInUploadsFolder}`, UPLOADS_URL)
|
||||
const files = await fs.promises.readdir(UPLOADS_FILES_URL)
|
||||
for (const file of files) {
|
||||
if (file !== '.gitkeep') {
|
||||
const pathStoredInDatabase = getPathStoredInDatabaseFromFile(
|
||||
file,
|
||||
folderInUploadsFolder
|
||||
)
|
||||
const element = await getElementInDatabase(pathStoredInDatabase)
|
||||
if (element == null) {
|
||||
const fileURL = new URL(
|
||||
`./${folderInUploadsFolder}/${file}`,
|
||||
UPLOADS_URL
|
||||
)
|
||||
await fs.promises.rm(fileURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const main = async (): Promise<void> => {
|
||||
await deleteDeadUploadedFiles('guilds', async (icon: string) => {
|
||||
return await prisma.guild.findFirst({
|
||||
where: { icon }
|
||||
})
|
||||
})
|
||||
await deleteDeadUploadedFiles('messages', async (value: string) => {
|
||||
return await prisma.message.findFirst({
|
||||
where: { type: 'file', value }
|
||||
})
|
||||
})
|
||||
await deleteDeadUploadedFiles('users', async (logo: string) => {
|
||||
return await prisma.user.findFirst({
|
||||
where: { logo }
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
})
|
77
src/tools/plugins/__test__/authenticateUser.test.ts
Normal file
77
src/tools/plugins/__test__/authenticateUser.test.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import httpErrors from 'http-errors'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import { userExample } from '../../../models/User.js'
|
||||
import { prismaMock } from '../../../__test__/setup.js'
|
||||
import { getUserWithBearerToken } from '../authenticateUser.js'
|
||||
|
||||
const { Unauthorized, Forbidden, BadRequest } = httpErrors
|
||||
|
||||
describe('tools/plugins/authenticateUser - getUserWithBearerToken', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('shoulds succeeds with the right information', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
const currentStrategy = 'local'
|
||||
jwt.verify = jest.fn<any, any[]>((() => {
|
||||
return { id: userExample.id, currentStrategy }
|
||||
}) as any)
|
||||
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
|
||||
expect(userWithBearerToken.current.id).toEqual(userExample.id)
|
||||
expect(userWithBearerToken.current.name).toEqual(userExample.name)
|
||||
expect(userWithBearerToken.accessToken).toEqual('token')
|
||||
expect(userWithBearerToken.currentStrategy).toEqual(currentStrategy)
|
||||
})
|
||||
|
||||
it('shoulds throws `Unauthorized` if `bearerToken` is not a string', async () => {
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken(undefined)
|
||||
).rejects.toThrow(Unauthorized)
|
||||
})
|
||||
|
||||
it('shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`', async () => {
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken('Bearer')
|
||||
).rejects.toThrow(Unauthorized)
|
||||
await expect(async () => await getUserWithBearerToken('')).rejects.toThrow(
|
||||
Unauthorized
|
||||
)
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken('Bearer token token2')
|
||||
).rejects.toThrow(Unauthorized)
|
||||
})
|
||||
|
||||
it('shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`', async () => {
|
||||
jwt.verify = jest.fn<any, any[]>((() => {
|
||||
throw new Error('Invalid token')
|
||||
}) as any)
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken('Bearer token')
|
||||
).rejects.toThrow(Forbidden)
|
||||
})
|
||||
|
||||
it("shoulds throws `Forbidden` if the user doesn't exist", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(null)
|
||||
jwt.verify = jest.fn<any, any[]>((() => {
|
||||
return { id: userExample.id }
|
||||
}) as any)
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken('Bearer token')
|
||||
).rejects.toThrow(Forbidden)
|
||||
})
|
||||
|
||||
it('shoulds throws `BadRequest` if the user account is not confirmed', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
})
|
||||
jwt.verify = jest.fn<any, any[]>((() => {
|
||||
return { id: userExample.id, currentStrategy: 'local' }
|
||||
}) as any)
|
||||
await expect(
|
||||
async () => await getUserWithBearerToken('Bearer token')
|
||||
).rejects.toThrow(BadRequest)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user