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