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