Fix overview endpoint
[oweals/peertube.git] / server / tests / api / check-params / video-channels.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import { omit } from 'lodash'
5 import 'mocha'
6 import {
7   cleanupTests,
8   createUser,
9   deleteVideoChannel,
10   flushAndRunServer,
11   getAccountVideoChannelsList,
12   immutableAssign,
13   makeGetRequest,
14   makePostBodyRequest,
15   makePutBodyRequest,
16   makeUploadRequest,
17   ServerInfo,
18   setAccessTokensToServers,
19   userLogin
20 } from '../../../../shared/extra-utils'
21 import {
22   checkBadCountPagination,
23   checkBadSortPagination,
24   checkBadStartPagination
25 } from '../../../../shared/extra-utils/requests/check-api-params'
26 import { join } from 'path'
27
28 const expect = chai.expect
29
30 describe('Test video channels API validator', function () {
31   const videoChannelPath = '/api/v1/video-channels'
32   let server: ServerInfo
33   let accessTokenUser: string
34
35   // ---------------------------------------------------------------
36
37   before(async function () {
38     this.timeout(30000)
39
40     server = await flushAndRunServer(1)
41
42     await setAccessTokensToServers([ server ])
43
44     const user = {
45       username: 'fake',
46       password: 'fake_password'
47     }
48
49     {
50       await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
51       accessTokenUser = await userLogin(server, user)
52     }
53   })
54
55   describe('When listing a video channels', function () {
56     it('Should fail with a bad start pagination', async function () {
57       await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
58     })
59
60     it('Should fail with a bad count pagination', async function () {
61       await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
62     })
63
64     it('Should fail with an incorrect sort', async function () {
65       await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
66     })
67   })
68
69   describe('When listing account video channels', function () {
70     it('Should fail with a unknown account', async function () {
71       await getAccountVideoChannelsList(server.url, 'unknown', 404)
72     })
73   })
74
75   describe('When adding a video channel', function () {
76     const baseCorrectParams = {
77       name: 'super_channel',
78       displayName: 'hello',
79       description: 'super description',
80       support: 'super support text'
81     }
82
83     it('Should fail with a non authenticated user', async function () {
84       await makePostBodyRequest({
85         url: server.url,
86         path: videoChannelPath,
87         token: 'none',
88         fields: baseCorrectParams,
89         statusCodeExpected: 401
90       })
91     })
92
93     it('Should fail with nothing', async function () {
94       const fields = {}
95       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
96     })
97
98     it('Should fail without a name', async function () {
99       const fields = omit(baseCorrectParams, 'name')
100       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
101     })
102
103     it('Should fail with a bad name', async function () {
104       const fields = immutableAssign(baseCorrectParams, { name: 'super name' })
105       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
106     })
107
108     it('Should fail without a name', async function () {
109       const fields = omit(baseCorrectParams, 'displayName')
110       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
111     })
112
113     it('Should fail with a long name', async function () {
114       const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
115       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
116     })
117
118     it('Should fail with a long description', async function () {
119       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
120       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
121     })
122
123     it('Should fail with a long support text', async function () {
124       const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
125       await makePostBodyRequest({ url: server.url, path: videoChannelPath, token: server.accessToken, fields })
126     })
127
128     it('Should succeed with the correct parameters', async function () {
129       await makePostBodyRequest({
130         url: server.url,
131         path: videoChannelPath,
132         token: server.accessToken,
133         fields: baseCorrectParams,
134         statusCodeExpected: 200
135       })
136     })
137
138     it('Should fail when adding a channel with the same username', async function () {
139       await makePostBodyRequest({
140         url: server.url,
141         path: videoChannelPath,
142         token: server.accessToken,
143         fields: baseCorrectParams,
144         statusCodeExpected: 409
145       })
146     })
147   })
148
149   describe('When updating a video channel', function () {
150     const baseCorrectParams = {
151       displayName: 'hello',
152       description: 'super description'
153     }
154     let path: string
155
156     before(async function () {
157       path = videoChannelPath + '/super_channel'
158     })
159
160     it('Should fail with a non authenticated user', async function () {
161       await makePutBodyRequest({
162         url: server.url,
163         path,
164         token: 'hi',
165         fields: baseCorrectParams,
166         statusCodeExpected: 401
167       })
168     })
169
170     it('Should fail with another authenticated user', async function () {
171       await makePutBodyRequest({
172         url: server.url,
173         path,
174         token: accessTokenUser,
175         fields: baseCorrectParams,
176         statusCodeExpected: 403
177       })
178     })
179
180     it('Should fail with a long name', async function () {
181       const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
182       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
183     })
184
185     it('Should fail with a long description', async function () {
186       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(201) })
187       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
188     })
189
190     it('Should fail with a long support text', async function () {
191       const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
192       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
193     })
194
195     it('Should succeed with the correct parameters', async function () {
196       await makePutBodyRequest({
197         url: server.url,
198         path,
199         token: server.accessToken,
200         fields: baseCorrectParams,
201         statusCodeExpected: 204
202       })
203     })
204   })
205
206   describe('When updating video channel avatar', function () {
207     let path: string
208
209     before(async function () {
210       path = videoChannelPath + '/super_channel'
211     })
212
213     it('Should fail with an incorrect input file', async function () {
214       const fields = {}
215       const attaches = {
216         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
217       }
218       await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
219     })
220
221     it('Should fail with a big file', async function () {
222       const fields = {}
223       const attaches = {
224         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
225       }
226       await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
227     })
228
229     it('Should fail with an unauthenticated user', async function () {
230       const fields = {}
231       const attaches = {
232         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
233       }
234       await makeUploadRequest({
235         url: server.url,
236         path: path + '/avatar/pick',
237         fields,
238         attaches,
239         statusCodeExpected: 401
240       })
241     })
242
243     it('Should succeed with the correct params', async function () {
244       const fields = {}
245       const attaches = {
246         'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
247       }
248       await makeUploadRequest({
249         url: server.url,
250         path: path + '/avatar/pick',
251         token: server.accessToken,
252         fields,
253         attaches,
254         statusCodeExpected: 200
255       })
256     })
257   })
258
259   describe('When getting a video channel', function () {
260     it('Should return the list of the video channels with nothing', async function () {
261       const res = await makeGetRequest({
262         url: server.url,
263         path: videoChannelPath,
264         statusCodeExpected: 200
265       })
266
267       expect(res.body.data).to.be.an('array')
268     })
269
270     it('Should return 404 with an incorrect video channel', async function () {
271       await makeGetRequest({
272         url: server.url,
273         path: videoChannelPath + '/super_channel2',
274         statusCodeExpected: 404
275       })
276     })
277
278     it('Should succeed with the correct parameters', async function () {
279       await makeGetRequest({
280         url: server.url,
281         path: videoChannelPath + '/super_channel',
282         statusCodeExpected: 200
283       })
284     })
285   })
286
287   describe('When deleting a video channel', function () {
288     it('Should fail with a non authenticated user', async function () {
289       await deleteVideoChannel(server.url, 'coucou', 'super_channel', 401)
290     })
291
292     it('Should fail with another authenticated user', async function () {
293       await deleteVideoChannel(server.url, accessTokenUser, 'super_channel', 403)
294     })
295
296     it('Should fail with an unknown video channel id', async function () {
297       await deleteVideoChannel(server.url, server.accessToken,'super_channel2', 404)
298     })
299
300     it('Should succeed with the correct parameters', async function () {
301       await deleteVideoChannel(server.url, server.accessToken, 'super_channel')
302     })
303
304     it('Should fail to delete the last user video channel', async function () {
305       await deleteVideoChannel(server.url, server.accessToken, 'root_channel', 409)
306     })
307   })
308
309   after(async function () {
310     await cleanupTests([ server ])
311   })
312 })