Merge branch 'release/v1.2.0'
[oweals/peertube.git] / server / tests / api / videos / video-captions.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   checkVideoFilesWereRemoved,
7   doubleFollow,
8   flushAndRunMultipleServers,
9   removeVideo,
10   uploadVideo,
11   wait
12 } from '../../../../shared/utils'
13 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/utils/index'
14 import { waitJobs } from '../../../../shared/utils/server/jobs'
15 import { createVideoCaption, deleteVideoCaption, listVideoCaptions, testCaptionFile } from '../../../../shared/utils/videos/video-captions'
16 import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
17
18 const expect = chai.expect
19
20 describe('Test video captions', function () {
21   let servers: ServerInfo[]
22   let videoUUID: string
23
24   before(async function () {
25     this.timeout(30000)
26
27     await flushTests()
28
29     servers = await flushAndRunMultipleServers(2)
30
31     await setAccessTokensToServers(servers)
32     await doubleFollow(servers[0], servers[1])
33
34     await waitJobs(servers)
35
36     const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'my video name' })
37     videoUUID = res.body.video.uuid
38
39     await waitJobs(servers)
40   })
41
42   it('Should list the captions and return an empty list', async function () {
43     for (const server of servers) {
44       const res = await listVideoCaptions(server.url, videoUUID)
45       expect(res.body.total).to.equal(0)
46       expect(res.body.data).to.have.lengthOf(0)
47     }
48   })
49
50   it('Should create two new captions', async function () {
51     this.timeout(30000)
52
53     await createVideoCaption({
54       url: servers[0].url,
55       accessToken: servers[0].accessToken,
56       language: 'ar',
57       videoId: videoUUID,
58       fixture: 'subtitle-good1.vtt'
59     })
60
61     await createVideoCaption({
62       url: servers[0].url,
63       accessToken: servers[0].accessToken,
64       language: 'zh',
65       videoId: videoUUID,
66       fixture: 'subtitle-good2.vtt',
67       mimeType: 'application/octet-stream'
68     })
69
70     await waitJobs(servers)
71   })
72
73   it('Should list these uploaded captions', async function () {
74     for (const server of servers) {
75       const res = await listVideoCaptions(server.url, videoUUID)
76       expect(res.body.total).to.equal(2)
77       expect(res.body.data).to.have.lengthOf(2)
78
79       const caption1: VideoCaption = res.body.data[0]
80       expect(caption1.language.id).to.equal('ar')
81       expect(caption1.language.label).to.equal('Arabic')
82       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
83       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
84
85       const caption2: VideoCaption = res.body.data[1]
86       expect(caption2.language.id).to.equal('zh')
87       expect(caption2.language.label).to.equal('Chinese')
88       expect(caption2.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
89       await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
90     }
91   })
92
93   it('Should replace an existing caption', async function () {
94     this.timeout(30000)
95
96     await createVideoCaption({
97       url: servers[0].url,
98       accessToken: servers[0].accessToken,
99       language: 'ar',
100       videoId: videoUUID,
101       fixture: 'subtitle-good2.vtt'
102     })
103
104     await waitJobs(servers)
105   })
106
107   it('Should have this caption updated', async function () {
108     for (const server of servers) {
109       const res = await listVideoCaptions(server.url, videoUUID)
110       expect(res.body.total).to.equal(2)
111       expect(res.body.data).to.have.lengthOf(2)
112
113       const caption1: VideoCaption = res.body.data[0]
114       expect(caption1.language.id).to.equal('ar')
115       expect(caption1.language.label).to.equal('Arabic')
116       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
117       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
118     }
119   })
120
121   it('Should replace an existing caption with a srt file and convert it', async function () {
122     this.timeout(30000)
123
124     await createVideoCaption({
125       url: servers[0].url,
126       accessToken: servers[0].accessToken,
127       language: 'ar',
128       videoId: videoUUID,
129       fixture: 'subtitle-good.srt'
130     })
131
132     await waitJobs(servers)
133
134     // Cache invalidation
135     await wait(3000)
136   })
137
138   it('Should have this caption updated and converted', async function () {
139     for (const server of servers) {
140       const res = await listVideoCaptions(server.url, videoUUID)
141       expect(res.body.total).to.equal(2)
142       expect(res.body.data).to.have.lengthOf(2)
143
144       const caption1: VideoCaption = res.body.data[0]
145       expect(caption1.language.id).to.equal('ar')
146       expect(caption1.language.label).to.equal('Arabic')
147       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
148
149       const expected = 'WEBVTT FILE\r\n' +
150         '\r\n' +
151         '1\r\n' +
152         '00:00:01.600 --> 00:00:04.200\r\n' +
153         'English (US)\r\n' +
154         '\r\n' +
155         '2\r\n' +
156         '00:00:05.900 --> 00:00:07.999\r\n' +
157         'This is a subtitle in American English\r\n' +
158         '\r\n' +
159         '3\r\n' +
160         '00:00:10.000 --> 00:00:14.000\r\n' +
161         'Adding subtitles is very easy to do\r\n'
162       await testCaptionFile(server.url, caption1.captionPath, expected)
163     }
164   })
165
166   it('Should remove one caption', async function () {
167     this.timeout(30000)
168
169     await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
170
171     await waitJobs(servers)
172   })
173
174   it('Should only list the caption that was not deleted', async function () {
175     for (const server of servers) {
176       const res = await listVideoCaptions(server.url, videoUUID)
177       expect(res.body.total).to.equal(1)
178       expect(res.body.data).to.have.lengthOf(1)
179
180       const caption: VideoCaption = res.body.data[0]
181
182       expect(caption.language.id).to.equal('zh')
183       expect(caption.language.label).to.equal('Chinese')
184       expect(caption.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
185       await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
186     }
187   })
188
189   it('Should remove the video, and thus all video captions', async function () {
190     await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
191
192     await checkVideoFilesWereRemoved(videoUUID, 1)
193   })
194
195   after(async function () {
196     killallServers(servers)
197   })
198 })