1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
6 import { HttpStatusCode, VideoBlacklistType } from '@shared/models'
10 createMultipleServers,
15 setAccessTokensToServers,
17 } from '@shared/server-commands'
19 describe('Test video blacklist API validators', function () {
20 let servers: PeerTubeServer[]
21 let notBlacklistedVideoId: string
22 let remoteVideoUUID: string
23 let userAccessToken1 = ''
24 let userAccessToken2 = ''
25 let command: BlacklistCommand
27 // ---------------------------------------------------------------
29 before(async function () {
32 servers = await createMultipleServers(2)
34 await setAccessTokensToServers(servers)
35 await doubleFollow(servers[0], servers[1])
38 const username = 'user1'
39 const password = 'my super password'
40 await servers[0].users.create({ username, password })
41 userAccessToken1 = await servers[0].login.getAccessToken({ username, password })
45 const username = 'user2'
46 const password = 'my super password'
47 await servers[0].users.create({ username, password })
48 userAccessToken2 = await servers[0].login.getAccessToken({ username, password })
52 servers[0].store.videoCreated = await servers[0].videos.upload({ token: userAccessToken1 })
56 const { uuid } = await servers[0].videos.upload()
57 notBlacklistedVideoId = uuid
61 const { uuid } = await servers[1].videos.upload()
62 remoteVideoUUID = uuid
65 await waitJobs(servers)
67 command = servers[0].blacklist
70 describe('When adding a video in blacklist', function () {
71 const basePath = '/api/v1/videos/'
73 it('Should fail with nothing', async function () {
74 const path = basePath + servers[0].store.videoCreated + '/blacklist'
76 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
79 it('Should fail with a wrong video', async function () {
80 const wrongPath = '/api/v1/videos/blabla/blacklist'
82 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
85 it('Should fail with a non authenticated user', async function () {
86 const path = basePath + servers[0].store.videoCreated + '/blacklist'
88 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
91 it('Should fail with a non admin user', async function () {
92 const path = basePath + servers[0].store.videoCreated + '/blacklist'
94 await makePostBodyRequest({
97 token: userAccessToken2,
99 expectedStatus: HttpStatusCode.FORBIDDEN_403
103 it('Should fail with an invalid reason', async function () {
104 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
105 const fields = { reason: 'a'.repeat(305) }
107 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
110 it('Should fail to unfederate a remote video', async function () {
111 const path = basePath + remoteVideoUUID + '/blacklist'
112 const fields = { unfederate: true }
114 await makePostBodyRequest({
117 token: servers[0].accessToken,
119 expectedStatus: HttpStatusCode.CONFLICT_409
123 it('Should succeed with the correct params', async function () {
124 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
127 await makePostBodyRequest({
130 token: servers[0].accessToken,
132 expectedStatus: HttpStatusCode.NO_CONTENT_204
137 describe('When updating a video in blacklist', function () {
138 const basePath = '/api/v1/videos/'
140 it('Should fail with a wrong video', async function () {
141 const wrongPath = '/api/v1/videos/blabla/blacklist'
143 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
146 it('Should fail with a video not blacklisted', async function () {
147 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
149 await makePutBodyRequest({
152 token: servers[0].accessToken,
154 expectedStatus: HttpStatusCode.NOT_FOUND_404
158 it('Should fail with a non authenticated user', async function () {
159 const path = basePath + servers[0].store.videoCreated + '/blacklist'
161 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
164 it('Should fail with a non admin user', async function () {
165 const path = basePath + servers[0].store.videoCreated + '/blacklist'
167 await makePutBodyRequest({
170 token: userAccessToken2,
172 expectedStatus: HttpStatusCode.FORBIDDEN_403
176 it('Should fail with an invalid reason', async function () {
177 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
178 const fields = { reason: 'a'.repeat(305) }
180 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
183 it('Should succeed with the correct params', async function () {
184 const path = basePath + servers[0].store.videoCreated.shortUUID + '/blacklist'
185 const fields = { reason: 'hello' }
187 await makePutBodyRequest({
190 token: servers[0].accessToken,
192 expectedStatus: HttpStatusCode.NO_CONTENT_204
197 describe('When getting blacklisted video', function () {
199 it('Should fail with a non authenticated user', async function () {
200 await servers[0].videos.get({ id: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
203 it('Should fail with another user', async function () {
204 await servers[0].videos.getWithToken({
205 token: userAccessToken2,
206 id: servers[0].store.videoCreated.uuid,
207 expectedStatus: HttpStatusCode.FORBIDDEN_403
211 it('Should succeed with the owner authenticated user', async function () {
212 const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.videoCreated.uuid })
213 expect(video.blacklisted).to.be.true
216 it('Should succeed with an admin', async function () {
217 const video = servers[0].store.videoCreated
219 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
220 const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 })
221 expect(video.blacklisted).to.be.true
226 describe('When removing a video in blacklist', function () {
228 it('Should fail with a non authenticated user', async function () {
229 await command.remove({
231 videoId: servers[0].store.videoCreated.uuid,
232 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
236 it('Should fail with a non admin user', async function () {
237 await command.remove({
238 token: userAccessToken2,
239 videoId: servers[0].store.videoCreated.uuid,
240 expectedStatus: HttpStatusCode.FORBIDDEN_403
244 it('Should fail with an incorrect id', async function () {
245 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
248 it('Should fail with a not blacklisted video', async function () {
249 // The video was not added to the blacklist so it should fail
250 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
253 it('Should succeed with the correct params', async function () {
254 await command.remove({ videoId: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
258 describe('When listing videos in blacklist', function () {
259 const basePath = '/api/v1/videos/blacklist/'
261 it('Should fail with a non authenticated user', async function () {
262 await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
265 it('Should fail with a non admin user', async function () {
266 await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
269 it('Should fail with a bad start pagination', async function () {
270 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
273 it('Should fail with a bad count pagination', async function () {
274 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
277 it('Should fail with an incorrect sort', async function () {
278 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
281 it('Should fail with an invalid type', async function () {
282 await servers[0].blacklist.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
285 it('Should succeed with the correct parameters', async function () {
286 await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL })
290 after(async function () {
291 await cleanupTests(servers)