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