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