Add tests to handle down server
[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 { testVideoImage } 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(10000)
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     const test = await testVideoImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
87     expect(test).to.equal(true)
88
89     expect(comment.totalReplies).to.equal(0)
90     expect(dateIsValid(comment.createdAt as string)).to.be.true
91     expect(dateIsValid(comment.updatedAt as string)).to.be.true
92
93     threadId = comment.threadId
94   })
95
96   it('Should get all the thread created', async function () {
97     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
98
99     const rootComment = res.body.comment
100     expect(rootComment.inReplyToCommentId).to.be.null
101     expect(rootComment.text).equal('my super first comment')
102     expect(rootComment.videoId).to.equal(videoId)
103     expect(dateIsValid(rootComment.createdAt as string)).to.be.true
104     expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
105   })
106
107   it('Should create multiple replies in this thread', async function () {
108     const text1 = 'my super answer to thread 1'
109     const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
110     const childCommentId = childCommentRes.body.comment.id
111
112     const text2 = 'my super answer to answer of thread 1'
113     await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
114
115     const text3 = 'my second answer to thread 1'
116     await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
117   })
118
119   it('Should get correctly the replies', async function () {
120     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
121
122     const tree: VideoCommentThreadTree = res.body
123     expect(tree.comment.text).equal('my super first comment')
124     expect(tree.children).to.have.lengthOf(2)
125
126     const firstChild = tree.children[0]
127     expect(firstChild.comment.text).to.equal('my super answer to thread 1')
128     expect(firstChild.children).to.have.lengthOf(1)
129
130     const childOfFirstChild = firstChild.children[0]
131     expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
132     expect(childOfFirstChild.children).to.have.lengthOf(0)
133
134     const secondChild = tree.children[1]
135     expect(secondChild.comment.text).to.equal('my second answer to thread 1')
136     expect(secondChild.children).to.have.lengthOf(0)
137
138     replyToDeleteId = secondChild.comment.id
139   })
140
141   it('Should create other threads', async function () {
142     const text1 = 'super thread 2'
143     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
144
145     const text2 = 'super thread 3'
146     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
147   })
148
149   it('Should list the threads', async function () {
150     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
151
152     expect(res.body.total).to.equal(3)
153     expect(res.body.data).to.be.an('array')
154     expect(res.body.data).to.have.lengthOf(3)
155
156     expect(res.body.data[0].text).to.equal('my super first comment')
157     expect(res.body.data[0].totalReplies).to.equal(3)
158     expect(res.body.data[1].text).to.equal('super thread 2')
159     expect(res.body.data[1].totalReplies).to.equal(0)
160     expect(res.body.data[2].text).to.equal('super thread 3')
161     expect(res.body.data[2].totalReplies).to.equal(0)
162   })
163
164   it('Should delete a reply', async function () {
165     await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
166
167     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
168
169     const tree: VideoCommentThreadTree = res.body
170     expect(tree.comment.text).equal('my super first comment')
171     expect(tree.children).to.have.lengthOf(1)
172
173     const firstChild = tree.children[0]
174     expect(firstChild.comment.text).to.equal('my super answer to thread 1')
175     expect(firstChild.children).to.have.lengthOf(1)
176
177     const childOfFirstChild = firstChild.children[0]
178     expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
179     expect(childOfFirstChild.children).to.have.lengthOf(0)
180   })
181
182   it('Should delete a complete thread', async function () {
183     await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
184
185     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
186     expect(res.body.total).to.equal(2)
187     expect(res.body.data).to.be.an('array')
188     expect(res.body.data).to.have.lengthOf(2)
189
190     expect(res.body.data[0].text).to.equal('super thread 2')
191     expect(res.body.data[0].totalReplies).to.equal(0)
192     expect(res.body.data[1].text).to.equal('super thread 3')
193     expect(res.body.data[1].totalReplies).to.equal(0)
194   })
195
196   after(async function () {
197     killallServers([ server ])
198
199     // Keep the logs if the test failed
200     if (this['ok']) {
201       await flushTests()
202     }
203   })
204 })