]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/handle-down.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / handle-down.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2ccaeeb3
C
2
3import * as chai from 'chai'
4import 'mocha'
cc6373e6 5import { JobState, Video } from '../../../../shared/models'
2ccaeeb3 6import { VideoPrivacy } from '../../../../shared/models/videos'
2b02c520 7import { VideoCommentThreadTree } from '../../../../shared/models/videos/comment/video-comment.model'
ae28cdf3 8
2ccaeeb3 9import {
7c3b7976 10 cleanupTests,
a1587156 11 closeAllSequelize,
9639bd17 12 completeVideoCheck,
94831479 13 flushAndRunMultipleServers,
a4101923 14 getVideo,
94831479 15 getVideosList,
a4101923 16 immutableAssign,
94831479 17 killallServers,
a4101923 18 reRunServer,
94831479
C
19 ServerInfo,
20 setAccessTokensToServers,
a1587156 21 setActorFollowScores,
a4101923 22 unfollow,
ae28cdf3 23 updateVideo,
a1587156
C
24 uploadVideo,
25 uploadVideoAndGetId,
26 wait
94565d52
C
27} from '../../../../shared/extra-utils'
28import { follow, getFollowersListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
29import { getJobsListPaginationAndSort, waitJobs } from '../../../../shared/extra-utils/server/jobs'
7bc29171 30import {
94831479
C
31 addVideoCommentReply,
32 addVideoCommentThread,
33 getVideoCommentThreads,
7bc29171 34 getVideoThreadComments
94565d52 35} from '../../../../shared/extra-utils/videos/video-comments'
f2eb23cd 36import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
2ccaeeb3
C
37
38const expect = chai.expect
39
40describe('Test handle downs', function () {
41 let servers: ServerInfo[] = []
7bc29171
C
42 let threadIdServer1: number
43 let threadIdServer2: number
44 let commentIdServer1: number
45 let commentIdServer2: number
cc6373e6
C
46 let missedVideo1: Video
47 let missedVideo2: Video
48 let unlistedVideo: Video
2ccaeeb3 49
a1587156 50 const videoIdsServer1: number[] = []
6b9c966f 51
2ccaeeb3
C
52 const videoAttributes = {
53 name: 'my super name for server 1',
54 category: 5,
55 licence: 4,
9d3ef9fe 56 language: 'ja',
2ccaeeb3 57 nsfw: true,
7bc29171 58 privacy: VideoPrivacy.PUBLIC,
2ccaeeb3 59 description: 'my super description for server 1',
2422c46b 60 support: 'my super support text for server 1',
2ccaeeb3
C
61 tags: [ 'tag1p1', 'tag2p1' ],
62 fixture: 'video_short1.webm'
63 }
64
7bc29171
C
65 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
66 privacy: VideoPrivacy.UNLISTED
67 })
68
48f07b4a
C
69 let checkAttributes: any
70 let unlistedCheckAttributes: any
7bc29171 71
2ccaeeb3 72 before(async function () {
e212f887 73 this.timeout(30000)
2ccaeeb3 74
cc6373e6 75 servers = await flushAndRunMultipleServers(3)
2ccaeeb3 76
48f07b4a
C
77 checkAttributes = {
78 name: 'my super name for server 1',
79 category: 5,
80 licence: 4,
81 language: 'ja',
82 nsfw: true,
83 description: 'my super description for server 1',
84 support: 'my super support text for server 1',
85 account: {
86 name: 'root',
87 host: 'localhost:' + servers[0].port
88 },
89 isLocal: false,
90 duration: 10,
91 tags: [ 'tag1p1', 'tag2p1' ],
92 privacy: VideoPrivacy.PUBLIC,
93 commentsEnabled: true,
94 downloadEnabled: true,
95 channel: {
96 name: 'root_channel',
97 displayName: 'Main root channel',
98 description: '',
99 isLocal: false
100 },
101 fixture: 'video_short1.webm',
102 files: [
103 {
104 resolution: 720,
105 size: 572456
106 }
107 ]
108 }
109 unlistedCheckAttributes = immutableAssign(checkAttributes, {
110 privacy: VideoPrivacy.UNLISTED
111 })
112
2ccaeeb3
C
113 // Get the access tokens
114 await setAccessTokensToServers(servers)
115 })
116
117 it('Should remove followers that are often down', async function () {
2284f202 118 this.timeout(240000)
2ccaeeb3 119
cc6373e6 120 // Server 2 and 3 follow server 1
2ccaeeb3 121 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
cc6373e6 122 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
2ccaeeb3 123
3cd0734f 124 await waitJobs(servers)
2ccaeeb3 125
cc6373e6 126 // Upload a video to server 1
2ccaeeb3
C
127 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
128
3cd0734f 129 await waitJobs(servers)
2ccaeeb3 130
cc6373e6 131 // And check all servers have this video
2ccaeeb3
C
132 for (const server of servers) {
133 const res = await getVideosList(server.url)
134 expect(res.body.data).to.be.an('array')
135 expect(res.body.data).to.have.lengthOf(1)
136 }
137
cc6373e6 138 // Kill server 2
2ccaeeb3
C
139 killallServers([ servers[1] ])
140
141 // Remove server 2 follower
142 for (let i = 0; i < 10; i++) {
a1587156 143 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
7bc29171
C
144 }
145
f67d1dca 146 await waitJobs([ servers[0], servers[2] ])
6502c3d4 147
cc6373e6
C
148 // Kill server 3
149 killallServers([ servers[2] ])
150
a1587156 151 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
cc6373e6
C
152 missedVideo1 = resLastVideo1.body.video
153
a1587156 154 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
cc6373e6
C
155 missedVideo2 = resLastVideo2.body.video
156
157 // Unlisted video
a1587156 158 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes)
cc6373e6 159 unlistedVideo = resVideo.body.video
6502c3d4 160
7bc29171
C
161 // Add comments to video 2
162 {
163 const text = 'thread 1'
cc6373e6 164 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
7bc29171
C
165 let comment = resComment.body.comment
166 threadIdServer1 = comment.id
167
cc6373e6 168 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
7bc29171
C
169 comment = resComment.body.comment
170
cc6373e6 171 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
7bc29171 172 commentIdServer1 = resComment.body.comment.id
2ccaeeb3
C
173 }
174
3cd0734f
C
175 await waitJobs(servers[0])
176 // Wait scheduler
9a4a9b6c 177 await wait(11000)
2ccaeeb3 178
cc6373e6 179 // Only server 3 is still a follower of server 1
a1587156 180 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' })
2ccaeeb3 181 expect(res.body.data).to.be.an('array')
cc6373e6 182 expect(res.body.data).to.have.lengthOf(1)
7243f84d 183 expect(res.body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
2ccaeeb3
C
184 })
185
186 it('Should not have pending/processing jobs anymore', async function () {
94831479 187 const states: JobState[] = [ 'waiting', 'active' ]
2ccaeeb3 188
94a5ff8a 189 for (const state of states) {
1061c73f 190 const res = await getJobsListPaginationAndSort({
a1587156
C
191 url: servers[0].url,
192 accessToken: servers[0].accessToken,
1061c73f
C
193 state: state,
194 start: 0,
195 count: 50,
196 sort: '-createdAt'
197 })
94a5ff8a 198 expect(res.body.data).to.have.length(0)
2ccaeeb3
C
199 }
200 })
201
cc6373e6 202 it('Should re-follow server 1', async function () {
cf7a61b5 203 this.timeout(35000)
7bc29171
C
204
205 await reRunServer(servers[1])
cc6373e6
C
206 await reRunServer(servers[2])
207
208 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
209 await waitJobs(servers)
2ccaeeb3
C
210
211 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
212
3cd0734f 213 await waitJobs(servers)
2ccaeeb3 214
a1587156 215 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' })
2ccaeeb3 216 expect(res.body.data).to.be.an('array')
cc6373e6 217 expect(res.body.data).to.have.lengthOf(2)
2ccaeeb3
C
218 })
219
8cf99873 220 it('Should send an update to server 3, and automatically fetch the video', async function () {
7bc29171 221 this.timeout(15000)
2ccaeeb3 222
cc6373e6
C
223 const res1 = await getVideosList(servers[2].url)
224 expect(res1.body.data).to.be.an('array')
225 expect(res1.body.data).to.have.lengthOf(11)
226
a1587156
C
227 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {})
228 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {})
2ccaeeb3 229
3cd0734f 230 await waitJobs(servers)
2ccaeeb3 231
cc6373e6 232 const res = await getVideosList(servers[2].url)
7bc29171 233 expect(res.body.data).to.be.an('array')
cc6373e6
C
234 // 1 video is unlisted
235 expect(res.body.data).to.have.lengthOf(12)
7bc29171 236
cc6373e6
C
237 // Check unlisted video
238 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
7bc29171
C
239 expect(resVideo.body).not.to.be.undefined
240
cc6373e6 241 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
7bc29171
C
242 })
243
cc6373e6 244 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
7bc29171 245 this.timeout(25000)
2ccaeeb3 246
cc6373e6 247 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
7bc29171 248
3cd0734f 249 await waitJobs(servers)
7bc29171 250
cc6373e6 251 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
7bc29171
C
252 expect(resVideo.body).not.to.be.undefined
253
7bc29171 254 {
cc6373e6 255 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
7bc29171
C
256 expect(resComment.body.data).to.be.an('array')
257 expect(resComment.body.data).to.have.lengthOf(1)
258
259 threadIdServer2 = resComment.body.data[0].id
260
cc6373e6 261 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
7bc29171
C
262
263 const tree: VideoCommentThreadTree = resComment.body
264 expect(tree.comment.text).equal('thread 1')
265 expect(tree.children).to.have.lengthOf(1)
266
267 const firstChild = tree.children[0]
268 expect(firstChild.comment.text).to.equal('comment 1-1')
269 expect(firstChild.children).to.have.lengthOf(1)
270
271 const childOfFirstChild = firstChild.children[0]
272 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
273 expect(childOfFirstChild.children).to.have.lengthOf(1)
274
275 const childOfChildFirstChild = childOfFirstChild.children[0]
276 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
277 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
278
279 commentIdServer2 = childOfChildFirstChild.comment.id
280 }
281 })
282
283 it('Should correctly reply to the comment', async function () {
284 this.timeout(15000)
285
cc6373e6 286 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
7bc29171 287
3cd0734f 288 await waitJobs(servers)
7bc29171
C
289
290 {
cc6373e6 291 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
7bc29171
C
292
293 const tree: VideoCommentThreadTree = resComment.body
294 expect(tree.comment.text).equal('thread 1')
295 expect(tree.children).to.have.lengthOf(1)
296
297 const firstChild = tree.children[0]
298 expect(firstChild.comment.text).to.equal('comment 1-1')
299 expect(firstChild.children).to.have.lengthOf(1)
300
301 const childOfFirstChild = firstChild.children[0]
302 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
303 expect(childOfFirstChild.children).to.have.lengthOf(1)
304
305 const childOfChildFirstChild = childOfFirstChild.children[0]
306 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
307 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
308
309 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
310 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
311 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
312 }
2ccaeeb3
C
313 })
314
6b9c966f
C
315 it('Should upload many videos on server 1', async function () {
316 this.timeout(120000)
317
318 for (let i = 0; i < 10; i++) {
a1587156 319 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid
6b9c966f
C
320 videoIdsServer1.push(uuid)
321 }
322
323 await waitJobs(servers)
324
325 for (const id of videoIdsServer1) {
a1587156 326 await getVideo(servers[1].url, id)
6b9c966f
C
327 }
328
329 await waitJobs(servers)
330 await setActorFollowScores(servers[1].internalServerNumber, 20)
331
332 // Wait video expiration
333 await wait(11000)
334
335 // Refresh video -> score + 10 = 30
336 await getVideo(servers[1].url, videoIdsServer1[0])
337
338 await waitJobs(servers)
339 })
340
341 it('Should remove followings that are down', async function () {
342 this.timeout(120000)
343
344 killallServers([ servers[0] ])
345
346 // Wait video expiration
347 await wait(11000)
348
a3c997b3
C
349 for (let i = 0; i < 5; i++) {
350 try {
351 await getVideo(servers[1].url, videoIdsServer1[i])
352 await waitJobs([ servers[1] ])
353 await wait(1500)
354 } catch {}
6b9c966f
C
355 }
356
357 for (const id of videoIdsServer1) {
f2eb23cd 358 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403)
6b9c966f
C
359 }
360 })
361
7c3b7976 362 after(async function () {
6b9c966f
C
363 await closeAllSequelize([ servers[1] ])
364
7c3b7976 365 await cleanupTests(servers)
2ccaeeb3
C
366 })
367})