]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-imports.ts
Introduce user command
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-imports.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
590fb506 2
590fb506 3import 'mocha'
b488ba1e 4import * as chai from 'chai'
590fb506 5import {
6910f20f 6 areHttpImportTestsDisabled,
7c3b7976 7 cleanupTests,
590fb506
C
8 doubleFollow,
9 flushAndRunMultipleServers,
590fb506
C
10 getMyVideos,
11 getVideo,
12 getVideosList,
6910f20f 13 ImportsCommand,
590fb506 14 ServerInfo,
b488ba1e 15 setAccessTokensToServers,
6910f20f
C
16 testCaptionFile,
17 testImage,
18 waitJobs
19} from '@shared/extra-utils'
20import { VideoDetails, VideoPrivacy, VideoResolution } from '@shared/models'
590fb506
C
21
22const expect = chai.expect
23
24describe('Test video imports', function () {
25 let servers: ServerInfo[] = []
26 let channelIdServer1: number
27 let channelIdServer2: number
28
b488ba1e
C
29 if (areHttpImportTestsDisabled()) return
30
a2470c9f
C
31 async function checkVideosServer1 (server: ServerInfo, idHttp: string, idMagnet: string, idTorrent: string) {
32 const resHttp = await getVideo(server.url, idHttp)
3e17515e
C
33 const videoHttp: VideoDetails = resHttp.body
34
35 expect(videoHttp.name).to.equal('small video - youtube')
f9b6d51f
C
36 // FIXME: youtube-dl seems broken
37 // expect(videoHttp.category.label).to.equal('News & Politics')
1dee8d68 38 // expect(videoHttp.licence.label).to.equal('Attribution')
3e17515e
C
39 expect(videoHttp.language.label).to.equal('Unknown')
40 expect(videoHttp.nsfw).to.be.false
41 expect(videoHttp.description).to.equal('this is a super description')
42 expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
43 expect(videoHttp.files).to.have.lengthOf(1)
17036be5
C
44
45 const originallyPublishedAt = new Date(videoHttp.originallyPublishedAt)
46 expect(originallyPublishedAt.getDate()).to.equal(14)
47 expect(originallyPublishedAt.getMonth()).to.equal(0)
48 expect(originallyPublishedAt.getFullYear()).to.equal(2019)
3e17515e 49
a2470c9f 50 const resMagnet = await getVideo(server.url, idMagnet)
3e17515e 51 const videoMagnet: VideoDetails = resMagnet.body
a2470c9f 52 const resTorrent = await getVideo(server.url, idTorrent)
3e17515e
C
53 const videoTorrent: VideoDetails = resTorrent.body
54
55 for (const video of [ videoMagnet, videoTorrent ]) {
56 expect(video.category.label).to.equal('Misc')
57 expect(video.licence.label).to.equal('Unknown')
58 expect(video.language.label).to.equal('Unknown')
59 expect(video.nsfw).to.be.false
60 expect(video.description).to.equal('this is a super torrent description')
61 expect(video.tags).to.deep.equal([ 'tag_torrent1', 'tag_torrent2' ])
62 expect(video.files).to.have.lengthOf(1)
63 }
590fb506 64
3e17515e
C
65 expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
66 expect(videoMagnet.name).to.contain('super peertube2 video')
652c6416 67
a2470c9f
C
68 const bodyCaptions = await server.captionsCommand.listVideoCaptions({ videoId: idHttp })
69 expect(bodyCaptions.total).to.equal(2)
590fb506
C
70 }
71
a2470c9f
C
72 async function checkVideoServer2 (server: ServerInfo, id: number | string) {
73 const res = await getVideo(server.url, id)
652c6416 74 const video: VideoDetails = res.body
590fb506
C
75
76 expect(video.name).to.equal('my super name')
77 expect(video.category.label).to.equal('Entertainment')
78 expect(video.licence.label).to.equal('Public Domain Dedication')
79 expect(video.language.label).to.equal('English')
80 expect(video.nsfw).to.be.false
81 expect(video.description).to.equal('my super description')
82 expect(video.tags).to.deep.equal([ 'supertag1', 'supertag2' ])
83
84 expect(video.files).to.have.lengthOf(1)
652c6416 85
a2470c9f
C
86 const bodyCaptions = await server.captionsCommand.listVideoCaptions({ videoId: id })
87 expect(bodyCaptions.total).to.equal(2)
590fb506
C
88 }
89
90 before(async function () {
454c20fa 91 this.timeout(30_000)
590fb506
C
92
93 // Run servers
94 servers = await flushAndRunMultipleServers(2)
95
96 await setAccessTokensToServers(servers)
97
98 {
7926c5f9
C
99 const { videoChannels } = await servers[0].usersCommand.getMyInfo()
100 channelIdServer1 = videoChannels[0].id
590fb506
C
101 }
102
103 {
7926c5f9
C
104 const { videoChannels } = await servers[1].usersCommand.getMyInfo()
105 channelIdServer2 = videoChannels[0].id
590fb506
C
106 }
107
108 await doubleFollow(servers[0], servers[1])
109 })
110
3e17515e 111 it('Should import videos on server 1', async function () {
454c20fa 112 this.timeout(60_000)
590fb506 113
3e17515e 114 const baseAttributes = {
590fb506
C
115 channelId: channelIdServer1,
116 privacy: VideoPrivacy.PUBLIC
117 }
3e17515e
C
118
119 {
6c5065a0 120 const attributes = { ...baseAttributes, targetUrl: ImportsCommand.getYoutubeVideoUrl() }
6910f20f
C
121 const { video } = await servers[0].importsCommand.importVideo({ attributes })
122 expect(video.name).to.equal('small video - youtube')
53c06121 123
6910f20f
C
124 expect(video.thumbnailPath).to.match(new RegExp(`^/static/thumbnails/.+.jpg$`))
125 expect(video.previewPath).to.match(new RegExp(`^/lazy-static/previews/.+.jpg$`))
53c06121 126
6910f20f
C
127 await testImage(servers[0].url, 'video_import_thumbnail', video.thumbnailPath)
128 await testImage(servers[0].url, 'video_import_preview', video.previewPath)
ba6e9e8f 129
6910f20f 130 const bodyCaptions = await servers[0].captionsCommand.listVideoCaptions({ videoId: video.id })
a2470c9f 131 const videoCaptions = bodyCaptions.data
ba6e9e8f 132 expect(videoCaptions).to.have.lengthOf(2)
133
652c6416
C
134 const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
135 expect(enCaption).to.exist
136 expect(enCaption.language.label).to.equal('English')
53c06121 137 expect(enCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-en.vtt$`))
ba6e9e8f 138 await testCaptionFile(servers[0].url, enCaption.captionPath, `WEBVTT
652c6416
C
139Kind: captions
140Language: en
ba6e9e8f 141
652c6416
C
14200:00:01.600 --> 00:00:04.200
143English (US)
ba6e9e8f 144
652c6416
C
14500:00:05.900 --> 00:00:07.999
146This is a subtitle in American English
ba6e9e8f 147
652c6416
C
14800:00:10.000 --> 00:00:14.000
149Adding subtitles is very easy to do`)
ba6e9e8f 150
652c6416
C
151 const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
152 expect(frCaption).to.exist
153 expect(frCaption.language.label).to.equal('French')
53c06121 154 expect(frCaption.captionPath).to.match(new RegExp(`^/lazy-static/video-captions/.+-fr.vtt`))
ba6e9e8f 155 await testCaptionFile(servers[0].url, frCaption.captionPath, `WEBVTT
652c6416
C
156Kind: captions
157Language: fr
ba6e9e8f 158
652c6416
C
15900:00:01.600 --> 00:00:04.200
160Français (FR)
ba6e9e8f 161
652c6416
C
16200:00:05.900 --> 00:00:07.999
163C'est un sous-titre français
ba6e9e8f 164
652c6416
C
16500:00:10.000 --> 00:00:14.000
166Ajouter un sous-titre est vraiment facile`)
3e17515e
C
167 }
168
169 {
6c5065a0
C
170 const attributes = {
171 ...baseAttributes,
6910f20f 172 magnetUri: ImportsCommand.getMagnetURI(),
3e17515e
C
173 description: 'this is a super torrent description',
174 tags: [ 'tag_torrent1', 'tag_torrent2' ]
6c5065a0 175 }
6910f20f
C
176 const { video } = await servers[0].importsCommand.importVideo({ attributes })
177 expect(video.name).to.equal('super peertube2 video')
3e17515e
C
178 }
179
180 {
6c5065a0
C
181 const attributes = {
182 ...baseAttributes,
9a7fd960 183 torrentfile: 'video-720p.torrent' as any,
3e17515e
C
184 description: 'this is a super torrent description',
185 tags: [ 'tag_torrent1', 'tag_torrent2' ]
6c5065a0 186 }
6910f20f
C
187 const { video } = await servers[0].importsCommand.importVideo({ attributes })
188 expect(video.name).to.equal('你好 世界 720p.mp4')
3e17515e 189 }
590fb506
C
190 })
191
3e17515e
C
192 it('Should list the videos to import in my videos on server 1', async function () {
193 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5, 'createdAt')
590fb506 194
3e17515e 195 expect(res.body.total).to.equal(3)
590fb506
C
196
197 const videos = res.body.data
3e17515e 198 expect(videos).to.have.lengthOf(3)
590fb506 199 expect(videos[0].name).to.equal('small video - youtube')
3e17515e
C
200 expect(videos[1].name).to.equal('super peertube2 video')
201 expect(videos[2].name).to.equal('你好 世界 720p.mp4')
590fb506
C
202 })
203
3e17515e 204 it('Should list the videos to import in my imports on server 1', async function () {
6910f20f
C
205 const { total, data: videoImports } = await servers[0].importsCommand.getMyVideoImports({ sort: '-createdAt' })
206 expect(total).to.equal(3)
3e17515e 207
3e17515e
C
208 expect(videoImports).to.have.lengthOf(3)
209
6910f20f 210 expect(videoImports[2].targetUrl).to.equal(ImportsCommand.getYoutubeVideoUrl())
3e17515e
C
211 expect(videoImports[2].magnetUri).to.be.null
212 expect(videoImports[2].torrentName).to.be.null
213 expect(videoImports[2].video.name).to.equal('small video - youtube')
590fb506 214
3e17515e 215 expect(videoImports[1].targetUrl).to.be.null
6910f20f 216 expect(videoImports[1].magnetUri).to.equal(ImportsCommand.getMagnetURI())
3e17515e
C
217 expect(videoImports[1].torrentName).to.be.null
218 expect(videoImports[1].video.name).to.equal('super peertube2 video')
590fb506 219
3e17515e
C
220 expect(videoImports[0].targetUrl).to.be.null
221 expect(videoImports[0].magnetUri).to.be.null
222 expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
223 expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
590fb506
C
224 })
225
3e17515e 226 it('Should have the video listed on the two instances', async function () {
454c20fa 227 this.timeout(120_000)
590fb506
C
228
229 await waitJobs(servers)
230
231 for (const server of servers) {
232 const res = await getVideosList(server.url)
3e17515e
C
233 expect(res.body.total).to.equal(3)
234 expect(res.body.data).to.have.lengthOf(3)
590fb506 235
3e17515e 236 const [ videoHttp, videoMagnet, videoTorrent ] = res.body.data
a2470c9f 237 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
590fb506
C
238 }
239 })
240
241 it('Should import a video on server 2 with some fields', async function () {
454c20fa 242 this.timeout(60_000)
590fb506
C
243
244 const attributes = {
6910f20f 245 targetUrl: ImportsCommand.getYoutubeVideoUrl(),
3e17515e 246 channelId: channelIdServer2,
590fb506
C
247 privacy: VideoPrivacy.PUBLIC,
248 category: 10,
249 licence: 7,
250 language: 'en',
251 name: 'my super name',
252 description: 'my super description',
253 tags: [ 'supertag1', 'supertag2' ]
254 }
6910f20f
C
255 const { video } = await servers[1].importsCommand.importVideo({ attributes })
256 expect(video.name).to.equal('my super name')
590fb506
C
257 })
258
3e17515e 259 it('Should have the videos listed on the two instances', async function () {
454c20fa 260 this.timeout(120_000)
590fb506
C
261
262 await waitJobs(servers)
263
264 for (const server of servers) {
265 const res = await getVideosList(server.url)
3e17515e
C
266 expect(res.body.total).to.equal(4)
267 expect(res.body.data).to.have.lengthOf(4)
590fb506 268
a2470c9f 269 await checkVideoServer2(server, res.body.data[0].uuid)
3e17515e 270
a1587156 271 const [ , videoHttp, videoMagnet, videoTorrent ] = res.body.data
a2470c9f 272 await checkVideosServer1(server, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
3e17515e
C
273 }
274 })
275
276 it('Should import a video that will be transcoded', async function () {
454c20fa 277 this.timeout(120_000)
3e17515e
C
278
279 const attributes = {
280 name: 'transcoded video',
6910f20f 281 magnetUri: ImportsCommand.getMagnetURI(),
3e17515e
C
282 channelId: channelIdServer2,
283 privacy: VideoPrivacy.PUBLIC
284 }
6910f20f
C
285 const { video } = await servers[1].importsCommand.importVideo({ attributes })
286 const videoUUID = video.uuid
3e17515e
C
287
288 await waitJobs(servers)
289
290 for (const server of servers) {
291 const res = await getVideo(server.url, videoUUID)
292 const video: VideoDetails = res.body
293
294 expect(video.name).to.equal('transcoded video')
295 expect(video.files).to.have.lengthOf(4)
590fb506
C
296 }
297 })
298
454c20fa
RK
299 it('Should import no HDR version on a HDR video', async function () {
300 this.timeout(120_000)
301
302 const config = {
303 transcoding: {
304 enabled: true,
305 resolutions: {
306 '240p': false,
307 '360p': false,
308 '480p': false,
309 '720p': false,
310 '1080p': true, // the resulting resolution shouldn't be higher than this, and not vp9.2/av01
311 '1440p': false,
312 '2160p': false
313 },
314 webtorrent: { enabled: true },
315 hls: { enabled: false }
316 },
317 import: {
318 videos: {
319 http: {
320 enabled: true
321 },
322 torrent: {
323 enabled: true
324 }
325 }
326 }
327 }
65e6e260 328 await servers[0].configCommand.updateCustomSubConfig({ newConfig: config })
454c20fa
RK
329
330 const attributes = {
331 name: 'hdr video',
6910f20f 332 targetUrl: ImportsCommand.getYoutubeHDRVideoUrl(),
454c20fa
RK
333 channelId: channelIdServer1,
334 privacy: VideoPrivacy.PUBLIC
335 }
6910f20f
C
336 const { video: videoImported } = await servers[0].importsCommand.importVideo({ attributes })
337 const videoUUID = videoImported.uuid
454c20fa
RK
338
339 await waitJobs(servers)
340
341 // test resolution
342 const res2 = await getVideo(servers[0].url, videoUUID)
343 const video: VideoDetails = res2.body
344 expect(video.name).to.equal('hdr video')
345 const maxResolution = Math.max.apply(Math, video.files.map(function (o) { return o.resolution.id }))
346 expect(maxResolution, 'expected max resolution not met').to.equals(VideoResolution.H_1080P)
347 })
348
7c3b7976
C
349 after(async function () {
350 await cleanupTests(servers)
590fb506
C
351 })
352})