Move to eslint
[oweals/peertube.git] / server / tests / cli / peertube.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import {
6   addVideoChannel,
7   buildAbsoluteFixturePath,
8   cleanupTests,
9   createUser,
10   doubleFollow,
11   execCLI,
12   flushAndRunServer,
13   getEnvCli,
14   getLocalIdByUUID,
15   getVideo,
16   getVideosList,
17   removeVideo,
18   ServerInfo,
19   setAccessTokensToServers,
20   uploadVideoAndGetId,
21   userLogin,
22   waitJobs
23 } from '../../../shared/extra-utils'
24 import { Video, VideoDetails } from '../../../shared'
25 import { getYoutubeVideoUrl } from '../../../shared/extra-utils/videos/video-imports'
26
27 describe('Test CLI wrapper', function () {
28   let server: ServerInfo
29   let userAccessToken: string
30
31   const cmd = 'node ./dist/server/tools/peertube.js'
32
33   before(async function () {
34     this.timeout(30000)
35
36     server = await flushAndRunServer(1)
37     await setAccessTokensToServers([ server ])
38
39     await createUser({ url: server.url, accessToken: server.accessToken, username: 'user_1', password: 'super_password' })
40
41     userAccessToken = await userLogin(server, { username: 'user_1', password: 'super_password' })
42
43     {
44       const args = { name: 'user_channel', displayName: 'User channel', support: 'super support text' }
45       await addVideoChannel(server.url, userAccessToken, args)
46     }
47   })
48
49   describe('Authentication and instance selection', function () {
50
51     it('Should display no selected instance', async function () {
52       this.timeout(60000)
53
54       const env = getEnvCli(server)
55       const stdout = await execCLI(`${env} ${cmd} --help`)
56
57       expect(stdout).to.contain('no instance selected')
58     })
59
60     it('Should add a user', async function () {
61       this.timeout(60000)
62
63       const env = getEnvCli(server)
64       await execCLI(`${env} ${cmd} auth add -u ${server.url} -U user_1 -p super_password`)
65     })
66
67     it('Should default to this user', async function () {
68       this.timeout(60000)
69
70       const env = getEnvCli(server)
71       const stdout = await execCLI(`${env} ${cmd} --help`)
72
73       expect(stdout).to.contain(`instance ${server.url} selected`)
74     })
75
76     it('Should remember the user', async function () {
77       this.timeout(60000)
78
79       const env = getEnvCli(server)
80       const stdout = await execCLI(`${env} ${cmd} auth list`)
81
82       expect(stdout).to.contain(server.url)
83     })
84   })
85
86   describe('Video upload/import', function () {
87
88     it('Should upload a video', async function () {
89       this.timeout(60000)
90
91       const env = getEnvCli(server)
92
93       const fixture = buildAbsoluteFixturePath('60fps_720p_small.mp4')
94
95       const params = `-f ${fixture} --video-name 'test upload' --channel-name user_channel --support 'support_text'`
96
97       await execCLI(`${env} ${cmd} upload ${params}`)
98     })
99
100     it('Should have the video uploaded', async function () {
101       const res = await getVideosList(server.url)
102
103       expect(res.body.total).to.equal(1)
104
105       const videos: Video[] = res.body.data
106
107       const video: VideoDetails = (await getVideo(server.url, videos[0].uuid)).body
108
109       expect(video.name).to.equal('test upload')
110       expect(video.support).to.equal('support_text')
111       expect(video.channel.name).to.equal('user_channel')
112     })
113
114     it('Should import a video', async function () {
115       this.timeout(60000)
116
117       const env = getEnvCli(server)
118
119       const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel`
120
121       await execCLI(`${env} ${cmd} import ${params}`)
122     })
123
124     it('Should have imported the video', async function () {
125       this.timeout(60000)
126
127       await waitJobs([ server ])
128
129       const res = await getVideosList(server.url)
130
131       expect(res.body.total).to.equal(2)
132
133       const videos: Video[] = res.body.data
134       const video = videos.find(v => v.name === 'small video - youtube')
135       expect(video).to.not.be.undefined
136
137       const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body
138       expect(videoDetails.channel.name).to.equal('user_channel')
139       expect(videoDetails.support).to.equal('super support text')
140       expect(videoDetails.nsfw).to.be.false
141
142       // So we can reimport it
143       await removeVideo(server.url, userAccessToken, video.id)
144     })
145
146     it('Should import and override some imported attributes', async function () {
147       this.timeout(60000)
148
149       const env = getEnvCli(server)
150
151       const params = `--target-url ${getYoutubeVideoUrl()} --channel-name user_channel --video-name toto --nsfw --support support`
152
153       await execCLI(`${env} ${cmd} import ${params}`)
154
155       await waitJobs([ server ])
156
157       {
158         const res = await getVideosList(server.url)
159         expect(res.body.total).to.equal(2)
160
161         const videos: Video[] = res.body.data
162         const video = videos.find(v => v.name === 'toto')
163         expect(video).to.not.be.undefined
164
165         const videoDetails: VideoDetails = (await getVideo(server.url, video.id)).body
166         expect(videoDetails.channel.name).to.equal('user_channel')
167         expect(videoDetails.support).to.equal('support')
168         expect(videoDetails.nsfw).to.be.true
169         expect(videoDetails.commentsEnabled).to.be.true
170       }
171     })
172   })
173
174   describe('Admin auth', function () {
175
176     it('Should remove the auth user', async function () {
177       const env = getEnvCli(server)
178
179       await execCLI(`${env} ${cmd} auth del ${server.url}`)
180
181       const stdout = await execCLI(`${env} ${cmd} --help`)
182
183       expect(stdout).to.contain('no instance selected')
184     })
185
186     it('Should add the admin user', async function () {
187       const env = getEnvCli(server)
188       await execCLI(`${env} ${cmd} auth add -u ${server.url} -U root -p test${server.internalServerNumber}`)
189     })
190   })
191
192   describe('Manage plugins', function () {
193
194     it('Should install a plugin', async function () {
195       this.timeout(60000)
196
197       const env = getEnvCli(server)
198       await execCLI(`${env} ${cmd} plugins install --npm-name peertube-plugin-hello-world`)
199     })
200
201     it('Should list installed plugins', async function () {
202       const env = getEnvCli(server)
203       const res = await execCLI(`${env} ${cmd} plugins list`)
204
205       expect(res).to.contain('peertube-plugin-hello-world')
206     })
207
208     it('Should uninstall the plugin', async function () {
209       const env = getEnvCli(server)
210       const res = await execCLI(`${env} ${cmd} plugins uninstall --npm-name peertube-plugin-hello-world`)
211
212       expect(res).to.not.contain('peertube-plugin-hello-world')
213     })
214   })
215
216   describe('Manage video redundancies', function () {
217     let anotherServer: ServerInfo
218     let video1Server2: number
219     let servers: ServerInfo[]
220
221     before(async function () {
222       this.timeout(120000)
223
224       anotherServer = await flushAndRunServer(2)
225       await setAccessTokensToServers([ anotherServer ])
226
227       await doubleFollow(server, anotherServer)
228
229       servers = [ server, anotherServer ]
230       await waitJobs(servers)
231
232       const uuid = (await uploadVideoAndGetId({ server: anotherServer, videoName: 'super video' })).uuid
233       await waitJobs(servers)
234
235       video1Server2 = await getLocalIdByUUID(server.url, uuid)
236     })
237
238     it('Should add a redundancy', async function () {
239       this.timeout(60000)
240
241       const env = getEnvCli(server)
242
243       const params = `add --video ${video1Server2}`
244
245       await execCLI(`${env} ${cmd} redundancy ${params}`)
246
247       await waitJobs(servers)
248     })
249
250     it('Should list redundancies', async function () {
251       this.timeout(60000)
252
253       {
254         const env = getEnvCli(server)
255
256         const params = 'list-my-redundancies'
257         const stdout = await execCLI(`${env} ${cmd} redundancy ${params}`)
258
259         expect(stdout).to.contain('super video')
260         expect(stdout).to.contain(`localhost:${server.port}`)
261       }
262     })
263
264     it('Should remove a redundancy', async function () {
265       this.timeout(60000)
266
267       const env = getEnvCli(server)
268
269       const params = `remove --video ${video1Server2}`
270
271       await execCLI(`${env} ${cmd} redundancy ${params}`)
272
273       await waitJobs(servers)
274
275       {
276         const env = getEnvCli(server)
277         const params = 'list-my-redundancies'
278         const stdout = await execCLI(`${env} ${cmd} redundancy ${params}`)
279
280         expect(stdout).to.not.contain('super video')
281       }
282     })
283
284     after(async function () {
285       this.timeout(10000)
286
287       await cleanupTests([ anotherServer ])
288     })
289   })
290
291   after(async function () {
292     this.timeout(10000)
293
294     await cleanupTests([ server ])
295   })
296 })