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