]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/handle-down.ts
Add tests to handle down server
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / handle-down.ts
CommitLineData
2ccaeeb3
C
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import { VideoPrivacy } from '../../../../shared/models/videos'
7bc29171
C
6import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7import { completeVideoCheck, getVideo, immutableAssign, reRunServer, viewVideo } from '../../utils'
2ccaeeb3
C
8
9import {
10 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
11 wait
12} from '../../utils/index'
13import { follow, getFollowersListPaginationAndSort } from '../../utils/server/follows'
14import { getJobsListPaginationAndSort } from '../../utils/server/jobs'
7bc29171
C
15import {
16 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
17 getVideoThreadComments
18} from '../../utils/videos/video-comments'
2ccaeeb3
C
19
20const expect = chai.expect
21
22describe('Test handle downs', function () {
23 let servers: ServerInfo[] = []
7bc29171
C
24 const videos = []
25 let threadIdServer1: number
26 let threadIdServer2: number
27 let commentIdServer1: number
28 let commentIdServer2: number
2ccaeeb3
C
29
30 const videoAttributes = {
31 name: 'my super name for server 1',
32 category: 5,
33 licence: 4,
34 language: 9,
35 nsfw: true,
7bc29171 36 privacy: VideoPrivacy.PUBLIC,
2ccaeeb3
C
37 description: 'my super description for server 1',
38 tags: [ 'tag1p1', 'tag2p1' ],
39 fixture: 'video_short1.webm'
40 }
41
7bc29171
C
42 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
43 privacy: VideoPrivacy.UNLISTED
44 })
45
2ccaeeb3
C
46 const checkAttributes = {
47 name: 'my super name for server 1',
48 category: 5,
49 licence: 4,
50 language: 9,
51 nsfw: true,
52 description: 'my super description for server 1',
53 host: 'localhost:9001',
54 account: 'root',
55 isLocal: false,
56 duration: 10,
57 tags: [ 'tag1p1', 'tag2p1' ],
58 privacy: VideoPrivacy.PUBLIC,
59 commentsEnabled: true,
60 channel: {
61 name: 'Default root channel',
62 description: '',
63 isLocal: false
64 },
65 fixture: 'video_short1.webm',
66 files: [
67 {
68 resolution: 720,
69 size: 572456
70 }
71 ]
72 }
73
7bc29171
C
74 const unlistedCheckAttributes = immutableAssign(checkAttributes, {
75 privacy: VideoPrivacy.UNLISTED
76 })
77
2ccaeeb3
C
78 before(async function () {
79 this.timeout(20000)
80
81 servers = await flushAndRunMultipleServers(2)
82
83 // Get the access tokens
84 await setAccessTokensToServers(servers)
85 })
86
87 it('Should remove followers that are often down', async function () {
88 this.timeout(60000)
89
90 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
91
92 await wait(5000)
93
94 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
95
96 await wait(5000)
97
98 for (const server of servers) {
99 const res = await getVideosList(server.url)
100 expect(res.body.data).to.be.an('array')
101 expect(res.body.data).to.have.lengthOf(1)
102 }
103
104 // Kill server 1
105 killallServers([ servers[1] ])
106
7bc29171
C
107 let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes)
108 videos.push(resVideo.body.video)
109
2ccaeeb3
C
110 // Remove server 2 follower
111 for (let i = 0; i < 10; i++) {
7bc29171
C
112 resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
113 videos.push(resVideo.body.video)
114 }
115
116 // Add comments to video 2
117 {
118 const text = 'thread 1'
119 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videos[1].uuid, text)
120 let comment = resComment.body.comment
121 threadIdServer1 = comment.id
122
123 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-1')
124 comment = resComment.body.comment
125
126 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-2')
127 commentIdServer1 = resComment.body.comment.id
2ccaeeb3
C
128 }
129
130 await wait(10000)
131
132 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
133 expect(res.body.data).to.be.an('array')
134 expect(res.body.data).to.have.lengthOf(0)
135 })
136
137 it('Should not have pending/processing jobs anymore', async function () {
138 const res = await getJobsListPaginationAndSort(servers[0].url, servers[0].accessToken, 0, 50, '-createdAt')
139 const jobs = res.body.data
140
141 for (const job of jobs) {
142 expect(job.state).not.to.equal('pending')
143 expect(job.state).not.to.equal('processing')
144 }
145 })
146
147 it('Should follow server 1', async function () {
7bc29171
C
148 this.timeout(15000)
149
150 await reRunServer(servers[1])
2ccaeeb3
C
151
152 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
153
154 await wait(5000)
155
156 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
157 expect(res.body.data).to.be.an('array')
158 expect(res.body.data).to.have.lengthOf(1)
159 })
160
161 it('Should send a view to server 2, and automatically fetch the video', async function () {
7bc29171 162 this.timeout(15000)
2ccaeeb3 163
7bc29171 164 await viewVideo(servers[0].url, videos[0].uuid)
2ccaeeb3
C
165
166 await wait(5000)
167
168 const res = await getVideosList(servers[1].url)
7bc29171
C
169 expect(res.body.data).to.be.an('array')
170 // Video is unlisted
171 expect(res.body.data).to.have.lengthOf(1)
172
173 const resVideo = await getVideo(servers[1].url, videos[0].uuid)
174 expect(resVideo.body).not.to.be.undefined
175
176 await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
177 })
178
179 it('Should send comments on a video to server 2, and automatically fetch the video', async function () {
180 this.timeout(25000)
2ccaeeb3 181
7bc29171
C
182 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, commentIdServer1, 'comment 1-3')
183
184 await wait(5000)
185
186 const res = await getVideosList(servers[1].url)
187 expect(res.body.data).to.be.an('array')
188 expect(res.body.data).to.have.lengthOf(2)
2ccaeeb3 189
7bc29171
C
190 const resVideo = await getVideo(servers[1].url, videos[0].uuid)
191 expect(resVideo.body).not.to.be.undefined
192
193 await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
194
195 {
196 let resComment = await getVideoCommentThreads(servers[1].url, videos[1].uuid, 0, 5)
197 expect(resComment.body.data).to.be.an('array')
198 expect(resComment.body.data).to.have.lengthOf(1)
199
200 threadIdServer2 = resComment.body.data[0].id
201
202 resComment = await getVideoThreadComments(servers[1].url, videos[1].uuid, threadIdServer2)
203
204 const tree: VideoCommentThreadTree = resComment.body
205 expect(tree.comment.text).equal('thread 1')
206 expect(tree.children).to.have.lengthOf(1)
207
208 const firstChild = tree.children[0]
209 expect(firstChild.comment.text).to.equal('comment 1-1')
210 expect(firstChild.children).to.have.lengthOf(1)
211
212 const childOfFirstChild = firstChild.children[0]
213 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
214 expect(childOfFirstChild.children).to.have.lengthOf(1)
215
216 const childOfChildFirstChild = childOfFirstChild.children[0]
217 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
218 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
219
220 commentIdServer2 = childOfChildFirstChild.comment.id
221 }
222 })
223
224 it('Should correctly reply to the comment', async function () {
225 this.timeout(15000)
226
227 await addVideoCommentReply(servers[1].url, servers[1].accessToken, videos[1].uuid, commentIdServer2, 'comment 1-4')
228
229 await wait(5000)
230
231 {
232 const resComment = await getVideoThreadComments(servers[0].url, videos[1].uuid, threadIdServer1)
233
234 const tree: VideoCommentThreadTree = resComment.body
235 expect(tree.comment.text).equal('thread 1')
236 expect(tree.children).to.have.lengthOf(1)
237
238 const firstChild = tree.children[0]
239 expect(firstChild.comment.text).to.equal('comment 1-1')
240 expect(firstChild.children).to.have.lengthOf(1)
241
242 const childOfFirstChild = firstChild.children[0]
243 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
244 expect(childOfFirstChild.children).to.have.lengthOf(1)
245
246 const childOfChildFirstChild = childOfFirstChild.children[0]
247 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
248 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
249
250 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
251 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
252 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
253 }
2ccaeeb3
C
254 })
255
256 after(async function () {
257 killallServers(servers)
258
259 // Keep the logs if the test failed
260 if (this['ok']) {
261 await flushTests()
262 }
263 })
264})