Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / videos / video-comments.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
6 import { cleanupTests, testImage } from '../../../../shared/extra-utils'
7 import {
8   dateIsValid,
9   flushAndRunServer,
10   ServerInfo,
11   setAccessTokensToServers,
12   updateMyAvatar,
13   getAccessToken,
14   createUser,
15   uploadVideo
16 } from '../../../../shared/extra-utils/index'
17 import {
18   addVideoCommentReply,
19   addVideoCommentThread,
20   deleteVideoComment,
21   getVideoCommentThreads,
22   getVideoThreadComments
23 } from '../../../../shared/extra-utils/videos/video-comments'
24
25 const expect = chai.expect
26
27 describe('Test video comments', function () {
28   let server: ServerInfo
29   let videoId
30   let videoUUID
31   let threadId
32   let replyToDeleteId: number
33
34   let userAccessTokenServer1: string
35
36   before(async function () {
37     this.timeout(30000)
38
39     server = await flushAndRunServer(1)
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
46
47     await updateMyAvatar({
48       url: server.url,
49       accessToken: server.accessToken,
50       fixture: 'avatar.png'
51     })
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')
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
73     const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
74     const comment = res.body.comment
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 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')
102     expect(comment.account.host).to.equal('localhost:' + server.port)
103
104     await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
105
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
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)
155
156     replyToDeleteId = secondChild.comment.id
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')
175     expect(res.body.data[0].totalReplies).to.equal(3)
176     expect(res.body.data[1].text).to.equal('super thread 2')
177     expect(res.body.data[1].totalReplies).to.equal(0)
178     expect(res.body.data[2].text).to.equal('super thread 3')
179     expect(res.body.data[2].totalReplies).to.equal(0)
180   })
181
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')
189     expect(tree.children).to.have.lengthOf(2)
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)
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)
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')
211     expect(res.body.total).to.equal(3)
212     expect(res.body.data).to.be.an('array')
213     expect(res.body.data).to.have.lengthOf(3)
214
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')
221     expect(res.body.data[1].totalReplies).to.equal(0)
222     expect(res.body.data[2].text).to.equal('super thread 3')
223     expect(res.body.data[2].totalReplies).to.equal(0)
224   })
225
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
244   after(async function () {
245     await cleanupTests([ server ])
246   })
247 })