]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/videos/resumable-upload.ts
Fix silent 500 after resumable upload
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / resumable-upload.ts
index 6c01c7e78f0d38e153a745e52e1c06c24ff35765..59970aa943ea57854b3387cd0d7a3fa0617908c9 100644 (file)
@@ -4,21 +4,15 @@ import 'mocha'
 import * as chai from 'chai'
 import { pathExists, readdir, stat } from 'fs-extra'
 import { join } from 'path'
-import { HttpStatusCode } from '@shared/core-utils'
 import {
   buildAbsoluteFixturePath,
-  buildServerDirectory,
   cleanupTests,
-  flushAndRunServer,
-  getMyUserInformation,
-  prepareResumableUpload,
-  sendResumableChunks,
-  ServerInfo,
+  createSingleServer,
+  PeerTubeServer,
   setAccessTokensToServers,
-  setDefaultVideoChannel,
-  updateUser
+  setDefaultVideoChannel
 } from '@shared/extra-utils'
-import { MyUser, VideoPrivacy } from '@shared/models'
+import { HttpStatusCode, VideoPrivacy } from '@shared/models'
 
 const expect = chai.expect
 
@@ -26,7 +20,7 @@ const expect = chai.expect
 
 describe('Test resumable upload', function () {
   const defaultFixture = 'video_short.mp4'
-  let server: ServerInfo
+  let server: PeerTubeServer
   let rootId: number
 
   async function buildSize (fixture: string, size?: number) {
@@ -41,14 +35,14 @@ describe('Test resumable upload', function () {
 
     const attributes = {
       name: 'video',
-      channelId: server.videoChannel.id,
+      channelId: server.store.channel.id,
       privacy: VideoPrivacy.PUBLIC,
       fixture: defaultFixture
     }
 
     const mimetype = 'video/mp4'
 
-    const res = await prepareResumableUpload({ url: server.url, token: server.accessToken, attributes, size, mimetype })
+    const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype })
 
     return res.header['location'].split('?')[1]
   }
@@ -66,15 +60,13 @@ describe('Test resumable upload', function () {
     const size = await buildSize(defaultFixture, options.size)
     const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
 
-    return sendResumableChunks({
-      url: server.url,
-      token: server.accessToken,
+    return server.videos.sendResumableChunks({
       pathUploadId,
       videoFilePath: absoluteFilePath,
       size,
       contentLength,
       contentRangeBuilder,
-      specialStatus: expectedStatus
+      expectedStatus
     })
   }
 
@@ -82,7 +74,7 @@ describe('Test resumable upload', function () {
     const uploadId = uploadIdArg.replace(/^upload_id=/, '')
 
     const subPath = join('tmp', 'resumable-uploads', uploadId)
-    const filePath = buildServerDirectory(server, subPath)
+    const filePath = server.servers.buildDirectory(subPath)
     const exists = await pathExists(filePath)
 
     if (expectedSize === null) {
@@ -97,7 +89,7 @@ describe('Test resumable upload', function () {
 
   async function countResumableUploads () {
     const subPath = join('tmp', 'resumable-uploads')
-    const filePath = buildServerDirectory(server, subPath)
+    const filePath = server.servers.buildDirectory(subPath)
 
     const files = await readdir(filePath)
     return files.length
@@ -106,19 +98,14 @@ describe('Test resumable upload', function () {
   before(async function () {
     this.timeout(30000)
 
-    server = await flushAndRunServer(1)
+    server = await createSingleServer(1)
     await setAccessTokensToServers([ server ])
     await setDefaultVideoChannel([ server ])
 
-    const res = await getMyUserInformation(server.url, server.accessToken)
-    rootId = (res.body as MyUser).id
+    const body = await server.users.getMyInfo()
+    rootId = body.id
 
-    await updateUser({
-      url: server.url,
-      userId: rootId,
-      accessToken: server.accessToken,
-      videoQuota: 10_000_000
-    })
+    await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
   })
 
   describe('Directory cleaning', function () {
@@ -126,6 +113,7 @@ describe('Test resumable upload', function () {
     it('Should correctly delete files after an upload', async function () {
       const uploadId = await prepareUpload()
       await sendChunks({ pathUploadId: uploadId })
+      await server.videos.endResumableUpload({ pathUploadId: uploadId })
 
       expect(await countResumableUploads()).to.equal(0)
     })
@@ -137,13 +125,13 @@ describe('Test resumable upload', function () {
     })
 
     it('Should not delete recent uploads', async function () {
-      await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
+      await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
 
       expect(await countResumableUploads()).to.equal(2)
     })
 
     it('Should delete old uploads', async function () {
-      await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
+      await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
 
       expect(await countResumableUploads()).to.equal(0)
     })
@@ -159,8 +147,7 @@ describe('Test resumable upload', function () {
     })
 
     it('Should not accept more chunks than expected', async function () {
-      const size = 100
-      const uploadId = await prepareUpload(size)
+      const uploadId = await prepareUpload(100)
 
       await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
       await checkFileSize(uploadId, 0)
@@ -169,8 +156,14 @@ describe('Test resumable upload', function () {
     it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
       const uploadId = await prepareUpload(1500)
 
-      await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
-      await checkFileSize(uploadId, 0)
+      // Content length check seems to have changed in v16
+      if (process.version.startsWith('v16')) {
+        await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409, contentLength: 1000 })
+        await checkFileSize(uploadId, 1000)
+      } else {
+        await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
+        await checkFileSize(uploadId, 0)
+      }
     })
 
     it('Should not accept more chunks than expected with an invalid content length', async function () {
@@ -178,8 +171,13 @@ describe('Test resumable upload', function () {
 
       const size = 1000
 
-      const contentRangeBuilder = start => `bytes ${start}-${start + size - 1}/${size}`
-      await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentRangeBuilder, contentLength: size })
+      // Content length check seems to have changed in v16
+      const expectedStatus = process.version.startsWith('v16')
+        ? HttpStatusCode.CONFLICT_409
+        : HttpStatusCode.BAD_REQUEST_400
+
+      const contentRangeBuilder = (start: number) => `bytes ${start}-${start + size - 1}/${size}`
+      await sendChunks({ pathUploadId: uploadId, expectedStatus, contentRangeBuilder, contentLength: size })
       await checkFileSize(uploadId, 0)
     })
   })