]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-blacklist.ts
rename blacklist to block/blocklist, merge block and auto-block views
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-blacklist.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4
5 import {
6 cleanupTests,
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 getBlacklistedVideosList,
11 getVideo,
12 getVideoWithToken,
13 makePostBodyRequest,
14 makePutBodyRequest,
15 removeVideoFromBlacklist,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideo,
19 userLogin,
20 waitJobs
21 } from '../../../../shared/extra-utils'
22 import {
23 checkBadCountPagination,
24 checkBadSortPagination,
25 checkBadStartPagination
26 } from '../../../../shared/extra-utils/requests/check-api-params'
27 import { VideoBlockType, VideoDetails } from '../../../../shared/models/videos'
28 import { expect } from 'chai'
29
30 describe('Test video blacklist API validators', function () {
31 let servers: ServerInfo[]
32 let notBlacklistedVideoId: number
33 let remoteVideoUUID: string
34 let userAccessToken1 = ''
35 let userAccessToken2 = ''
36
37 // ---------------------------------------------------------------
38
39 before(async function () {
40 this.timeout(120000)
41
42 servers = await flushAndRunMultipleServers(2)
43
44 await setAccessTokensToServers(servers)
45 await doubleFollow(servers[0], servers[1])
46
47 {
48 const username = 'user1'
49 const password = 'my super password'
50 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
51 userAccessToken1 = await userLogin(servers[0], { username, password })
52 }
53
54 {
55 const username = 'user2'
56 const password = 'my super password'
57 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
58 userAccessToken2 = await userLogin(servers[0], { username, password })
59 }
60
61 {
62 const res = await uploadVideo(servers[0].url, userAccessToken1, {})
63 servers[0].video = res.body.video
64 }
65
66 {
67 const res = await uploadVideo(servers[0].url, servers[0].accessToken, {})
68 notBlacklistedVideoId = res.body.video.uuid
69 }
70
71 {
72 const res = await uploadVideo(servers[1].url, servers[1].accessToken, {})
73 remoteVideoUUID = res.body.video.uuid
74 }
75
76 await waitJobs(servers)
77 })
78
79 describe('When adding a video in blacklist', function () {
80 const basePath = '/api/v1/videos/'
81
82 it('Should fail with nothing', async function () {
83 const path = basePath + servers[0].video + '/blacklist'
84 const fields = {}
85 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
86 })
87
88 it('Should fail with a wrong video', async function () {
89 const wrongPath = '/api/v1/videos/blabla/blacklist'
90 const fields = {}
91 await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
92 })
93
94 it('Should fail with a non authenticated user', async function () {
95 const path = basePath + servers[0].video + '/blacklist'
96 const fields = {}
97 await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: 401 })
98 })
99
100 it('Should fail with a non admin user', async function () {
101 const path = basePath + servers[0].video + '/blacklist'
102 const fields = {}
103 await makePostBodyRequest({ url: servers[0].url, path, token: userAccessToken2, fields, statusCodeExpected: 403 })
104 })
105
106 it('Should fail with an invalid reason', async function () {
107 const path = basePath + servers[0].video.uuid + '/blacklist'
108 const fields = { reason: 'a'.repeat(305) }
109
110 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
111 })
112
113 it('Should fail to unfederate a remote video', async function () {
114 const path = basePath + remoteVideoUUID + '/blacklist'
115 const fields = { unfederate: true }
116
117 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields, statusCodeExpected: 409 })
118 })
119
120 it('Should succeed with the correct params', async function () {
121 const path = basePath + servers[0].video.uuid + '/blacklist'
122 const fields = {}
123
124 await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields, statusCodeExpected: 204 })
125 })
126 })
127
128 describe('When updating a video in blacklist', function () {
129 const basePath = '/api/v1/videos/'
130
131 it('Should fail with a wrong video', async function () {
132 const wrongPath = '/api/v1/videos/blabla/blacklist'
133 const fields = {}
134 await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
135 })
136
137 it('Should fail with a video not blacklisted', async function () {
138 const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
139 const fields = {}
140 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields, statusCodeExpected: 404 })
141 })
142
143 it('Should fail with a non authenticated user', async function () {
144 const path = basePath + servers[0].video + '/blacklist'
145 const fields = {}
146 await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: 401 })
147 })
148
149 it('Should fail with a non admin user', async function () {
150 const path = basePath + servers[0].video + '/blacklist'
151 const fields = {}
152 await makePutBodyRequest({ url: servers[0].url, path, token: userAccessToken2, fields, statusCodeExpected: 403 })
153 })
154
155 it('Should fail with an invalid reason', async function () {
156 const path = basePath + servers[0].video.uuid + '/blacklist'
157 const fields = { reason: 'a'.repeat(305) }
158
159 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
160 })
161
162 it('Should succeed with the correct params', async function () {
163 const path = basePath + servers[0].video.uuid + '/blacklist'
164 const fields = { reason: 'hello' }
165
166 await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields, statusCodeExpected: 204 })
167 })
168 })
169
170 describe('When getting blacklisted video', function () {
171
172 it('Should fail with a non authenticated user', async function () {
173 await getVideo(servers[0].url, servers[0].video.uuid, 401)
174 })
175
176 it('Should fail with another user', async function () {
177 await getVideoWithToken(servers[0].url, userAccessToken2, servers[0].video.uuid, 403)
178 })
179
180 it('Should succeed with the owner authenticated user', async function () {
181 const res = await getVideoWithToken(servers[0].url, userAccessToken1, servers[0].video.uuid, 200)
182 const video: VideoDetails = res.body
183
184 expect(video.blacklisted).to.be.true
185 })
186
187 it('Should succeed with an admin', async function () {
188 const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, servers[0].video.uuid, 200)
189 const video: VideoDetails = res.body
190
191 expect(video.blacklisted).to.be.true
192 })
193 })
194
195 describe('When removing a video in blacklist', function () {
196 it('Should fail with a non authenticated user', async function () {
197 await removeVideoFromBlacklist(servers[0].url, 'fake token', servers[0].video.uuid, 401)
198 })
199
200 it('Should fail with a non admin user', async function () {
201 await removeVideoFromBlacklist(servers[0].url, userAccessToken2, servers[0].video.uuid, 403)
202 })
203
204 it('Should fail with an incorrect id', async function () {
205 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 'hello', 400)
206 })
207
208 it('Should fail with a not blacklisted video', async function () {
209 // The video was not added to the blacklist so it should fail
210 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, notBlacklistedVideoId, 404)
211 })
212
213 it('Should succeed with the correct params', async function () {
214 await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, servers[0].video.uuid, 204)
215 })
216 })
217
218 describe('When listing videos in blacklist', function () {
219 const basePath = '/api/v1/videos/blacklist/'
220
221 it('Should fail with a non authenticated user', async function () {
222 await getBlacklistedVideosList({ url: servers[0].url, token: 'fake token', specialStatus: 401 })
223 })
224
225 it('Should fail with a non admin user', async function () {
226 await getBlacklistedVideosList({ url: servers[0].url, token: userAccessToken2, specialStatus: 403 })
227 })
228
229 it('Should fail with a bad start pagination', async function () {
230 await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
231 })
232
233 it('Should fail with a bad count pagination', async function () {
234 await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
235 })
236
237 it('Should fail with an incorrect sort', async function () {
238 await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
239 })
240
241 it('Should fail with an invalid type', async function () {
242 await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: 0, specialStatus: 400 })
243 })
244
245 it('Should succeed with the correct parameters', async function () {
246 await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: VideoBlockType.MANUAL })
247 })
248 })
249
250 after(async function () {
251 await cleanupTests(servers)
252 })
253 })