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