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