Merge branch 'master' into develop
[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   uploadVideo
14 } from '../../../../shared/extra-utils/index'
15 import {
16   addVideoCommentReply,
17   addVideoCommentThread,
18   deleteVideoComment,
19   getVideoCommentThreads,
20   getVideoThreadComments
21 } from '../../../../shared/extra-utils/videos/video-comments'
22
23 const expect = chai.expect
24
25 describe('Test video comments', function () {
26   let server: ServerInfo
27   let videoId
28   let videoUUID
29   let threadId
30   let replyToDeleteId: number
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
50   it('Should not have threads on this video', async function () {
51     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
52
53     expect(res.body.total).to.equal(0)
54     expect(res.body.data).to.be.an('array')
55     expect(res.body.data).to.have.lengthOf(0)
56   })
57
58   it('Should create a thread in this video', async function () {
59     const text = 'my super first comment'
60
61     const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
62     const comment = res.body.comment
63
64     expect(comment.inReplyToCommentId).to.be.null
65     expect(comment.text).equal('my super first comment')
66     expect(comment.videoId).to.equal(videoId)
67     expect(comment.id).to.equal(comment.threadId)
68     expect(comment.account.name).to.equal('root')
69     expect(comment.account.host).to.equal('localhost:9001')
70     expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
71     expect(comment.totalReplies).to.equal(0)
72     expect(dateIsValid(comment.createdAt as string)).to.be.true
73     expect(dateIsValid(comment.updatedAt as string)).to.be.true
74   })
75
76   it('Should list threads of this video', async function () {
77     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
78
79     expect(res.body.total).to.equal(1)
80     expect(res.body.data).to.be.an('array')
81     expect(res.body.data).to.have.lengthOf(1)
82
83     const comment: VideoComment = res.body.data[0]
84     expect(comment.inReplyToCommentId).to.be.null
85     expect(comment.text).equal('my super first comment')
86     expect(comment.videoId).to.equal(videoId)
87     expect(comment.id).to.equal(comment.threadId)
88     expect(comment.account.name).to.equal('root')
89     expect(comment.account.host).to.equal('localhost:9001')
90
91     await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
92
93     expect(comment.totalReplies).to.equal(0)
94     expect(dateIsValid(comment.createdAt as string)).to.be.true
95     expect(dateIsValid(comment.updatedAt as string)).to.be.true
96
97     threadId = comment.threadId
98   })
99
100   it('Should get all the thread created', async function () {
101     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
102
103     const rootComment = res.body.comment
104     expect(rootComment.inReplyToCommentId).to.be.null
105     expect(rootComment.text).equal('my super first comment')
106     expect(rootComment.videoId).to.equal(videoId)
107     expect(dateIsValid(rootComment.createdAt as string)).to.be.true
108     expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
109   })
110
111   it('Should create multiple replies in this thread', async function () {
112     const text1 = 'my super answer to thread 1'
113     const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
114     const childCommentId = childCommentRes.body.comment.id
115
116     const text2 = 'my super answer to answer of thread 1'
117     await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
118
119     const text3 = 'my second answer to thread 1'
120     await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
121   })
122
123   it('Should get correctly the replies', async function () {
124     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
125
126     const tree: VideoCommentThreadTree = res.body
127     expect(tree.comment.text).equal('my super first comment')
128     expect(tree.children).to.have.lengthOf(2)
129
130     const firstChild = tree.children[0]
131     expect(firstChild.comment.text).to.equal('my super answer to thread 1')
132     expect(firstChild.children).to.have.lengthOf(1)
133
134     const childOfFirstChild = firstChild.children[0]
135     expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
136     expect(childOfFirstChild.children).to.have.lengthOf(0)
137
138     const secondChild = tree.children[1]
139     expect(secondChild.comment.text).to.equal('my second answer to thread 1')
140     expect(secondChild.children).to.have.lengthOf(0)
141
142     replyToDeleteId = secondChild.comment.id
143   })
144
145   it('Should create other threads', async function () {
146     const text1 = 'super thread 2'
147     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
148
149     const text2 = 'super thread 3'
150     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
151   })
152
153   it('Should list the threads', async function () {
154     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
155
156     expect(res.body.total).to.equal(3)
157     expect(res.body.data).to.be.an('array')
158     expect(res.body.data).to.have.lengthOf(3)
159
160     expect(res.body.data[0].text).to.equal('my super first comment')
161     expect(res.body.data[0].totalReplies).to.equal(3)
162     expect(res.body.data[1].text).to.equal('super thread 2')
163     expect(res.body.data[1].totalReplies).to.equal(0)
164     expect(res.body.data[2].text).to.equal('super thread 3')
165     expect(res.body.data[2].totalReplies).to.equal(0)
166   })
167
168   it('Should delete a reply', async function () {
169     await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
170
171     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
172
173     const tree: VideoCommentThreadTree = res.body
174     expect(tree.comment.text).equal('my super first comment')
175     expect(tree.children).to.have.lengthOf(1)
176
177     const firstChild = tree.children[0]
178     expect(firstChild.comment.text).to.equal('my super answer to thread 1')
179     expect(firstChild.children).to.have.lengthOf(1)
180
181     const childOfFirstChild = firstChild.children[0]
182     expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
183     expect(childOfFirstChild.children).to.have.lengthOf(0)
184   })
185
186   it('Should delete a complete thread', async function () {
187     await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
188
189     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
190     expect(res.body.total).to.equal(2)
191     expect(res.body.data).to.be.an('array')
192     expect(res.body.data).to.have.lengthOf(2)
193
194     expect(res.body.data[0].text).to.equal('super thread 2')
195     expect(res.body.data[0].totalReplies).to.equal(0)
196     expect(res.body.data[1].text).to.equal('super thread 3')
197     expect(res.body.data[1].totalReplies).to.equal(0)
198   })
199
200   after(async function () {
201     await cleanupTests([ server ])
202   })
203 })