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