diff options
Diffstat (limited to 'server/tests/api/videos/video-comments.ts')
-rw-r--r-- | server/tests/api/videos/video-comments.ts | 225 |
1 files changed, 97 insertions, 128 deletions
diff --git a/server/tests/api/videos/video-comments.ts b/server/tests/api/videos/video-comments.ts index b6b002307..61ee54540 100644 --- a/server/tests/api/videos/video-comments.ts +++ b/server/tests/api/videos/video-comments.ts | |||
@@ -2,80 +2,62 @@ | |||
2 | 2 | ||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import * as chai from 'chai' | 4 | import * as chai from 'chai' |
5 | import { VideoComment, VideoCommentAdmin, VideoCommentThreadTree } from '@shared/models' | ||
6 | import { cleanupTests, testImage } from '../../../../shared/extra-utils' | ||
7 | import { | 5 | import { |
8 | createUser, | 6 | cleanupTests, |
7 | CommentsCommand, | ||
8 | createSingleServer, | ||
9 | dateIsValid, | 9 | dateIsValid, |
10 | flushAndRunServer, | 10 | PeerTubeServer, |
11 | getAccessToken, | ||
12 | ServerInfo, | ||
13 | setAccessTokensToServers, | 11 | setAccessTokensToServers, |
14 | updateMyAvatar, | 12 | testImage |
15 | uploadVideo | 13 | } from '@shared/extra-utils' |
16 | } from '../../../../shared/extra-utils/index' | ||
17 | import { | ||
18 | addVideoCommentReply, | ||
19 | addVideoCommentThread, | ||
20 | deleteVideoComment, | ||
21 | getAdminVideoComments, | ||
22 | getVideoCommentThreads, | ||
23 | getVideoThreadComments | ||
24 | } from '../../../../shared/extra-utils/videos/video-comments' | ||
25 | 14 | ||
26 | const expect = chai.expect | 15 | const expect = chai.expect |
27 | 16 | ||
28 | describe('Test video comments', function () { | 17 | describe('Test video comments', function () { |
29 | let server: ServerInfo | 18 | let server: PeerTubeServer |
30 | let videoId | 19 | let videoId: number |
31 | let videoUUID | 20 | let videoUUID: string |
32 | let threadId | 21 | let threadId: number |
33 | let replyToDeleteId: number | 22 | let replyToDeleteId: number |
34 | 23 | ||
35 | let userAccessTokenServer1: string | 24 | let userAccessTokenServer1: string |
36 | 25 | ||
26 | let command: CommentsCommand | ||
27 | |||
37 | before(async function () { | 28 | before(async function () { |
38 | this.timeout(30000) | 29 | this.timeout(30000) |
39 | 30 | ||
40 | server = await flushAndRunServer(1) | 31 | server = await createSingleServer(1) |
41 | 32 | ||
42 | await setAccessTokensToServers([ server ]) | 33 | await setAccessTokensToServers([ server ]) |
43 | 34 | ||
44 | const res = await uploadVideo(server.url, server.accessToken, {}) | 35 | const { id, uuid } = await server.videos.upload() |
45 | videoUUID = res.body.video.uuid | 36 | videoUUID = uuid |
46 | videoId = res.body.video.id | 37 | videoId = id |
47 | 38 | ||
48 | await updateMyAvatar({ | 39 | await server.users.updateMyAvatar({ fixture: 'avatar.png' }) |
49 | url: server.url, | ||
50 | accessToken: server.accessToken, | ||
51 | fixture: 'avatar.png' | ||
52 | }) | ||
53 | 40 | ||
54 | await createUser({ | 41 | userAccessTokenServer1 = await server.users.generateUserAndToken('user1') |
55 | url: server.url, | 42 | |
56 | accessToken: server.accessToken, | 43 | command = server.comments |
57 | username: 'user1', | ||
58 | password: 'password' | ||
59 | }) | ||
60 | userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password') | ||
61 | }) | 44 | }) |
62 | 45 | ||
63 | describe('User comments', function () { | 46 | describe('User comments', function () { |
64 | 47 | ||
65 | it('Should not have threads on this video', async function () { | 48 | it('Should not have threads on this video', async function () { |
66 | const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) | 49 | const body = await command.listThreads({ videoId: videoUUID }) |
67 | 50 | ||
68 | expect(res.body.total).to.equal(0) | 51 | expect(body.total).to.equal(0) |
69 | expect(res.body.totalNotDeletedComments).to.equal(0) | 52 | expect(body.totalNotDeletedComments).to.equal(0) |
70 | expect(res.body.data).to.be.an('array') | 53 | expect(body.data).to.be.an('array') |
71 | expect(res.body.data).to.have.lengthOf(0) | 54 | expect(body.data).to.have.lengthOf(0) |
72 | }) | 55 | }) |
73 | 56 | ||
74 | it('Should create a thread in this video', async function () { | 57 | it('Should create a thread in this video', async function () { |
75 | const text = 'my super first comment' | 58 | const text = 'my super first comment' |
76 | 59 | ||
77 | const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) | 60 | const comment = await command.createThread({ videoId: videoUUID, text }) |
78 | const comment = res.body.comment | ||
79 | 61 | ||
80 | expect(comment.inReplyToCommentId).to.be.null | 62 | expect(comment.inReplyToCommentId).to.be.null |
81 | expect(comment.text).equal('my super first comment') | 63 | expect(comment.text).equal('my super first comment') |
@@ -91,14 +73,14 @@ describe('Test video comments', function () { | |||
91 | }) | 73 | }) |
92 | 74 | ||
93 | it('Should list threads of this video', async function () { | 75 | it('Should list threads of this video', async function () { |
94 | const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) | 76 | const body = await command.listThreads({ videoId: videoUUID }) |
95 | 77 | ||
96 | expect(res.body.total).to.equal(1) | 78 | expect(body.total).to.equal(1) |
97 | expect(res.body.totalNotDeletedComments).to.equal(1) | 79 | expect(body.totalNotDeletedComments).to.equal(1) |
98 | expect(res.body.data).to.be.an('array') | 80 | expect(body.data).to.be.an('array') |
99 | expect(res.body.data).to.have.lengthOf(1) | 81 | expect(body.data).to.have.lengthOf(1) |
100 | 82 | ||
101 | const comment: VideoComment = res.body.data[0] | 83 | const comment = body.data[0] |
102 | expect(comment.inReplyToCommentId).to.be.null | 84 | expect(comment.inReplyToCommentId).to.be.null |
103 | expect(comment.text).equal('my super first comment') | 85 | expect(comment.text).equal('my super first comment') |
104 | expect(comment.videoId).to.equal(videoId) | 86 | expect(comment.videoId).to.equal(videoId) |
@@ -117,9 +99,9 @@ describe('Test video comments', function () { | |||
117 | }) | 99 | }) |
118 | 100 | ||
119 | it('Should get all the thread created', async function () { | 101 | it('Should get all the thread created', async function () { |
120 | const res = await getVideoThreadComments(server.url, videoUUID, threadId) | 102 | const body = await command.getThread({ videoId: videoUUID, threadId }) |
121 | 103 | ||
122 | const rootComment = res.body.comment | 104 | const rootComment = body.comment |
123 | expect(rootComment.inReplyToCommentId).to.be.null | 105 | expect(rootComment.inReplyToCommentId).to.be.null |
124 | expect(rootComment.text).equal('my super first comment') | 106 | expect(rootComment.text).equal('my super first comment') |
125 | expect(rootComment.videoId).to.equal(videoId) | 107 | expect(rootComment.videoId).to.equal(videoId) |
@@ -129,20 +111,19 @@ describe('Test video comments', function () { | |||
129 | 111 | ||
130 | it('Should create multiple replies in this thread', async function () { | 112 | it('Should create multiple replies in this thread', async function () { |
131 | const text1 = 'my super answer to thread 1' | 113 | const text1 = 'my super answer to thread 1' |
132 | const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1) | 114 | const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 }) |
133 | const childCommentId = childCommentRes.body.comment.id | 115 | const childCommentId = created.id |
134 | 116 | ||
135 | const text2 = 'my super answer to answer of thread 1' | 117 | const text2 = 'my super answer to answer of thread 1' |
136 | await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2) | 118 | await command.addReply({ videoId, toCommentId: childCommentId, text: text2 }) |
137 | 119 | ||
138 | const text3 = 'my second answer to thread 1' | 120 | const text3 = 'my second answer to thread 1' |
139 | await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3) | 121 | await command.addReply({ videoId, toCommentId: threadId, text: text3 }) |
140 | }) | 122 | }) |
141 | 123 | ||
142 | it('Should get correctly the replies', async function () { | 124 | it('Should get correctly the replies', async function () { |
143 | const res = await getVideoThreadComments(server.url, videoUUID, threadId) | 125 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
144 | 126 | ||
145 | const tree: VideoCommentThreadTree = res.body | ||
146 | expect(tree.comment.text).equal('my super first comment') | 127 | expect(tree.comment.text).equal('my super first comment') |
147 | expect(tree.children).to.have.lengthOf(2) | 128 | expect(tree.children).to.have.lengthOf(2) |
148 | 129 | ||
@@ -163,42 +144,41 @@ describe('Test video comments', function () { | |||
163 | 144 | ||
164 | it('Should create other threads', async function () { | 145 | it('Should create other threads', async function () { |
165 | const text1 = 'super thread 2' | 146 | const text1 = 'super thread 2' |
166 | await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1) | 147 | await command.createThread({ videoId: videoUUID, text: text1 }) |
167 | 148 | ||
168 | const text2 = 'super thread 3' | 149 | const text2 = 'super thread 3' |
169 | await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2) | 150 | await command.createThread({ videoId: videoUUID, text: text2 }) |
170 | }) | 151 | }) |
171 | 152 | ||
172 | it('Should list the threads', async function () { | 153 | it('Should list the threads', async function () { |
173 | const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') | 154 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) |
174 | 155 | ||
175 | expect(res.body.total).to.equal(3) | 156 | expect(body.total).to.equal(3) |
176 | expect(res.body.totalNotDeletedComments).to.equal(6) | 157 | expect(body.totalNotDeletedComments).to.equal(6) |
177 | expect(res.body.data).to.be.an('array') | 158 | expect(body.data).to.be.an('array') |
178 | expect(res.body.data).to.have.lengthOf(3) | 159 | expect(body.data).to.have.lengthOf(3) |
179 | 160 | ||
180 | expect(res.body.data[0].text).to.equal('my super first comment') | 161 | expect(body.data[0].text).to.equal('my super first comment') |
181 | expect(res.body.data[0].totalReplies).to.equal(3) | 162 | expect(body.data[0].totalReplies).to.equal(3) |
182 | expect(res.body.data[1].text).to.equal('super thread 2') | 163 | expect(body.data[1].text).to.equal('super thread 2') |
183 | expect(res.body.data[1].totalReplies).to.equal(0) | 164 | expect(body.data[1].totalReplies).to.equal(0) |
184 | expect(res.body.data[2].text).to.equal('super thread 3') | 165 | expect(body.data[2].text).to.equal('super thread 3') |
185 | expect(res.body.data[2].totalReplies).to.equal(0) | 166 | expect(body.data[2].totalReplies).to.equal(0) |
186 | }) | 167 | }) |
187 | 168 | ||
188 | it('Should delete a reply', async function () { | 169 | it('Should delete a reply', async function () { |
189 | await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId) | 170 | await command.delete({ videoId, commentId: replyToDeleteId }) |
190 | 171 | ||
191 | { | 172 | { |
192 | const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') | 173 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) |
193 | 174 | ||
194 | expect(res.body.total).to.equal(3) | 175 | expect(body.total).to.equal(3) |
195 | expect(res.body.totalNotDeletedComments).to.equal(5) | 176 | expect(body.totalNotDeletedComments).to.equal(5) |
196 | } | 177 | } |
197 | 178 | ||
198 | { | 179 | { |
199 | const res = await getVideoThreadComments(server.url, videoUUID, threadId) | 180 | const tree = await command.getThread({ videoId: videoUUID, threadId }) |
200 | 181 | ||
201 | const tree: VideoCommentThreadTree = res.body | ||
202 | expect(tree.comment.text).equal('my super first comment') | 182 | expect(tree.comment.text).equal('my super first comment') |
203 | expect(tree.children).to.have.lengthOf(2) | 183 | expect(tree.children).to.have.lengthOf(2) |
204 | 184 | ||
@@ -220,99 +200,88 @@ describe('Test video comments', function () { | |||
220 | }) | 200 | }) |
221 | 201 | ||
222 | it('Should delete a complete thread', async function () { | 202 | it('Should delete a complete thread', async function () { |
223 | await deleteVideoComment(server.url, server.accessToken, videoId, threadId) | 203 | await command.delete({ videoId, commentId: threadId }) |
224 | 204 | ||
225 | const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt') | 205 | const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' }) |
226 | expect(res.body.total).to.equal(3) | 206 | expect(body.total).to.equal(3) |
227 | expect(res.body.data).to.be.an('array') | 207 | expect(body.data).to.be.an('array') |
228 | expect(res.body.data).to.have.lengthOf(3) | 208 | expect(body.data).to.have.lengthOf(3) |
229 | 209 | ||
230 | expect(res.body.data[0].text).to.equal('') | 210 | expect(body.data[0].text).to.equal('') |
231 | expect(res.body.data[0].isDeleted).to.be.true | 211 | expect(body.data[0].isDeleted).to.be.true |
232 | expect(res.body.data[0].deletedAt).to.not.be.null | 212 | expect(body.data[0].deletedAt).to.not.be.null |
233 | expect(res.body.data[0].account).to.be.null | 213 | expect(body.data[0].account).to.be.null |
234 | expect(res.body.data[0].totalReplies).to.equal(2) | 214 | expect(body.data[0].totalReplies).to.equal(2) |
235 | expect(res.body.data[1].text).to.equal('super thread 2') | 215 | expect(body.data[1].text).to.equal('super thread 2') |
236 | expect(res.body.data[1].totalReplies).to.equal(0) | 216 | expect(body.data[1].totalReplies).to.equal(0) |
237 | expect(res.body.data[2].text).to.equal('super thread 3') | 217 | expect(body.data[2].text).to.equal('super thread 3') |
238 | expect(res.body.data[2].totalReplies).to.equal(0) | 218 | expect(body.data[2].totalReplies).to.equal(0) |
239 | }) | 219 | }) |
240 | 220 | ||
241 | it('Should count replies from the video author correctly', async function () { | 221 | it('Should count replies from the video author correctly', async function () { |
242 | const text = 'my super first comment' | 222 | await command.createThread({ videoId: videoUUID, text: 'my super first comment' }) |
243 | await addVideoCommentThread(server.url, server.accessToken, videoUUID, text) | 223 | |
244 | let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5) | 224 | const { data } = await command.listThreads({ videoId: videoUUID }) |
245 | const comment: VideoComment = res.body.data[0] | 225 | const threadId2 = data[0].threadId |
246 | const threadId2 = comment.threadId | ||
247 | 226 | ||
248 | const text2 = 'a first answer to thread 4 by a third party' | 227 | const text2 = 'a first answer to thread 4 by a third party' |
249 | await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2) | 228 | await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 }) |
250 | 229 | ||
251 | const text3 = 'my second answer to thread 4' | 230 | const text3 = 'my second answer to thread 4' |
252 | await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3) | 231 | await command.addReply({ videoId, toCommentId: threadId2, text: text3 }) |
253 | 232 | ||
254 | res = await getVideoThreadComments(server.url, videoUUID, threadId2) | 233 | const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 }) |
255 | const tree: VideoCommentThreadTree = res.body | ||
256 | expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) | 234 | expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1) |
257 | }) | 235 | }) |
258 | }) | 236 | }) |
259 | 237 | ||
260 | describe('All instance comments', function () { | 238 | describe('All instance comments', function () { |
261 | async function getComments (options: any = {}) { | ||
262 | const res = await getAdminVideoComments(Object.assign({ | ||
263 | url: server.url, | ||
264 | token: server.accessToken, | ||
265 | start: 0, | ||
266 | count: 10 | ||
267 | }, options)) | ||
268 | |||
269 | return { comments: res.body.data as VideoCommentAdmin[], total: res.body.total as number } | ||
270 | } | ||
271 | 239 | ||
272 | it('Should list instance comments as admin', async function () { | 240 | it('Should list instance comments as admin', async function () { |
273 | const { comments } = await getComments({ start: 0, count: 1 }) | 241 | const { data } = await command.listForAdmin({ start: 0, count: 1 }) |
274 | 242 | ||
275 | expect(comments[0].text).to.equal('my second answer to thread 4') | 243 | expect(data[0].text).to.equal('my second answer to thread 4') |
276 | }) | 244 | }) |
277 | 245 | ||
278 | it('Should filter instance comments by isLocal', async function () { | 246 | it('Should filter instance comments by isLocal', async function () { |
279 | const { total, comments } = await getComments({ isLocal: false }) | 247 | const { total, data } = await command.listForAdmin({ isLocal: false }) |
280 | 248 | ||
281 | expect(comments).to.have.lengthOf(0) | 249 | expect(data).to.have.lengthOf(0) |
282 | expect(total).to.equal(0) | 250 | expect(total).to.equal(0) |
283 | }) | 251 | }) |
284 | 252 | ||
285 | it('Should search instance comments by account', async function () { | 253 | it('Should search instance comments by account', async function () { |
286 | const { total, comments } = await getComments({ searchAccount: 'user' }) | 254 | const { total, data } = await command.listForAdmin({ searchAccount: 'user' }) |
287 | 255 | ||
288 | expect(comments).to.have.lengthOf(1) | 256 | expect(data).to.have.lengthOf(1) |
289 | expect(total).to.equal(1) | 257 | expect(total).to.equal(1) |
290 | 258 | ||
291 | expect(comments[0].text).to.equal('a first answer to thread 4 by a third party') | 259 | expect(data[0].text).to.equal('a first answer to thread 4 by a third party') |
292 | }) | 260 | }) |
293 | 261 | ||
294 | it('Should search instance comments by video', async function () { | 262 | it('Should search instance comments by video', async function () { |
295 | { | 263 | { |
296 | const { total, comments } = await getComments({ searchVideo: 'video' }) | 264 | const { total, data } = await command.listForAdmin({ searchVideo: 'video' }) |
297 | 265 | ||
298 | expect(comments).to.have.lengthOf(7) | 266 | expect(data).to.have.lengthOf(7) |
299 | expect(total).to.equal(7) | 267 | expect(total).to.equal(7) |
300 | } | 268 | } |
301 | 269 | ||
302 | { | 270 | { |
303 | const { total, comments } = await getComments({ searchVideo: 'hello' }) | 271 | const { total, data } = await command.listForAdmin({ searchVideo: 'hello' }) |
304 | 272 | ||
305 | expect(comments).to.have.lengthOf(0) | 273 | expect(data).to.have.lengthOf(0) |
306 | expect(total).to.equal(0) | 274 | expect(total).to.equal(0) |
307 | } | 275 | } |
308 | }) | 276 | }) |
309 | 277 | ||
310 | it('Should search instance comments', async function () { | 278 | it('Should search instance comments', async function () { |
311 | const { total, comments } = await getComments({ search: 'super thread 3' }) | 279 | const { total, data } = await command.listForAdmin({ search: 'super thread 3' }) |
312 | 280 | ||
313 | expect(comments).to.have.lengthOf(1) | ||
314 | expect(total).to.equal(1) | 281 | expect(total).to.equal(1) |
315 | expect(comments[0].text).to.equal('super thread 3') | 282 | |
283 | expect(data).to.have.lengthOf(1) | ||
284 | expect(data[0].text).to.equal('super thread 3') | ||
316 | }) | 285 | }) |
317 | }) | 286 | }) |
318 | 287 | ||