]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-channels.ts
Add get subscription endpoint
[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
141 describe('When updating a video channel', function () {
142 const baseCorrectParams = {
143 displayName: 'hello',
144 description: 'super description'
145 }
146 let path: string
147
148 before(async function () {
149 path = videoChannelPath + '/super_channel'
150 })
151
152 it('Should fail with a non authenticated user', async function () {
153 await makePutBodyRequest({
154 url: server.url,
155 path,
156 token: 'hi',
157 fields: baseCorrectParams,
158 statusCodeExpected: 401
159 })
160 })
161
162 it('Should fail with another authenticated user', async function () {
163 await makePutBodyRequest({
164 url: server.url,
165 path,
166 token: accessTokenUser,
167 fields: baseCorrectParams,
168 statusCodeExpected: 403
169 })
170 })
171
172 it('Should fail with a long name', async function () {
173 const fields = immutableAssign(baseCorrectParams, { displayName: 'super'.repeat(25) })
174 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
175 })
176
177 it('Should fail with a long description', async function () {
178 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(150) })
179 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
180 })
181
182 it('Should fail with a long support text', async function () {
183 const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(150) })
184 await makePutBodyRequest({ url: server.url, path, token: server.accessToken, fields })
185 })
186
187 it('Should succeed with the correct parameters', async function () {
188 await makePutBodyRequest({
189 url: server.url,
190 path,
191 token: server.accessToken,
192 fields: baseCorrectParams,
193 statusCodeExpected: 204
194 })
195 })
196 })
197
198 describe('When updating video channel avatar', function () {
199 let path: string
200
201 before(async function () {
202 path = videoChannelPath + '/super_channel'
203 })
204
205 it('Should fail with an incorrect input file', async function () {
206 const fields = {}
207 const attaches = {
208 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'video_short.mp4')
209 }
210 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
211 })
212
213 it('Should fail with a big file', async function () {
214 const fields = {}
215 const attaches = {
216 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
217 }
218 await makeUploadRequest({ url: server.url, path: path + '/avatar/pick', token: server.accessToken, fields, attaches })
219 })
220
221 it('Should fail with an unauthenticated user', async function () {
222 const fields = {}
223 const attaches = {
224 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
225 }
226 await makeUploadRequest({
227 url: server.url,
228 path: path + '/avatar/pick',
229 fields,
230 attaches,
231 statusCodeExpected: 401
232 })
233 })
234
235 it('Should succeed with the correct params', async function () {
236 const fields = {}
237 const attaches = {
238 'avatarfile': join(__dirname, '..', '..', 'fixtures', 'avatar.png')
239 }
240 await makeUploadRequest({
241 url: server.url,
242 path: path + '/avatar/pick',
243 token: server.accessToken,
244 fields,
245 attaches,
246 statusCodeExpected: 200
247 })
248 })
249 })
250
251 describe('When getting a video channel', function () {
252 it('Should return the list of the video channels with nothing', async function () {
253 const res = await makeGetRequest({
254 url: server.url,
255 path: videoChannelPath,
256 statusCodeExpected: 200
257 })
258
259 expect(res.body.data).to.be.an('array')
260 })
261
262 it('Should return 404 with an incorrect video channel', async function () {
263 await makeGetRequest({
264 url: server.url,
265 path: videoChannelPath + '/super_channel2',
266 statusCodeExpected: 404
267 })
268 })
269
270 it('Should succeed with the correct parameters', async function () {
271 await makeGetRequest({
272 url: server.url,
273 path: videoChannelPath + '/super_channel',
274 statusCodeExpected: 200
275 })
276 })
277 })
278
279 describe('When deleting a video channel', function () {
280 it('Should fail with a non authenticated user', async function () {
281 await deleteVideoChannel(server.url, 'coucou', 'super_channel', 401)
282 })
283
284 it('Should fail with another authenticated user', async function () {
285 await deleteVideoChannel(server.url, accessTokenUser, 'super_channel', 403)
286 })
287
288 it('Should fail with an unknown video channel id', async function () {
289 await deleteVideoChannel(server.url, server.accessToken,'super_channel2', 404)
290 })
291
292 it('Should succeed with the correct parameters', async function () {
293 await deleteVideoChannel(server.url, server.accessToken, 'super_channel')
294 })
295
296 it('Should fail to delete the last user video channel', async function () {
297 await deleteVideoChannel(server.url, server.accessToken, 'root_channel', 409)
298 })
299 })
300
301 after(async function () {
302 killallServers([ server ])
303
304 // Keep the logs if the test failed
305 if (this['ok']) {
306 await flushTests()
307 }
308 })
309 })