]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-comments.ts
Merge branch 'release/2.1.0' into develop
[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
C
2
3import * as chai from 'chai'
4import 'mocha'
c5d31dba 5import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7c3b7976 6import { cleanupTests, testImage } from '../../../../shared/extra-utils'
cf117aaa 7import {
a1587156 8 createUser,
3cd0734f 9 dateIsValid,
210feb6c 10 flushAndRunServer,
a1587156 11 getAccessToken,
3cd0734f
C
12 ServerInfo,
13 setAccessTokensToServers,
14 updateMyAvatar,
cf117aaa 15 uploadVideo
94565d52 16} from '../../../../shared/extra-utils/index'
c5d31dba 17import {
3cd0734f
C
18 addVideoCommentReply,
19 addVideoCommentThread,
20 deleteVideoComment,
21 getVideoCommentThreads,
c5d31dba 22 getVideoThreadComments
94565d52 23} from '../../../../shared/extra-utils/videos/video-comments'
d3ea8975
C
24
25const expect = chai.expect
26
e2e22e40 27describe('Test video comments', function () {
d3ea8975
C
28 let server: ServerInfo
29 let videoId
30 let videoUUID
31 let threadId
4cb6d457 32 let replyToDeleteId: number
d3ea8975 33
32c68d67
RK
34 let userAccessTokenServer1: string
35
d3ea8975 36 before(async function () {
e212f887 37 this.timeout(30000)
d3ea8975 38
210feb6c 39 server = await flushAndRunServer(1)
d3ea8975
C
40
41 await setAccessTokensToServers([ server ])
42
43 const res = await uploadVideo(server.url, server.accessToken, {})
44 videoUUID = res.body.video.uuid
45 videoId = res.body.video.id
cf117aaa
C
46
47 await updateMyAvatar({
48 url: server.url,
49 accessToken: server.accessToken,
50 fixture: 'avatar.png'
51 })
32c68d67
RK
52
53 await createUser({
54 url: server.url,
55 accessToken: server.accessToken,
56 username: 'user1',
57 password: 'password'
58 })
59 userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password')
d3ea8975
C
60 })
61
62 it('Should not have threads on this video', async function () {
63 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
64
65 expect(res.body.total).to.equal(0)
66 expect(res.body.data).to.be.an('array')
67 expect(res.body.data).to.have.lengthOf(0)
68 })
69
70 it('Should create a thread in this video', async function () {
71 const text = 'my super first comment'
72
4635f59d 73 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
d50acfab 74 const comment = res.body.comment
4635f59d
C
75
76 expect(comment.inReplyToCommentId).to.be.null
77 expect(comment.text).equal('my super first comment')
78 expect(comment.videoId).to.equal(videoId)
79 expect(comment.id).to.equal(comment.threadId)
80 expect(comment.account.name).to.equal('root')
7243f84d
C
81 expect(comment.account.host).to.equal('localhost:' + server.port)
82 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
4635f59d 83 expect(comment.totalReplies).to.equal(0)
32c68d67 84 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
4635f59d
C
85 expect(dateIsValid(comment.createdAt as string)).to.be.true
86 expect(dateIsValid(comment.updatedAt as string)).to.be.true
d3ea8975
C
87 })
88
89 it('Should list threads of this video', async function () {
90 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
91
92 expect(res.body.total).to.equal(1)
93 expect(res.body.data).to.be.an('array')
94 expect(res.body.data).to.have.lengthOf(1)
95
96 const comment: VideoComment = res.body.data[0]
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')
7243f84d 102 expect(comment.account.host).to.equal('localhost:' + server.port)
cf117aaa 103
7b0956ec 104 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
cf117aaa 105
4635f59d 106 expect(comment.totalReplies).to.equal(0)
32c68d67 107 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
d3ea8975
C
108 expect(dateIsValid(comment.createdAt as string)).to.be.true
109 expect(dateIsValid(comment.updatedAt as string)).to.be.true
110
111 threadId = comment.threadId
112 })
113
114 it('Should get all the thread created', async function () {
115 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
116
117 const rootComment = res.body.comment
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 })
124
125 it('Should create multiple replies in this thread', async function () {
126 const text1 = 'my super answer to thread 1'
127 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
128 const childCommentId = childCommentRes.body.comment.id
129
130 const text2 = 'my super answer to answer of thread 1'
131 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
132
133 const text3 = 'my second answer to thread 1'
134 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
135 })
136
137 it('Should get correctly the replies', async function () {
138 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
139
140 const tree: VideoCommentThreadTree = res.body
141 expect(tree.comment.text).equal('my super first comment')
142 expect(tree.children).to.have.lengthOf(2)
143
144 const firstChild = tree.children[0]
145 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
146 expect(firstChild.children).to.have.lengthOf(1)
147
148 const childOfFirstChild = firstChild.children[0]
149 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
150 expect(childOfFirstChild.children).to.have.lengthOf(0)
151
152 const secondChild = tree.children[1]
153 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
154 expect(secondChild.children).to.have.lengthOf(0)
4cb6d457
C
155
156 replyToDeleteId = secondChild.comment.id
d3ea8975
C
157 })
158
159 it('Should create other threads', async function () {
160 const text1 = 'super thread 2'
161 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
162
163 const text2 = 'super thread 3'
164 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
165 })
166
167 it('Should list the threads', async function () {
168 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
169
170 expect(res.body.total).to.equal(3)
171 expect(res.body.data).to.be.an('array')
172 expect(res.body.data).to.have.lengthOf(3)
173
174 expect(res.body.data[0].text).to.equal('my super first comment')
d50acfab 175 expect(res.body.data[0].totalReplies).to.equal(3)
d3ea8975 176 expect(res.body.data[1].text).to.equal('super thread 2')
d50acfab 177 expect(res.body.data[1].totalReplies).to.equal(0)
d3ea8975 178 expect(res.body.data[2].text).to.equal('super thread 3')
4635f59d 179 expect(res.body.data[2].totalReplies).to.equal(0)
d3ea8975
C
180 })
181
4cb6d457
C
182 it('Should delete a reply', async function () {
183 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
184
185 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
186
187 const tree: VideoCommentThreadTree = res.body
188 expect(tree.comment.text).equal('my super first comment')
69222afa 189 expect(tree.children).to.have.lengthOf(2)
4cb6d457
C
190
191 const firstChild = tree.children[0]
192 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
193 expect(firstChild.children).to.have.lengthOf(1)
194
195 const childOfFirstChild = firstChild.children[0]
196 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
197 expect(childOfFirstChild.children).to.have.lengthOf(0)
69222afa
JM
198
199 const deletedChildOfFirstChild = tree.children[1]
200 expect(deletedChildOfFirstChild.comment.text).to.equal('')
201 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
202 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
203 expect(deletedChildOfFirstChild.comment.account).to.be.null
204 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
4cb6d457
C
205 })
206
207 it('Should delete a complete thread', async function () {
208 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
209
210 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
69222afa 211 expect(res.body.total).to.equal(3)
4cb6d457 212 expect(res.body.data).to.be.an('array')
69222afa 213 expect(res.body.data).to.have.lengthOf(3)
4cb6d457 214
69222afa
JM
215 expect(res.body.data[0].text).to.equal('')
216 expect(res.body.data[0].isDeleted).to.be.true
217 expect(res.body.data[0].deletedAt).to.not.be.null
218 expect(res.body.data[0].account).to.be.null
219 expect(res.body.data[0].totalReplies).to.equal(3)
220 expect(res.body.data[1].text).to.equal('super thread 2')
4cb6d457 221 expect(res.body.data[1].totalReplies).to.equal(0)
69222afa
JM
222 expect(res.body.data[2].text).to.equal('super thread 3')
223 expect(res.body.data[2].totalReplies).to.equal(0)
4cb6d457
C
224 })
225
32c68d67
RK
226 it('Should count replies from the video author correctly', async function () {
227 const text = 'my super first comment'
228 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
229 let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
230 const comment: VideoComment = res.body.data[0]
231 const threadId2 = comment.threadId
232
233 const text2 = 'a first answer to thread 4 by a third party'
234 await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2)
235
236 const text3 = 'my second answer to thread 4'
237 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3)
238
239 res = await getVideoThreadComments(server.url, videoUUID, threadId2)
240 const tree: VideoCommentThreadTree = res.body
241 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
242 })
243
7c3b7976
C
244 after(async function () {
245 await cleanupTests([ server ])
d3ea8975
C
246 })
247})