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