allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / tests / api / videos / video-imports.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoDetails, VideoImport, VideoPrivacy, VideoCaption } from '../../../../shared/models/videos'
6 import {
7   cleanupTests,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   getMyUserInformation,
11   getMyVideos,
12   getVideo,
13   getVideosList,
14   listVideoCaptions,
15   testCaptionFile,
16   immutableAssign,
17   ServerInfo,
18   setAccessTokensToServers
19 } from '../../../../shared/extra-utils'
20 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21 import { getMagnetURI, getMyVideoImports, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports'
22 import { testImage } from '../../../../shared/extra-utils/miscs/miscs'
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   async function checkVideosServer1 (url: string, idHttp: string, idMagnet: string, idTorrent: string) {
32     const resHttp = await getVideo(url, idHttp)
33     const videoHttp: VideoDetails = resHttp.body
34
35     expect(videoHttp.name).to.equal('small video - youtube')
36     // FIXME: youtube-dl seems broken
37     // expect(videoHttp.category.label).to.equal('News & Politics')
38     expect(videoHttp.licence.label).to.equal('Attribution')
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)
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)
49
50     const resMagnet = await getVideo(url, idMagnet)
51     const videoMagnet: VideoDetails = resMagnet.body
52     const resTorrent = await getVideo(url, idTorrent)
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     }
64
65     expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
66     expect(videoMagnet.name).to.contain('super peertube2 video')
67
68     const resCaptions = await listVideoCaptions(url, idHttp)
69     expect(resCaptions.body.total).to.equal(2)
70   }
71
72   async function checkVideoServer2 (url: string, id: number | string) {
73     const res = await getVideo(url, id)
74     const video: VideoDetails = res.body
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)
85
86     const resCaptions = await listVideoCaptions(url, id)
87     expect(resCaptions.body.total).to.equal(2)
88   }
89
90   before(async function () {
91     this.timeout(30000)
92
93     // Run servers
94     servers = await flushAndRunMultipleServers(2)
95
96     await setAccessTokensToServers(servers)
97
98     {
99       const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
100       channelIdServer1 = res.body.videoChannels[0].id
101     }
102
103     {
104       const res = await getMyUserInformation(servers[1].url, servers[1].accessToken)
105       channelIdServer2 = res.body.videoChannels[0].id
106     }
107
108     await doubleFollow(servers[0], servers[1])
109   })
110
111   it('Should import videos on server 1', async function () {
112     this.timeout(60000)
113
114     const baseAttributes = {
115       channelId: channelIdServer1,
116       privacy: VideoPrivacy.PUBLIC
117     }
118
119     {
120       const attributes = immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() })
121       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
122       expect(res.body.video.name).to.equal('small video - youtube')
123       expect(res.body.video.thumbnailPath).to.equal(`/static/thumbnails/${res.body.video.uuid}.jpg`)
124       expect(res.body.video.previewPath).to.equal(`/static/previews/${res.body.video.uuid}.jpg`)
125       await testImage(servers[0].url, 'video_import_thumbnail', res.body.video.thumbnailPath)
126       await testImage(servers[0].url, 'video_import_preview', res.body.video.previewPath)
127
128       const resCaptions = await listVideoCaptions(servers[0].url, res.body.video.id)
129       const videoCaptions: VideoCaption[] = resCaptions.body.data
130       expect(videoCaptions).to.have.lengthOf(2)
131
132       const enCaption = videoCaptions.find(caption => caption.language.id === 'en')
133       expect(enCaption).to.exist
134       expect(enCaption.language.label).to.equal('English')
135       expect(enCaption.captionPath).to.equal(`/static/video-captions/${res.body.video.uuid}-en.vtt`)
136       await testCaptionFile(servers[0].url, enCaption.captionPath, `WEBVTT
137 Kind: captions
138 Language: en
139
140 00:00:01.600 --> 00:00:04.200
141 English (US)
142
143 00:00:05.900 --> 00:00:07.999
144 This is a subtitle in American English
145
146 00:00:10.000 --> 00:00:14.000
147 Adding subtitles is very easy to do`)
148
149       const frCaption = videoCaptions.find(caption => caption.language.id === 'fr')
150       expect(frCaption).to.exist
151       expect(frCaption.language.label).to.equal('French')
152       expect(frCaption.captionPath).to.equal(`/static/video-captions/${res.body.video.uuid}-fr.vtt`)
153       await testCaptionFile(servers[0].url, frCaption.captionPath, `WEBVTT
154 Kind: captions
155 Language: fr
156
157 00:00:01.600 --> 00:00:04.200
158 Français (FR)
159
160 00:00:05.900 --> 00:00:07.999
161 C'est un sous-titre français
162
163 00:00:10.000 --> 00:00:14.000
164 Ajouter un sous-titre est vraiment facile`)
165     }
166
167     {
168       const attributes = immutableAssign(baseAttributes, {
169         magnetUri: getMagnetURI(),
170         description: 'this is a super torrent description',
171         tags: [ 'tag_torrent1', 'tag_torrent2' ]
172       })
173       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
174       expect(res.body.video.name).to.equal('super peertube2 video')
175     }
176
177     {
178       const attributes = immutableAssign(baseAttributes, {
179         torrentfile: 'video-720p.torrent' as any,
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('你好 世界 720p.mp4')
185     }
186   })
187
188   it('Should list the videos to import in my videos on server 1', async function () {
189     const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5, 'createdAt')
190
191     expect(res.body.total).to.equal(3)
192
193     const videos = res.body.data
194     expect(videos).to.have.lengthOf(3)
195     expect(videos[0].name).to.equal('small video - youtube')
196     expect(videos[1].name).to.equal('super peertube2 video')
197     expect(videos[2].name).to.equal('你好 世界 720p.mp4')
198   })
199
200   it('Should list the videos to import in my imports on server 1', async function () {
201     const res = await getMyVideoImports(servers[0].url, servers[0].accessToken, '-createdAt')
202
203     expect(res.body.total).to.equal(3)
204     const videoImports: VideoImport[] = res.body.data
205     expect(videoImports).to.have.lengthOf(3)
206
207     expect(videoImports[2].targetUrl).to.equal(getYoutubeVideoUrl())
208     expect(videoImports[2].magnetUri).to.be.null
209     expect(videoImports[2].torrentName).to.be.null
210     expect(videoImports[2].video.name).to.equal('small video - youtube')
211
212     expect(videoImports[1].targetUrl).to.be.null
213     expect(videoImports[1].magnetUri).to.equal(getMagnetURI())
214     expect(videoImports[1].torrentName).to.be.null
215     expect(videoImports[1].video.name).to.equal('super peertube2 video')
216
217     expect(videoImports[0].targetUrl).to.be.null
218     expect(videoImports[0].magnetUri).to.be.null
219     expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
220     expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
221   })
222
223   it('Should have the video listed on the two instances', async function () {
224     this.timeout(120000)
225
226     await waitJobs(servers)
227
228     for (const server of servers) {
229       const res = await getVideosList(server.url)
230       expect(res.body.total).to.equal(3)
231       expect(res.body.data).to.have.lengthOf(3)
232
233       const [ videoHttp, videoMagnet, videoTorrent ] = res.body.data
234       await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
235     }
236   })
237
238   it('Should import a video on server 2 with some fields', async function () {
239     this.timeout(60000)
240
241     const attributes = {
242       targetUrl: getYoutubeVideoUrl(),
243       channelId: channelIdServer2,
244       privacy: VideoPrivacy.PUBLIC,
245       category: 10,
246       licence: 7,
247       language: 'en',
248       name: 'my super name',
249       description: 'my super description',
250       tags: [ 'supertag1', 'supertag2' ]
251     }
252     const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
253     expect(res.body.video.name).to.equal('my super name')
254   })
255
256   it('Should have the videos listed on the two instances', async function () {
257     this.timeout(120000)
258
259     await waitJobs(servers)
260
261     for (const server of servers) {
262       const res = await getVideosList(server.url)
263       expect(res.body.total).to.equal(4)
264       expect(res.body.data).to.have.lengthOf(4)
265
266       await checkVideoServer2(server.url, res.body.data[0].uuid)
267
268       const [ , videoHttp, videoMagnet, videoTorrent ] = res.body.data
269       await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
270     }
271   })
272
273   it('Should import a video that will be transcoded', async function () {
274     this.timeout(120000)
275
276     const attributes = {
277       name: 'transcoded video',
278       magnetUri: getMagnetURI(),
279       channelId: channelIdServer2,
280       privacy: VideoPrivacy.PUBLIC
281     }
282     const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
283     const videoUUID = res.body.video.uuid
284
285     await waitJobs(servers)
286
287     for (const server of servers) {
288       const res = await getVideo(server.url, videoUUID)
289       const video: VideoDetails = res.body
290
291       expect(video.name).to.equal('transcoded video')
292       expect(video.files).to.have.lengthOf(4)
293     }
294   })
295
296   after(async function () {
297     await cleanupTests(servers)
298   })
299 })