]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-comments.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-comments.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5
6 import { cleanupTests, testImage } from '../../../../shared/extra-utils'
7 import {
8 createUser,
9 dateIsValid,
10 flushAndRunServer,
11 getAccessToken,
12 ServerInfo,
13 setAccessTokensToServers,
14 updateMyAvatar,
15 uploadVideo
16 } from '../../../../shared/extra-utils/index'
17 import {
18 addVideoCommentReply,
19 addVideoCommentThread,
20 deleteVideoComment,
21 getAdminVideoComments,
22 getVideoCommentThreads,
23 getVideoThreadComments
24 } from '../../../../shared/extra-utils/videos/video-comments'
25 import { VideoComment, VideoCommentAdmin, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
26
27 const expect = chai.expect
28
29 describe('Test video comments', function () {
30 let server: ServerInfo
31 let videoId
32 let videoUUID
33 let threadId
34 let replyToDeleteId: number
35
36 let userAccessTokenServer1: string
37
38 before(async function () {
39 this.timeout(30000)
40
41 server = await flushAndRunServer(1)
42
43 await setAccessTokensToServers([ server ])
44
45 const res = await uploadVideo(server.url, server.accessToken, {})
46 videoUUID = res.body.video.uuid
47 videoId = res.body.video.id
48
49 await updateMyAvatar({
50 url: server.url,
51 accessToken: server.accessToken,
52 fixture: 'avatar.png'
53 })
54
55 await createUser({
56 url: server.url,
57 accessToken: server.accessToken,
58 username: 'user1',
59 password: 'password'
60 })
61 userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password')
62 })
63
64 describe('User comments', function () {
65
66 it('Should not have threads on this video', async function () {
67 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
68
69 expect(res.body.total).to.equal(0)
70 expect(res.body.data).to.be.an('array')
71 expect(res.body.data).to.have.lengthOf(0)
72 })
73
74 it('Should create a thread in this video', async function () {
75 const text = 'my super first comment'
76
77 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
78 const comment = res.body.comment
79
80 expect(comment.inReplyToCommentId).to.be.null
81 expect(comment.text).equal('my super first comment')
82 expect(comment.videoId).to.equal(videoId)
83 expect(comment.id).to.equal(comment.threadId)
84 expect(comment.account.name).to.equal('root')
85 expect(comment.account.host).to.equal('localhost:' + server.port)
86 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
87 expect(comment.totalReplies).to.equal(0)
88 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
89 expect(dateIsValid(comment.createdAt as string)).to.be.true
90 expect(dateIsValid(comment.updatedAt as string)).to.be.true
91 })
92
93 it('Should list threads of this video', async function () {
94 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
95
96 expect(res.body.total).to.equal(1)
97 expect(res.body.data).to.be.an('array')
98 expect(res.body.data).to.have.lengthOf(1)
99
100 const comment: VideoComment = res.body.data[0]
101 expect(comment.inReplyToCommentId).to.be.null
102 expect(comment.text).equal('my super first comment')
103 expect(comment.videoId).to.equal(videoId)
104 expect(comment.id).to.equal(comment.threadId)
105 expect(comment.account.name).to.equal('root')
106 expect(comment.account.host).to.equal('localhost:' + server.port)
107
108 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
109
110 expect(comment.totalReplies).to.equal(0)
111 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
112 expect(dateIsValid(comment.createdAt as string)).to.be.true
113 expect(dateIsValid(comment.updatedAt as string)).to.be.true
114
115 threadId = comment.threadId
116 })
117
118 it('Should get all the thread created', async function () {
119 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
120
121 const rootComment = res.body.comment
122 expect(rootComment.inReplyToCommentId).to.be.null
123 expect(rootComment.text).equal('my super first comment')
124 expect(rootComment.videoId).to.equal(videoId)
125 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
126 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
127 })
128
129 it('Should create multiple replies in this thread', async function () {
130 const text1 = 'my super answer to thread 1'
131 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
132 const childCommentId = childCommentRes.body.comment.id
133
134 const text2 = 'my super answer to answer of thread 1'
135 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
136
137 const text3 = 'my second answer to thread 1'
138 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
139 })
140
141 it('Should get correctly the replies', async function () {
142 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
143
144 const tree: VideoCommentThreadTree = res.body
145 expect(tree.comment.text).equal('my super first comment')
146 expect(tree.children).to.have.lengthOf(2)
147
148 const firstChild = tree.children[0]
149 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
150 expect(firstChild.children).to.have.lengthOf(1)
151
152 const childOfFirstChild = firstChild.children[0]
153 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
154 expect(childOfFirstChild.children).to.have.lengthOf(0)
155
156 const secondChild = tree.children[1]
157 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
158 expect(secondChild.children).to.have.lengthOf(0)
159
160 replyToDeleteId = secondChild.comment.id
161 })
162
163 it('Should create other threads', async function () {
164 const text1 = 'super thread 2'
165 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
166
167 const text2 = 'super thread 3'
168 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
169 })
170
171 it('Should list the threads', async function () {
172 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
173
174 expect(res.body.total).to.equal(3)
175 expect(res.body.data).to.be.an('array')
176 expect(res.body.data).to.have.lengthOf(3)
177
178 expect(res.body.data[0].text).to.equal('my super first comment')
179 expect(res.body.data[0].totalReplies).to.equal(3)
180 expect(res.body.data[1].text).to.equal('super thread 2')
181 expect(res.body.data[1].totalReplies).to.equal(0)
182 expect(res.body.data[2].text).to.equal('super thread 3')
183 expect(res.body.data[2].totalReplies).to.equal(0)
184 })
185
186 it('Should delete a reply', async function () {
187 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
188
189 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
190
191 const tree: VideoCommentThreadTree = res.body
192 expect(tree.comment.text).equal('my super first comment')
193 expect(tree.children).to.have.lengthOf(2)
194
195 const firstChild = tree.children[0]
196 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
197 expect(firstChild.children).to.have.lengthOf(1)
198
199 const childOfFirstChild = firstChild.children[0]
200 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
201 expect(childOfFirstChild.children).to.have.lengthOf(0)
202
203 const deletedChildOfFirstChild = tree.children[1]
204 expect(deletedChildOfFirstChild.comment.text).to.equal('')
205 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
206 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
207 expect(deletedChildOfFirstChild.comment.account).to.be.null
208 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
209 })
210
211 it('Should delete a complete thread', async function () {
212 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
213
214 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
215 expect(res.body.total).to.equal(3)
216 expect(res.body.data).to.be.an('array')
217 expect(res.body.data).to.have.lengthOf(3)
218
219 expect(res.body.data[0].text).to.equal('')
220 expect(res.body.data[0].isDeleted).to.be.true
221 expect(res.body.data[0].deletedAt).to.not.be.null
222 expect(res.body.data[0].account).to.be.null
223 expect(res.body.data[0].totalReplies).to.equal(3)
224 expect(res.body.data[1].text).to.equal('super thread 2')
225 expect(res.body.data[1].totalReplies).to.equal(0)
226 expect(res.body.data[2].text).to.equal('super thread 3')
227 expect(res.body.data[2].totalReplies).to.equal(0)
228 })
229
230 it('Should count replies from the video author correctly', async function () {
231 const text = 'my super first comment'
232 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
233 let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
234 const comment: VideoComment = res.body.data[0]
235 const threadId2 = comment.threadId
236
237 const text2 = 'a first answer to thread 4 by a third party'
238 await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2)
239
240 const text3 = 'my second answer to thread 4'
241 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3)
242
243 res = await getVideoThreadComments(server.url, videoUUID, threadId2)
244 const tree: VideoCommentThreadTree = res.body
245 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
246 })
247 })
248
249 describe('All instance comments', function () {
250 async function getComments (options: any = {}) {
251 const res = await getAdminVideoComments(Object.assign({
252 url: server.url,
253 token: server.accessToken,
254 start: 0,
255 count: 10
256 }, options))
257
258 return { comments: res.body.data as VideoCommentAdmin[], total: res.body.total as number }
259 }
260
261 it('Should list instance comments as admin', async function () {
262 const { comments } = await getComments({ start: 0, count: 1 })
263
264 expect(comments[0].text).to.equal('my second answer to thread 4')
265 })
266
267 it('Should filter instance comments by isLocal', async function () {
268 const { total, comments } = await getComments({ isLocal: false })
269
270 expect(comments).to.have.lengthOf(0)
271 expect(total).to.equal(0)
272 })
273
274 it('Should search instance comments by account', async function () {
275 const { total, comments } = await getComments({ searchAccount: 'user' })
276
277 expect(comments).to.have.lengthOf(1)
278 expect(total).to.equal(1)
279
280 expect(comments[0].text).to.equal('a first answer to thread 4 by a third party')
281 })
282
283 it('Should search instance comments by video', async function () {
284 {
285 const { total, comments } = await getComments({ searchVideo: 'video' })
286
287 expect(comments).to.have.lengthOf(7)
288 expect(total).to.equal(7)
289 }
290
291 {
292 const { total, comments } = await getComments({ searchVideo: 'hello' })
293
294 expect(comments).to.have.lengthOf(0)
295 expect(total).to.equal(0)
296 }
297 })
298
299 it('Should search instance comments', async function () {
300 const { total, comments } = await getComments({ search: 'super thread 3' })
301
302 expect(comments).to.have.lengthOf(1)
303 expect(total).to.equal(1)
304 expect(comments[0].text).to.equal('super thread 3')
305 })
306 })
307
308 after(async function () {
309 await cleanupTests([ server ])
310 })
311 })