]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/user-subscriptions.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / user-subscriptions.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
06a05d5f
C
2
3import * as chai from 'chai'
4import 'mocha'
9639bd17 5import {
7c3b7976 6 cleanupTests,
9639bd17 7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 follow,
11 getVideosList,
12 unfollow,
13 updateVideo,
14 userLogin
94565d52 15} from '../../../../shared/extra-utils'
a1587156 16import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
94565d52 17import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
06a05d5f 18import { Video, VideoChannel } from '../../../../shared/models/videos'
94565d52 19import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
06a05d5f
C
20import {
21 addUserSubscription,
a1587156
C
22 areSubscriptionsExist,
23 getUserSubscription,
06a05d5f
C
24 listUserSubscriptions,
25 listUserSubscriptionVideos,
a1587156 26 removeUserSubscription
94565d52 27} from '../../../../shared/extra-utils/users/user-subscriptions'
06a05d5f
C
28
29const expect = chai.expect
30
31describe('Test users subscriptions', function () {
32 let servers: ServerInfo[] = []
8a19bee1 33 const users: { accessToken: string }[] = []
f6eebcb3 34 let video3UUID: string
06a05d5f
C
35
36 before(async function () {
37 this.timeout(120000)
38
39 servers = await flushAndRunMultipleServers(3)
40
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
43
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
46
06a05d5f
C
47 {
48 for (const server of servers) {
49 const user = { username: 'user' + server.serverNumber, password: 'password' }
1eddc9a7 50 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
06a05d5f
C
51
52 const accessToken = await userLogin(server, user)
8a19bee1 53 users.push({ accessToken })
06a05d5f
C
54
55 const videoName1 = 'video 1-' + server.serverNumber
56 await uploadVideo(server.url, accessToken, { name: videoName1 })
57
58 const videoName2 = 'video 2-' + server.serverNumber
59 await uploadVideo(server.url, accessToken, { name: videoName2 })
60 }
61 }
62
63 await waitJobs(servers)
64 })
65
66 it('Should display videos of server 2 on server 1', async function () {
67 const res = await getVideosList(servers[0].url)
68
69 expect(res.body.total).to.equal(4)
70 })
71
72 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
8a19bee1 73 this.timeout(60000)
06a05d5f 74
48f07b4a
C
75 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
76 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
06a05d5f
C
77
78 await waitJobs(servers)
79
f6eebcb3
C
80 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
81 video3UUID = res.body.video.uuid
06a05d5f
C
82
83 await waitJobs(servers)
84 })
85
86 it('Should not display videos of server 3 on server 1', async function () {
87 const res = await getVideosList(servers[0].url)
88
89 expect(res.body.total).to.equal(4)
90 for (const video of res.body.data) {
91 expect(video.name).to.not.contain('1-3')
92 expect(video.name).to.not.contain('2-3')
93 expect(video.name).to.not.contain('video server 3 added after follow')
94 }
95 })
96
97 it('Should list subscriptions', async function () {
98 {
99 const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
100 expect(res.body.total).to.equal(0)
101 expect(res.body.data).to.be.an('array')
102 expect(res.body.data).to.have.lengthOf(0)
103 }
104
105 {
c25e8ce5 106 const res = await listUserSubscriptions(servers[0].url, users[0].accessToken, 'createdAt')
06a05d5f
C
107 expect(res.body.total).to.equal(2)
108
109 const subscriptions: VideoChannel[] = res.body.data
110 expect(subscriptions).to.be.an('array')
111 expect(subscriptions).to.have.lengthOf(2)
112
8a19bee1
C
113 expect(subscriptions[0].name).to.equal('user3_channel')
114 expect(subscriptions[1].name).to.equal('root_channel')
06a05d5f
C
115 }
116 })
117
99492dbc
C
118 it('Should get subscription', async function () {
119 {
a1587156 120 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
99492dbc
C
121 const videoChannel: VideoChannel = res.body
122
123 expect(videoChannel.name).to.equal('user3_channel')
48f07b4a 124 expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
99492dbc
C
125 expect(videoChannel.displayName).to.equal('Main user3 channel')
126 expect(videoChannel.followingCount).to.equal(0)
127 expect(videoChannel.followersCount).to.equal(1)
128 }
129
130 {
a1587156 131 const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
99492dbc
C
132 const videoChannel: VideoChannel = res.body
133
134 expect(videoChannel.name).to.equal('root_channel')
48f07b4a 135 expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
99492dbc
C
136 expect(videoChannel.displayName).to.equal('Main root channel')
137 expect(videoChannel.followingCount).to.equal(0)
138 expect(videoChannel.followersCount).to.equal(1)
139 }
140 })
141
f37dc0dd
C
142 it('Should return the existing subscriptions', async function () {
143 const uris = [
48f07b4a
C
144 'user3_channel@localhost:' + servers[2].port,
145 'root2_channel@localhost:' + servers[0].port,
146 'root_channel@localhost:' + servers[0].port,
147 'user3_channel@localhost:' + servers[0].port
f37dc0dd
C
148 ]
149
a1587156 150 const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris)
f37dc0dd
C
151 const body = res.body
152
48f07b4a
C
153 expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
154 expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
155 expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
156 expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
f37dc0dd
C
157 })
158
06a05d5f
C
159 it('Should list subscription videos', async function () {
160 {
161 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
162 expect(res.body.total).to.equal(0)
163 expect(res.body.data).to.be.an('array')
164 expect(res.body.data).to.have.lengthOf(0)
165 }
166
167 {
168 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
169 expect(res.body.total).to.equal(3)
170
171 const videos: Video[] = res.body.data
172 expect(videos).to.be.an('array')
173 expect(videos).to.have.lengthOf(3)
174
175 expect(videos[0].name).to.equal('video 1-3')
176 expect(videos[1].name).to.equal('video 2-3')
177 expect(videos[2].name).to.equal('video server 3 added after follow')
178 }
179 })
180
181 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
8a19bee1 182 this.timeout(60000)
06a05d5f
C
183
184 const videoName = 'video server 1 added after follow'
185 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
186
187 await waitJobs(servers)
188
189 {
190 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
191 expect(res.body.total).to.equal(0)
192 expect(res.body.data).to.be.an('array')
193 expect(res.body.data).to.have.lengthOf(0)
194 }
195
196 {
197 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
198 expect(res.body.total).to.equal(4)
199
200 const videos: Video[] = res.body.data
201 expect(videos).to.be.an('array')
202 expect(videos).to.have.lengthOf(4)
203
204 expect(videos[0].name).to.equal('video 1-3')
205 expect(videos[1].name).to.equal('video 2-3')
206 expect(videos[2].name).to.equal('video server 3 added after follow')
207 expect(videos[3].name).to.equal('video server 1 added after follow')
208 }
209
210 {
211 const res = await getVideosList(servers[0].url)
212
213 expect(res.body.total).to.equal(5)
214 for (const video of res.body.data) {
215 expect(video.name).to.not.contain('1-3')
216 expect(video.name).to.not.contain('2-3')
217 expect(video.name).to.not.contain('video server 3 added after follow')
218 }
219 }
220 })
221
222 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
8a19bee1 223 this.timeout(60000)
06a05d5f
C
224
225 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
226
227 await waitJobs(servers)
228
229 const res = await getVideosList(servers[0].url)
230
231 expect(res.body.total).to.equal(8)
232
233 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
234 for (const name of names) {
235 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
236 expect(video).to.not.be.undefined
237 }
238 })
239
240 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
8a19bee1 241 this.timeout(60000)
06a05d5f
C
242
243 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
244
245 await waitJobs(servers)
246
247 const res = await getVideosList(servers[0].url)
248
249 expect(res.body.total).to.equal(5)
250 for (const video of res.body.data) {
251 expect(video.name).to.not.contain('1-3')
252 expect(video.name).to.not.contain('2-3')
253 expect(video.name).to.not.contain('video server 3 added after follow')
254 }
255 })
256
257 it('Should still list subscription videos', async function () {
258 {
259 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
260 expect(res.body.total).to.equal(0)
261 expect(res.body.data).to.be.an('array')
262 expect(res.body.data).to.have.lengthOf(0)
263 }
264
265 {
266 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
267 expect(res.body.total).to.equal(4)
268
269 const videos: Video[] = res.body.data
270 expect(videos).to.be.an('array')
271 expect(videos).to.have.lengthOf(4)
272
273 expect(videos[0].name).to.equal('video 1-3')
274 expect(videos[1].name).to.equal('video 2-3')
275 expect(videos[2].name).to.equal('video server 3 added after follow')
276 expect(videos[3].name).to.equal('video server 1 added after follow')
277 }
278 })
279
f6eebcb3
C
280 it('Should update a video of server 3 and see the updated video on server 1', async function () {
281 this.timeout(30000)
282
283 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
284
285 await waitJobs(servers)
286
287 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
288 const videos: Video[] = res.body.data
289 expect(videos[2].name).to.equal('video server 3 added after follow updated')
290 })
291
06a05d5f 292 it('Should remove user of server 3 subscription', async function () {
f6eebcb3
C
293 this.timeout(30000)
294
48f07b4a 295 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
06a05d5f
C
296
297 await waitJobs(servers)
298 })
299
300 it('Should not display its videos anymore', async function () {
301 {
302 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
303 expect(res.body.total).to.equal(1)
304
305 const videos: Video[] = res.body.data
306 expect(videos).to.be.an('array')
307 expect(videos).to.have.lengthOf(1)
308
309 expect(videos[0].name).to.equal('video server 1 added after follow')
310 }
311 })
312
313 it('Should remove the root subscription and not display the videos anymore', async function () {
f6eebcb3
C
314 this.timeout(30000)
315
48f07b4a 316 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
06a05d5f
C
317
318 await waitJobs(servers)
319
320 {
321 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
322 expect(res.body.total).to.equal(0)
323
324 const videos: Video[] = res.body.data
325 expect(videos).to.be.an('array')
326 expect(videos).to.have.lengthOf(0)
327 }
328 })
329
330 it('Should correctly display public videos on server 1', async function () {
331 const res = await getVideosList(servers[0].url)
332
333 expect(res.body.total).to.equal(5)
334 for (const video of res.body.data) {
335 expect(video.name).to.not.contain('1-3')
336 expect(video.name).to.not.contain('2-3')
f6eebcb3 337 expect(video.name).to.not.contain('video server 3 added after follow updated')
06a05d5f
C
338 }
339 })
340
341 it('Should follow user of server 3 again', async function () {
8a19bee1 342 this.timeout(60000)
06a05d5f 343
48f07b4a 344 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
06a05d5f
C
345
346 await waitJobs(servers)
347
348 {
349 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
350 expect(res.body.total).to.equal(3)
351
352 const videos: Video[] = res.body.data
353 expect(videos).to.be.an('array')
354 expect(videos).to.have.lengthOf(3)
355
356 expect(videos[0].name).to.equal('video 1-3')
357 expect(videos[1].name).to.equal('video 2-3')
f6eebcb3 358 expect(videos[2].name).to.equal('video server 3 added after follow updated')
06a05d5f
C
359 }
360
361 {
362 const res = await getVideosList(servers[0].url)
363
364 expect(res.body.total).to.equal(5)
365 for (const video of res.body.data) {
366 expect(video.name).to.not.contain('1-3')
367 expect(video.name).to.not.contain('2-3')
f6eebcb3 368 expect(video.name).to.not.contain('video server 3 added after follow updated')
06a05d5f
C
369 }
370 }
371 })
372
7c3b7976
C
373 after(async function () {
374 await cleanupTests(servers)
06a05d5f
C
375 })
376})