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