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