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