]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-abuses.ts
e2813d470cf5c8ef6b0a48e55b3e9d224331de62
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-abuses.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6 createUser,
7 deleteVideoAbuse,
8 flushTests,
9 killallServers,
10 makeGetRequest,
11 makePostBodyRequest,
12 flushAndRunServer,
13 ServerInfo,
14 setAccessTokensToServers,
15 updateVideoAbuse,
16 uploadVideo,
17 userLogin
18 } from '../../../../shared/extra-utils'
19 import {
20 checkBadCountPagination,
21 checkBadSortPagination,
22 checkBadStartPagination
23 } from '../../../../shared/extra-utils/requests/check-api-params'
24 import { VideoAbuseState } from '../../../../shared/models/videos'
25
26 describe('Test video abuses API validators', function () {
27 let server: ServerInfo
28 let userAccessToken = ''
29 let videoAbuseId: number
30
31 // ---------------------------------------------------------------
32
33 before(async function () {
34 this.timeout(30000)
35
36 server = await flushAndRunServer(1)
37
38 await setAccessTokensToServers([ server ])
39
40 const username = 'user1'
41 const password = 'my super password'
42 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
43 userAccessToken = await userLogin(server, { username, password })
44
45 const res = await uploadVideo(server.url, server.accessToken, {})
46 server.video = res.body.video
47 })
48
49 describe('When listing video abuses', function () {
50 const path = '/api/v1/videos/abuse'
51
52 it('Should fail with a bad start pagination', async function () {
53 await checkBadStartPagination(server.url, path, server.accessToken)
54 })
55
56 it('Should fail with a bad count pagination', async function () {
57 await checkBadCountPagination(server.url, path, server.accessToken)
58 })
59
60 it('Should fail with an incorrect sort', async function () {
61 await checkBadSortPagination(server.url, path, server.accessToken)
62 })
63
64 it('Should fail with a non authenticated user', async function () {
65 await makeGetRequest({
66 url: server.url,
67 path,
68 statusCodeExpected: 401
69 })
70 })
71
72 it('Should fail with a non admin user', async function () {
73 await makeGetRequest({
74 url: server.url,
75 path,
76 token: userAccessToken,
77 statusCodeExpected: 403
78 })
79 })
80 })
81
82 describe('When reporting a video abuse', function () {
83 const basePath = '/api/v1/videos/'
84 let path: string
85
86 before(() => {
87 path = basePath + server.video.id + '/abuse'
88 })
89
90 it('Should fail with nothing', async function () {
91 const fields = {}
92 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
93 })
94
95 it('Should fail with a wrong video', async function () {
96 const wrongPath = '/api/v1/videos/blabla/abuse'
97 const fields = { reason: 'my super reason' }
98
99 await makePostBodyRequest({ url: server.url, path: wrongPath, token: server.accessToken, fields })
100 })
101
102 it('Should fail with a non authenticated user', async function () {
103 const fields = { reason: 'my super reason' }
104
105 await makePostBodyRequest({ url: server.url, path, token: 'hello', fields, statusCodeExpected: 401 })
106 })
107
108 it('Should fail with a reason too short', async function () {
109 const fields = { reason: 'h' }
110
111 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
112 })
113
114 it('Should fail with a too big reason', async function () {
115 const fields = { reason: 'super'.repeat(605) }
116
117 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
118 })
119
120 it('Should succeed with the correct parameters', async function () {
121 const fields = { reason: 'super reason' }
122
123 const res = await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 200 })
124 videoAbuseId = res.body.videoAbuse.id
125 })
126 })
127
128 describe('When updating a video abuse', function () {
129 const basePath = '/api/v1/videos/'
130 let path: string
131
132 before(() => {
133 path = basePath + server.video.id + '/abuse/' + videoAbuseId
134 })
135
136 it('Should fail with a non authenticated user', async function () {
137 await updateVideoAbuse(server.url, 'blabla', server.video.uuid, videoAbuseId, {}, 401)
138 })
139
140 it('Should fail with a non admin user', async function () {
141 await updateVideoAbuse(server.url, userAccessToken, server.video.uuid, videoAbuseId, {}, 403)
142 })
143
144 it('Should fail with a bad video id or bad video abuse id', async function () {
145 await updateVideoAbuse(server.url, server.accessToken, server.video.uuid, 45, {}, 404)
146 await updateVideoAbuse(server.url, server.accessToken, 52, videoAbuseId, {}, 404)
147 })
148
149 it('Should fail with a bad state', async function () {
150 const body = { state: 5 }
151 await updateVideoAbuse(server.url, server.accessToken, server.video.uuid, videoAbuseId, body, 400)
152 })
153
154 it('Should fail with a bad moderation comment', async function () {
155 const body = { moderationComment: 'b'.repeat(3001) }
156 await updateVideoAbuse(server.url, server.accessToken, server.video.uuid, videoAbuseId, body, 400)
157 })
158
159 it('Should succeed with the correct params', async function () {
160 const body = { state: VideoAbuseState.ACCEPTED }
161 await updateVideoAbuse(server.url, server.accessToken, server.video.uuid, videoAbuseId, body)
162 })
163 })
164
165 describe('When deleting a video abuse', function () {
166 const basePath = '/api/v1/videos/'
167 let path: string
168
169 before(() => {
170 path = basePath + server.video.id + '/abuse/' + videoAbuseId
171 })
172
173 it('Should fail with a non authenticated user', async function () {
174 await deleteVideoAbuse(server.url, 'blabla', server.video.uuid, videoAbuseId, 401)
175 })
176
177 it('Should fail with a non admin user', async function () {
178 await deleteVideoAbuse(server.url, userAccessToken, server.video.uuid, videoAbuseId, 403)
179 })
180
181 it('Should fail with a bad video id or bad video abuse id', async function () {
182 await deleteVideoAbuse(server.url, server.accessToken, server.video.uuid, 45, 404)
183 await deleteVideoAbuse(server.url, server.accessToken, 52, videoAbuseId, 404)
184 })
185
186 it('Should succeed with the correct params', async function () {
187 await deleteVideoAbuse(server.url, server.accessToken, server.video.uuid, videoAbuseId)
188 })
189 })
190
191 after(function () {
192 killallServers([ server ])
193 })
194 })