]>
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' |
cf117aaa | 5 | import { |
12edc149 C |
6 | cleanupTests, |
7 | CommentsCommand, | |
254d3579 | 8 | createSingleServer, |
4c7e60bc | 9 | dateIsValid, |
254d3579 | 10 | PeerTubeServer, |
3cd0734f | 11 | setAccessTokensToServers, |
d23dd9fb | 12 | testImage |
12edc149 | 13 | } from '@shared/extra-utils' |
d3ea8975 C |
14 | |
15 | const expect = chai.expect | |
16 | ||
e2e22e40 | 17 | describe('Test video comments', function () { |
254d3579 | 18 | let server: PeerTubeServer |
12edc149 C |
19 | let videoId: number |
20 | let videoUUID: string | |
21 | let threadId: number | |
4cb6d457 | 22 | let replyToDeleteId: number |
d3ea8975 | 23 | |
32c68d67 RK |
24 | let userAccessTokenServer1: string |
25 | ||
12edc149 C |
26 | let command: CommentsCommand |
27 | ||
d3ea8975 | 28 | before(async function () { |
e212f887 | 29 | this.timeout(30000) |
d3ea8975 | 30 | |
254d3579 | 31 | server = await createSingleServer(1) |
d3ea8975 C |
32 | |
33 | await setAccessTokensToServers([ server ]) | |
34 | ||
89d241a7 | 35 | const { id, uuid } = await server.videos.upload() |
d23dd9fb C |
36 | videoUUID = uuid |
37 | videoId = id | |
cf117aaa | 38 | |
89d241a7 | 39 | await server.users.updateMyAvatar({ fixture: 'avatar.png' }) |
32c68d67 | 40 | |
89d241a7 | 41 | userAccessTokenServer1 = await server.users.generateUserAndToken('user1') |
12edc149 | 42 | |
89d241a7 | 43 | command = server.comments |
d3ea8975 C |
44 | }) |
45 | ||
f1273314 | 46 | describe('User comments', function () { |
d3ea8975 | 47 | |
f1273314 | 48 | it('Should not have threads on this video', async function () { |
12edc149 | 49 | const body = await command.listThreads({ videoId: videoUUID }) |
d3ea8975 | 50 | |
12edc149 C |
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) | |
f1273314 | 55 | }) |
d3ea8975 | 56 | |
f1273314 C |
57 | it('Should create a thread in this video', async function () { |
58 | const text = 'my super first comment' | |
59 | ||
12edc149 | 60 | const comment = await command.createThread({ videoId: videoUUID, text }) |
f1273314 C |
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 | }) | |
d3ea8975 | 74 | |
f1273314 | 75 | it('Should list threads of this video', async function () { |
12edc149 | 76 | const body = await command.listThreads({ videoId: videoUUID }) |
d3ea8975 | 77 | |
12edc149 C |
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) | |
cf117aaa | 82 | |
12edc149 | 83 | const comment = body.data[0] |
f1273314 C |
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) | |
cf117aaa | 90 | |
f1273314 | 91 | await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png') |
d3ea8975 | 92 | |
f1273314 C |
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 | |
d3ea8975 | 97 | |
f1273314 C |
98 | threadId = comment.threadId |
99 | }) | |
d3ea8975 | 100 | |
f1273314 | 101 | it('Should get all the thread created', async function () { |
12edc149 | 102 | const body = await command.getThread({ videoId: videoUUID, threadId }) |
d3ea8975 | 103 | |
12edc149 | 104 | const rootComment = body.comment |
f1273314 C |
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 | }) | |
d3ea8975 | 111 | |
f1273314 C |
112 | it('Should create multiple replies in this thread', async function () { |
113 | const text1 = 'my super answer to thread 1' | |
12edc149 C |
114 | const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 }) |
115 | const childCommentId = created.id | |
d3ea8975 | 116 | |
f1273314 | 117 | const text2 = 'my super answer to answer of thread 1' |
12edc149 | 118 | await command.addReply({ videoId, toCommentId: childCommentId, text: text2 }) |
d3ea8975 | 119 | |
f1273314 | 120 | const text3 = 'my second answer to thread 1' |
12edc149 | 121 | await command.addReply({ videoId, toCommentId: threadId, text: text3 }) |
f1273314 | 122 | }) |
d3ea8975 | 123 | |
f1273314 | 124 | it('Should get correctly the replies', async function () { |
12edc149 | 125 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
d3ea8975 | 126 | |
f1273314 C |
127 | expect(tree.comment.text).equal('my super first comment') |
128 | expect(tree.children).to.have.lengthOf(2) | |
d3ea8975 | 129 | |
f1273314 C |
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) | |
d3ea8975 | 133 | |
f1273314 C |
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) | |
d3ea8975 | 137 | |
f1273314 C |
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) | |
4cb6d457 | 141 | |
f1273314 C |
142 | replyToDeleteId = secondChild.comment.id |
143 | }) | |
d3ea8975 | 144 | |
f1273314 C |
145 | it('Should create other threads', async function () { |
146 | const text1 = 'super thread 2' | |
12edc149 | 147 | await command.createThread({ videoId: videoUUID, text: text1 }) |
d3ea8975 | 148 | |
f1273314 | 149 | const text2 = 'super thread 3' |
12edc149 | 150 | await command.createThread({ videoId: videoUUID, text: text2 }) |
f1273314 | 151 | }) |
d3ea8975 | 152 | |
f1273314 | 153 | it('Should list the threads', async function () { |
12edc149 C |
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) | |
f1273314 | 167 | }) |
d3ea8975 | 168 | |
f1273314 | 169 | it('Should delete a reply', async function () { |
12edc149 | 170 | await command.delete({ videoId, commentId: replyToDeleteId }) |
4cb6d457 | 171 | |
9d6b9d10 | 172 | { |
12edc149 | 173 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) |
4cb6d457 | 174 | |
12edc149 C |
175 | expect(body.total).to.equal(3) |
176 | expect(body.totalNotDeletedComments).to.equal(5) | |
9d6b9d10 | 177 | } |
4cb6d457 | 178 | |
9d6b9d10 | 179 | { |
12edc149 | 180 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
4cb6d457 | 181 | |
9d6b9d10 C |
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) | |
69222afa | 192 | |
9d6b9d10 C |
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 | } | |
f1273314 | 200 | }) |
4cb6d457 | 201 | |
f1273314 | 202 | it('Should delete a complete thread', async function () { |
12edc149 C |
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) | |
f1273314 | 219 | }) |
4cb6d457 | 220 | |
f1273314 | 221 | it('Should count replies from the video author correctly', async function () { |
12edc149 C |
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 | |
f1273314 C |
226 | |
227 | const text2 = 'a first answer to thread 4 by a third party' | |
12edc149 | 228 | await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 }) |
f1273314 C |
229 | |
230 | const text3 = 'my second answer to thread 4' | |
12edc149 | 231 | await command.addReply({ videoId, toCommentId: threadId2, text: text3 }) |
f1273314 | 232 | |
12edc149 | 233 | const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 }) |
f1273314 C |
234 | expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) |
235 | }) | |
4cb6d457 C |
236 | }) |
237 | ||
f1273314 | 238 | describe('All instance comments', function () { |
f1273314 C |
239 | |
240 | it('Should list instance comments as admin', async function () { | |
12edc149 | 241 | const { data } = await command.listForAdmin({ start: 0, count: 1 }) |
f1273314 | 242 | |
12edc149 | 243 | expect(data[0].text).to.equal('my second answer to thread 4') |
f1273314 C |
244 | }) |
245 | ||
246 | it('Should filter instance comments by isLocal', async function () { | |
12edc149 | 247 | const { total, data } = await command.listForAdmin({ isLocal: false }) |
32c68d67 | 248 | |
12edc149 | 249 | expect(data).to.have.lengthOf(0) |
f1273314 C |
250 | expect(total).to.equal(0) |
251 | }) | |
252 | ||
253 | it('Should search instance comments by account', async function () { | |
12edc149 | 254 | const { total, data } = await command.listForAdmin({ searchAccount: 'user' }) |
f1273314 | 255 | |
12edc149 | 256 | expect(data).to.have.lengthOf(1) |
f1273314 C |
257 | expect(total).to.equal(1) |
258 | ||
12edc149 | 259 | expect(data[0].text).to.equal('a first answer to thread 4 by a third party') |
f1273314 | 260 | }) |
32c68d67 | 261 | |
f1273314 C |
262 | it('Should search instance comments by video', async function () { |
263 | { | |
12edc149 | 264 | const { total, data } = await command.listForAdmin({ searchVideo: 'video' }) |
32c68d67 | 265 | |
12edc149 | 266 | expect(data).to.have.lengthOf(7) |
f1273314 C |
267 | expect(total).to.equal(7) |
268 | } | |
32c68d67 | 269 | |
f1273314 | 270 | { |
12edc149 | 271 | const { total, data } = await command.listForAdmin({ searchVideo: 'hello' }) |
f1273314 | 272 | |
12edc149 | 273 | expect(data).to.have.lengthOf(0) |
f1273314 C |
274 | expect(total).to.equal(0) |
275 | } | |
276 | }) | |
277 | ||
278 | it('Should search instance comments', async function () { | |
12edc149 | 279 | const { total, data } = await command.listForAdmin({ search: 'super thread 3' }) |
f1273314 | 280 | |
f1273314 | 281 | expect(total).to.equal(1) |
12edc149 C |
282 | |
283 | expect(data).to.have.lengthOf(1) | |
284 | expect(data[0].text).to.equal('super thread 3') | |
f1273314 | 285 | }) |
32c68d67 RK |
286 | }) |
287 | ||
7c3b7976 C |
288 | after(async function () { |
289 | await cleanupTests([ server ]) | |
d3ea8975 C |
290 | }) |
291 | }) |