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