]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-comments.ts
Remove tmp file on image processing error
[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'
9639bd17 6import { testImage } from '../../../../shared/utils'
cf117aaa 7import {
3cd0734f
C
8 dateIsValid,
9 flushTests,
10 killallServers,
11 runServer,
12 ServerInfo,
13 setAccessTokensToServers,
14 updateMyAvatar,
cf117aaa 15 uploadVideo
9639bd17 16} from '../../../../shared/utils/index'
c5d31dba 17import {
3cd0734f
C
18 addVideoCommentReply,
19 addVideoCommentThread,
20 deleteVideoComment,
21 getVideoCommentThreads,
c5d31dba 22 getVideoThreadComments
9639bd17 23} from '../../../../shared/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
C
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
cf117aaa
C
46
47 await updateMyAvatar({
48 url: server.url,
49 accessToken: server.accessToken,
50 fixture: 'avatar.png'
51 })
d3ea8975
C
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
4635f59d 65 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
d50acfab 66 const comment = res.body.comment
4635f59d
C
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')
4cb6d457 74 expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
4635f59d
C
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
d3ea8975
C
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')
4635f59d 93 expect(comment.account.host).to.equal('localhost:9001')
cf117aaa 94
7b0956ec 95 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
cf117aaa 96
4635f59d 97 expect(comment.totalReplies).to.equal(0)
d3ea8975
C
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)
4cb6d457
C
145
146 replyToDeleteId = secondChild.comment.id
d3ea8975
C
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')
d50acfab 165 expect(res.body.data[0].totalReplies).to.equal(3)
d3ea8975 166 expect(res.body.data[1].text).to.equal('super thread 2')
d50acfab 167 expect(res.body.data[1].totalReplies).to.equal(0)
d3ea8975 168 expect(res.body.data[2].text).to.equal('super thread 3')
4635f59d 169 expect(res.body.data[2].totalReplies).to.equal(0)
d3ea8975
C
170 })
171
4cb6d457
C
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
d3ea8975
C
204 after(async function () {
205 killallServers([ server ])
d3ea8975
C
206 })
207})