]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
792dbaf0 GS |
2 | |
3 | import 'mocha' | |
e3d15a6a | 4 | import { expect } from 'chai' |
792dbaf0 | 5 | import { |
e3d15a6a C |
6 | BlacklistCommand, |
7 | checkBadCountPagination, | |
8 | checkBadSortPagination, | |
9 | checkBadStartPagination, | |
7c3b7976 | 10 | cleanupTests, |
254d3579 | 11 | createMultipleServers, |
4c7e60bc | 12 | doubleFollow, |
26b7305a C |
13 | makePostBodyRequest, |
14 | makePutBodyRequest, | |
254d3579 | 15 | PeerTubeServer, |
26b7305a | 16 | setAccessTokensToServers, |
a1587156 | 17 | waitJobs |
bf54587a | 18 | } from '@shared/server-commands' |
4c7e60bc | 19 | import { HttpStatusCode, VideoBlacklistType } from '@shared/models' |
792dbaf0 GS |
20 | |
21 | describe('Test video blacklist API validators', function () { | |
254d3579 | 22 | let servers: PeerTubeServer[] |
d23dd9fb | 23 | let notBlacklistedVideoId: string |
5abb9fbb | 24 | let remoteVideoUUID: string |
e5e7f7fe C |
25 | let userAccessToken1 = '' |
26 | let userAccessToken2 = '' | |
e3d15a6a | 27 | let command: BlacklistCommand |
792dbaf0 GS |
28 | |
29 | // --------------------------------------------------------------- | |
30 | ||
31 | before(async function () { | |
32 | this.timeout(120000) | |
33 | ||
254d3579 | 34 | servers = await createMultipleServers(2) |
792dbaf0 | 35 | |
5abb9fbb C |
36 | await setAccessTokensToServers(servers) |
37 | await doubleFollow(servers[0], servers[1]) | |
792dbaf0 | 38 | |
e5e7f7fe C |
39 | { |
40 | const username = 'user1' | |
41 | const password = 'my super password' | |
89d241a7 C |
42 | await servers[0].users.create({ username: username, password: password }) |
43 | userAccessToken1 = await servers[0].login.getAccessToken({ username, password }) | |
e5e7f7fe | 44 | } |
792dbaf0 | 45 | |
26b7305a | 46 | { |
e5e7f7fe C |
47 | const username = 'user2' |
48 | const password = 'my super password' | |
89d241a7 C |
49 | await servers[0].users.create({ username: username, password: password }) |
50 | userAccessToken2 = await servers[0].login.getAccessToken({ username, password }) | |
e5e7f7fe C |
51 | } |
52 | ||
53 | { | |
83903cb6 | 54 | servers[0].store.videoCreated = await servers[0].videos.upload({ token: userAccessToken1 }) |
26b7305a C |
55 | } |
56 | ||
57 | { | |
89d241a7 | 58 | const { uuid } = await servers[0].videos.upload() |
d23dd9fb | 59 | notBlacklistedVideoId = uuid |
26b7305a | 60 | } |
5abb9fbb C |
61 | |
62 | { | |
89d241a7 | 63 | const { uuid } = await servers[1].videos.upload() |
d23dd9fb | 64 | remoteVideoUUID = uuid |
5abb9fbb C |
65 | } |
66 | ||
67 | await waitJobs(servers) | |
e3d15a6a | 68 | |
89d241a7 | 69 | command = servers[0].blacklist |
792dbaf0 GS |
70 | }) |
71 | ||
72 | describe('When adding a video in blacklist', function () { | |
73 | const basePath = '/api/v1/videos/' | |
74 | ||
75 | it('Should fail with nothing', async function () { | |
83903cb6 | 76 | const path = basePath + servers[0].store.videoCreated + '/blacklist' |
792dbaf0 | 77 | const fields = {} |
5abb9fbb | 78 | await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) |
792dbaf0 GS |
79 | }) |
80 | ||
81 | it('Should fail with a wrong video', async function () { | |
82 | const wrongPath = '/api/v1/videos/blabla/blacklist' | |
83 | const fields = {} | |
5abb9fbb | 84 | await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields }) |
792dbaf0 GS |
85 | }) |
86 | ||
87 | it('Should fail with a non authenticated user', async function () { | |
83903cb6 | 88 | const path = basePath + servers[0].store.videoCreated + '/blacklist' |
11ba2ab3 | 89 | const fields = {} |
c0e8b12e | 90 | await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
792dbaf0 GS |
91 | }) |
92 | ||
93 | it('Should fail with a non admin user', async function () { | |
83903cb6 | 94 | const path = basePath + servers[0].store.videoCreated + '/blacklist' |
11ba2ab3 | 95 | const fields = {} |
2d53be02 RK |
96 | await makePostBodyRequest({ |
97 | url: servers[0].url, | |
98 | path, | |
99 | token: userAccessToken2, | |
100 | fields, | |
c0e8b12e | 101 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
2d53be02 | 102 | }) |
792dbaf0 GS |
103 | }) |
104 | ||
26b7305a | 105 | it('Should fail with an invalid reason', async function () { |
83903cb6 | 106 | const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist' |
26b7305a C |
107 | const fields = { reason: 'a'.repeat(305) } |
108 | ||
5abb9fbb C |
109 | await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) |
110 | }) | |
111 | ||
112 | it('Should fail to unfederate a remote video', async function () { | |
113 | const path = basePath + remoteVideoUUID + '/blacklist' | |
114 | const fields = { unfederate: true } | |
115 | ||
2d53be02 RK |
116 | await makePostBodyRequest({ |
117 | url: servers[0].url, | |
118 | path, | |
119 | token: servers[0].accessToken, | |
120 | fields, | |
c0e8b12e | 121 | expectedStatus: HttpStatusCode.CONFLICT_409 |
2d53be02 | 122 | }) |
26b7305a C |
123 | }) |
124 | ||
125 | it('Should succeed with the correct params', async function () { | |
83903cb6 | 126 | const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist' |
a1587156 | 127 | const fields = {} |
26b7305a | 128 | |
2d53be02 RK |
129 | await makePostBodyRequest({ |
130 | url: servers[0].url, | |
131 | path, | |
132 | token: servers[0].accessToken, | |
133 | fields, | |
c0e8b12e | 134 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
2d53be02 | 135 | }) |
26b7305a C |
136 | }) |
137 | }) | |
138 | ||
139 | describe('When updating a video in blacklist', function () { | |
140 | const basePath = '/api/v1/videos/' | |
141 | ||
142 | it('Should fail with a wrong video', async function () { | |
143 | const wrongPath = '/api/v1/videos/blabla/blacklist' | |
144 | const fields = {} | |
5abb9fbb | 145 | await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields }) |
26b7305a C |
146 | }) |
147 | ||
148 | it('Should fail with a video not blacklisted', async function () { | |
149 | const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist' | |
150 | const fields = {} | |
2d53be02 RK |
151 | await makePutBodyRequest({ |
152 | url: servers[0].url, | |
153 | path, | |
154 | token: servers[0].accessToken, | |
155 | fields, | |
c0e8b12e | 156 | expectedStatus: HttpStatusCode.NOT_FOUND_404 |
2d53be02 | 157 | }) |
26b7305a C |
158 | }) |
159 | ||
160 | it('Should fail with a non authenticated user', async function () { | |
83903cb6 | 161 | const path = basePath + servers[0].store.videoCreated + '/blacklist' |
26b7305a | 162 | const fields = {} |
c0e8b12e | 163 | await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
26b7305a C |
164 | }) |
165 | ||
166 | it('Should fail with a non admin user', async function () { | |
83903cb6 | 167 | const path = basePath + servers[0].store.videoCreated + '/blacklist' |
11ba2ab3 | 168 | const fields = {} |
2d53be02 RK |
169 | await makePutBodyRequest({ |
170 | url: servers[0].url, | |
171 | path, | |
172 | token: userAccessToken2, | |
173 | fields, | |
c0e8b12e | 174 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
2d53be02 | 175 | }) |
26b7305a C |
176 | }) |
177 | ||
178 | it('Should fail with an invalid reason', async function () { | |
83903cb6 | 179 | const path = basePath + servers[0].store.videoCreated.uuid + '/blacklist' |
26b7305a C |
180 | const fields = { reason: 'a'.repeat(305) } |
181 | ||
5abb9fbb | 182 | await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields }) |
26b7305a C |
183 | }) |
184 | ||
185 | it('Should succeed with the correct params', async function () { | |
83903cb6 | 186 | const path = basePath + servers[0].store.videoCreated.shortUUID + '/blacklist' |
26b7305a C |
187 | const fields = { reason: 'hello' } |
188 | ||
2d53be02 RK |
189 | await makePutBodyRequest({ |
190 | url: servers[0].url, | |
191 | path, | |
192 | token: servers[0].accessToken, | |
193 | fields, | |
c0e8b12e | 194 | expectedStatus: HttpStatusCode.NO_CONTENT_204 |
2d53be02 | 195 | }) |
792dbaf0 GS |
196 | }) |
197 | }) | |
198 | ||
e5e7f7fe C |
199 | describe('When getting blacklisted video', function () { |
200 | ||
201 | it('Should fail with a non authenticated user', async function () { | |
83903cb6 | 202 | await servers[0].videos.get({ id: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
e5e7f7fe C |
203 | }) |
204 | ||
205 | it('Should fail with another user', async function () { | |
89d241a7 | 206 | await servers[0].videos.getWithToken({ |
d23dd9fb | 207 | token: userAccessToken2, |
83903cb6 | 208 | id: servers[0].store.videoCreated.uuid, |
d23dd9fb C |
209 | expectedStatus: HttpStatusCode.FORBIDDEN_403 |
210 | }) | |
e5e7f7fe C |
211 | }) |
212 | ||
213 | it('Should succeed with the owner authenticated user', async function () { | |
83903cb6 | 214 | const video = await servers[0].videos.getWithToken({ token: userAccessToken1, id: servers[0].store.videoCreated.uuid }) |
e5e7f7fe C |
215 | expect(video.blacklisted).to.be.true |
216 | }) | |
217 | ||
218 | it('Should succeed with an admin', async function () { | |
83903cb6 | 219 | const video = servers[0].store.videoCreated |
e5e7f7fe | 220 | |
d4a8e7a6 | 221 | for (const id of [ video.id, video.uuid, video.shortUUID ]) { |
89d241a7 | 222 | const video = await servers[0].videos.getWithToken({ id, expectedStatus: HttpStatusCode.OK_200 }) |
d4a8e7a6 C |
223 | expect(video.blacklisted).to.be.true |
224 | } | |
e5e7f7fe C |
225 | }) |
226 | }) | |
227 | ||
792dbaf0 | 228 | describe('When removing a video in blacklist', function () { |
e3d15a6a | 229 | |
792dbaf0 | 230 | it('Should fail with a non authenticated user', async function () { |
83903cb6 C |
231 | await command.remove({ |
232 | token: 'fake token', | |
233 | videoId: servers[0].store.videoCreated.uuid, | |
234 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 | |
235 | }) | |
792dbaf0 GS |
236 | }) |
237 | ||
238 | it('Should fail with a non admin user', async function () { | |
83903cb6 C |
239 | await command.remove({ |
240 | token: userAccessToken2, | |
241 | videoId: servers[0].store.videoCreated.uuid, | |
242 | expectedStatus: HttpStatusCode.FORBIDDEN_403 | |
243 | }) | |
792dbaf0 GS |
244 | }) |
245 | ||
246 | it('Should fail with an incorrect id', async function () { | |
e3d15a6a | 247 | await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
792dbaf0 GS |
248 | }) |
249 | ||
250 | it('Should fail with a not blacklisted video', async function () { | |
251 | // The video was not added to the blacklist so it should fail | |
e3d15a6a | 252 | await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
26b7305a C |
253 | }) |
254 | ||
255 | it('Should succeed with the correct params', async function () { | |
83903cb6 | 256 | await command.remove({ videoId: servers[0].store.videoCreated.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 }) |
792dbaf0 GS |
257 | }) |
258 | }) | |
259 | ||
260 | describe('When listing videos in blacklist', function () { | |
35bf0c83 | 261 | const basePath = '/api/v1/videos/blacklist/' |
792dbaf0 GS |
262 | |
263 | it('Should fail with a non authenticated user', async function () { | |
89d241a7 | 264 | await servers[0].blacklist.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
792dbaf0 GS |
265 | }) |
266 | ||
267 | it('Should fail with a non admin user', async function () { | |
89d241a7 | 268 | await servers[0].blacklist.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) |
792dbaf0 GS |
269 | }) |
270 | ||
271 | it('Should fail with a bad start pagination', async function () { | |
5abb9fbb | 272 | await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken) |
792dbaf0 GS |
273 | }) |
274 | ||
275 | it('Should fail with a bad count pagination', async function () { | |
5abb9fbb | 276 | await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken) |
792dbaf0 GS |
277 | }) |
278 | ||
279 | it('Should fail with an incorrect sort', async function () { | |
5abb9fbb | 280 | await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken) |
792dbaf0 | 281 | }) |
7ccddd7b JM |
282 | |
283 | it('Should fail with an invalid type', async function () { | |
89d241a7 | 284 | await servers[0].blacklist.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
7ccddd7b JM |
285 | }) |
286 | ||
287 | it('Should succeed with the correct parameters', async function () { | |
89d241a7 | 288 | await servers[0].blacklist.list({ type: VideoBlacklistType.MANUAL }) |
7ccddd7b | 289 | }) |
792dbaf0 GS |
290 | }) |
291 | ||
7c3b7976 C |
292 | after(async function () { |
293 | await cleanupTests(servers) | |
792dbaf0 GS |
294 | }) |
295 | }) |