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