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