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