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