Cleanup tests
[oweals/peertube.git] / server / tests / api / videos / video-imports.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoDetails, VideoImport, VideoPrivacy } from '../../../../shared/models/videos'
6 import {
7   doubleFollow,
8   flushAndRunMultipleServers,
9   getMyUserInformation,
10   getMyVideos,
11   getVideo,
12   getVideosList,
13   immutableAssign,
14   killallServers,
15   ServerInfo,
16   setAccessTokensToServers
17 } from '../../../../shared/extra-utils'
18 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
19 import { getMagnetURI, getYoutubeVideoUrl, importVideo, getMyVideoImports } from '../../../../shared/extra-utils/videos/video-imports'
20
21 const expect = chai.expect
22
23 describe('Test video imports', function () {
24   let servers: ServerInfo[] = []
25   let channelIdServer1: number
26   let channelIdServer2: number
27
28   async function checkVideosServer1 (url: string, idHttp: string, idMagnet: string, idTorrent: string) {
29     const resHttp = await getVideo(url, idHttp)
30     const videoHttp: VideoDetails = resHttp.body
31
32     expect(videoHttp.name).to.equal('small video - youtube')
33     expect(videoHttp.category.label).to.equal('News & Politics')
34     expect(videoHttp.licence.label).to.equal('Attribution')
35     expect(videoHttp.language.label).to.equal('Unknown')
36     expect(videoHttp.nsfw).to.be.false
37     expect(videoHttp.description).to.equal('this is a super description')
38     expect(videoHttp.tags).to.deep.equal([ 'tag1', 'tag2' ])
39     expect(videoHttp.files).to.have.lengthOf(1)
40
41     const originallyPublishedAt = new Date(videoHttp.originallyPublishedAt)
42     expect(originallyPublishedAt.getDate()).to.equal(14)
43     expect(originallyPublishedAt.getMonth()).to.equal(0)
44     expect(originallyPublishedAt.getFullYear()).to.equal(2019)
45
46     const resMagnet = await getVideo(url, idMagnet)
47     const videoMagnet: VideoDetails = resMagnet.body
48     const resTorrent = await getVideo(url, idTorrent)
49     const videoTorrent: VideoDetails = resTorrent.body
50
51     for (const video of [ videoMagnet, videoTorrent ]) {
52       expect(video.category.label).to.equal('Misc')
53       expect(video.licence.label).to.equal('Unknown')
54       expect(video.language.label).to.equal('Unknown')
55       expect(video.nsfw).to.be.false
56       expect(video.description).to.equal('this is a super torrent description')
57       expect(video.tags).to.deep.equal([ 'tag_torrent1', 'tag_torrent2' ])
58       expect(video.files).to.have.lengthOf(1)
59     }
60
61     expect(videoTorrent.name).to.contain('你好 世界 720p.mp4')
62     expect(videoMagnet.name).to.contain('super peertube2 video')
63   }
64
65   async function checkVideoServer2 (url: string, id: number | string) {
66     const res = await getVideo(url, id)
67     const video = res.body
68
69     expect(video.name).to.equal('my super name')
70     expect(video.category.label).to.equal('Entertainment')
71     expect(video.licence.label).to.equal('Public Domain Dedication')
72     expect(video.language.label).to.equal('English')
73     expect(video.nsfw).to.be.false
74     expect(video.description).to.equal('my super description')
75     expect(video.tags).to.deep.equal([ 'supertag1', 'supertag2' ])
76
77     expect(video.files).to.have.lengthOf(1)
78   }
79
80   before(async function () {
81     this.timeout(30000)
82
83     // Run servers
84     servers = await flushAndRunMultipleServers(2)
85
86     await setAccessTokensToServers(servers)
87
88     {
89       const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
90       channelIdServer1 = res.body.videoChannels[ 0 ].id
91     }
92
93     {
94       const res = await getMyUserInformation(servers[1].url, servers[1].accessToken)
95       channelIdServer2 = res.body.videoChannels[ 0 ].id
96     }
97
98     await doubleFollow(servers[0], servers[1])
99   })
100
101   it('Should import videos on server 1', async function () {
102     this.timeout(60000)
103
104     const baseAttributes = {
105       channelId: channelIdServer1,
106       privacy: VideoPrivacy.PUBLIC
107     }
108
109     {
110       const attributes = immutableAssign(baseAttributes, { targetUrl: getYoutubeVideoUrl() })
111       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
112       expect(res.body.video.name).to.equal('small video - youtube')
113     }
114
115     {
116       const attributes = immutableAssign(baseAttributes, {
117         magnetUri: getMagnetURI(),
118         description: 'this is a super torrent description',
119         tags: [ 'tag_torrent1', 'tag_torrent2' ]
120       })
121       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
122       expect(res.body.video.name).to.equal('super peertube2 video')
123     }
124
125     {
126       const attributes = immutableAssign(baseAttributes, {
127         torrentfile: 'video-720p.torrent',
128         description: 'this is a super torrent description',
129         tags: [ 'tag_torrent1', 'tag_torrent2' ]
130       })
131       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
132       expect(res.body.video.name).to.equal('你好 世界 720p.mp4')
133     }
134   })
135
136   it('Should list the videos to import in my videos on server 1', async function () {
137     const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5, 'createdAt')
138
139     expect(res.body.total).to.equal(3)
140
141     const videos = res.body.data
142     expect(videos).to.have.lengthOf(3)
143     expect(videos[0].name).to.equal('small video - youtube')
144     expect(videos[1].name).to.equal('super peertube2 video')
145     expect(videos[2].name).to.equal('你好 世界 720p.mp4')
146   })
147
148   it('Should list the videos to import in my imports on server 1', async function () {
149     const res = await getMyVideoImports(servers[0].url, servers[0].accessToken, '-createdAt')
150
151     expect(res.body.total).to.equal(3)
152     const videoImports: VideoImport[] = res.body.data
153     expect(videoImports).to.have.lengthOf(3)
154
155     expect(videoImports[2].targetUrl).to.equal(getYoutubeVideoUrl())
156     expect(videoImports[2].magnetUri).to.be.null
157     expect(videoImports[2].torrentName).to.be.null
158     expect(videoImports[2].video.name).to.equal('small video - youtube')
159
160     expect(videoImports[1].targetUrl).to.be.null
161     expect(videoImports[1].magnetUri).to.equal(getMagnetURI())
162     expect(videoImports[1].torrentName).to.be.null
163     expect(videoImports[1].video.name).to.equal('super peertube2 video')
164
165     expect(videoImports[0].targetUrl).to.be.null
166     expect(videoImports[0].magnetUri).to.be.null
167     expect(videoImports[0].torrentName).to.equal('video-720p.torrent')
168     expect(videoImports[0].video.name).to.equal('你好 世界 720p.mp4')
169   })
170
171   it('Should have the video listed on the two instances', async function () {
172     this.timeout(120000)
173
174     await waitJobs(servers)
175
176     for (const server of servers) {
177       const res = await getVideosList(server.url)
178       expect(res.body.total).to.equal(3)
179       expect(res.body.data).to.have.lengthOf(3)
180
181       const [ videoHttp, videoMagnet, videoTorrent ] = res.body.data
182       await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
183     }
184   })
185
186   it('Should import a video on server 2 with some fields', async function () {
187     this.timeout(60000)
188
189     const attributes = {
190       targetUrl: getYoutubeVideoUrl(),
191       channelId: channelIdServer2,
192       privacy: VideoPrivacy.PUBLIC,
193       category: 10,
194       licence: 7,
195       language: 'en',
196       name: 'my super name',
197       description: 'my super description',
198       tags: [ 'supertag1', 'supertag2' ]
199     }
200     const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
201     expect(res.body.video.name).to.equal('my super name')
202   })
203
204   it('Should have the videos listed on the two instances', async function () {
205     this.timeout(120000)
206
207     await waitJobs(servers)
208
209     for (const server of servers) {
210       const res = await getVideosList(server.url)
211       expect(res.body.total).to.equal(4)
212       expect(res.body.data).to.have.lengthOf(4)
213
214       await checkVideoServer2(server.url, res.body.data[0].uuid)
215
216       const [ ,videoHttp, videoMagnet, videoTorrent ] = res.body.data
217       await checkVideosServer1(server.url, videoHttp.uuid, videoMagnet.uuid, videoTorrent.uuid)
218     }
219   })
220
221   it('Should import a video that will be transcoded', async function () {
222     this.timeout(120000)
223
224     const attributes = {
225       name: 'transcoded video',
226       magnetUri: getMagnetURI(),
227       channelId: channelIdServer2,
228       privacy: VideoPrivacy.PUBLIC
229     }
230     const res = await importVideo(servers[1].url, servers[1].accessToken, attributes)
231     const videoUUID = res.body.video.uuid
232
233     await waitJobs(servers)
234
235     for (const server of servers) {
236       const res = await getVideo(server.url, videoUUID)
237       const video: VideoDetails = res.body
238
239       expect(video.name).to.equal('transcoded video')
240       expect(video.files).to.have.lengthOf(4)
241     }
242   })
243
244   after(function () {
245     killallServers(servers)
246   })
247 })