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