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