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