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