]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/handle-down.ts
6ca8cfb648dcc80718f576b5b9f3b937dc9dfc7b
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / handle-down.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoPrivacy } from '../../../../shared/models/videos'
6 import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7 import { completeVideoCheck, getVideo, immutableAssign, reRunServer, viewVideo } from '../../utils'
8
9 import {
10 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
11 wait
12 } from '../../utils/index'
13 import { follow, getFollowersListPaginationAndSort } from '../../utils/server/follows'
14 import { getJobsListPaginationAndSort } from '../../utils/server/jobs'
15 import {
16 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
17 getVideoThreadComments
18 } from '../../utils/videos/video-comments'
19
20 const expect = chai.expect
21
22 describe('Test handle downs', function () {
23 let servers: ServerInfo[] = []
24 const videos = []
25 let threadIdServer1: number
26 let threadIdServer2: number
27 let commentIdServer1: number
28 let commentIdServer2: number
29
30 const videoAttributes = {
31 name: 'my super name for server 1',
32 category: 5,
33 licence: 4,
34 language: 9,
35 nsfw: true,
36 privacy: VideoPrivacy.PUBLIC,
37 description: 'my super description for server 1',
38 tags: [ 'tag1p1', 'tag2p1' ],
39 fixture: 'video_short1.webm'
40 }
41
42 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
43 privacy: VideoPrivacy.UNLISTED
44 })
45
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
74 const unlistedCheckAttributes = immutableAssign(checkAttributes, {
75 privacy: VideoPrivacy.UNLISTED
76 })
77
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
107 let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes)
108 videos.push(resVideo.body.video)
109
110 // Remove server 2 follower
111 for (let i = 0; i < 10; i++) {
112 resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
113 videos.push(resVideo.body.video)
114 }
115
116 await wait(2000)
117
118 await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
119
120 // Add comments to video 2
121 {
122 const text = 'thread 1'
123 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videos[1].uuid, text)
124 let comment = resComment.body.comment
125 threadIdServer1 = comment.id
126
127 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-1')
128 comment = resComment.body.comment
129
130 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, comment.id, 'comment 1-2')
131 commentIdServer1 = resComment.body.comment.id
132 }
133
134 await wait(10000)
135
136 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
137 expect(res.body.data).to.be.an('array')
138 expect(res.body.data).to.have.lengthOf(0)
139 })
140
141 it('Should not have pending/processing jobs anymore', async function () {
142 const res = await getJobsListPaginationAndSort(servers[0].url, servers[0].accessToken, 0, 50, '-createdAt')
143 const jobs = res.body.data
144
145 for (const job of jobs) {
146 expect(job.state).not.to.equal('pending')
147 expect(job.state).not.to.equal('processing')
148 }
149 })
150
151 it('Should follow server 1', async function () {
152 this.timeout(15000)
153
154 await reRunServer(servers[1])
155
156 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
157
158 await wait(5000)
159
160 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
161 expect(res.body.data).to.be.an('array')
162 expect(res.body.data).to.have.lengthOf(1)
163 })
164
165 it('Should send a view to server 2, and automatically fetch the video', async function () {
166 this.timeout(15000)
167
168 await viewVideo(servers[0].url, videos[0].uuid)
169
170 await wait(5000)
171
172 const res = await getVideosList(servers[1].url)
173 expect(res.body.data).to.be.an('array')
174 // Video is unlisted
175 expect(res.body.data).to.have.lengthOf(1)
176
177 const resVideo = await getVideo(servers[1].url, videos[0].uuid)
178 expect(resVideo.body).not.to.be.undefined
179
180 await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
181 })
182
183 it('Should send comments on a video to server 2, and automatically fetch the video', async function () {
184 this.timeout(25000)
185
186 await addVideoCommentReply(servers[0].url, servers[0].accessToken, videos[1].uuid, commentIdServer1, 'comment 1-3')
187
188 await wait(5000)
189
190 const res = await getVideosList(servers[1].url)
191 expect(res.body.data).to.be.an('array')
192 expect(res.body.data).to.have.lengthOf(2)
193
194 const resVideo = await getVideo(servers[1].url, videos[0].uuid)
195 expect(resVideo.body).not.to.be.undefined
196
197 await completeVideoCheck(servers[1].url, resVideo.body, unlistedCheckAttributes)
198
199 {
200 let resComment = await getVideoCommentThreads(servers[1].url, videos[1].uuid, 0, 5)
201 expect(resComment.body.data).to.be.an('array')
202 expect(resComment.body.data).to.have.lengthOf(1)
203
204 threadIdServer2 = resComment.body.data[0].id
205
206 resComment = await getVideoThreadComments(servers[1].url, videos[1].uuid, threadIdServer2)
207
208 const tree: VideoCommentThreadTree = resComment.body
209 expect(tree.comment.text).equal('thread 1')
210 expect(tree.children).to.have.lengthOf(1)
211
212 const firstChild = tree.children[0]
213 expect(firstChild.comment.text).to.equal('comment 1-1')
214 expect(firstChild.children).to.have.lengthOf(1)
215
216 const childOfFirstChild = firstChild.children[0]
217 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
218 expect(childOfFirstChild.children).to.have.lengthOf(1)
219
220 const childOfChildFirstChild = childOfFirstChild.children[0]
221 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
222 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
223
224 commentIdServer2 = childOfChildFirstChild.comment.id
225 }
226 })
227
228 it('Should correctly reply to the comment', async function () {
229 this.timeout(15000)
230
231 await addVideoCommentReply(servers[1].url, servers[1].accessToken, videos[1].uuid, commentIdServer2, 'comment 1-4')
232
233 await wait(5000)
234
235 {
236 const resComment = await getVideoThreadComments(servers[0].url, videos[1].uuid, threadIdServer1)
237
238 const tree: VideoCommentThreadTree = resComment.body
239 expect(tree.comment.text).equal('thread 1')
240 expect(tree.children).to.have.lengthOf(1)
241
242 const firstChild = tree.children[0]
243 expect(firstChild.comment.text).to.equal('comment 1-1')
244 expect(firstChild.children).to.have.lengthOf(1)
245
246 const childOfFirstChild = firstChild.children[0]
247 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
248 expect(childOfFirstChild.children).to.have.lengthOf(1)
249
250 const childOfChildFirstChild = childOfFirstChild.children[0]
251 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
252 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
253
254 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
255 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
256 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
257 }
258 })
259
260 after(async function () {
261 killallServers(servers)
262
263 // Keep the logs if the test failed
264 if (this['ok']) {
265 await flushTests()
266 }
267 })
268 })