]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-blacklist.ts
3d4837d58e294a7993f8ff61c8045875c3ab2d69
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-blacklist.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4
5 import {
6 cleanupTests,
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 getBlacklistedVideosList,
11 getVideo,
12 getVideoWithToken,
13 makePostBodyRequest,
14 makePutBodyRequest,
15 removeVideoFromBlacklist,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideo,
19 userLogin,
20 waitJobs
21 } from '../../../../shared/extra-utils'
22 import {
23 checkBadCountPagination,
24 checkBadSortPagination,
25 checkBadStartPagination
26 } from '../../../../shared/extra-utils/requests/check-api-params'
27 import { VideoBlacklistType, VideoDetails } from '../../../../shared/models/videos'
28 import { expect } from 'chai'
29 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
30
31 describe('Test video blacklist API validators', function () {
32 let servers: ServerInfo[]
33 let notBlacklistedVideoId: number
34 let remoteVideoUUID: string
35 let userAccessToken1 = ''
36 let userAccessToken2 = ''
37
38 // ---------------------------------------------------------------
39
40 before(async function () {
41 this.timeout(120000)
42
43 servers = await flushAndRunMultipleServers(2)
44
45 await setAccessTokensToServers(servers)
46 await doubleFollow(servers[0], servers[1])
47
48 {
49 const username = 'user1'
50 const password = 'my super password'
51 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
52 userAccessToken1 = await userLogin(servers[0], { username, password })
53 }
54
55 {
56 const username = 'user2'
57 const password = 'my super password'
58 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
59 userAccessToken2 = await userLogin(servers[0], { username, password })
60 }
61
62 {
63 const res = await uploadVideo(servers[0].url, userAccessToken1, {})
64 servers[0].video = res.body.video
65 }
66
67 {
68 const res = await uploadVideo(servers[0].url, servers[0].accessToken, {})
69 notBlacklistedVideoId = res.body.video.uuid
70 }
71
72 {
73 const res = await uploadVideo(servers[1].url, servers[1].accessToken, {})
74 remoteVideoUUID = res.body.video.uuid
75 }
76
77 await waitJobs(servers)
78 })
79
80 describe('When adding a video in blacklist', function () {
81 const basePath = '/api/v1/videos/'
82
83 it('Should fail with nothing', async function () {
84 const path = basePath + servers[0].video + '/blacklist'
85 const fields = {}
86 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
87 })
88
89 it('Should fail with a wrong video', async function () {
90 const wrongPath = '/api/v1/videos/blabla/blacklist'
91 const fields = {}
92 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
93 })
94
95 it('Should fail with a non authenticated user', async function () {
96 const path = basePath + servers[0].video + '/blacklist'
97 const fields = {}
98 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
99 })
100
101 it('Should fail with a non admin user', async function () {
102 const path = basePath + servers[0].video + '/blacklist'
103 const fields = {}
104 await makePostBodyRequest({
105 url: servers[0].url,
106 path,
107 token: userAccessToken2,
108 fields,
109 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
110 })
111 })
112
113 it('Should fail with an invalid reason', async function () {
114 const path = basePath + servers[0].video.uuid + '/blacklist'
115 const fields = { reason: 'a'.repeat(305) }
116
117 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
118 })
119
120 it('Should fail to unfederate a remote video', async function () {
121 const path = basePath + remoteVideoUUID + '/blacklist'
122 const fields = { unfederate: true }
123
124 await makePostBodyRequest({
125 url: servers[0].url,
126 path,
127 token: servers[0].accessToken,
128 fields,
129 statusCodeExpected: HttpStatusCode.CONFLICT_409
130 })
131 })
132
133 it('Should succeed with the correct params', async function () {
134 const path = basePath + servers[0].video.uuid + '/blacklist'
135 const fields = {}
136
137 await makePostBodyRequest({
138 url: servers[0].url,
139 path,
140 token: servers[0].accessToken,
141 fields,
142 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
143 })
144 })
145 })
146
147 describe('When updating a video in blacklist', function () {
148 const basePath = '/api/v1/videos/'
149
150 it('Should fail with a wrong video', async function () {
151 const wrongPath = '/api/v1/videos/blabla/blacklist'
152 const fields = {}
153 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
154 })
155
156 it('Should fail with a video not blacklisted', async function () {
157 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
158 const fields = {}
159 await makePutBodyRequest({
160 url: servers[0].url,
161 path,
162 token: servers[0].accessToken,
163 fields,
164 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
165 })
166 })
167
168 it('Should fail with a non authenticated user', async function () {
169 const path = basePath + servers[0].video + '/blacklist'
170 const fields = {}
171 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
172 })
173
174 it('Should fail with a non admin user', async function () {
175 const path = basePath + servers[0].video + '/blacklist'
176 const fields = {}
177 await makePutBodyRequest({
178 url: servers[0].url,
179 path,
180 token: userAccessToken2,
181 fields,
182 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
183 })
184 })
185
186 it('Should fail with an invalid reason', async function () {
187 const path = basePath + servers[0].video.uuid + '/blacklist'
188 const fields = { reason: 'a'.repeat(305) }
189
190 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
191 })
192
193 it('Should succeed with the correct params', async function () {
194 const path = basePath + servers[0].video.uuid + '/blacklist'
195 const fields = { reason: 'hello' }
196
197 await makePutBodyRequest({
198 url: servers[0].url,
199 path,
200 token: servers[0].accessToken,
201 fields,
202 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
203 })
204 })
205 })
206
207 describe('When getting blacklisted video', function () {
208
209 it('Should fail with a non authenticated user', async function () {
210 await getVideo(servers[0].url, servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401)
211 })
212
213 it('Should fail with another user', async function () {
214 await getVideoWithToken(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403)
215 })
216
217 it('Should succeed with the owner authenticated user', async function () {
218 const res = await getVideoWithToken(servers[0].url, userAccessToken1, servers[0].video.uuid, HttpStatusCode.OK_200)
219 const video: VideoDetails = res.body
220
221 expect(video.blacklisted).to.be.true
222 })
223
224 it('Should succeed with an admin', async function () {
225 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.OK_200)
226 const video: VideoDetails = res.body
227
228 expect(video.blacklisted).to.be.true
229 })
230 })
231
232 describe('When removing a video in blacklist', function () {
233 it('Should fail with a non authenticated user', async function () {
234 await removeVideoFromBlacklist(servers[0].url, 'fake token', servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401)
235 })
236
237 it('Should fail with a non admin user', async function () {
238 await removeVideoFromBlacklist(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403)
239 })
240
241 it('Should fail with an incorrect id', async function () {
242 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400)
243 })
244
245 it('Should fail with a not blacklisted video', async function () {
246 // The video was not added to the blacklist so it should fail
247 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, notBlacklistedVideoId, HttpStatusCode.NOT_FOUND_404)
248 })
249
250 it('Should succeed with the correct params', async function () {
251 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.NO_CONTENT_204)
252 })
253 })
254
255 describe('When listing videos in blacklist', function () {
256 const basePath = '/api/v1/videos/blacklist/'
257
258 it('Should fail with a non authenticated user', async function () {
259 await getBlacklistedVideosList({ url: servers[0].url, token: 'fake token', specialStatus: HttpStatusCode.UNAUTHORIZED_401 })
260 })
261
262 it('Should fail with a non admin user', async function () {
263 await getBlacklistedVideosList({ url: servers[0].url, token: userAccessToken2, specialStatus: HttpStatusCode.FORBIDDEN_403 })
264 })
265
266 it('Should fail with a bad start pagination', async function () {
267 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
268 })
269
270 it('Should fail with a bad count pagination', async function () {
271 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
272 })
273
274 it('Should fail with an incorrect sort', async function () {
275 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
276 })
277
278 it('Should fail with an invalid type', async function () {
279 await getBlacklistedVideosList({
280 url: servers[0].url,
281 token: servers[0].accessToken,
282 type: 0,
283 specialStatus: HttpStatusCode.BAD_REQUEST_400
284 })
285 })
286
287 it('Should succeed with the correct parameters', async function () {
288 await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: VideoBlacklistType.MANUAL })
289 })
290 })
291
292 after(async function () {
293 await cleanupTests(servers)
294 })
295 })