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