From 419b520ca4434d17f3505013174e195c3a316716 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 19 Jan 2022 14:23:00 +0100 Subject: Add ability to cancel & delete video imports --- server/tests/api/check-params/jobs.ts | 43 +++++++++++++- server/tests/api/check-params/video-imports.ts | 66 ++++++++++++++++++++- server/tests/api/server/jobs.ts | 25 ++++++++ server/tests/api/videos/video-imports.ts | 81 +++++++++++++++++++++++++- 4 files changed, 212 insertions(+), 3 deletions(-) (limited to 'server/tests/api') diff --git a/server/tests/api/check-params/jobs.ts b/server/tests/api/check-params/jobs.ts index d85961d62..801b13d1e 100644 --- a/server/tests/api/check-params/jobs.ts +++ b/server/tests/api/check-params/jobs.ts @@ -3,7 +3,14 @@ import 'mocha' import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared' import { HttpStatusCode } from '@shared/models' -import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' +import { + cleanupTests, + createSingleServer, + makeGetRequest, + makePostBodyRequest, + PeerTubeServer, + setAccessTokensToServers +} from '@shared/server-commands' describe('Test jobs API validators', function () { const path = '/api/v1/jobs/failed' @@ -76,7 +83,41 @@ describe('Test jobs API validators', function () { expectedStatus: HttpStatusCode.FORBIDDEN_403 }) }) + }) + + describe('When pausing/resuming the job queue', async function () { + const commands = [ 'pause', 'resume' ] + + it('Should fail with a non authenticated user', async function () { + for (const command of commands) { + await makePostBodyRequest({ + url: server.url, + path: '/api/v1/jobs/' + command, + expectedStatus: HttpStatusCode.UNAUTHORIZED_401 + }) + } + }) + it('Should fail with a non admin user', async function () { + for (const command of commands) { + await makePostBodyRequest({ + url: server.url, + path: '/api/v1/jobs/' + command, + expectedStatus: HttpStatusCode.UNAUTHORIZED_401 + }) + } + }) + + it('Should succeed with the correct params', async function () { + for (const command of commands) { + await makePostBodyRequest({ + url: server.url, + path: '/api/v1/jobs/' + command, + token: server.accessToken, + expectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + }) }) after(async function () { diff --git a/server/tests/api/check-params/video-imports.ts b/server/tests/api/check-params/video-imports.ts index da05793a0..156a612ee 100644 --- a/server/tests/api/check-params/video-imports.ts +++ b/server/tests/api/check-params/video-imports.ts @@ -12,7 +12,9 @@ import { makePostBodyRequest, makeUploadRequest, PeerTubeServer, - setAccessTokensToServers + setAccessTokensToServers, + setDefaultVideoChannel, + waitJobs } from '@shared/server-commands' describe('Test video imports API validator', function () { @@ -29,6 +31,7 @@ describe('Test video imports API validator', function () { server = await createSingleServer(1) await setAccessTokensToServers([ server ]) + await setDefaultVideoChannel([ server ]) const username = 'user1' const password = 'my super password' @@ -347,6 +350,67 @@ describe('Test video imports API validator', function () { }) }) + describe('Deleting/cancelling a video import', function () { + let importId: number + + async function importVideo () { + const attributes = { channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo } + const res = await server.imports.importVideo({ attributes }) + + return res.id + } + + before(async function () { + importId = await importVideo() + }) + + it('Should fail with an invalid import id', async function () { + await server.imports.cancel({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + await server.imports.delete({ importId: 'artyom' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + }) + + it('Should fail with an unknown import id', async function () { + await server.imports.cancel({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) + await server.imports.delete({ importId: 42, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) + }) + + it('Should fail without token', async function () { + await server.imports.cancel({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) + await server.imports.delete({ importId, token: null, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) + }) + + it('Should fail with another user token', async function () { + await server.imports.cancel({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) + await server.imports.delete({ importId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) + }) + + it('Should fail to cancel non pending import', async function () { + this.timeout(60000) + + await waitJobs([ server ]) + + await server.imports.cancel({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) + }) + + it('Should succeed to delete an import', async function () { + await server.imports.delete({ importId }) + }) + + it('Should fail to delete a pending import', async function () { + await server.jobs.pauseJobQueue() + + importId = await importVideo() + + await server.imports.delete({ importId, expectedStatus: HttpStatusCode.CONFLICT_409 }) + }) + + it('Should succeed to cancel an import', async function () { + importId = await importVideo() + + await server.imports.cancel({ importId }) + }) + }) + after(async function () { await cleanupTests([ server ]) }) diff --git a/server/tests/api/server/jobs.ts b/server/tests/api/server/jobs.ts index 4294e1fd5..bd8ffe188 100644 --- a/server/tests/api/server/jobs.ts +++ b/server/tests/api/server/jobs.ts @@ -11,6 +11,7 @@ import { setAccessTokensToServers, waitJobs } from '@shared/server-commands' +import { wait } from '@shared/core-utils' const expect = chai.expect @@ -91,6 +92,30 @@ describe('Test jobs', function () { expect(jobs.find(j => j.state === 'completed')).to.not.be.undefined }) + it('Should pause the job queue', async function () { + this.timeout(120000) + + await servers[1].jobs.pauseJobQueue() + + await servers[1].videos.upload({ attributes: { name: 'video2' } }) + + await wait(5000) + + const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' }) + expect(body.data).to.have.lengthOf(1) + }) + + it('Should resume the job queue', async function () { + this.timeout(120000) + + await servers[1].jobs.resumeJobQueue() + + await waitJobs(servers) + + const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' }) + expect(body.data).to.have.lengthOf(0) + }) + after(async function () { await cleanupTests(servers) }) diff --git a/server/tests/api/videos/video-imports.ts b/server/tests/api/videos/video-imports.ts index e8e0f01f1..ba21ab17a 100644 --- a/server/tests/api/videos/video-imports.ts +++ b/server/tests/api/videos/video-imports.ts @@ -6,7 +6,7 @@ import { pathExists, readdir, remove } from 'fs-extra' import { join } from 'path' import { FIXTURE_URLS, testCaptionFile, testImage } from '@server/tests/shared' import { areHttpImportTestsDisabled } from '@shared/core-utils' -import { VideoPrivacy, VideoResolution } from '@shared/models' +import { HttpStatusCode, Video, VideoImportState, VideoPrivacy, VideoResolution, VideoState } from '@shared/models' import { cleanupTests, createMultipleServers, @@ -382,6 +382,85 @@ describe('Test video imports', function () { runSuite('yt-dlp') + describe('Delete/cancel an import', function () { + let server: PeerTubeServer + + let finishedImportId: number + let finishedVideo: Video + let pendingImportId: number + + async function importVideo (name: string) { + const attributes = { name, channelId: server.store.channel.id, targetUrl: FIXTURE_URLS.goodVideo } + const res = await server.imports.importVideo({ attributes }) + + return res.id + } + + before(async function () { + this.timeout(120_000) + + server = await createSingleServer(1) + + await setAccessTokensToServers([ server ]) + await setDefaultVideoChannel([ server ]) + + finishedImportId = await importVideo('finished') + await waitJobs([ server ]) + + await server.jobs.pauseJobQueue() + pendingImportId = await importVideo('pending') + + const { data } = await server.imports.getMyVideoImports() + expect(data).to.have.lengthOf(2) + + finishedVideo = data.find(i => i.id === finishedImportId).video + }) + + it('Should delete a video import', async function () { + await server.imports.delete({ importId: finishedImportId }) + + const { data } = await server.imports.getMyVideoImports() + expect(data).to.have.lengthOf(1) + expect(data[0].id).to.equal(pendingImportId) + expect(data[0].state.id).to.equal(VideoImportState.PENDING) + }) + + it('Should not have deleted the associated video', async function () { + const video = await server.videos.get({ id: finishedVideo.id, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 }) + expect(video.name).to.equal('finished') + expect(video.state.id).to.equal(VideoState.PUBLISHED) + }) + + it('Should cancel a video import', async function () { + await server.imports.cancel({ importId: pendingImportId }) + + const { data } = await server.imports.getMyVideoImports() + expect(data).to.have.lengthOf(1) + expect(data[0].id).to.equal(pendingImportId) + expect(data[0].state.id).to.equal(VideoImportState.CANCELLED) + }) + + it('Should not have processed the cancelled video import', async function () { + this.timeout(60_000) + + await server.jobs.resumeJobQueue() + + await waitJobs([ server ]) + + const { data } = await server.imports.getMyVideoImports() + expect(data).to.have.lengthOf(1) + expect(data[0].id).to.equal(pendingImportId) + expect(data[0].state.id).to.equal(VideoImportState.CANCELLED) + expect(data[0].video.state.id).to.equal(VideoState.TO_IMPORT) + }) + + it('Should delete the cancelled video import', async function () { + await server.imports.delete({ importId: pendingImportId }) + const { data } = await server.imports.getMyVideoImports() + expect(data).to.have.lengthOf(0) + }) + }) + describe('Auto update', function () { let server: PeerTubeServer -- cgit v1.2.3