Fix video channel update with an admin account
[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   createUser,
8   deleteVideoChannel,
9   flushTests,
10   getAccountVideoChannelsList, getMyUserInformation,
11   getVideoChannelsList,
12   immutableAssign,
13   killallServers,
14   makeGetRequest,
15   makePostBodyRequest,
16   makePutBodyRequest,
17   runServer,
18   ServerInfo,
19   setAccessTokensToServers,
20   userLogin
21 } from '../../utils'
22 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
23 import { getAccountsList } from '../../utils/users/accounts'
24 import { User } from '../../../../shared/models/users'
25
26 const expect = chai.expect
27
28 describe('Test video channels API validator', function () {
29   const videoChannelPath = '/api/v1/video-channels'
30   let server: ServerInfo
31   let accessTokenUser: string
32   let videoChannelUUID: string
33
34   // ---------------------------------------------------------------
35
36   before(async function () {
37     this.timeout(30000)
38
39     await flushTests()
40
41     server = await runServer(1)
42
43     await setAccessTokensToServers([ server ])
44
45     const user = {
46       username: 'fake',
47       password: 'fake_password'
48     }
49
50     {
51       await createUser(server.url, server.accessToken, user.username, user.password)
52       accessTokenUser = await userLogin(server, user)
53     }
54
55     {
56       const res = await getMyUserInformation(server.url, server.accessToken)
57       const user: User = res.body
58       videoChannelUUID = user.videoChannels[0].uuid
59     }
60   })
61
62   describe('When listing a video channels', function () {
63     it('Should fail with a bad start pagination', async function () {
64       await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
65     })
66
67     it('Should fail with a bad count pagination', async function () {
68       await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
69     })
70
71     it('Should fail with an incorrect sort', async function () {
72       await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
73     })
74   })
75
76   describe('When listing account video channels', function () {
77     it('Should fail with bad account', async function () {
78       await getAccountVideoChannelsList(server.url, 'hello', 400)
79     })
80
81     it('Should fail with a unknown account', async function () {
82       await getAccountVideoChannelsList(server.url, 154, 404)
83     })
84   })
85
86   describe('When adding a video channel', function () {
87     const baseCorrectParams = {
88       displayName: 'hello',
89       description: 'super description',
90       support: 'super support text'
91     }
92
93     it('Should fail with a non authenticated user', async function () {
94       await makePostBodyRequest({
95         url: server.url,
96         path: videoChannelPath,
97         token: 'none',
98         fields: baseCorrectParams,
99         statusCodeExpected: 401
100       })
101     })
102
103     it('Should fail with nothing', async function () {
104       const fields = {}
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(150) })
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(150) })
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
139   describe('When updating a video channel', function () {
140     const baseCorrectParams = {
141       displayName: 'hello',
142       description: 'super description'
143     }
144     let path: string
145
146     before(async function () {
147       path = videoChannelPath + '/' + videoChannelUUID
148     })
149
150     it('Should fail with a non authenticated user', async function () {
151       await makePutBodyRequest({
152         url: server.url,
153         path,
154         token: 'hi',
155         fields: baseCorrectParams,
156         statusCodeExpected: 401
157       })
158     })
159
160     it('Should fail with another authenticated user', async function () {
161       await makePutBodyRequest({
162         url: server.url,
163         path,
164         token: accessTokenUser,
165         fields: baseCorrectParams,
166         statusCodeExpected: 403
167       })
168     })
169
170     it('Should fail with a long name', async function () {
171       const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
172       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
173     })
174
175     it('Should fail with a long description', async function () {
176       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(150) })
177       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
178     })
179
180     it('Should fail with a long support text', async function () {
181       const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(150) })
182       await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
183     })
184
185     it('Should succeed with the correct parameters', async function () {
186       await makePutBodyRequest({
187         url: server.url,
188         path,
189         token: server.accessToken,
190         fields: baseCorrectParams,
191         statusCodeExpected: 204
192       })
193     })
194   })
195
196   describe('When getting a video channel', function () {
197     it('Should return the list of the video channels with nothing', async function () {
198       const res = await makeGetRequest({
199         url: server.url,
200         path: videoChannelPath,
201         statusCodeExpected: 200
202       })
203
204       expect(res.body.data).to.be.an('array')
205     })
206
207     it('Should fail without a correct uuid', async function () {
208       await makeGetRequest({
209         url: server.url,
210         path: videoChannelPath + '/coucou',
211         statusCodeExpected: 400
212       })
213     })
214
215     it('Should return 404 with an incorrect video channel', async function () {
216       await makeGetRequest({
217         url: server.url,
218         path: videoChannelPath + '/4da6fde3-88f7-4d16-b119-108df5630b06',
219         statusCodeExpected: 404
220       })
221     })
222
223     it('Should succeed with the correct parameters', async function () {
224       await makeGetRequest({
225         url: server.url,
226         path: videoChannelPath + '/' + videoChannelUUID,
227         statusCodeExpected: 200
228       })
229     })
230   })
231
232   describe('When deleting a video channel', function () {
233     it('Should fail with a non authenticated user', async function () {
234       await deleteVideoChannel(server.url, 'coucou', videoChannelUUID, 401)
235     })
236
237     it('Should fail with another authenticated user', async function () {
238       await deleteVideoChannel(server.url, accessTokenUser, videoChannelUUID, 403)
239     })
240
241     it('Should fail with an unknown video channel id', async function () {
242       await deleteVideoChannel(server.url, server.accessToken,454554, 404)
243     })
244
245     it('Should succeed with the correct parameters', async function () {
246       await deleteVideoChannel(server.url, server.accessToken, videoChannelUUID)
247     })
248
249     it('Should fail to delete the last user video channel', async function () {
250       const res = await getVideoChannelsList(server.url, 0, 1)
251       const lastVideoChannelUUID = res.body.data[0].uuid
252
253       await deleteVideoChannel(server.url, server.accessToken, lastVideoChannelUUID, 409)
254     })
255   })
256
257   after(async function () {
258     killallServers([ server ])
259
260     // Keep the logs if the test failed
261     if (this['ok']) {
262       await flushTests()
263     }
264   })
265 })