]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
d3ea8975 | 2 | |
d3ea8975 | 3 | import 'mocha' |
19149d45 | 4 | import * as chai from 'chai' |
c55e3d72 | 5 | import { dateIsValid, testImage } from '@server/tests/shared' |
d0800f76 | 6 | import { |
7 | cleanupTests, | |
8 | CommentsCommand, | |
9 | createSingleServer, | |
10 | PeerTubeServer, | |
11 | setAccessTokensToServers, | |
12 | setDefaultAccountAvatar, | |
13 | setDefaultChannelAvatar | |
14 | } from '@shared/server-commands' | |
d3ea8975 C |
15 | |
16 | const expect = chai.expect | |
17 | ||
e2e22e40 | 18 | describe('Test video comments', function () { |
254d3579 | 19 | let server: PeerTubeServer |
12edc149 C |
20 | let videoId: number |
21 | let videoUUID: string | |
22 | let threadId: number | |
4cb6d457 | 23 | let replyToDeleteId: number |
d3ea8975 | 24 | |
32c68d67 RK |
25 | let userAccessTokenServer1: string |
26 | ||
12edc149 C |
27 | let command: CommentsCommand |
28 | ||
d3ea8975 | 29 | before(async function () { |
e212f887 | 30 | this.timeout(30000) |
d3ea8975 | 31 | |
254d3579 | 32 | server = await createSingleServer(1) |
d3ea8975 C |
33 | |
34 | await setAccessTokensToServers([ server ]) | |
35 | ||
89d241a7 | 36 | const { id, uuid } = await server.videos.upload() |
d23dd9fb C |
37 | videoUUID = uuid |
38 | videoId = id | |
cf117aaa | 39 | |
d0800f76 | 40 | await setDefaultChannelAvatar(server) |
41 | await setDefaultAccountAvatar(server) | |
32c68d67 | 42 | |
89d241a7 | 43 | userAccessTokenServer1 = await server.users.generateUserAndToken('user1') |
12edc149 | 44 | |
89d241a7 | 45 | command = server.comments |
d3ea8975 C |
46 | }) |
47 | ||
f1273314 | 48 | describe('User comments', function () { |
d3ea8975 | 49 | |
f1273314 | 50 | it('Should not have threads on this video', async function () { |
12edc149 | 51 | const body = await command.listThreads({ videoId: videoUUID }) |
d3ea8975 | 52 | |
12edc149 C |
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) | |
f1273314 | 57 | }) |
d3ea8975 | 58 | |
f1273314 C |
59 | it('Should create a thread in this video', async function () { |
60 | const text = 'my super first comment' | |
61 | ||
12edc149 | 62 | const comment = await command.createThread({ videoId: videoUUID, text }) |
f1273314 C |
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 | }) | |
d3ea8975 | 76 | |
f1273314 | 77 | it('Should list threads of this video', async function () { |
12edc149 | 78 | const body = await command.listThreads({ videoId: videoUUID }) |
d3ea8975 | 79 | |
12edc149 C |
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) | |
cf117aaa | 84 | |
12edc149 | 85 | const comment = body.data[0] |
f1273314 C |
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) | |
cf117aaa | 92 | |
d0800f76 | 93 | for (const avatar of comment.account.avatars) { |
94 | await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png') | |
95 | } | |
d3ea8975 | 96 | |
f1273314 C |
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 | |
d3ea8975 | 101 | |
f1273314 C |
102 | threadId = comment.threadId |
103 | }) | |
d3ea8975 | 104 | |
f1273314 | 105 | it('Should get all the thread created', async function () { |
12edc149 | 106 | const body = await command.getThread({ videoId: videoUUID, threadId }) |
d3ea8975 | 107 | |
12edc149 | 108 | const rootComment = body.comment |
f1273314 C |
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 | }) | |
d3ea8975 | 115 | |
f1273314 C |
116 | it('Should create multiple replies in this thread', async function () { |
117 | const text1 = 'my super answer to thread 1' | |
12edc149 C |
118 | const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 }) |
119 | const childCommentId = created.id | |
d3ea8975 | 120 | |
f1273314 | 121 | const text2 = 'my super answer to answer of thread 1' |
12edc149 | 122 | await command.addReply({ videoId, toCommentId: childCommentId, text: text2 }) |
d3ea8975 | 123 | |
f1273314 | 124 | const text3 = 'my second answer to thread 1' |
12edc149 | 125 | await command.addReply({ videoId, toCommentId: threadId, text: text3 }) |
f1273314 | 126 | }) |
d3ea8975 | 127 | |
f1273314 | 128 | it('Should get correctly the replies', async function () { |
12edc149 | 129 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
d3ea8975 | 130 | |
f1273314 C |
131 | expect(tree.comment.text).equal('my super first comment') |
132 | expect(tree.children).to.have.lengthOf(2) | |
d3ea8975 | 133 | |
f1273314 C |
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) | |
d3ea8975 | 137 | |
f1273314 C |
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) | |
d3ea8975 | 141 | |
f1273314 C |
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) | |
4cb6d457 | 145 | |
f1273314 C |
146 | replyToDeleteId = secondChild.comment.id |
147 | }) | |
d3ea8975 | 148 | |
f1273314 C |
149 | it('Should create other threads', async function () { |
150 | const text1 = 'super thread 2' | |
12edc149 | 151 | await command.createThread({ videoId: videoUUID, text: text1 }) |
d3ea8975 | 152 | |
f1273314 | 153 | const text2 = 'super thread 3' |
12edc149 | 154 | await command.createThread({ videoId: videoUUID, text: text2 }) |
f1273314 | 155 | }) |
d3ea8975 | 156 | |
f1273314 | 157 | it('Should list the threads', async function () { |
12edc149 C |
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) | |
f1273314 | 171 | }) |
d3ea8975 | 172 | |
f1273314 | 173 | it('Should delete a reply', async function () { |
12edc149 | 174 | await command.delete({ videoId, commentId: replyToDeleteId }) |
4cb6d457 | 175 | |
9d6b9d10 | 176 | { |
12edc149 | 177 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) |
4cb6d457 | 178 | |
12edc149 C |
179 | expect(body.total).to.equal(3) |
180 | expect(body.totalNotDeletedComments).to.equal(5) | |
9d6b9d10 | 181 | } |
4cb6d457 | 182 | |
9d6b9d10 | 183 | { |
12edc149 | 184 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
4cb6d457 | 185 | |
9d6b9d10 C |
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) | |
69222afa | 196 | |
9d6b9d10 C |
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 | } | |
f1273314 | 204 | }) |
4cb6d457 | 205 | |
f1273314 | 206 | it('Should delete a complete thread', async function () { |
12edc149 C |
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) | |
f1273314 | 223 | }) |
4cb6d457 | 224 | |
f1273314 | 225 | it('Should count replies from the video author correctly', async function () { |
12edc149 C |
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 | |
f1273314 C |
230 | |
231 | const text2 = 'a first answer to thread 4 by a third party' | |
12edc149 | 232 | await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 }) |
f1273314 C |
233 | |
234 | const text3 = 'my second answer to thread 4' | |
12edc149 | 235 | await command.addReply({ videoId, toCommentId: threadId2, text: text3 }) |
f1273314 | 236 | |
12edc149 | 237 | const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 }) |
f1273314 C |
238 | expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) |
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 () { | |
12edc149 | 245 | const { data } = await command.listForAdmin({ start: 0, count: 1 }) |
f1273314 | 246 | |
12edc149 | 247 | expect(data[0].text).to.equal('my second answer to thread 4') |
f1273314 C |
248 | }) |
249 | ||
250 | it('Should filter instance comments by isLocal', async function () { | |
12edc149 | 251 | const { total, data } = await command.listForAdmin({ isLocal: false }) |
32c68d67 | 252 | |
12edc149 | 253 | expect(data).to.have.lengthOf(0) |
f1273314 C |
254 | expect(total).to.equal(0) |
255 | }) | |
256 | ||
257 | it('Should search instance comments by account', async function () { | |
12edc149 | 258 | const { total, data } = await command.listForAdmin({ searchAccount: 'user' }) |
f1273314 | 259 | |
12edc149 | 260 | expect(data).to.have.lengthOf(1) |
f1273314 C |
261 | expect(total).to.equal(1) |
262 | ||
12edc149 | 263 | expect(data[0].text).to.equal('a first answer to thread 4 by a third party') |
f1273314 | 264 | }) |
32c68d67 | 265 | |
f1273314 C |
266 | it('Should search instance comments by video', async function () { |
267 | { | |
12edc149 | 268 | const { total, data } = await command.listForAdmin({ searchVideo: 'video' }) |
32c68d67 | 269 | |
12edc149 | 270 | expect(data).to.have.lengthOf(7) |
f1273314 C |
271 | expect(total).to.equal(7) |
272 | } | |
32c68d67 | 273 | |
f1273314 | 274 | { |
12edc149 | 275 | const { total, data } = await command.listForAdmin({ searchVideo: 'hello' }) |
f1273314 | 276 | |
12edc149 | 277 | expect(data).to.have.lengthOf(0) |
f1273314 C |
278 | expect(total).to.equal(0) |
279 | } | |
280 | }) | |
281 | ||
282 | it('Should search instance comments', async function () { | |
12edc149 | 283 | const { total, data } = await command.listForAdmin({ search: 'super thread 3' }) |
f1273314 | 284 | |
f1273314 | 285 | expect(total).to.equal(1) |
12edc149 C |
286 | |
287 | expect(data).to.have.lengthOf(1) | |
288 | expect(data[0].text).to.equal('super thread 3') | |
f1273314 | 289 | }) |
32c68d67 RK |
290 | }) |
291 | ||
7c3b7976 C |
292 | after(async function () { |
293 | await cleanupTests([ server ]) | |
d3ea8975 C |
294 | }) |
295 | }) |