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