]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-channels.ts
25b2dc9b950c29db840ab4cb16e97f947c62e222
[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, 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 videos API validator', function () {
29 const videoChannelPath = '/api/v1/video-channels'
30 const accountPath = '/api/v1/accounts/'
31 let server: ServerInfo
32 let accessTokenUser: string
33 let accountUUID: string
34 let videoChannelUUID: string
35
36 // ---------------------------------------------------------------
37
38 before(async function () {
39 this.timeout(30000)
40
41 await flushTests()
42
43 server = await runServer(1)
44
45 await setAccessTokensToServers([ server ])
46
47 const user = {
48 username: 'fake',
49 password: 'fake_password'
50 }
51
52 {
53 await createUser(server.url, server.accessToken, user.username, user.password)
54 accessTokenUser = await userLogin(server, user)
55 }
56
57 {
58 const res = await getMyUserInformation(server.url, server.accessToken)
59 const user: User = res.body
60 accountUUID = user.account.uuid
61 videoChannelUUID = user.videoChannels[0].uuid
62 }
63 })
64
65 describe('When listing a video channels', function () {
66 it('Should fail with a bad start pagination', async function () {
67 await checkBadStartPagination(server.url, videoChannelPath, server.accessToken)
68 })
69
70 it('Should fail with a bad count pagination', async function () {
71 await checkBadCountPagination(server.url, videoChannelPath, server.accessToken)
72 })
73
74 it('Should fail with an incorrect sort', async function () {
75 await checkBadSortPagination(server.url, videoChannelPath, server.accessToken)
76 })
77 })
78
79 describe('When listing account video channels', function () {
80 it('Should fail with bad account', async function () {
81 await getAccountVideoChannelsList(server.url, 'hello', 400)
82 })
83
84 it('Should fail with a unknown account', async function () {
85 await getAccountVideoChannelsList(server.url, 154, 404)
86 })
87 })
88
89 describe('When adding a video channel', function () {
90 const baseCorrectParams = {
91 name: 'hello',
92 description: 'super description',
93 support: 'super support text'
94 }
95 let path: string
96
97 before(async function () {
98 path = accountPath + accountUUID + '/video-channels'
99 })
100
101 it('Should fail with a non authenticated user', async function () {
102 await makePostBodyRequest({ url: server.url, path, token: 'none', fields: baseCorrectParams, statusCodeExpected: 401 })
103 })
104
105 it('Should fail with nothing', async function () {
106 const fields = {}
107 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
108 })
109
110 it('Should fail without name', async function () {
111 const fields = omit(baseCorrectParams, 'name')
112 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
113 })
114
115 it('Should fail with a long name', async function () {
116 const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(25) })
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
118 })
119
120 it('Should fail with a long description', async function () {
121 const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(60) })
122 await makePostBodyRequest({ url: server.url, path, 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(70) })
127 await makePostBodyRequest({ url: server.url, path, 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,
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 name: 'hello',
144 description: 'super description'
145 }
146 let path: string
147
148 before(async function () {
149 path = accountPath + accountUUID + '/video-channels/' + videoChannelUUID
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, { name: '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(60) })
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(70) })
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 getting a video channel', function () {
199 let basePath: string
200
201 before(async function () {
202 basePath = accountPath + accountUUID + '/video-channels'
203 })
204
205 it('Should return the list of the video channels with nothing', async function () {
206 const res = await makeGetRequest({
207 url: server.url,
208 path: basePath,
209 statusCodeExpected: 200
210 })
211
212 expect(res.body.data).to.be.an('array')
213 })
214
215 it('Should fail without a correct uuid', async function () {
216 await makeGetRequest({
217 url: server.url,
218 path: basePath + '/coucou',
219 statusCodeExpected: 400
220 })
221 })
222
223 it('Should return 404 with an incorrect video channel', async function () {
224 await makeGetRequest({
225 url: server.url,
226 path: basePath + '/4da6fde3-88f7-4d16-b119-108df5630b06',
227 statusCodeExpected: 404
228 })
229 })
230
231 it('Should succeed with the correct parameters', async function () {
232 await makeGetRequest({
233 url: server.url,
234 path: basePath + '/' + videoChannelUUID,
235 statusCodeExpected: 200
236 })
237 })
238 })
239
240 describe('When deleting a video channel', function () {
241 it('Should fail with a non authenticated user', async function () {
242 await deleteVideoChannel(server.url, 'coucou', accountUUID, videoChannelUUID, 401)
243 })
244
245 it('Should fail with another authenticated user', async function () {
246 await deleteVideoChannel(server.url, accessTokenUser, accountUUID, videoChannelUUID, 403)
247 })
248
249 it('Should fail with an unknown account id', async function () {
250 await deleteVideoChannel(server.url, server.accessToken, 454554,videoChannelUUID, 404)
251 })
252
253 it('Should fail with an unknown video channel id', async function () {
254 await deleteVideoChannel(server.url, server.accessToken, accountUUID,454554, 404)
255 })
256
257 it('Should succeed with the correct parameters', async function () {
258 await deleteVideoChannel(server.url, server.accessToken, accountUUID, videoChannelUUID)
259 })
260
261 it('Should fail to delete the last user video channel', async function () {
262 const res = await getVideoChannelsList(server.url, 0, 1)
263 const lastVideoChannelUUID = res.body.data[0].uuid
264
265 await deleteVideoChannel(server.url, server.accessToken, accountUUID, lastVideoChannelUUID, 409)
266 })
267 })
268
269 after(async function () {
270 killallServers([ server ])
271
272 // Keep the logs if the test failed
273 if (this['ok']) {
274 await flushTests()
275 }
276 })
277 })