Add follow tests
[oweals/peertube.git] / server / tests / api / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5
6 import {
7   flushAndRunMultipleServers,
8   flushTests,
9   getVideosList,
10   killallServers,
11   ServerInfo,
12   setAccessTokensToServers,
13   uploadVideo,
14   wait
15 } from '../utils'
16 import { follow, getFollowersListPaginationAndSort, getFollowingListPaginationAndSort, unfollow } from '../utils/follows'
17
18 const expect = chai.expect
19
20 describe('Test follows', function () {
21   let servers: ServerInfo[] = []
22   let server3Id: number
23
24   before(async function () {
25     this.timeout(120000)
26
27     servers = await flushAndRunMultipleServers(3)
28
29     // Get the access tokens
30     await setAccessTokensToServers(servers)
31   })
32
33   it('Should not have followers', async function () {
34     for (const server of servers) {
35       const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
36       const follows = res.body.data
37
38       expect(res.body.total).to.equal(0)
39       expect(follows).to.be.an('array')
40       expect(follows.length).to.equal(0)
41     }
42   })
43
44   it('Should not have following', async function () {
45     for (const server of servers) {
46       const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
47       const follows = res.body.data
48
49       expect(res.body.total).to.equal(0)
50       expect(follows).to.be.an('array')
51       expect(follows.length).to.equal(0)
52     }
53   })
54
55   it('Should have server 1 following server 2 and 3', async function () {
56     this.timeout(10000)
57
58     await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
59
60     await wait(7000)
61   })
62
63   it('Should have 2 followings on server 1', async function () {
64     let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
65     let follows = res.body.data
66
67     expect(res.body.total).to.equal(2)
68     expect(follows).to.be.an('array')
69     expect(follows.length).to.equal(1)
70
71     res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
72     follows = follows.concat(res.body.data)
73
74     const server2Follow = follows.find(f => f.following.host === 'localhost:9002')
75     const server3Follow = follows.find(f => f.following.host === 'localhost:9003')
76
77     expect(server2Follow).to.not.be.undefined
78     expect(server3Follow).to.not.be.undefined
79     expect(server2Follow.state).to.equal('accepted')
80     expect(server3Follow.state).to.equal('accepted')
81
82     server3Id = server3Follow.following.id
83   })
84
85   it('Should have 0 followings on server 1 and 2', async function () {
86     for (const server of [ servers[1], servers[2] ]) {
87       const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
88       const follows = res.body.data
89
90       expect(res.body.total).to.equal(0)
91       expect(follows).to.be.an('array')
92       expect(follows.length).to.equal(0)
93     }
94   })
95
96   it('Should have 1 followers on server 2 and 3', async function () {
97     for (const server of [ servers[1], servers[2] ]) {
98       let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
99
100       let follows = res.body.data
101       expect(res.body.total).to.equal(1)
102       expect(follows).to.be.an('array')
103       expect(follows.length).to.equal(1)
104       expect(follows[0].follower.host).to.equal('localhost:9001')
105     }
106   })
107
108   it('Should have 0 followers on server 1', async function () {
109     const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
110     const follows = res.body.data
111
112     expect(res.body.total).to.equal(0)
113     expect(follows).to.be.an('array')
114     expect(follows.length).to.equal(0)
115   })
116
117   it('Should unfollow server 3 on server 1', async function () {
118     this.timeout(5000)
119
120     await unfollow(servers[0].url, servers[0].accessToken, server3Id)
121
122     await wait(3000)
123   })
124
125   it('Should not follow server 3 on server 1 anymore', async function () {
126     const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
127     let follows = res.body.data
128
129     expect(res.body.total).to.equal(1)
130     expect(follows).to.be.an('array')
131     expect(follows.length).to.equal(1)
132
133     expect(follows[0].following.host).to.equal('localhost:9002')
134   })
135
136   it('Should not have server 1 as follower on server 3 anymore', async function () {
137     const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
138
139     let follows = res.body.data
140     expect(res.body.total).to.equal(0)
141     expect(follows).to.be.an('array')
142     expect(follows.length).to.equal(0)
143   })
144
145   it('Should upload a video on server 2 ans 3 and propagate only the video of server 2', async function () {
146     this.timeout(10000)
147
148     await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
149     await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
150
151     await wait(5000)
152
153     let res = await getVideosList(servers[0].url)
154     expect(res.body.total).to.equal(1)
155     expect(res.body.data[0].name).to.equal('server2')
156
157     res = await getVideosList(servers[1].url)
158     expect(res.body.total).to.equal(1)
159     expect(res.body.data[0].name).to.equal('server2')
160
161     res = await getVideosList(servers[2].url)
162     expect(res.body.total).to.equal(1)
163     expect(res.body.data[0].name).to.equal('server3')
164   })
165
166   after(async function () {
167     killallServers(servers)
168
169     // Keep the logs if the test failed
170     if (this['ok']) {
171       await flushTests()
172     }
173   })
174 })