PeerTube/server/tests/cli/regenerate-thumbnails.ts

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-03-12 17:04:49 +01:00
import { expect } from 'chai'
import { writeFile } from 'fs-extra'
import { basename, join } from 'path'
2021-07-16 14:27:30 +02:00
import { HttpStatusCode, Video } from '@shared/models'
2021-03-12 17:04:49 +01:00
import {
cleanupTests,
2021-07-16 09:47:51 +02:00
createMultipleServers,
2021-07-16 14:27:30 +02:00
doubleFollow,
makeGetRequest,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2021-03-12 17:04:49 +01:00
setAccessTokensToServers,
waitJobs
} from '../../../shared/server-commands'
2021-03-12 17:04:49 +01:00
2021-07-16 09:47:51 +02:00
async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
2021-07-16 09:04:35 +02:00
const video = await server.videos.get({ id: videoId })
2021-07-15 10:02:54 +02:00
const requests = [
makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 }),
makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
2021-07-15 10:02:54 +02:00
]
2021-07-15 10:02:54 +02:00
for (const req of requests) {
const res = await req
expect(res.body).to.not.have.lengthOf(0)
}
}
2021-03-12 17:04:49 +01:00
describe('Test regenerate thumbnails script', function () {
2021-07-16 09:47:51 +02:00
let servers: PeerTubeServer[]
2021-03-12 17:04:49 +01:00
let video1: Video
let video2: Video
let remoteVideo: Video
let thumbnail1Path: string
let thumbnailRemotePath: string
before(async function () {
this.timeout(60000)
2021-07-16 09:47:51 +02:00
servers = await createMultipleServers(2)
2021-03-12 17:04:49 +01:00
await setAccessTokensToServers(servers)
await doubleFollow(servers[0], servers[1])
{
2021-07-16 09:04:35 +02:00
const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
video1 = await servers[0].videos.get({ id: videoUUID1 })
2021-03-12 17:04:49 +01:00
2021-07-16 09:04:35 +02:00
thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
2021-03-12 17:04:49 +01:00
2021-07-16 09:04:35 +02:00
const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
video2 = await servers[0].videos.get({ id: videoUUID2 })
2021-03-12 17:04:49 +01:00
}
{
2021-07-16 09:04:35 +02:00
const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
2021-03-12 17:04:49 +01:00
await waitJobs(servers)
2021-07-16 09:04:35 +02:00
remoteVideo = await servers[0].videos.get({ id: videoUUID })
2021-03-12 17:04:49 +01:00
2021-07-16 09:04:35 +02:00
thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
2021-03-12 17:04:49 +01:00
}
await writeFile(thumbnail1Path, '')
await writeFile(thumbnailRemotePath, '')
})
it('Should have empty thumbnails', async function () {
{
const res = await makeGetRequest({ url: servers[0].url, path: video1.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
2021-03-12 17:04:49 +01:00
expect(res.body).to.have.lengthOf(0)
}
{
const res = await makeGetRequest({ url: servers[0].url, path: video2.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
2021-03-12 17:04:49 +01:00
expect(res.body).to.not.have.lengthOf(0)
}
{
const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
2021-03-12 17:04:49 +01:00
expect(res.body).to.have.lengthOf(0)
}
})
2021-04-08 10:08:11 +02:00
it('Should regenerate local thumbnails from the CLI', async function () {
2021-03-12 17:04:49 +01:00
this.timeout(15000)
2021-07-16 09:04:35 +02:00
await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
2021-03-12 17:04:49 +01:00
})
it('Should have generated new thumbnail files', async function () {
await testThumbnail(servers[0], video1.uuid)
await testThumbnail(servers[0], video2.uuid)
2021-03-12 17:04:49 +01:00
const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
expect(res.body).to.have.lengthOf(0)
})
it('Should have deleted old thumbnail files', async function () {
{
await makeGetRequest({ url: servers[0].url, path: video1.thumbnailPath, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
2021-03-12 17:04:49 +01:00
}
{
await makeGetRequest({ url: servers[0].url, path: video2.thumbnailPath, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
2021-03-12 17:04:49 +01:00
}
{
const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
2021-03-12 17:04:49 +01:00
expect(res.body).to.have.lengthOf(0)
}
})
after(async function () {
await cleanupTests(servers)
})
})