1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
8 checkBadCountPagination,
9 checkBadSortPagination,
10 checkBadStartPagination,
14 flushAndRunMultipleServers,
20 setAccessTokensToServers,
24 } from '@shared/extra-utils'
25 import { VideoBlacklistType, VideoDetails } from '@shared/models'
27 describe('Test video blacklist API validators', function () {
28 let servers: ServerInfo[]
29 let notBlacklistedVideoId: number
30 let remoteVideoUUID: string
31 let userAccessToken1 = ''
32 let userAccessToken2 = ''
33 let command: BlacklistCommand
35 // ---------------------------------------------------------------
37 before(async function () {
40 servers = await flushAndRunMultipleServers(2)
42 await setAccessTokensToServers(servers)
43 await doubleFollow(servers[0], servers[1])
46 const username = 'user1'
47 const password = 'my super password'
48 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
49 userAccessToken1 = await userLogin(servers[0], { username, password })
53 const username = 'user2'
54 const password = 'my super password'
55 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
56 userAccessToken2 = await userLogin(servers[0], { username, password })
60 const res = await uploadVideo(servers[0].url, userAccessToken1, {})
61 servers[0].video = res.body.video
65 const res = await uploadVideo(servers[0].url, servers[0].accessToken, {})
66 notBlacklistedVideoId = res.body.video.uuid
70 const res = await uploadVideo(servers[1].url, servers[1].accessToken, {})
71 remoteVideoUUID = res.body.video.uuid
74 await waitJobs(servers)
76 command = servers[0].blacklistCommand
79 describe('When adding a video in blacklist', function () {
80 const basePath = '/api/v1/videos/'
82 it('Should fail with nothing', async function () {
83 const path = basePath + servers[0].video + '/blacklist'
85 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
88 it('Should fail with a wrong video', async function () {
89 const wrongPath = '/api/v1/videos/blabla/blacklist'
91 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
94 it('Should fail with a non authenticated user', async function () {
95 const path = basePath + servers[0].video + '/blacklist'
97 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
100 it('Should fail with a non admin user', async function () {
101 const path = basePath + servers[0].video + '/blacklist'
103 await makePostBodyRequest({
106 token: userAccessToken2,
108 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
112 it('Should fail with an invalid reason', async function () {
113 const path = basePath + servers[0].video.uuid + '/blacklist'
114 const fields = { reason: 'a'.repeat(305) }
116 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
119 it('Should fail to unfederate a remote video', async function () {
120 const path = basePath + remoteVideoUUID + '/blacklist'
121 const fields = { unfederate: true }
123 await makePostBodyRequest({
126 token: servers[0].accessToken,
128 statusCodeExpected: HttpStatusCode.CONFLICT_409
132 it('Should succeed with the correct params', async function () {
133 const path = basePath + servers[0].video.uuid + '/blacklist'
136 await makePostBodyRequest({
139 token: servers[0].accessToken,
141 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
146 describe('When updating a video in blacklist', function () {
147 const basePath = '/api/v1/videos/'
149 it('Should fail with a wrong video', async function () {
150 const wrongPath = '/api/v1/videos/blabla/blacklist'
152 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
155 it('Should fail with a video not blacklisted', async function () {
156 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
158 await makePutBodyRequest({
161 token: servers[0].accessToken,
163 statusCodeExpected: HttpStatusCode.NOT_FOUND_404
167 it('Should fail with a non authenticated user', async function () {
168 const path = basePath + servers[0].video + '/blacklist'
170 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
173 it('Should fail with a non admin user', async function () {
174 const path = basePath + servers[0].video + '/blacklist'
176 await makePutBodyRequest({
179 token: userAccessToken2,
181 statusCodeExpected: HttpStatusCode.FORBIDDEN_403
185 it('Should fail with an invalid reason', async function () {
186 const path = basePath + servers[0].video.uuid + '/blacklist'
187 const fields = { reason: 'a'.repeat(305) }
189 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
192 it('Should succeed with the correct params', async function () {
193 const path = basePath + servers[0].video.shortUUID + '/blacklist'
194 const fields = { reason: 'hello' }
196 await makePutBodyRequest({
199 token: servers[0].accessToken,
201 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
206 describe('When getting blacklisted video', function () {
208 it('Should fail with a non authenticated user', async function () {
209 await getVideo(servers[0].url, servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401)
212 it('Should fail with another user', async function () {
213 await getVideoWithToken(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403)
216 it('Should succeed with the owner authenticated user', async function () {
217 const res = await getVideoWithToken(servers[0].url, userAccessToken1, servers[0].video.uuid, HttpStatusCode.OK_200)
218 const video: VideoDetails = res.body
220 expect(video.blacklisted).to.be.true
223 it('Should succeed with an admin', async function () {
224 const video = servers[0].video
226 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
227 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, id, HttpStatusCode.OK_200)
228 const video: VideoDetails = res.body
230 expect(video.blacklisted).to.be.true
235 describe('When removing a video in blacklist', function () {
237 it('Should fail with a non authenticated user', async function () {
238 await command.remove({ token: 'fake token', videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
241 it('Should fail with a non admin user', async function () {
242 await command.remove({ token: userAccessToken2, videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
245 it('Should fail with an incorrect id', async function () {
246 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
249 it('Should fail with a not blacklisted video', async function () {
250 // The video was not added to the blacklist so it should fail
251 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
254 it('Should succeed with the correct params', async function () {
255 await command.remove({ videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
259 describe('When listing videos in blacklist', function () {
260 const basePath = '/api/v1/videos/blacklist/'
262 it('Should fail with a non authenticated user', async function () {
263 await servers[0].blacklistCommand.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
266 it('Should fail with a non admin user', async function () {
267 await servers[0].blacklistCommand.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
270 it('Should fail with a bad start pagination', async function () {
271 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
274 it('Should fail with a bad count pagination', async function () {
275 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
278 it('Should fail with an incorrect sort', async function () {
279 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
282 it('Should fail with an invalid type', async function () {
283 await servers[0].blacklistCommand.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
286 it('Should succeed with the correct parameters', async function () {
287 await servers[0].blacklistCommand.list({ type: VideoBlacklistType.MANUAL })
291 after(async function () {
292 await cleanupTests(servers)