1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { createUser, doubleFollow, flushAndRunMultipleServers, follow, getVideosList, unfollow, updateVideo, 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,
16 } from '../../utils/users/user-subscriptions'
18 const expect = chai.expect
20 describe('Test users subscriptions', function () {
21 let servers: ServerInfo[] = []
22 const users: { accessToken: string }[] = []
23 let video3UUID: string
25 before(async function () {
28 servers = await flushAndRunMultipleServers(3)
30 // Get the access tokens
31 await setAccessTokensToServers(servers)
33 // Server 1 and server 2 follow each other
34 await doubleFollow(servers[0], servers[1])
37 for (const server of servers) {
38 const user = { username: 'user' + server.serverNumber, password: 'password' }
39 await createUser(server.url, server.accessToken, user.username, user.password)
41 const accessToken = await userLogin(server, user)
42 users.push({ accessToken })
44 const videoName1 = 'video 1-' + server.serverNumber
45 await uploadVideo(server.url, accessToken, { name: videoName1 })
47 const videoName2 = 'video 2-' + server.serverNumber
48 await uploadVideo(server.url, accessToken, { name: videoName2 })
52 await waitJobs(servers)
55 it('Should display videos of server 2 on server 1', async function () {
56 const res = await getVideosList(servers[0].url)
58 expect(res.body.total).to.equal(4)
61 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
64 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
65 await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:9001')
67 await waitJobs(servers)
69 const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
70 video3UUID = res.body.video.uuid
72 await waitJobs(servers)
75 it('Should not display videos of server 3 on server 1', async function () {
76 const res = await getVideosList(servers[0].url)
78 expect(res.body.total).to.equal(4)
79 for (const video of res.body.data) {
80 expect(video.name).to.not.contain('1-3')
81 expect(video.name).to.not.contain('2-3')
82 expect(video.name).to.not.contain('video server 3 added after follow')
86 it('Should list subscriptions', async function () {
88 const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
89 expect(res.body.total).to.equal(0)
90 expect(res.body.data).to.be.an('array')
91 expect(res.body.data).to.have.lengthOf(0)
95 const res = await listUserSubscriptions(servers[0].url, users[0].accessToken)
96 expect(res.body.total).to.equal(2)
98 const subscriptions: VideoChannel[] = res.body.data
99 expect(subscriptions).to.be.an('array')
100 expect(subscriptions).to.have.lengthOf(2)
102 expect(subscriptions[0].name).to.equal('user3_channel')
103 expect(subscriptions[1].name).to.equal('root_channel')
107 it('Should get subscription', async function () {
109 const res = await getUserSubscription(servers[ 0 ].url, users[ 0 ].accessToken, 'user3_channel@localhost:9003')
110 const videoChannel: VideoChannel = res.body
112 expect(videoChannel.name).to.equal('user3_channel')
113 expect(videoChannel.host).to.equal('localhost:9003')
114 expect(videoChannel.displayName).to.equal('Main user3 channel')
115 expect(videoChannel.followingCount).to.equal(0)
116 expect(videoChannel.followersCount).to.equal(1)
120 const res = await getUserSubscription(servers[ 0 ].url, users[ 0 ].accessToken, 'root_channel@localhost:9001')
121 const videoChannel: VideoChannel = res.body
123 expect(videoChannel.name).to.equal('root_channel')
124 expect(videoChannel.host).to.equal('localhost:9001')
125 expect(videoChannel.displayName).to.equal('Main root channel')
126 expect(videoChannel.followingCount).to.equal(0)
127 expect(videoChannel.followersCount).to.equal(1)
131 it('Should list subscription videos', async function () {
133 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
134 expect(res.body.total).to.equal(0)
135 expect(res.body.data).to.be.an('array')
136 expect(res.body.data).to.have.lengthOf(0)
140 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
141 expect(res.body.total).to.equal(3)
143 const videos: Video[] = res.body.data
144 expect(videos).to.be.an('array')
145 expect(videos).to.have.lengthOf(3)
147 expect(videos[0].name).to.equal('video 1-3')
148 expect(videos[1].name).to.equal('video 2-3')
149 expect(videos[2].name).to.equal('video server 3 added after follow')
153 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
156 const videoName = 'video server 1 added after follow'
157 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
159 await waitJobs(servers)
162 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
163 expect(res.body.total).to.equal(0)
164 expect(res.body.data).to.be.an('array')
165 expect(res.body.data).to.have.lengthOf(0)
169 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
170 expect(res.body.total).to.equal(4)
172 const videos: Video[] = res.body.data
173 expect(videos).to.be.an('array')
174 expect(videos).to.have.lengthOf(4)
176 expect(videos[0].name).to.equal('video 1-3')
177 expect(videos[1].name).to.equal('video 2-3')
178 expect(videos[2].name).to.equal('video server 3 added after follow')
179 expect(videos[3].name).to.equal('video server 1 added after follow')
183 const res = await getVideosList(servers[0].url)
185 expect(res.body.total).to.equal(5)
186 for (const video of res.body.data) {
187 expect(video.name).to.not.contain('1-3')
188 expect(video.name).to.not.contain('2-3')
189 expect(video.name).to.not.contain('video server 3 added after follow')
194 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
197 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
199 await waitJobs(servers)
201 const res = await getVideosList(servers[0].url)
203 expect(res.body.total).to.equal(8)
205 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
206 for (const name of names) {
207 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
208 expect(video).to.not.be.undefined
212 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
215 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
217 await waitJobs(servers)
219 const res = await getVideosList(servers[0].url)
221 expect(res.body.total).to.equal(5)
222 for (const video of res.body.data) {
223 expect(video.name).to.not.contain('1-3')
224 expect(video.name).to.not.contain('2-3')
225 expect(video.name).to.not.contain('video server 3 added after follow')
229 it('Should still list subscription videos', async function () {
231 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
232 expect(res.body.total).to.equal(0)
233 expect(res.body.data).to.be.an('array')
234 expect(res.body.data).to.have.lengthOf(0)
238 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
239 expect(res.body.total).to.equal(4)
241 const videos: Video[] = res.body.data
242 expect(videos).to.be.an('array')
243 expect(videos).to.have.lengthOf(4)
245 expect(videos[0].name).to.equal('video 1-3')
246 expect(videos[1].name).to.equal('video 2-3')
247 expect(videos[2].name).to.equal('video server 3 added after follow')
248 expect(videos[3].name).to.equal('video server 1 added after follow')
252 it('Should update a video of server 3 and see the updated video on server 1', async function () {
255 await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
257 await waitJobs(servers)
259 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
260 const videos: Video[] = res.body.data
261 expect(videos[2].name).to.equal('video server 3 added after follow updated')
264 it('Should remove user of server 3 subscription', async function () {
267 await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
269 await waitJobs(servers)
272 it('Should not display its videos anymore', async function () {
274 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
275 expect(res.body.total).to.equal(1)
277 const videos: Video[] = res.body.data
278 expect(videos).to.be.an('array')
279 expect(videos).to.have.lengthOf(1)
281 expect(videos[0].name).to.equal('video server 1 added after follow')
285 it('Should remove the root subscription and not display the videos anymore', async function () {
288 await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:9001')
290 await waitJobs(servers)
293 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
294 expect(res.body.total).to.equal(0)
296 const videos: Video[] = res.body.data
297 expect(videos).to.be.an('array')
298 expect(videos).to.have.lengthOf(0)
302 it('Should correctly display public videos on server 1', async function () {
303 const res = await getVideosList(servers[0].url)
305 expect(res.body.total).to.equal(5)
306 for (const video of res.body.data) {
307 expect(video.name).to.not.contain('1-3')
308 expect(video.name).to.not.contain('2-3')
309 expect(video.name).to.not.contain('video server 3 added after follow updated')
313 it('Should follow user of server 3 again', async function () {
316 await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:9003')
318 await waitJobs(servers)
321 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
322 expect(res.body.total).to.equal(3)
324 const videos: Video[] = res.body.data
325 expect(videos).to.be.an('array')
326 expect(videos).to.have.lengthOf(3)
328 expect(videos[0].name).to.equal('video 1-3')
329 expect(videos[1].name).to.equal('video 2-3')
330 expect(videos[2].name).to.equal('video server 3 added after follow updated')
334 const res = await getVideosList(servers[0].url)
336 expect(res.body.total).to.equal(5)
337 for (const video of res.body.data) {
338 expect(video.name).to.not.contain('1-3')
339 expect(video.name).to.not.contain('2-3')
340 expect(video.name).to.not.contain('video server 3 added after follow updated')
345 after(async function () {
346 killallServers(servers)