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