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