]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-comments.ts
Fix rerunserver function
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-comments.ts
CommitLineData
d3ea8975
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
c5d31dba 5import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
94565d52 6import { testImage } from '../../../../shared/extra-utils'
cf117aaa 7import {
3cd0734f
C
8 dateIsValid,
9 flushTests,
10 killallServers,
210feb6c 11 flushAndRunServer,
3cd0734f
C
12 ServerInfo,
13 setAccessTokensToServers,
14 updateMyAvatar,
cf117aaa 15 uploadVideo
94565d52 16} from '../../../../shared/extra-utils/index'
c5d31dba 17import {
3cd0734f
C
18 addVideoCommentReply,
19 addVideoCommentThread,
20 deleteVideoComment,
21 getVideoCommentThreads,
c5d31dba 22 getVideoThreadComments
94565d52 23} from '../../../../shared/extra-utils/videos/video-comments'
d3ea8975
C
24
25const expect = chai.expect
26
e2e22e40 27describe('Test video comments', function () {
d3ea8975
C
28 let server: ServerInfo
29 let videoId
30 let videoUUID
31 let threadId
4cb6d457 32 let replyToDeleteId: number
d3ea8975
C
33
34 before(async function () {
e212f887 35 this.timeout(30000)
d3ea8975 36
210feb6c 37 server = await flushAndRunServer(1)
d3ea8975
C
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
cf117aaa
C
44
45 await updateMyAvatar({
46 url: server.url,
47 accessToken: server.accessToken,
48 fixture: 'avatar.png'
49 })
d3ea8975
C
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
4635f59d 63 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
d50acfab 64 const comment = res.body.comment
4635f59d
C
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')
4cb6d457 72 expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
4635f59d
C
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
d3ea8975
C
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')
4635f59d 91 expect(comment.account.host).to.equal('localhost:9001')
cf117aaa 92
7b0956ec 93 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
cf117aaa 94
4635f59d 95 expect(comment.totalReplies).to.equal(0)
d3ea8975
C
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)
4cb6d457
C
143
144 replyToDeleteId = secondChild.comment.id
d3ea8975
C
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')
d50acfab 163 expect(res.body.data[0].totalReplies).to.equal(3)
d3ea8975 164 expect(res.body.data[1].text).to.equal('super thread 2')
d50acfab 165 expect(res.body.data[1].totalReplies).to.equal(0)
d3ea8975 166 expect(res.body.data[2].text).to.equal('super thread 3')
4635f59d 167 expect(res.body.data[2].totalReplies).to.equal(0)
d3ea8975
C
168 })
169
4cb6d457
C
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
210feb6c 202 after(function () {
d3ea8975 203 killallServers([ server ])
d3ea8975
C
204 })
205})