1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
11 createMultipleServers,
16 setAccessTokensToServers,
18 } from '@shared/extra-utils'
19 import { HttpStatusCode, VideoBlacklistType } from '@shared/models'
21 describe('Test video blacklist API validators', function () {
22 let servers: PeerTubeServer[]
23 let notBlacklistedVideoId: string
24 let remoteVideoUUID: string
25 let userAccessToken1 = ''
26 let userAccessToken2 = ''
27 let command: BlacklistCommand
29 // ---------------------------------------------------------------
31 before(async function () {
34 servers = await createMultipleServers(2)
36 await setAccessTokensToServers(servers)
37 await doubleFollow(servers[0], servers[1])
40 const username = 'user1'
41 const password = 'my super password'
42 await servers[0].users.create({ username: username, password: password })
43 userAccessToken1 = await servers[0].login.getAccessToken({ username, password })
47 const username = 'user2'
48 const password = 'my super password'
49 await servers[0].users.create({ username: username, password: password })
50 userAccessToken2 = await servers[0].login.getAccessToken({ username, password })
54 servers[0].store.videoCreated = await servers[0].videos.upload({ token: userAccessToken1 })
58 const { uuid } = await servers[0].videos.upload()
59 notBlacklistedVideoId = uuid
63 const { uuid } = await servers[1].videos.upload()
64 remoteVideoUUID = uuid
67 await waitJobs(servers)
69 command = servers[0].blacklist
72 describe('When adding a video in blacklist', function () {
73 const basePath = '/api/v1/videos/'
75 it('Should fail with nothing', async function () {
76 const path = basePath + servers[0].store.videoCreated + '/blacklist'
78 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
81 it('Should fail with a wrong video', async function () {
82 const wrongPath = '/api/v1/videos/blabla/blacklist'
84 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
87 it('Should fail with a non authenticated user', async function () {
88 const path = basePath + servers[0].store.videoCreated + '/blacklist'
90 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
93 it('Should fail with a non admin user', async function () {
94 const path = basePath + servers[0].store.videoCreated + '/blacklist'
96 await makePostBodyRequest({
99 token: userAccessToken2,
101 expectedStatus: HttpStatusCode.FORBIDDEN_403
105 it('Should fail with an invalid reason', async function () {
106 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
107 const fields = { reason: 'a'.repeat(305) }
109 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
112 it('Should fail to unfederate a remote video', async function () {
113 const path = basePath + remoteVideoUUID + '/blacklist'
114 const fields = { unfederate: true }
116 await makePostBodyRequest({
119 token: servers[0].accessToken,
121 expectedStatus: HttpStatusCode.CONFLICT_409
125 it('Should succeed with the correct params', async function () {
126 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
129 await makePostBodyRequest({
132 token: servers[0].accessToken,
134 expectedStatus: HttpStatusCode.NO_CONTENT_204
139 describe('When updating a video in blacklist', function () {
140 const basePath = '/api/v1/videos/'
142 it('Should fail with a wrong video', async function () {
143 const wrongPath = '/api/v1/videos/blabla/blacklist'
145 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
148 it('Should fail with a video not blacklisted', async function () {
149 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
151 await makePutBodyRequest({
154 token: servers[0].accessToken,
156 expectedStatus: HttpStatusCode.NOT_FOUND_404
160 it('Should fail with a non authenticated user', async function () {
161 const path = basePath + servers[0].store.videoCreated + '/blacklist'
163 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
166 it('Should fail with a non admin user', async function () {
167 const path = basePath + servers[0].store.videoCreated + '/blacklist'
169 await makePutBodyRequest({
172 token: userAccessToken2,
174 expectedStatus: HttpStatusCode.FORBIDDEN_403
178 it('Should fail with an invalid reason', async function () {
179 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
180 const fields = { reason: 'a'.repeat(305) }
182 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
185 it('Should succeed with the correct params', async function () {
186 const path = basePath + servers[0].store.videoCreated.shortUUID + '/blacklist'
187 const fields = { reason: 'hello' }
189 await makePutBodyRequest({
192 token: servers[0].accessToken,
194 expectedStatus: HttpStatusCode.NO_CONTENT_204
199 describe('When getting blacklisted video', function () {
201 it('Should fail with a non authenticated user', async function () {
202 await servers[0].videos.get({ id: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
205 it('Should fail with another user', async function () {
206 await servers[0].videos.getWithToken({
207 token: userAccessToken2,
208 id: servers[0].store.videoCreated.uuid,
209 expectedStatus: HttpStatusCode.FORBIDDEN_403
213 it('Should succeed with the owner authenticated user', async function () {
214 const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.videoCreated.uuid })
215 expect(video.blacklisted).to.be.true
218 it('Should succeed with an admin', async function () {
219 const video = servers[0].store.videoCreated
221 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
222 const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 })
223 expect(video.blacklisted).to.be.true
228 describe('When removing a video in blacklist', function () {
230 it('Should fail with a non authenticated user', async function () {
231 await command.remove({
233 videoId: servers[0].store.videoCreated.uuid,
234 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
238 it('Should fail with a non admin user', async function () {
239 await command.remove({
240 token: userAccessToken2,
241 videoId: servers[0].store.videoCreated.uuid,
242 expectedStatus: HttpStatusCode.FORBIDDEN_403
246 it('Should fail with an incorrect id', async function () {
247 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
250 it('Should fail with a not blacklisted video', async function () {
251 // The video was not added to the blacklist so it should fail
252 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
255 it('Should succeed with the correct params', async function () {
256 await command.remove({ videoId: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
260 describe('When listing videos in blacklist', function () {
261 const basePath = '/api/v1/videos/blacklist/'
263 it('Should fail with a non authenticated user', async function () {
264 await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
267 it('Should fail with a non admin user', async function () {
268 await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
271 it('Should fail with a bad start pagination', async function () {
272 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
275 it('Should fail with a bad count pagination', async function () {
276 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
279 it('Should fail with an incorrect sort', async function () {
280 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
283 it('Should fail with an invalid type', async function () {
284 await servers[0].blacklist.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
287 it('Should succeed with the correct parameters', async function () {
288 await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL })
292 after(async function () {
293 await cleanupTests(servers)