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