Add ability to add custom css/javascript
[oweals/peertube.git] / server / tests / api / users / users-multiple-servers.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { Account } from '../../../../shared/models/actors'
6 import {
7   checkVideoFilesWereRemoved, createUser, doubleFollow, flushAndRunMultipleServers, removeUser, updateMyUser, userLogin,
8   wait
9 } from '../../utils'
10 import { flushTests, getMyUserInformation, killallServers, ServerInfo, testImage, updateMyAvatar, uploadVideo } from '../../utils/index'
11 import { checkActorFilesWereRemoved, getAccount, getAccountsList } from '../../utils/users/accounts'
12 import { setAccessTokensToServers } from '../../utils/users/login'
13
14 const expect = chai.expect
15
16 describe('Test users with multiple servers', function () {
17   let servers: ServerInfo[] = []
18   let user
19   let userUUID
20   let userId
21   let videoUUID
22   let userAccessToken
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     // Server 1 and server 2 follow each other
33     await doubleFollow(servers[0], servers[1])
34     // Server 1 and server 3 follow each other
35     await doubleFollow(servers[0], servers[2])
36     // Server 2 and server 3 follow each other
37     await doubleFollow(servers[1], servers[2])
38
39     // The root user of server 1 is propagated to servers 2 and 3
40     await uploadVideo(servers[0].url, servers[0].accessToken, {})
41
42     const user = {
43       username: 'user1',
44       password: 'password'
45     }
46     const resUser = await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
47     userUUID = resUser.body.user.uuid
48     userId = resUser.body.user.id
49     userAccessToken = await userLogin(servers[0], user)
50
51     const resVideo = await uploadVideo(servers[0].url, userAccessToken, {})
52     videoUUID = resVideo.body.uuid
53
54     await wait(5000)
55   })
56
57   it('Should be able to update my description', async function () {
58     this.timeout(10000)
59
60     await updateMyUser({
61       url: servers[0].url,
62       accessToken: servers[0].accessToken,
63       description: 'my super description updated'
64     })
65
66     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
67     user = res.body
68     expect(user.account.description).to.equal('my super description updated')
69
70     await wait(5000)
71   })
72
73   it('Should be able to update my avatar', async function () {
74     this.timeout(10000)
75
76     const fixture = 'avatar2.png'
77
78     await updateMyAvatar({
79       url: servers[0].url,
80       accessToken: servers[0].accessToken,
81       fixture
82     })
83
84     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
85     user = res.body
86
87     await testImage(servers[0].url, 'avatar2-resized', user.account.avatar.path, '.png')
88
89     await wait(5000)
90   })
91
92   it('Should have updated my avatar and my description on other servers too', async function () {
93     for (const server of servers) {
94       const resAccounts = await getAccountsList(server.url, '-createdAt')
95
96       const rootServer1List = resAccounts.body.data.find(a => a.name === 'root' && a.host === 'localhost:9001') as Account
97       expect(rootServer1List).not.to.be.undefined
98
99       const resAccount = await getAccount(server.url, rootServer1List.id)
100       const rootServer1Get = resAccount.body as Account
101       expect(rootServer1Get.name).to.equal('root')
102       expect(rootServer1Get.host).to.equal('localhost:9001')
103       expect(rootServer1Get.description).to.equal('my super description updated')
104
105       await testImage(server.url, 'avatar2-resized', rootServer1Get.avatar.path, '.png')
106     }
107   })
108
109   it('Should remove the user', async function () {
110     this.timeout(10000)
111
112     for (const server of servers) {
113       const resAccounts = await getAccountsList(server.url, '-createdAt')
114
115       const userServer1List = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
116       expect(userServer1List).not.to.be.undefined
117     }
118
119     await removeUser(servers[0].url, userId, servers[0].accessToken)
120
121     await wait(5000)
122
123     for (const server of servers) {
124       const resAccounts = await getAccountsList(server.url, '-createdAt')
125
126       const userServer1List = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
127       expect(userServer1List).to.be.undefined
128     }
129   })
130
131   it('Should not have actor files', async () => {
132     for (const server of servers) {
133       await checkActorFilesWereRemoved(userUUID, server.serverNumber)
134     }
135   })
136
137   it('Should not have video files', async () => {
138     for (const server of servers) {
139       await checkVideoFilesWereRemoved(videoUUID, server.serverNumber)
140     }
141   })
142
143   after(async function () {
144     killallServers(servers)
145
146     // Keep the logs if the test failed
147     if (this[ 'ok' ]) {
148       await flushTests()
149     }
150   })
151 })