6b82643f4fbcbfa2e9f74f144e224ee9f98025a6
[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   getVideo,
12   getVideoWithToken,
13   killallServers,
14   makePostBodyRequest,
15   makePutBodyRequest,
16   removeVideoFromBlacklist,
17   ServerInfo,
18   setAccessTokensToServers,
19   uploadVideo,
20   userLogin, waitJobs
21 } from '../../../../shared/utils'
22 import {
23   checkBadCountPagination,
24   checkBadSortPagination,
25   checkBadStartPagination
26 } from '../../../../shared/utils/requests/check-api-params'
27 import { 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     await flushTests()
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(servers[0].url, servers[0].accessToken, username, 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(servers[0].url, servers[0].accessToken, username, 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(servers[0].url, 'fake token', 401)
224     })
225
226     it('Should fail with a non admin user', async function () {
227       await getBlacklistedVideosList(servers[0].url, userAccessToken2, 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
243   after(async function () {
244     killallServers(servers)
245
246     // Keep the logs if the test failed
247     if (this['ok']) {
248       await flushTests()
249     }
250   })
251 })