1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { HttpStatusCode, VideoBlacklistType } from '@shared/models'
14 setAccessTokensToServers,
16 } from '@shared/server-commands'
18 describe('Test video blacklist API validators', function () {
19 let servers: PeerTubeServer[]
20 let notBlacklistedVideoId: string
21 let remoteVideoUUID: string
22 let userAccessToken1 = ''
23 let userAccessToken2 = ''
24 let command: BlacklistCommand
26 // ---------------------------------------------------------------
28 before(async function () {
31 servers = await createMultipleServers(2)
33 await setAccessTokensToServers(servers)
34 await doubleFollow(servers[0], servers[1])
37 const username = 'user1'
38 const password = 'my super password'
39 await servers[0].users.create({ username, password })
40 userAccessToken1 = await servers[0].login.getAccessToken({ username, password })
44 const username = 'user2'
45 const password = 'my super password'
46 await servers[0].users.create({ username, password })
47 userAccessToken2 = await servers[0].login.getAccessToken({ username, password })
51 servers[0].store.videoCreated = await servers[0].videos.upload({ token: userAccessToken1 })
55 const { uuid } = await servers[0].videos.upload()
56 notBlacklistedVideoId = uuid
60 const { uuid } = await servers[1].videos.upload()
61 remoteVideoUUID = uuid
64 await waitJobs(servers)
66 command = servers[0].blacklist
69 describe('When adding a video in blacklist', function () {
70 const basePath = '/api/v1/videos/'
72 it('Should fail with nothing', async function () {
73 const path = basePath + servers[0].store.videoCreated + '/blacklist'
75 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
78 it('Should fail with a wrong video', async function () {
79 const wrongPath = '/api/v1/videos/blabla/blacklist'
81 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
84 it('Should fail with a non authenticated user', async function () {
85 const path = basePath + servers[0].store.videoCreated + '/blacklist'
87 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
90 it('Should fail with a non admin user', async function () {
91 const path = basePath + servers[0].store.videoCreated + '/blacklist'
93 await makePostBodyRequest({
96 token: userAccessToken2,
98 expectedStatus: HttpStatusCode.FORBIDDEN_403
102 it('Should fail with an invalid reason', async function () {
103 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
104 const fields = { reason: 'a'.repeat(305) }
106 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
109 it('Should fail to unfederate a remote video', async function () {
110 const path = basePath + remoteVideoUUID + '/blacklist'
111 const fields = { unfederate: true }
113 await makePostBodyRequest({
116 token: servers[0].accessToken,
118 expectedStatus: HttpStatusCode.CONFLICT_409
122 it('Should succeed with the correct params', async function () {
123 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
126 await makePostBodyRequest({
129 token: servers[0].accessToken,
131 expectedStatus: HttpStatusCode.NO_CONTENT_204
136 describe('When updating a video in blacklist', function () {
137 const basePath = '/api/v1/videos/'
139 it('Should fail with a wrong video', async function () {
140 const wrongPath = '/api/v1/videos/blabla/blacklist'
142 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
145 it('Should fail with a video not blacklisted', async function () {
146 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
148 await makePutBodyRequest({
151 token: servers[0].accessToken,
153 expectedStatus: HttpStatusCode.NOT_FOUND_404
157 it('Should fail with a non authenticated user', async function () {
158 const path = basePath + servers[0].store.videoCreated + '/blacklist'
160 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
163 it('Should fail with a non admin user', async function () {
164 const path = basePath + servers[0].store.videoCreated + '/blacklist'
166 await makePutBodyRequest({
169 token: userAccessToken2,
171 expectedStatus: HttpStatusCode.FORBIDDEN_403
175 it('Should fail with an invalid reason', async function () {
176 const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist'
177 const fields = { reason: 'a'.repeat(305) }
179 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
182 it('Should succeed with the correct params', async function () {
183 const path = basePath + servers[0].store.videoCreated.shortUUID + '/blacklist'
184 const fields = { reason: 'hello' }
186 await makePutBodyRequest({
189 token: servers[0].accessToken,
191 expectedStatus: HttpStatusCode.NO_CONTENT_204
196 describe('When getting blacklisted video', function () {
198 it('Should fail with a non authenticated user', async function () {
199 await servers[0].videos.get({ id: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
202 it('Should fail with another user', async function () {
203 await servers[0].videos.getWithToken({
204 token: userAccessToken2,
205 id: servers[0].store.videoCreated.uuid,
206 expectedStatus: HttpStatusCode.FORBIDDEN_403
210 it('Should succeed with the owner authenticated user', async function () {
211 const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.videoCreated.uuid })
212 expect(video.blacklisted).to.be.true
215 it('Should succeed with an admin', async function () {
216 const video = servers[0].store.videoCreated
218 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
219 const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 })
220 expect(video.blacklisted).to.be.true
225 describe('When removing a video in blacklist', function () {
227 it('Should fail with a non authenticated user', async function () {
228 await command.remove({
230 videoId: servers[0].store.videoCreated.uuid,
231 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
235 it('Should fail with a non admin user', async function () {
236 await command.remove({
237 token: userAccessToken2,
238 videoId: servers[0].store.videoCreated.uuid,
239 expectedStatus: HttpStatusCode.FORBIDDEN_403
243 it('Should fail with an incorrect id', async function () {
244 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
247 it('Should fail with a not blacklisted video', async function () {
248 // The video was not added to the blacklist so it should fail
249 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
252 it('Should succeed with the correct params', async function () {
253 await command.remove({ videoId: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
257 describe('When listing videos in blacklist', function () {
258 const basePath = '/api/v1/videos/blacklist/'
260 it('Should fail with a non authenticated user', async function () {
261 await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
264 it('Should fail with a non admin user', async function () {
265 await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
268 it('Should fail with a bad start pagination', async function () {
269 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
272 it('Should fail with a bad count pagination', async function () {
273 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
276 it('Should fail with an incorrect sort', async function () {
277 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
280 it('Should fail with an invalid type', async function () {
281 await servers[0].blacklist.list({ type: 0 as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
284 it('Should succeed with the correct parameters', async function () {
285 await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL })
289 after(async function () {
290 await cleanupTests(servers)