]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/video-comments.ts
bb14abbfd2c77e6c2a19efd2ba3e30ee12297435
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / video-comments.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 createUser,
7 flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePostBodyRequest, flushAndRunServer, ServerInfo, setAccessTokensToServers,
8 uploadVideo, userLogin
9 } from '../../../../shared/extra-utils'
10 import {
11 checkBadCountPagination,
12 checkBadSortPagination,
13 checkBadStartPagination
14 } from '../../../../shared/extra-utils/requests/check-api-params'
15 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
16
17 const expect = chai.expect
18
19 describe('Test video comments API validator', function () {
20 let pathThread: string
21 let pathComment: string
22 let server: ServerInfo
23 let videoUUID: string
24 let userAccessToken: string
25 let commentId: number
26
27 // ---------------------------------------------------------------
28
29 before(async function () {
30 this.timeout(30000)
31
32 server = await flushAndRunServer(1)
33
34 await setAccessTokensToServers([ server ])
35
36 {
37 const res = await uploadVideo(server.url, server.accessToken, {})
38 videoUUID = res.body.video.uuid
39 pathThread = '/api/v1/videos/' + videoUUID + '/comment-threads'
40 }
41
42 {
43 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, 'coucou')
44 commentId = res.body.comment.id
45 pathComment = '/api/v1/videos/' + videoUUID + '/comments/' + commentId
46 }
47
48 {
49 const user = {
50 username: 'user1',
51 password: 'my super password'
52 }
53 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
54 userAccessToken = await userLogin(server, user)
55 }
56 })
57
58 describe('When listing video comment threads', function () {
59 it('Should fail with a bad start pagination', async function () {
60 await checkBadStartPagination(server.url, pathThread, server.accessToken)
61 })
62
63 it('Should fail with a bad count pagination', async function () {
64 await checkBadCountPagination(server.url, pathThread, server.accessToken)
65 })
66
67 it('Should fail with an incorrect sort', async function () {
68 await checkBadSortPagination(server.url, pathThread, server.accessToken)
69 })
70
71 it('Should fail with an incorrect video', async function () {
72 await makeGetRequest({
73 url: server.url,
74 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads',
75 statusCodeExpected: 404
76 })
77 })
78 })
79
80 describe('When listing comments of a thread', function () {
81 it('Should fail with an incorrect video', async function () {
82 await makeGetRequest({
83 url: server.url,
84 path: '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads/' + commentId,
85 statusCodeExpected: 404
86 })
87 })
88
89 it('Should fail with an incorrect thread id', async function () {
90 await makeGetRequest({
91 url: server.url,
92 path: '/api/v1/videos/' + videoUUID + '/comment-threads/156',
93 statusCodeExpected: 404
94 })
95 })
96
97 it('Should success with the correct params', async function () {
98 await makeGetRequest({
99 url: server.url,
100 path: '/api/v1/videos/' + videoUUID + '/comment-threads/' + commentId,
101 statusCodeExpected: 200
102 })
103 })
104 })
105
106 describe('When adding a video thread', function () {
107
108 it('Should fail with a non authenticated user', async function () {
109 const fields = {
110 text: 'text'
111 }
112 await makePostBodyRequest({ url: server.url, path: pathThread, token: 'none', fields, statusCodeExpected: 401 })
113 })
114
115 it('Should fail with nothing', async function () {
116 const fields = {}
117 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
118 })
119
120 it('Should fail with a short comment', async function () {
121 const fields = {
122 text: ''
123 }
124 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
125 })
126
127 it('Should fail with a long comment', async function () {
128 const fields = {
129 text: 'h'.repeat(3001)
130 }
131 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields })
132 })
133
134 it('Should fail with an incorrect video', async function () {
135 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comment-threads'
136 const fields = {
137 text: 'super comment'
138 }
139 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
140 })
141
142 it('Should succeed with the correct parameters', async function () {
143 const fields = {
144 text: 'super comment'
145 }
146 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields, statusCodeExpected: 200 })
147 })
148 })
149
150 describe('When adding a comment to a thread', function () {
151 it('Should fail with a non authenticated user', async function () {
152 const fields = {
153 text: 'text'
154 }
155 await makePostBodyRequest({ url: server.url, path: pathComment, token: 'none', fields, statusCodeExpected: 401 })
156 })
157
158 it('Should fail with nothing', async function () {
159 const fields = {}
160 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
161 })
162
163 it('Should fail with a short comment', async function () {
164 const fields = {
165 text: ''
166 }
167 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
168 })
169
170 it('Should fail with a long comment', async function () {
171 const fields = {
172 text: 'h'.repeat(3001)
173 }
174 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields })
175 })
176
177 it('Should fail with an incorrect video', async function () {
178 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
179 const fields = {
180 text: 'super comment'
181 }
182 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
183 })
184
185 it('Should fail with an incorrect comment', async function () {
186 const path = '/api/v1/videos/' + videoUUID + '/comments/124'
187 const fields = {
188 text: 'super comment'
189 }
190 await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 404 })
191 })
192
193 it('Should succeed with the correct parameters', async function () {
194 const fields = {
195 text: 'super comment'
196 }
197 await makePostBodyRequest({ url: server.url, path: pathComment, token: server.accessToken, fields, statusCodeExpected: 200 })
198 })
199 })
200
201 describe('When removing video comments', function () {
202 it('Should fail with a non authenticated user', async function () {
203 await makeDeleteRequest({ url: server.url, path: pathComment, token: 'none', statusCodeExpected: 401 })
204 })
205
206 it('Should fail with another user', async function () {
207 await makeDeleteRequest({ url: server.url, path: pathComment, token: userAccessToken, statusCodeExpected: 403 })
208 })
209
210 it('Should fail with an incorrect video', async function () {
211 const path = '/api/v1/videos/ba708d62-e3d7-45d9-9d73-41b9097cc02d/comments/' + commentId
212 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 404 })
213 })
214
215 it('Should fail with an incorrect comment', async function () {
216 const path = '/api/v1/videos/' + videoUUID + '/comments/124'
217 await makeDeleteRequest({ url: server.url, path, token: server.accessToken, statusCodeExpected: 404 })
218 })
219
220 it('Should succeed with the correct parameters', async function () {
221 await makeDeleteRequest({ url: server.url, path: pathComment, token: server.accessToken, statusCodeExpected: 204 })
222 })
223 })
224
225 describe('When a video has comments disabled', function () {
226 before(async function () {
227 const res = await uploadVideo(server.url, server.accessToken, { commentsEnabled: false })
228 videoUUID = res.body.video.uuid
229 pathThread = '/api/v1/videos/' + videoUUID + '/comment-threads'
230 })
231
232 it('Should return an empty thread list', async function () {
233 const res = await makeGetRequest({
234 url: server.url,
235 path: pathThread,
236 statusCodeExpected: 200
237 })
238 expect(res.body.total).to.equal(0)
239 expect(res.body.data).to.have.lengthOf(0)
240 })
241
242 it('Should return an thread comments list')
243
244 it('Should return conflict on thread add', async function () {
245 const fields = {
246 text: 'super comment'
247 }
248 await makePostBodyRequest({ url: server.url, path: pathThread, token: server.accessToken, fields, statusCodeExpected: 409 })
249 })
250
251 it('Should return conflict on comment thread add')
252 })
253
254 after(function () {
255 killallServers([ server ])
256 })
257 })