Add tests to handle down server
[oweals/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, deleteVideoChannel, flushTests, getAccountVideoChannelsList, getVideoChannelsList, immutableAssign, killallServers,
8   makeGetRequest, makePostBodyRequest, makePutBodyRequest, runServer, ServerInfo, setAccessTokensToServers, userLogin
9 } from '../../utils'
10 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
11
12 const expect = chai.expect
13
14 describe('Test videos API validator', function () {
15   const path = '/api/v1/videos/channels'
16   let server: ServerInfo
17   let accessTokenUser: string
18
19   // ---------------------------------------------------------------
20
21   before(async function () {
22     this.timeout(20000)
23
24     await flushTests()
25
26     server = await runServer(1)
27
28     await setAccessTokensToServers([ server ])
29
30     const user = {
31       username: 'fake',
32       password: 'fake_password'
33     }
34     await createUser(server.url, server.accessToken, user.username, user.password)
35     accessTokenUser = await userLogin(server, user)
36   })
37
38   describe('When listing a video channels', function () {
39     it('Should fail with a bad start pagination', async function () {
40       await checkBadStartPagination(server.url, path, server.accessToken)
41     })
42
43     it('Should fail with a bad count pagination', async function () {
44       await checkBadCountPagination(server.url, path, server.accessToken)
45     })
46
47     it('Should fail with an incorrect sort', async function () {
48       await checkBadSortPagination(server.url, path, server.accessToken)
49     })
50   })
51
52   describe('When listing account video channels', function () {
53     it('Should fail with bad account', async function () {
54       await getAccountVideoChannelsList(server.url, 'hello', 400)
55     })
56
57     it('Should fail with a unknown account', async function () {
58       await getAccountVideoChannelsList(server.url, 154, 404)
59     })
60   })
61
62   describe('When adding a video channel', function () {
63     const baseCorrectParams = {
64       name: 'hello',
65       description: 'super description'
66     }
67
68     it('Should fail with a non authenticated user', async function () {
69       await makePostBodyRequest({ url: server.url, path, token: 'none', fields: baseCorrectParams, statusCodeExpected: 401 })
70     })
71
72     it('Should fail with nothing', async function () {
73       const fields = {}
74       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
75     })
76
77     it('Should fail without name', async function () {
78       const fields = omit(baseCorrectParams, 'name')
79       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
80     })
81
82     it('Should fail with a long name', async function () {
83       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(25) })
84       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
85     })
86
87     it('Should fail with a long description', async function () {
88       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(60) })
89       await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
90     })
91
92     it('Should succeed with the correct parameters', async function () {
93       await makePostBodyRequest({
94         url: server.url,
95         path,
96         token: server.accessToken,
97         fields: baseCorrectParams,
98         statusCodeExpected: 204
99       })
100     })
101   })
102
103   describe('When updating a video channel', function () {
104     const baseCorrectParams = {
105       name: 'hello',
106       description: 'super description'
107     }
108
109     let videoChannelId
110
111     before(async function () {
112       const res = await getVideoChannelsList(server.url, 0, 1)
113       videoChannelId = res.body.data[0].id
114     })
115
116     it('Should fail with a non authenticated user', async function () {
117       await makePutBodyRequest({
118         url: server.url,
119         path: path + '/' + videoChannelId,
120         token: 'hi',
121         fields: baseCorrectParams,
122         statusCodeExpected: 401
123       })
124     })
125
126     it('Should fail with another authenticated user', async function () {
127       await makePutBodyRequest({
128         url: server.url,
129         path: path + '/' + videoChannelId,
130         token: accessTokenUser,
131         fields: baseCorrectParams,
132         statusCodeExpected: 403
133       })
134     })
135
136     it('Should fail with a long name', async function () {
137       const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(25) })
138       await makePutBodyRequest({ url: server.url, path: path + '/' + videoChannelId, token: server.accessToken, fields })
139     })
140
141     it('Should fail with a long description', async function () {
142       const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(60) })
143       await makePutBodyRequest({ url: server.url, path: path + '/' + videoChannelId, token: server.accessToken, fields })
144     })
145
146     it('Should succeed with the correct parameters', async function () {
147       await makePutBodyRequest({
148         url: server.url,
149         path: path + '/' + videoChannelId,
150         token: server.accessToken,
151         fields: baseCorrectParams,
152         statusCodeExpected: 204
153       })
154     })
155   })
156
157   describe('When getting a video channel', function () {
158     let videoChannelId: number
159
160     before(async function () {
161       const res = await getVideoChannelsList(server.url, 0, 1)
162       videoChannelId = res.body.data[0].id
163     })
164
165     it('Should return the list of the video channels with nothing', async function () {
166       const res = await makeGetRequest({
167         url: server.url,
168         path,
169         statusCodeExpected: 200
170       })
171
172       expect(res.body.data).to.be.an('array')
173     })
174
175     it('Should fail without a correct uuid', async function () {
176       await makeGetRequest({
177         url: server.url,
178         path: path + '/coucou',
179         statusCodeExpected: 400
180       })
181     })
182
183     it('Should return 404 with an incorrect video channel', async function () {
184       await makeGetRequest({
185         url: server.url,
186         path: path + '/4da6fde3-88f7-4d16-b119-108df5630b06',
187         statusCodeExpected: 404
188       })
189     })
190
191     it('Should succeed with the correct parameters', async function () {
192       await makeGetRequest({
193         url: server.url,
194         path: path + '/' + videoChannelId,
195         statusCodeExpected: 200
196       })
197     })
198   })
199
200   describe('When deleting a video channel', function () {
201     let videoChannelId: number
202
203     before(async function () {
204       const res = await getVideoChannelsList(server.url, 0, 1)
205       videoChannelId = res.body.data[0].id
206     })
207
208     it('Should fail with a non authenticated user', async function () {
209       await deleteVideoChannel(server.url, 'coucou', videoChannelId, 401)
210     })
211
212     it('Should fail with another authenticated user', async function () {
213       await deleteVideoChannel(server.url, accessTokenUser, videoChannelId, 403)
214     })
215
216     it('Should fail with an unknown id', async function () {
217       await deleteVideoChannel(server.url, server.accessToken, 454554, 404)
218     })
219
220     it('Should succeed with the correct parameters', async function () {
221       await deleteVideoChannel(server.url, server.accessToken, videoChannelId)
222     })
223
224     it('Should fail to delete the last user video channel', async function () {
225       const res = await getVideoChannelsList(server.url, 0, 1)
226       videoChannelId = res.body.data[0].id
227
228       await deleteVideoChannel(server.url, server.accessToken, videoChannelId, 409)
229     })
230   })
231
232   after(async function () {
233     killallServers([ server ])
234
235     // Keep the logs if the test failed
236     if (this['ok']) {
237       await flushTests()
238     }
239   })
240 })