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