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