aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/videos')
-rw-r--r--server/tests/api/videos/resumable-upload.ts79
1 files changed, 71 insertions, 8 deletions
diff --git a/server/tests/api/videos/resumable-upload.ts b/server/tests/api/videos/resumable-upload.ts
index 6b5e0c09d..1ba7cdbcc 100644
--- a/server/tests/api/videos/resumable-upload.ts
+++ b/server/tests/api/videos/resumable-upload.ts
@@ -22,6 +22,8 @@ describe('Test resumable upload', function () {
22 const defaultFixture = 'video_short.mp4' 22 const defaultFixture = 'video_short.mp4'
23 let server: PeerTubeServer 23 let server: PeerTubeServer
24 let rootId: number 24 let rootId: number
25 let userAccessToken: string
26 let userChannelId: number
25 27
26 async function buildSize (fixture: string, size?: number) { 28 async function buildSize (fixture: string, size?: number) {
27 if (size !== undefined) return size 29 if (size !== undefined) return size
@@ -30,24 +32,33 @@ describe('Test resumable upload', function () {
30 return (await stat(baseFixture)).size 32 return (await stat(baseFixture)).size
31 } 33 }
32 34
33 async function prepareUpload (sizeArg?: number) { 35 async function prepareUpload (options: {
34 const size = await buildSize(defaultFixture, sizeArg) 36 channelId?: number
37 token?: string
38 size?: number
39 originalName?: string
40 lastModified?: number
41 } = {}) {
42 const { token, originalName, lastModified } = options
43
44 const size = await buildSize(defaultFixture, options.size)
35 45
36 const attributes = { 46 const attributes = {
37 name: 'video', 47 name: 'video',
38 channelId: server.store.channel.id, 48 channelId: options.channelId ?? server.store.channel.id,
39 privacy: VideoPrivacy.PUBLIC, 49 privacy: VideoPrivacy.PUBLIC,
40 fixture: defaultFixture 50 fixture: defaultFixture
41 } 51 }
42 52
43 const mimetype = 'video/mp4' 53 const mimetype = 'video/mp4'
44 54
45 const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype }) 55 const res = await server.videos.prepareResumableUpload({ token, attributes, size, mimetype, originalName, lastModified })
46 56
47 return res.header['location'].split('?')[1] 57 return res.header['location'].split('?')[1]
48 } 58 }
49 59
50 async function sendChunks (options: { 60 async function sendChunks (options: {
61 token?: string
51 pathUploadId: string 62 pathUploadId: string
52 size?: number 63 size?: number
53 expectedStatus?: HttpStatusCode 64 expectedStatus?: HttpStatusCode
@@ -55,12 +66,13 @@ describe('Test resumable upload', function () {
55 contentRange?: string 66 contentRange?: string
56 contentRangeBuilder?: (start: number, chunk: any) => string 67 contentRangeBuilder?: (start: number, chunk: any) => string
57 }) { 68 }) {
58 const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options 69 const { token, pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options
59 70
60 const size = await buildSize(defaultFixture, options.size) 71 const size = await buildSize(defaultFixture, options.size)
61 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture) 72 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
62 73
63 return server.videos.sendResumableChunks({ 74 return server.videos.sendResumableChunks({
75 token,
64 pathUploadId, 76 pathUploadId,
65 videoFilePath: absoluteFilePath, 77 videoFilePath: absoluteFilePath,
66 size, 78 size,
@@ -105,6 +117,12 @@ describe('Test resumable upload', function () {
105 const body = await server.users.getMyInfo() 117 const body = await server.users.getMyInfo()
106 rootId = body.id 118 rootId = body.id
107 119
120 {
121 userAccessToken = await server.users.generateUserAndToken('user1')
122 const { videoChannels } = await server.users.getMyInfo({ token: userAccessToken })
123 userChannelId = videoChannels[0].id
124 }
125
108 await server.users.update({ userId: rootId, videoQuota: 10_000_000 }) 126 await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
109 }) 127 })
110 128
@@ -147,14 +165,14 @@ describe('Test resumable upload', function () {
147 }) 165 })
148 166
149 it('Should not accept more chunks than expected', async function () { 167 it('Should not accept more chunks than expected', async function () {
150 const uploadId = await prepareUpload(100) 168 const uploadId = await prepareUpload({ size: 100 })
151 169
152 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 }) 170 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
153 await checkFileSize(uploadId, 0) 171 await checkFileSize(uploadId, 0)
154 }) 172 })
155 173
156 it('Should not accept more chunks than expected with an invalid content length/content range', async function () { 174 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
157 const uploadId = await prepareUpload(1500) 175 const uploadId = await prepareUpload({ size: 1500 })
158 176
159 // Content length check seems to have changed in v16 177 // Content length check seems to have changed in v16
160 if (process.version.startsWith('v16')) { 178 if (process.version.startsWith('v16')) {
@@ -167,7 +185,7 @@ describe('Test resumable upload', function () {
167 }) 185 })
168 186
169 it('Should not accept more chunks than expected with an invalid content length', async function () { 187 it('Should not accept more chunks than expected with an invalid content length', async function () {
170 const uploadId = await prepareUpload(500) 188 const uploadId = await prepareUpload({ size: 500 })
171 189
172 const size = 1000 190 const size = 1000
173 191
@@ -195,6 +213,51 @@ describe('Test resumable upload', function () {
195 213
196 await checkFileSize(uploadId, null) 214 await checkFileSize(uploadId, null)
197 }) 215 })
216
217 it('Should not have the same upload id with 2 different users', async function () {
218 const originalName = 'toto.mp4'
219 const lastModified = new Date().getTime()
220
221 const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
222 const uploadId2 = await prepareUpload({ originalName, lastModified, channelId: userChannelId, token: userAccessToken })
223
224 expect(uploadId1).to.not.equal(uploadId2)
225 })
226
227 it('Should have the same upload id with the same user', async function () {
228 const originalName = 'toto.mp4'
229 const lastModified = new Date().getTime()
230
231 const uploadId1 = await prepareUpload({ originalName, lastModified })
232 const uploadId2 = await prepareUpload({ originalName, lastModified })
233
234 expect(uploadId1).to.equal(uploadId2)
235 })
236
237 it('Should not cache a request with 2 different users', async function () {
238 const originalName = 'toto.mp4'
239 const lastModified = new Date().getTime()
240
241 const uploadId = await prepareUpload({ originalName, lastModified, token: server.accessToken })
242
243 await sendChunks({ pathUploadId: uploadId, token: server.accessToken })
244 await sendChunks({ pathUploadId: uploadId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
245 })
246
247 it('Should not cache a request after a delete', async function () {
248 const originalName = 'toto.mp4'
249 const lastModified = new Date().getTime()
250 const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
251
252 await sendChunks({ pathUploadId: uploadId1 })
253 await server.videos.endResumableUpload({ pathUploadId: uploadId1 })
254
255 const uploadId2 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
256 expect(uploadId1).to.equal(uploadId2)
257
258 const result2 = await sendChunks({ pathUploadId: uploadId1 })
259 expect(result2.headers['x-resumable-upload-cached']).to.not.exist
260 })
198 }) 261 })
199 262
200 after(async function () { 263 after(async function () {