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