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