1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { createUser, doubleFollow, flushAndRunMultipleServers, follow, getVideosList, unfollow, userLogin } from '../../utils'
6 import { killallServers, ServerInfo, uploadVideo } from '../../utils/index'
7 import { setAccessTokensToServers } from '../../utils/users/login'
8 import { Video, VideoChannel } from '../../../../shared/models/videos'
9 import { waitJobs } from '../../utils/server/jobs'
12 listUserSubscriptions,
13 listUserSubscriptionVideos,
14 removeUserSubscription
15 } from '../../utils/users/user-subscriptions'
17 const expect = chai.expect
19 describe('Test users subscriptions', function () {
20 let servers: ServerInfo[] = []
21 const users: { accessToken: string }[] = []
23 before(async function () {
26 servers = await flushAndRunMultipleServers(3)
28 // Get the access tokens
29 await setAccessTokensToServers(servers)
31 // Server 1 and server 2 follow each other
32 await doubleFollow(servers[0], servers[1])
35 for (const server of servers) {
36 const user = { username: 'user' + server.serverNumber, password: 'password' }
37 await createUser(server.url, server.accessToken, user.username, user.password)
39 const accessToken = await userLogin(server, user)
40 users.push({ accessToken })
42 const videoName1 = 'video 1-' + server.serverNumber
43 await uploadVideo(server.url, accessToken, { name: videoName1 })
45 const videoName2 = 'video 2-' + server.serverNumber
46 await uploadVideo(server.url, accessToken, { name: videoName2 })
50 await waitJobs(servers)
53 it('Should display videos of server 2 on server 1', async function () {
54 const res = await getVideosList(servers[0].url)
56 expect(res.body.total).to.equal(4)
59 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
62 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
63 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:9001')
65 await waitJobs(servers)
67 await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
69 await waitJobs(servers)
72 it('Should not display videos of server 3 on server 1', async function () {
73 const res = await getVideosList(servers[0].url)
75 expect(res.body.total).to.equal(4)
76 for (const video of res.body.data) {
77 expect(video.name).to.not.contain('1-3')
78 expect(video.name).to.not.contain('2-3')
79 expect(video.name).to.not.contain('video server 3 added after follow')
83 it('Should list subscriptions', async function () {
85 const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
86 expect(res.body.total).to.equal(0)
87 expect(res.body.data).to.be.an('array')
88 expect(res.body.data).to.have.lengthOf(0)
92 const res = await listUserSubscriptions(servers[0].url, users[0].accessToken)
93 expect(res.body.total).to.equal(2)
95 const subscriptions: VideoChannel[] = res.body.data
96 expect(subscriptions).to.be.an('array')
97 expect(subscriptions).to.have.lengthOf(2)
99 expect(subscriptions[0].name).to.equal('user3_channel')
100 expect(subscriptions[1].name).to.equal('root_channel')
104 it('Should list subscription videos', async function () {
106 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
107 expect(res.body.total).to.equal(0)
108 expect(res.body.data).to.be.an('array')
109 expect(res.body.data).to.have.lengthOf(0)
113 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
114 expect(res.body.total).to.equal(3)
116 const videos: Video[] = res.body.data
117 expect(videos).to.be.an('array')
118 expect(videos).to.have.lengthOf(3)
120 expect(videos[0].name).to.equal('video 1-3')
121 expect(videos[1].name).to.equal('video 2-3')
122 expect(videos[2].name).to.equal('video server 3 added after follow')
126 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
129 const videoName = 'video server 1 added after follow'
130 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
132 await waitJobs(servers)
135 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
136 expect(res.body.total).to.equal(0)
137 expect(res.body.data).to.be.an('array')
138 expect(res.body.data).to.have.lengthOf(0)
142 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
143 expect(res.body.total).to.equal(4)
145 const videos: Video[] = res.body.data
146 expect(videos).to.be.an('array')
147 expect(videos).to.have.lengthOf(4)
149 expect(videos[0].name).to.equal('video 1-3')
150 expect(videos[1].name).to.equal('video 2-3')
151 expect(videos[2].name).to.equal('video server 3 added after follow')
152 expect(videos[3].name).to.equal('video server 1 added after follow')
156 const res = await getVideosList(servers[0].url)
158 expect(res.body.total).to.equal(5)
159 for (const video of res.body.data) {
160 expect(video.name).to.not.contain('1-3')
161 expect(video.name).to.not.contain('2-3')
162 expect(video.name).to.not.contain('video server 3 added after follow')
167 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
170 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
172 await waitJobs(servers)
174 const res = await getVideosList(servers[0].url)
176 expect(res.body.total).to.equal(8)
178 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
179 for (const name of names) {
180 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
181 expect(video).to.not.be.undefined
185 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
188 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
190 await waitJobs(servers)
192 const res = await getVideosList(servers[0].url)
194 expect(res.body.total).to.equal(5)
195 for (const video of res.body.data) {
196 expect(video.name).to.not.contain('1-3')
197 expect(video.name).to.not.contain('2-3')
198 expect(video.name).to.not.contain('video server 3 added after follow')
202 it('Should still list subscription videos', async function () {
204 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
205 expect(res.body.total).to.equal(0)
206 expect(res.body.data).to.be.an('array')
207 expect(res.body.data).to.have.lengthOf(0)
211 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
212 expect(res.body.total).to.equal(4)
214 const videos: Video[] = res.body.data
215 expect(videos).to.be.an('array')
216 expect(videos).to.have.lengthOf(4)
218 expect(videos[0].name).to.equal('video 1-3')
219 expect(videos[1].name).to.equal('video 2-3')
220 expect(videos[2].name).to.equal('video server 3 added after follow')
221 expect(videos[3].name).to.equal('video server 1 added after follow')
225 it('Should remove user of server 3 subscription', async function () {
226 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
228 await waitJobs(servers)
231 it('Should not display its videos anymore', async function () {
233 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
234 expect(res.body.total).to.equal(1)
236 const videos: Video[] = res.body.data
237 expect(videos).to.be.an('array')
238 expect(videos).to.have.lengthOf(1)
240 expect(videos[0].name).to.equal('video server 1 added after follow')
244 it('Should remove the root subscription and not display the videos anymore', async function () {
245 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:9001')
247 await waitJobs(servers)
250 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
251 expect(res.body.total).to.equal(0)
253 const videos: Video[] = res.body.data
254 expect(videos).to.be.an('array')
255 expect(videos).to.have.lengthOf(0)
259 it('Should correctly display public videos on server 1', async function () {
260 const res = await getVideosList(servers[0].url)
262 expect(res.body.total).to.equal(5)
263 for (const video of res.body.data) {
264 expect(video.name).to.not.contain('1-3')
265 expect(video.name).to.not.contain('2-3')
266 expect(video.name).to.not.contain('video server 3 added after follow')
270 it('Should follow user of server 3 again', async function () {
273 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
275 await waitJobs(servers)
278 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
279 expect(res.body.total).to.equal(3)
281 const videos: Video[] = res.body.data
282 expect(videos).to.be.an('array')
283 expect(videos).to.have.lengthOf(3)
285 expect(videos[0].name).to.equal('video 1-3')
286 expect(videos[1].name).to.equal('video 2-3')
287 expect(videos[2].name).to.equal('video server 3 added after follow')
291 const res = await getVideosList(servers[0].url)
293 expect(res.body.total).to.equal(5)
294 for (const video of res.body.data) {
295 expect(video.name).to.not.contain('1-3')
296 expect(video.name).to.not.contain('2-3')
297 expect(video.name).to.not.contain('video server 3 added after follow')
302 after(async function () {
303 killallServers(servers)