]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import * as chai from 'chai' | |
4 | import { dateIsValid, testImage } from '@server/tests/shared' | |
5 | import { | |
6 | cleanupTests, | |
7 | CommentsCommand, | |
8 | createSingleServer, | |
9 | PeerTubeServer, | |
10 | setAccessTokensToServers, | |
11 | setDefaultAccountAvatar, | |
12 | setDefaultChannelAvatar | |
13 | } from '@shared/server-commands' | |
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 setDefaultChannelAvatar(server) | |
40 | await setDefaultAccountAvatar(server) | |
41 | ||
42 | userAccessTokenServer1 = await server.users.generateUserAndToken('user1') | |
43 | ||
44 | command = server.comments | |
45 | }) | |
46 | ||
47 | describe('User comments', function () { | |
48 | ||
49 | it('Should not have threads on this video', async function () { | |
50 | const body = await command.listThreads({ videoId: videoUUID }) | |
51 | ||
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) | |
56 | }) | |
57 | ||
58 | it('Should create a thread in this video', async function () { | |
59 | const text = 'my super first comment' | |
60 | ||
61 | const comment = await command.createThread({ videoId: videoUUID, text }) | |
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') | |
68 | expect(comment.account.host).to.equal('localhost:' + server.port) | |
69 | expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root') | |
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 | }) | |
75 | ||
76 | it('Should list threads of this video', async function () { | |
77 | const body = await command.listThreads({ videoId: videoUUID }) | |
78 | ||
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) | |
83 | ||
84 | const comment = body.data[0] | |
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') | |
90 | expect(comment.account.host).to.equal('localhost:' + server.port) | |
91 | ||
92 | for (const avatar of comment.account.avatars) { | |
93 | await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png') | |
94 | } | |
95 | ||
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 | |
100 | ||
101 | threadId = comment.threadId | |
102 | }) | |
103 | ||
104 | it('Should get all the thread created', async function () { | |
105 | const body = await command.getThread({ videoId: videoUUID, threadId }) | |
106 | ||
107 | const rootComment = body.comment | |
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 | }) | |
114 | ||
115 | it('Should create multiple replies in this thread', async function () { | |
116 | const text1 = 'my super answer to thread 1' | |
117 | const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 }) | |
118 | const childCommentId = created.id | |
119 | ||
120 | const text2 = 'my super answer to answer of thread 1' | |
121 | await command.addReply({ videoId, toCommentId: childCommentId, text: text2 }) | |
122 | ||
123 | const text3 = 'my second answer to thread 1' | |
124 | await command.addReply({ videoId, toCommentId: threadId, text: text3 }) | |
125 | }) | |
126 | ||
127 | it('Should get correctly the replies', async function () { | |
128 | const tree = await command.getThread({ videoId: videoUUID, threadId }) | |
129 | ||
130 | expect(tree.comment.text).equal('my super first comment') | |
131 | expect(tree.children).to.have.lengthOf(2) | |
132 | ||
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) | |
136 | ||
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) | |
140 | ||
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) | |
144 | ||
145 | replyToDeleteId = secondChild.comment.id | |
146 | }) | |
147 | ||
148 | it('Should create other threads', async function () { | |
149 | const text1 = 'super thread 2' | |
150 | await command.createThread({ videoId: videoUUID, text: text1 }) | |
151 | ||
152 | const text2 = 'super thread 3' | |
153 | await command.createThread({ videoId: videoUUID, text: text2 }) | |
154 | }) | |
155 | ||
156 | it('Should list the threads', async function () { | |
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) | |
170 | }) | |
171 | ||
172 | it('Should delete a reply', async function () { | |
173 | await command.delete({ videoId, commentId: replyToDeleteId }) | |
174 | ||
175 | { | |
176 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) | |
177 | ||
178 | expect(body.total).to.equal(3) | |
179 | expect(body.totalNotDeletedComments).to.equal(5) | |
180 | } | |
181 | ||
182 | { | |
183 | const tree = await command.getThread({ videoId: videoUUID, threadId }) | |
184 | ||
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) | |
195 | ||
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 | } | |
203 | }) | |
204 | ||
205 | it('Should delete a complete thread', async function () { | |
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) | |
222 | }) | |
223 | ||
224 | it('Should count replies from the video author correctly', async function () { | |
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 | |
229 | ||
230 | const text2 = 'a first answer to thread 4 by a third party' | |
231 | await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 }) | |
232 | ||
233 | const text3 = 'my second answer to thread 4' | |
234 | await command.addReply({ videoId, toCommentId: threadId2, text: text3 }) | |
235 | ||
236 | const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 }) | |
237 | expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) | |
238 | }) | |
239 | }) | |
240 | ||
241 | describe('All instance comments', function () { | |
242 | ||
243 | it('Should list instance comments as admin', async function () { | |
244 | const { data } = await command.listForAdmin({ start: 0, count: 1 }) | |
245 | ||
246 | expect(data[0].text).to.equal('my second answer to thread 4') | |
247 | }) | |
248 | ||
249 | it('Should filter instance comments by isLocal', async function () { | |
250 | const { total, data } = await command.listForAdmin({ isLocal: false }) | |
251 | ||
252 | expect(data).to.have.lengthOf(0) | |
253 | expect(total).to.equal(0) | |
254 | }) | |
255 | ||
256 | it('Should filter instance comments by onLocalVideo', async function () { | |
257 | { | |
258 | const { total, data } = await command.listForAdmin({ onLocalVideo: false }) | |
259 | ||
260 | expect(data).to.have.lengthOf(0) | |
261 | expect(total).to.equal(0) | |
262 | } | |
263 | ||
264 | { | |
265 | const { total, data } = await command.listForAdmin({ onLocalVideo: true }) | |
266 | ||
267 | expect(data).to.not.have.lengthOf(0) | |
268 | expect(total).to.not.equal(0) | |
269 | } | |
270 | }) | |
271 | ||
272 | it('Should search instance comments by account', async function () { | |
273 | const { total, data } = await command.listForAdmin({ searchAccount: 'user' }) | |
274 | ||
275 | expect(data).to.have.lengthOf(1) | |
276 | expect(total).to.equal(1) | |
277 | ||
278 | expect(data[0].text).to.equal('a first answer to thread 4 by a third party') | |
279 | }) | |
280 | ||
281 | it('Should search instance comments by video', async function () { | |
282 | { | |
283 | const { total, data } = await command.listForAdmin({ searchVideo: 'video' }) | |
284 | ||
285 | expect(data).to.have.lengthOf(7) | |
286 | expect(total).to.equal(7) | |
287 | } | |
288 | ||
289 | { | |
290 | const { total, data } = await command.listForAdmin({ searchVideo: 'hello' }) | |
291 | ||
292 | expect(data).to.have.lengthOf(0) | |
293 | expect(total).to.equal(0) | |
294 | } | |
295 | }) | |
296 | ||
297 | it('Should search instance comments', async function () { | |
298 | const { total, data } = await command.listForAdmin({ search: 'super thread 3' }) | |
299 | ||
300 | expect(total).to.equal(1) | |
301 | ||
302 | expect(data).to.have.lengthOf(1) | |
303 | expect(data[0].text).to.equal('super thread 3') | |
304 | }) | |
305 | }) | |
306 | ||
307 | after(async function () { | |
308 | await cleanupTests([ server ]) | |
309 | }) | |
310 | }) |