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 { getMyUserInformation, 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, videoChannelName: string }[] = []
22 let rootChannelNameServer1: string
24 before(async function () {
27 servers = await flushAndRunMultipleServers(3)
29 // Get the access tokens
30 await setAccessTokensToServers(servers)
32 // Server 1 and server 2 follow each other
33 await doubleFollow(servers[0], servers[1])
35 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
36 rootChannelNameServer1 = res.body.videoChannels[0].name
39 for (const server of servers) {
40 const user = { username: 'user' + server.serverNumber, password: 'password' }
41 await createUser(server.url, server.accessToken, user.username, user.password)
43 const accessToken = await userLogin(server, user)
44 const res = await getMyUserInformation(server.url, accessToken)
45 const videoChannels: VideoChannel[] = res.body.videoChannels
47 users.push({ accessToken, videoChannelName: videoChannels[0].name })
49 const videoName1 = 'video 1-' + server.serverNumber
50 await uploadVideo(server.url, accessToken, { name: videoName1 })
52 const videoName2 = 'video 2-' + server.serverNumber
53 await uploadVideo(server.url, accessToken, { name: videoName2 })
57 await waitJobs(servers)
60 it('Should display videos of server 2 on server 1', async function () {
61 const res = await getVideosList(servers[0].url)
63 expect(res.body.total).to.equal(4)
66 it('User of server 1 should follow user of server 3 and root of server 1', async function () {
69 await addUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003')
70 await addUserSubscription(servers[0].url, users[0].accessToken, rootChannelNameServer1 + '@localhost:9001')
72 await waitJobs(servers)
74 await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
76 await waitJobs(servers)
79 it('Should not display videos of server 3 on server 1', async function () {
80 const res = await getVideosList(servers[0].url)
82 expect(res.body.total).to.equal(4)
83 for (const video of res.body.data) {
84 expect(video.name).to.not.contain('1-3')
85 expect(video.name).to.not.contain('2-3')
86 expect(video.name).to.not.contain('video server 3 added after follow')
90 it('Should list subscriptions', async function () {
92 const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken)
93 expect(res.body.total).to.equal(0)
94 expect(res.body.data).to.be.an('array')
95 expect(res.body.data).to.have.lengthOf(0)
99 const res = await listUserSubscriptions(servers[0].url, users[0].accessToken)
100 expect(res.body.total).to.equal(2)
102 const subscriptions: VideoChannel[] = res.body.data
103 expect(subscriptions).to.be.an('array')
104 expect(subscriptions).to.have.lengthOf(2)
106 expect(subscriptions[0].name).to.equal(users[2].videoChannelName)
107 expect(subscriptions[1].name).to.equal(rootChannelNameServer1)
111 it('Should list subscription videos', async function () {
113 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
114 expect(res.body.total).to.equal(0)
115 expect(res.body.data).to.be.an('array')
116 expect(res.body.data).to.have.lengthOf(0)
120 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
121 expect(res.body.total).to.equal(3)
123 const videos: Video[] = res.body.data
124 expect(videos).to.be.an('array')
125 expect(videos).to.have.lengthOf(3)
127 expect(videos[0].name).to.equal('video 1-3')
128 expect(videos[1].name).to.equal('video 2-3')
129 expect(videos[2].name).to.equal('video server 3 added after follow')
133 it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
136 const videoName = 'video server 1 added after follow'
137 await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
139 await waitJobs(servers)
142 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
143 expect(res.body.total).to.equal(0)
144 expect(res.body.data).to.be.an('array')
145 expect(res.body.data).to.have.lengthOf(0)
149 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
150 expect(res.body.total).to.equal(4)
152 const videos: Video[] = res.body.data
153 expect(videos).to.be.an('array')
154 expect(videos).to.have.lengthOf(4)
156 expect(videos[0].name).to.equal('video 1-3')
157 expect(videos[1].name).to.equal('video 2-3')
158 expect(videos[2].name).to.equal('video server 3 added after follow')
159 expect(videos[3].name).to.equal('video server 1 added after follow')
163 const res = await getVideosList(servers[0].url)
165 expect(res.body.total).to.equal(5)
166 for (const video of res.body.data) {
167 expect(video.name).to.not.contain('1-3')
168 expect(video.name).to.not.contain('2-3')
169 expect(video.name).to.not.contain('video server 3 added after follow')
174 it('Should have server 1 follow server 3 and display server 3 videos', async function () {
177 await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
179 await waitJobs(servers)
181 const res = await getVideosList(servers[0].url)
183 expect(res.body.total).to.equal(8)
185 const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
186 for (const name of names) {
187 const video = res.body.data.find(v => v.name.indexOf(name) === -1)
188 expect(video).to.not.be.undefined
192 it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
195 await unfollow(servers[0].url, servers[0].accessToken, servers[2])
197 await waitJobs(servers)
199 const res = await getVideosList(servers[0].url)
201 expect(res.body.total).to.equal(5)
202 for (const video of res.body.data) {
203 expect(video.name).to.not.contain('1-3')
204 expect(video.name).to.not.contain('2-3')
205 expect(video.name).to.not.contain('video server 3 added after follow')
209 it('Should still list subscription videos', async function () {
211 const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
212 expect(res.body.total).to.equal(0)
213 expect(res.body.data).to.be.an('array')
214 expect(res.body.data).to.have.lengthOf(0)
218 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
219 expect(res.body.total).to.equal(4)
221 const videos: Video[] = res.body.data
222 expect(videos).to.be.an('array')
223 expect(videos).to.have.lengthOf(4)
225 expect(videos[0].name).to.equal('video 1-3')
226 expect(videos[1].name).to.equal('video 2-3')
227 expect(videos[2].name).to.equal('video server 3 added after follow')
228 expect(videos[3].name).to.equal('video server 1 added after follow')
232 it('Should remove user of server 3 subscription', async function () {
233 await removeUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003')
235 await waitJobs(servers)
238 it('Should not display its videos anymore', async function () {
240 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
241 expect(res.body.total).to.equal(1)
243 const videos: Video[] = res.body.data
244 expect(videos).to.be.an('array')
245 expect(videos).to.have.lengthOf(1)
247 expect(videos[0].name).to.equal('video server 1 added after follow')
251 it('Should remove the root subscription and not display the videos anymore', async function () {
252 await removeUserSubscription(servers[0].url, users[0].accessToken, rootChannelNameServer1 + '@localhost:9001')
254 await waitJobs(servers)
257 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
258 expect(res.body.total).to.equal(0)
260 const videos: Video[] = res.body.data
261 expect(videos).to.be.an('array')
262 expect(videos).to.have.lengthOf(0)
266 it('Should correctly display public videos on server 1', async function () {
267 const res = await getVideosList(servers[0].url)
269 expect(res.body.total).to.equal(5)
270 for (const video of res.body.data) {
271 expect(video.name).to.not.contain('1-3')
272 expect(video.name).to.not.contain('2-3')
273 expect(video.name).to.not.contain('video server 3 added after follow')
277 it('Should follow user of server 3 again', async function () {
280 await addUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003')
282 await waitJobs(servers)
285 const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
286 expect(res.body.total).to.equal(3)
288 const videos: Video[] = res.body.data
289 expect(videos).to.be.an('array')
290 expect(videos).to.have.lengthOf(3)
292 expect(videos[0].name).to.equal('video 1-3')
293 expect(videos[1].name).to.equal('video 2-3')
294 expect(videos[2].name).to.equal('video server 3 added after follow')
298 const res = await getVideosList(servers[0].url)
300 expect(res.body.total).to.equal(5)
301 for (const video of res.body.data) {
302 expect(video.name).to.not.contain('1-3')
303 expect(video.name).to.not.contain('2-3')
304 expect(video.name).to.not.contain('video server 3 added after follow')
309 after(async function () {
310 killallServers(servers)