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