]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/resumable-upload.ts
Reorganize imports
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / resumable-upload.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { pathExists, readdir, stat } from 'fs-extra'
6 import { join } from 'path'
7 import {
8 buildAbsoluteFixturePath,
9 cleanupTests,
10 createSingleServer,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel
14 } from '@shared/extra-utils'
15 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
16
17 const expect = chai.expect
18
19 // Most classic resumable upload tests are done in other test suites
20
21 describe('Test resumable upload', function () {
22 const defaultFixture = 'video_short.mp4'
23 let server: PeerTubeServer
24 let rootId: number
25
26 async function buildSize (fixture: string, size?: number) {
27 if (size !== undefined) return size
28
29 const baseFixture = buildAbsoluteFixturePath(fixture)
30 return (await stat(baseFixture)).size
31 }
32
33 async function prepareUpload (sizeArg?: number) {
34 const size = await buildSize(defaultFixture, sizeArg)
35
36 const attributes = {
37 name: 'video',
38 channelId: server.store.channel.id,
39 privacy: VideoPrivacy.PUBLIC,
40 fixture: defaultFixture
41 }
42
43 const mimetype = 'video/mp4'
44
45 const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype })
46
47 return res.header['location'].split('?')[1]
48 }
49
50 async function sendChunks (options: {
51 pathUploadId: string
52 size?: number
53 expectedStatus?: HttpStatusCode
54 contentLength?: number
55 contentRange?: string
56 contentRangeBuilder?: (start: number, chunk: any) => string
57 }) {
58 const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options
59
60 const size = await buildSize(defaultFixture, options.size)
61 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
62
63 return server.videos.sendResumableChunks({
64 pathUploadId,
65 videoFilePath: absoluteFilePath,
66 size,
67 contentLength,
68 contentRangeBuilder,
69 expectedStatus
70 })
71 }
72
73 async function checkFileSize (uploadIdArg: string, expectedSize: number | null) {
74 const uploadId = uploadIdArg.replace(/^upload_id=/, '')
75
76 const subPath = join('tmp', 'resumable-uploads', uploadId)
77 const filePath = server.servers.buildDirectory(subPath)
78 const exists = await pathExists(filePath)
79
80 if (expectedSize === null) {
81 expect(exists).to.be.false
82 return
83 }
84
85 expect(exists).to.be.true
86
87 expect((await stat(filePath)).size).to.equal(expectedSize)
88 }
89
90 async function countResumableUploads () {
91 const subPath = join('tmp', 'resumable-uploads')
92 const filePath = server.servers.buildDirectory(subPath)
93
94 const files = await readdir(filePath)
95 return files.length
96 }
97
98 before(async function () {
99 this.timeout(30000)
100
101 server = await createSingleServer(1)
102 await setAccessTokensToServers([ server ])
103 await setDefaultVideoChannel([ server ])
104
105 const body = await server.users.getMyInfo()
106 rootId = body.id
107
108 await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
109 })
110
111 describe('Directory cleaning', function () {
112
113 it('Should correctly delete files after an upload', async function () {
114 const uploadId = await prepareUpload()
115 await sendChunks({ pathUploadId: uploadId })
116
117 expect(await countResumableUploads()).to.equal(0)
118 })
119
120 it('Should not delete files after an unfinished upload', async function () {
121 await prepareUpload()
122
123 expect(await countResumableUploads()).to.equal(2)
124 })
125
126 it('Should not delete recent uploads', async function () {
127 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
128
129 expect(await countResumableUploads()).to.equal(2)
130 })
131
132 it('Should delete old uploads', async function () {
133 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
134
135 expect(await countResumableUploads()).to.equal(0)
136 })
137 })
138
139 describe('Resumable upload and chunks', function () {
140
141 it('Should accept the same amount of chunks', async function () {
142 const uploadId = await prepareUpload()
143 await sendChunks({ pathUploadId: uploadId })
144
145 await checkFileSize(uploadId, null)
146 })
147
148 it('Should not accept more chunks than expected', async function () {
149 const size = 100
150 const uploadId = await prepareUpload(size)
151
152 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
153 await checkFileSize(uploadId, 0)
154 })
155
156 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
157 const uploadId = await prepareUpload(1500)
158
159 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
160 await checkFileSize(uploadId, 0)
161 })
162
163 it('Should not accept more chunks than expected with an invalid content length', async function () {
164 const uploadId = await prepareUpload(500)
165
166 const size = 1000
167
168 const contentRangeBuilder = start => `bytes ${start}-${start + size - 1}/${size}`
169 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentRangeBuilder, contentLength: size })
170 await checkFileSize(uploadId, 0)
171 })
172 })
173
174 after(async function () {
175 await cleanupTests([ server ])
176 })
177 })