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