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