1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '@shared/models'
8 checkBadCountPagination,
9 checkBadSortPagination,
10 checkBadStartPagination,
13 createMultipleServers,
17 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { VideoBlacklistType } from '@shared/models'
22 describe('Test video blacklist API validators', function () {
23 let servers: PeerTubeServer[]
24 let notBlacklistedVideoId: string
25 let remoteVideoUUID: string
26 let userAccessToken1 = ''
27 let userAccessToken2 = ''
28 let command: BlacklistCommand
30 // ---------------------------------------------------------------
32 before(async function () {
35 servers = await createMultipleServers(2)
37 await setAccessTokensToServers(servers)
38 await doubleFollow(servers[0], servers[1])
41 const username = 'user1'
42 const password = 'my super password'
43 await servers[0].users.create({ username: username, password: password })
44 userAccessToken1 = await servers[0].login.getAccessToken({ username, password })
48 const username = 'user2'
49 const password = 'my super password'
50 await servers[0].users.create({ username: username, password: password })
51 userAccessToken2 = await servers[0].login.getAccessToken({ username, password })
55 servers[0].store.video = await servers[0].videos.upload({ token: userAccessToken1 })
59 const { uuid } = await servers[0].videos.upload()
60 notBlacklistedVideoId = uuid
64 const { uuid } = await servers[1].videos.upload()
65 remoteVideoUUID = uuid
68 await waitJobs(servers)
70 command = servers[0].blacklist
73 describe('When adding a video in blacklist', function () {
74 const basePath = '/api/v1/videos/'
76 it('Should fail with nothing', async function () {
77 const path = basePath + servers[0].store.video + '/blacklist'
79 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
82 it('Should fail with a wrong video', async function () {
83 const wrongPath = '/api/v1/videos/blabla/blacklist'
85 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
88 it('Should fail with a non authenticated user', async function () {
89 const path = basePath + servers[0].store.video + '/blacklist'
91 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
94 it('Should fail with a non admin user', async function () {
95 const path = basePath + servers[0].store.video + '/blacklist'
97 await makePostBodyRequest({
100 token: userAccessToken2,
102 expectedStatus: HttpStatusCode.FORBIDDEN_403
106 it('Should fail with an invalid reason', async function () {
107 const path = basePath + servers[0].store.video.uuid + '/blacklist'
108 const fields = { reason: 'a'.repeat(305) }
110 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
113 it('Should fail to unfederate a remote video', async function () {
114 const path = basePath + remoteVideoUUID + '/blacklist'
115 const fields = { unfederate: true }
117 await makePostBodyRequest({
120 token: servers[0].accessToken,
122 expectedStatus: HttpStatusCode.CONFLICT_409
126 it('Should succeed with the correct params', async function () {
127 const path = basePath + servers[0].store.video.uuid + '/blacklist'
130 await makePostBodyRequest({
133 token: servers[0].accessToken,
135 expectedStatus: HttpStatusCode.NO_CONTENT_204
140 describe('When updating a video in blacklist', function () {
141 const basePath = '/api/v1/videos/'
143 it('Should fail with a wrong video', async function () {
144 const wrongPath = '/api/v1/videos/blabla/blacklist'
146 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
149 it('Should fail with a video not blacklisted', async function () {
150 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
152 await makePutBodyRequest({
155 token: servers[0].accessToken,
157 expectedStatus: HttpStatusCode.NOT_FOUND_404
161 it('Should fail with a non authenticated user', async function () {
162 const path = basePath + servers[0].store.video + '/blacklist'
164 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
167 it('Should fail with a non admin user', async function () {
168 const path = basePath + servers[0].store.video + '/blacklist'
170 await makePutBodyRequest({
173 token: userAccessToken2,
175 expectedStatus: HttpStatusCode.FORBIDDEN_403
179 it('Should fail with an invalid reason', async function () {
180 const path = basePath + servers[0].store.video.uuid + '/blacklist'
181 const fields = { reason: 'a'.repeat(305) }
183 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
186 it('Should succeed with the correct params', async function () {
187 const path = basePath + servers[0].store.video.shortUUID + '/blacklist'
188 const fields = { reason: 'hello' }
190 await makePutBodyRequest({
193 token: servers[0].accessToken,
195 expectedStatus: HttpStatusCode.NO_CONTENT_204
200 describe('When getting blacklisted video', function () {
202 it('Should fail with a non authenticated user', async function () {
203 await servers[0].videos.get({ id: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
206 it('Should fail with another user', async function () {
207 await servers[0].videos.getWithToken({
208 token: userAccessToken2,
209 id: servers[0].store.video.uuid,
210 expectedStatus: HttpStatusCode.FORBIDDEN_403
214 it('Should succeed with the owner authenticated user', async function () {
215 const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.video.uuid })
216 expect(video.blacklisted).to.be.true
219 it('Should succeed with an admin', async function () {
220 const video = servers[0].store.video
222 for (const id of [ video.id, video.uuid, video.shortUUID ]) {
223 const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 })
224 expect(video.blacklisted).to.be.true
229 describe('When removing a video in blacklist', function () {
231 it('Should fail with a non authenticated user', async function () {
232 await command.remove({ token: 'fake token', videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
235 it('Should fail with a non admin user', async function () {
236 await command.remove({ token: userAccessToken2, videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
239 it('Should fail with an incorrect id', async function () {
240 await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
243 it('Should fail with a not blacklisted video', async function () {
244 // The video was not added to the blacklist so it should fail
245 await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
248 it('Should succeed with the correct params', async function () {
249 await command.remove({ videoId: servers[0].store.video.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 })
253 describe('When listing videos in blacklist', function () {
254 const basePath = '/api/v1/videos/blacklist/'
256 it('Should fail with a non authenticated user', async function () {
257 await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
260 it('Should fail with a non admin user', async function () {
261 await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
264 it('Should fail with a bad start pagination', async function () {
265 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
268 it('Should fail with a bad count pagination', async function () {
269 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
272 it('Should fail with an incorrect sort', async function () {
273 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
276 it('Should fail with an invalid type', async function () {
277 await servers[0].blacklist.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
280 it('Should succeed with the correct parameters', async function () {
281 await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL })
285 after(async function () {
286 await cleanupTests(servers)