]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/follows.ts
Add get old comment on follow tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoComment, VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
6
7 import {
8 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
9 wait
10 } from '../utils'
11 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../utils/follows'
12 import { getUserAccessToken } from '../utils/login'
13 import { dateIsValid, webtorrentAdd } from '../utils/miscs'
14 import { createUser } from '../utils/users'
15 import { addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, getVideoThreadComments } from '../utils/video-comments'
16 import { getVideo, rateVideo, testVideoImage } from '../utils/videos'
17
18 const expect = chai.expect
19
20 describe('Test follows', function () {
21 let servers: ServerInfo[] = []
22
23 before(async function () {
24 this.timeout(20000)
25
26 servers = await flushAndRunMultipleServers(3)
27
28 // Get the access tokens
29 await setAccessTokensToServers(servers)
30 })
31
32 it('Should not have followers', async function () {
33 for (const server of servers) {
34 const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
35 const follows = res.body.data
36
37 expect(res.body.total).to.equal(0)
38 expect(follows).to.be.an('array')
39 expect(follows.length).to.equal(0)
40 }
41 })
42
43 it('Should not have following', async function () {
44 for (const server of servers) {
45 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
46 const follows = res.body.data
47
48 expect(res.body.total).to.equal(0)
49 expect(follows).to.be.an('array')
50 expect(follows.length).to.equal(0)
51 }
52 })
53
54 it('Should have server 1 following server 2 and 3', async function () {
55 this.timeout(10000)
56
57 await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
58
59 await wait(7000)
60 })
61
62 it('Should have 2 followings on server 1', async function () {
63 let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
64 let follows = res.body.data
65
66 expect(res.body.total).to.equal(2)
67 expect(follows).to.be.an('array')
68 expect(follows.length).to.equal(1)
69
70 res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
71 follows = follows.concat(res.body.data)
72
73 const server2Follow = follows.find(f => f.following.host === 'localhost:9002')
74 const server3Follow = follows.find(f => f.following.host === 'localhost:9003')
75
76 expect(server2Follow).to.not.be.undefined
77 expect(server3Follow).to.not.be.undefined
78 expect(server2Follow.state).to.equal('accepted')
79 expect(server3Follow.state).to.equal('accepted')
80 })
81
82 it('Should have 0 followings on server 1 and 2', async function () {
83 for (const server of [ servers[1], servers[2] ]) {
84 const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
85 const follows = res.body.data
86
87 expect(res.body.total).to.equal(0)
88 expect(follows).to.be.an('array')
89 expect(follows.length).to.equal(0)
90 }
91 })
92
93 it('Should have 1 followers on server 2 and 3', async function () {
94 for (const server of [ servers[1], servers[2] ]) {
95 let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
96
97 let follows = res.body.data
98 expect(res.body.total).to.equal(1)
99 expect(follows).to.be.an('array')
100 expect(follows.length).to.equal(1)
101 expect(follows[0].follower.host).to.equal('localhost:9001')
102 }
103 })
104
105 it('Should have 0 followers on server 1', async function () {
106 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
107 const follows = res.body.data
108
109 expect(res.body.total).to.equal(0)
110 expect(follows).to.be.an('array')
111 expect(follows.length).to.equal(0)
112 })
113
114 it('Should unfollow server 3 on server 1', async function () {
115 this.timeout(5000)
116
117 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
118
119 await wait(3000)
120 })
121
122 it('Should not follow server 3 on server 1 anymore', async function () {
123 const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
124 let follows = res.body.data
125
126 expect(res.body.total).to.equal(1)
127 expect(follows).to.be.an('array')
128 expect(follows.length).to.equal(1)
129
130 expect(follows[0].following.host).to.equal('localhost:9002')
131 })
132
133 it('Should not have server 1 as follower on server 3 anymore', async function () {
134 const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
135
136 let follows = res.body.data
137 expect(res.body.total).to.equal(0)
138 expect(follows).to.be.an('array')
139 expect(follows.length).to.equal(0)
140 })
141
142 it('Should upload a video on server 2 ans 3 and propagate only the video of server 2', async function () {
143 this.timeout(10000)
144
145 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
146 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
147
148 await wait(5000)
149
150 let res = await getVideosList(servers[0].url)
151 expect(res.body.total).to.equal(1)
152 expect(res.body.data[0].name).to.equal('server2')
153
154 res = await getVideosList(servers[1].url)
155 expect(res.body.total).to.equal(1)
156 expect(res.body.data[0].name).to.equal('server2')
157
158 res = await getVideosList(servers[2].url)
159 expect(res.body.total).to.equal(1)
160 expect(res.body.data[0].name).to.equal('server3')
161 })
162
163 it('Should propagate previous uploaded videos on a new following', async function () {
164 this.timeout(20000)
165
166 const video4Attributes = {
167 name: 'server3-4',
168 category: 2,
169 nsfw: true,
170 licence: 6,
171 tags: [ 'tag1', 'tag2', 'tag3' ]
172 }
173
174 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
175 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
176 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
177 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
178 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
179
180 {
181 const user = { username: 'captain', password: 'password' }
182 await createUser(servers[2].url, servers[2].accessToken, user.username, user.password)
183 const userAccessToken = await getUserAccessToken(servers[2], user)
184
185 const resVideos = await getVideosList(servers[ 2 ].url)
186 const video4 = resVideos.body.data.find(v => v.name === 'server3-4')
187
188 {
189 await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
190 await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
191 }
192
193 {
194 const text = 'my super first comment'
195 const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
196 const threadId = res.body.comment.id
197
198 const text1 = 'my super answer to thread 1'
199 const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
200 const childCommentId = childCommentRes.body.comment.id
201
202 const text2 = 'my super answer to answer of thread 1'
203 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
204
205 const text3 = 'my second answer to thread 1'
206 await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
207 }
208 }
209
210 await wait(5000)
211
212 // Server 1 follows server 3
213 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
214
215 await wait(7000)
216
217 let res = await getVideosList(servers[0].url)
218 expect(res.body.total).to.equal(7)
219
220 const video2 = res.body.data.find(v => v.name === 'server3-2')
221 const video4 = res.body.data.find(v => v.name === 'server3-4')
222 const video6 = res.body.data.find(v => v.name === 'server3-6')
223
224 expect(video2).to.not.be.undefined
225 expect(video4).to.not.be.undefined
226 expect(video6).to.not.be.undefined
227
228 const res2 = await getVideo(servers[0].url, video4.id)
229 const videoDetails = res2.body
230
231 expect(videoDetails.name).to.equal('server3-4')
232 expect(videoDetails.category).to.equal(2)
233 expect(videoDetails.categoryLabel).to.equal('Films')
234 expect(videoDetails.licence).to.equal(6)
235 expect(videoDetails.licenceLabel).to.equal('Attribution - Non Commercial - No Derivatives')
236 expect(videoDetails.language).to.equal(3)
237 expect(videoDetails.languageLabel).to.equal('Mandarin')
238 expect(videoDetails.nsfw).to.be.ok
239 expect(videoDetails.description).to.equal('my super description')
240 expect(videoDetails.serverHost).to.equal('localhost:9003')
241 expect(videoDetails.accountName).to.equal('root')
242 expect(videoDetails.likes).to.equal(1)
243 expect(videoDetails.dislikes).to.equal(1)
244 expect(videoDetails.isLocal).to.be.false
245 expect(videoDetails.tags).to.deep.equal([ 'tag1', 'tag2', 'tag3' ])
246 expect(dateIsValid(videoDetails.createdAt)).to.be.true
247 expect(dateIsValid(videoDetails.updatedAt)).to.be.true
248 expect(videoDetails.files).to.have.lengthOf(1)
249
250 const file = videoDetails.files[0]
251 const magnetUri = file.magnetUri
252 expect(file.magnetUri).to.have.lengthOf.above(2)
253 expect(file.torrentUrl).to.equal(`${servers[2].url}/static/torrents/${videoDetails.uuid}-${file.resolution}.torrent`)
254 expect(file.fileUrl).to.equal(`${servers[2].url}/static/webseed/${videoDetails.uuid}-${file.resolution}.webm`)
255 expect(file.resolution).to.equal(720)
256 expect(file.resolutionLabel).to.equal('720p')
257 expect(file.size).to.equal(218910)
258
259 const test = await testVideoImage(servers[2].url, 'video_short.webm', videoDetails.thumbnailPath)
260 expect(test).to.equal(true)
261
262 const torrent = await webtorrentAdd(magnetUri)
263 expect(torrent.files).to.be.an('array')
264 expect(torrent.files.length).to.equal(1)
265 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
266
267 {
268 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
269
270 expect(res1.body.total).to.equal(1)
271 expect(res1.body.data).to.be.an('array')
272 expect(res1.body.data).to.have.lengthOf(1)
273
274 const comment: VideoComment = res1.body.data[0]
275 expect(comment.inReplyToCommentId).to.be.null
276 expect(comment.text).equal('my super first comment')
277 expect(comment.videoId).to.equal(video4.id)
278 expect(comment.id).to.equal(comment.threadId)
279 expect(comment.account.name).to.equal('root')
280 expect(comment.account.host).to.equal('localhost:9003')
281 expect(comment.totalReplies).to.equal(3)
282 expect(dateIsValid(comment.createdAt as string)).to.be.true
283 expect(dateIsValid(comment.updatedAt as string)).to.be.true
284
285 const threadId = comment.threadId
286
287 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
288
289 const tree: VideoCommentThreadTree = res2.body
290 expect(tree.comment.text).equal('my super first comment')
291 expect(tree.children).to.have.lengthOf(2)
292
293 const firstChild = tree.children[0]
294 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
295 expect(firstChild.children).to.have.lengthOf(1)
296
297 const childOfFirstChild = firstChild.children[0]
298 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
299 expect(childOfFirstChild.children).to.have.lengthOf(0)
300
301 const secondChild = tree.children[1]
302 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
303 expect(secondChild.children).to.have.lengthOf(0)
304 }
305 })
306
307 after(async function () {
308 killallServers(servers)
309
310 // Keep the logs if the test failed
311 if (this['ok']) {
312 await flushTests()
313 }
314 })
315 })