Add ability to remove an instance follower in API
[oweals/peertube.git] / server / tests / api / server / follow-constraints.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   doubleFollow,
7   getAccountVideos,
8   getVideo,
9   getVideoChannelVideos,
10   getVideoWithToken,
11   flushAndRunMultipleServers,
12   killallServers,
13   ServerInfo,
14   setAccessTokensToServers,
15   uploadVideo
16 } from '../../../../shared/utils'
17 import { unfollow } from '../../../../shared/utils/server/follows'
18 import { userLogin } from '../../../../shared/utils/users/login'
19 import { createUser } from '../../../../shared/utils/users/users'
20
21 const expect = chai.expect
22
23 describe('Test follow constraints', function () {
24   let servers: ServerInfo[] = []
25   let video1UUID: string
26   let video2UUID: string
27   let userAccessToken: string
28
29   before(async function () {
30     this.timeout(30000)
31
32     servers = await flushAndRunMultipleServers(2)
33
34     // Get the access tokens
35     await setAccessTokensToServers(servers)
36
37     {
38       const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video server 1' })
39       video1UUID = res.body.video.uuid
40     }
41     {
42       const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video server 2' })
43       video2UUID = res.body.video.uuid
44     }
45
46     const user = {
47       username: 'user1',
48       password: 'super_password'
49     }
50     await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
51     userAccessToken = await userLogin(servers[0], user)
52
53     await doubleFollow(servers[0], servers[1])
54   })
55
56   describe('With a followed instance', function () {
57
58     describe('With an unlogged user', function () {
59
60       it('Should get the local video', async function () {
61         await getVideo(servers[0].url, video1UUID, 200)
62       })
63
64       it('Should get the remote video', async function () {
65         await getVideo(servers[0].url, video2UUID, 200)
66       })
67
68       it('Should list local account videos', async function () {
69         const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9001', 0, 5)
70
71         expect(res.body.total).to.equal(1)
72         expect(res.body.data).to.have.lengthOf(1)
73       })
74
75       it('Should list remote account videos', async function () {
76         const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9002', 0, 5)
77
78         expect(res.body.total).to.equal(1)
79         expect(res.body.data).to.have.lengthOf(1)
80       })
81
82       it('Should list local channel videos', async function () {
83         const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9001', 0, 5)
84
85         expect(res.body.total).to.equal(1)
86         expect(res.body.data).to.have.lengthOf(1)
87       })
88
89       it('Should list remote channel videos', async function () {
90         const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9002', 0, 5)
91
92         expect(res.body.total).to.equal(1)
93         expect(res.body.data).to.have.lengthOf(1)
94       })
95     })
96
97     describe('With a logged user', function () {
98       it('Should get the local video', async function () {
99         await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, 200)
100       })
101
102       it('Should get the remote video', async function () {
103         await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, 200)
104       })
105
106       it('Should list local account videos', async function () {
107         const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9001', 0, 5)
108
109         expect(res.body.total).to.equal(1)
110         expect(res.body.data).to.have.lengthOf(1)
111       })
112
113       it('Should list remote account videos', async function () {
114         const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9002', 0, 5)
115
116         expect(res.body.total).to.equal(1)
117         expect(res.body.data).to.have.lengthOf(1)
118       })
119
120       it('Should list local channel videos', async function () {
121         const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9001', 0, 5)
122
123         expect(res.body.total).to.equal(1)
124         expect(res.body.data).to.have.lengthOf(1)
125       })
126
127       it('Should list remote channel videos', async function () {
128         const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9002', 0, 5)
129
130         expect(res.body.total).to.equal(1)
131         expect(res.body.data).to.have.lengthOf(1)
132       })
133     })
134   })
135
136   describe('With a non followed instance', function () {
137
138     before(async function () {
139       this.timeout(30000)
140
141       await unfollow(servers[0].url, servers[0].accessToken, servers[1])
142     })
143
144     describe('With an unlogged user', function () {
145
146       it('Should get the local video', async function () {
147         await getVideo(servers[0].url, video1UUID, 200)
148       })
149
150       it('Should not get the remote video', async function () {
151         await getVideo(servers[0].url, video2UUID, 403)
152       })
153
154       it('Should list local account videos', async function () {
155         const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9001', 0, 5)
156
157         expect(res.body.total).to.equal(1)
158         expect(res.body.data).to.have.lengthOf(1)
159       })
160
161       it('Should not list remote account videos', async function () {
162         const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9002', 0, 5)
163
164         expect(res.body.total).to.equal(0)
165         expect(res.body.data).to.have.lengthOf(0)
166       })
167
168       it('Should list local channel videos', async function () {
169         const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9001', 0, 5)
170
171         expect(res.body.total).to.equal(1)
172         expect(res.body.data).to.have.lengthOf(1)
173       })
174
175       it('Should not list remote channel videos', async function () {
176         const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9002', 0, 5)
177
178         expect(res.body.total).to.equal(0)
179         expect(res.body.data).to.have.lengthOf(0)
180       })
181     })
182
183     describe('With a logged user', function () {
184       it('Should get the local video', async function () {
185         await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, 200)
186       })
187
188       it('Should get the remote video', async function () {
189         await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, 200)
190       })
191
192       it('Should list local account videos', async function () {
193         const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9001', 0, 5)
194
195         expect(res.body.total).to.equal(1)
196         expect(res.body.data).to.have.lengthOf(1)
197       })
198
199       it('Should list remote account videos', async function () {
200         const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9002', 0, 5)
201
202         expect(res.body.total).to.equal(1)
203         expect(res.body.data).to.have.lengthOf(1)
204       })
205
206       it('Should list local channel videos', async function () {
207         const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9001', 0, 5)
208
209         expect(res.body.total).to.equal(1)
210         expect(res.body.data).to.have.lengthOf(1)
211       })
212
213       it('Should list remote channel videos', async function () {
214         const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9002', 0, 5)
215
216         expect(res.body.total).to.equal(1)
217         expect(res.body.data).to.have.lengthOf(1)
218       })
219     })
220   })
221
222   after(async function () {
223     killallServers(servers)
224   })
225 })