Add quota used in users list
[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,
8   createUser,
9   doubleFollow,
10   flushAndRunMultipleServers,
11   getAccountVideos,
12   getVideoChannelsList,
13   removeUser,
14   updateMyUser,
15   userLogin,
16   wait
17 } from '../../utils'
18 import { flushTests, getMyUserInformation, killallServers, ServerInfo, testImage, updateMyAvatar, uploadVideo } from '../../utils/index'
19 import { checkActorFilesWereRemoved, getAccount, getAccountsList } from '../../utils/users/accounts'
20 import { setAccessTokensToServers } from '../../utils/users/login'
21 import { User } from '../../../../shared/models/users'
22 import { VideoChannel } from '../../../../shared/models/videos'
23 import { waitJobs } from '../../utils/server/jobs'
24
25 const expect = chai.expect
26
27 describe('Test users with multiple servers', function () {
28   let servers: ServerInfo[] = []
29   let user: User
30   let userAccountName: string
31   let userVideoChannelUUID: string
32   let userId: number
33   let videoUUID: string
34   let userAccessToken: string
35
36   before(async function () {
37     this.timeout(120000)
38
39     servers = await flushAndRunMultipleServers(3)
40
41     // Get the access tokens
42     await setAccessTokensToServers(servers)
43
44     // Server 1 and server 2 follow each other
45     await doubleFollow(servers[0], servers[1])
46     // Server 1 and server 3 follow each other
47     await doubleFollow(servers[0], servers[2])
48     // Server 2 and server 3 follow each other
49     await doubleFollow(servers[1], servers[2])
50
51     // The root user of server 1 is propagated to servers 2 and 3
52     await uploadVideo(servers[0].url, servers[0].accessToken, {})
53
54     {
55       const user = {
56         username: 'user1',
57         password: 'password'
58       }
59       const res = await createUser(servers[ 0 ].url, servers[ 0 ].accessToken, user.username, user.password)
60       userId = res.body.user.id
61       userAccessToken = await userLogin(servers[ 0 ], user)
62     }
63
64     {
65       const res = await getMyUserInformation(servers[0].url, userAccessToken)
66       userAccountName = res.body.account.name + '@' + res.body.account.host
67     }
68
69     {
70       const res = await getMyUserInformation(servers[ 0 ].url, servers[ 0 ].accessToken)
71       const user: User = res.body
72       userVideoChannelUUID = user.videoChannels[0].uuid
73     }
74
75     {
76       const resVideo = await uploadVideo(servers[ 0 ].url, userAccessToken, {})
77       videoUUID = resVideo.body.video.uuid
78     }
79
80     await waitJobs(servers)
81   })
82
83   it('Should be able to update my display name', async function () {
84     this.timeout(10000)
85
86     await updateMyUser({
87       url: servers[0].url,
88       accessToken: servers[0].accessToken,
89       displayName: 'my super display name'
90     })
91
92     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
93     user = res.body
94     expect(user.account.displayName).to.equal('my super display name')
95
96     await waitJobs(servers)
97   })
98
99   it('Should be able to update my description', async function () {
100     this.timeout(10000)
101
102     await updateMyUser({
103       url: servers[0].url,
104       accessToken: servers[0].accessToken,
105       description: 'my super description updated'
106     })
107
108     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
109     user = res.body
110     expect(user.account.displayName).to.equal('my super display name')
111     expect(user.account.description).to.equal('my super description updated')
112
113     await waitJobs(servers)
114   })
115
116   it('Should be able to update my avatar', async function () {
117     this.timeout(10000)
118
119     const fixture = 'avatar2.png'
120
121     await updateMyAvatar({
122       url: servers[0].url,
123       accessToken: servers[0].accessToken,
124       fixture
125     })
126
127     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
128     user = res.body
129
130     await testImage(servers[0].url, 'avatar2-resized', user.account.avatar.path, '.png')
131
132     await waitJobs(servers)
133   })
134
135   it('Should have updated my profile on other servers too', async function () {
136     for (const server of servers) {
137       const resAccounts = await getAccountsList(server.url, '-createdAt')
138
139       const rootServer1List = resAccounts.body.data.find(a => a.name === 'root' && a.host === 'localhost:9001') as Account
140       expect(rootServer1List).not.to.be.undefined
141
142       const resAccount = await getAccount(server.url, rootServer1List.name + '@' + rootServer1List.host)
143       const rootServer1Get = resAccount.body as Account
144       expect(rootServer1Get.name).to.equal('root')
145       expect(rootServer1Get.host).to.equal('localhost:9001')
146       expect(rootServer1Get.displayName).to.equal('my super display name')
147       expect(rootServer1Get.description).to.equal('my super description updated')
148
149       await testImage(server.url, 'avatar2-resized', rootServer1Get.avatar.path, '.png')
150     }
151   })
152
153   it('Should list account videos', async function () {
154     for (const server of servers) {
155       const res = await getAccountVideos(server.url, server.accessToken, userAccountName, 0, 5)
156
157       expect(res.body.total).to.equal(1)
158       expect(res.body.data).to.be.an('array')
159       expect(res.body.data).to.have.lengthOf(1)
160       expect(res.body.data[0].uuid).to.equal(videoUUID)
161     }
162   })
163
164   it('Should remove the user', async function () {
165     this.timeout(10000)
166
167     for (const server of servers) {
168       const resAccounts = await getAccountsList(server.url, '-createdAt')
169
170       const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
171       expect(accountDeleted).not.to.be.undefined
172
173       const resVideoChannels = await getVideoChannelsList(server.url, 0, 10)
174       const videoChannelDeleted = resVideoChannels.body.data.find(a => {
175         return a.displayName === 'Default user1 channel' && a.host === 'localhost:9001'
176       }) as VideoChannel
177       expect(videoChannelDeleted).not.to.be.undefined
178     }
179
180     await removeUser(servers[0].url, userId, servers[0].accessToken)
181
182     await waitJobs(servers)
183
184     for (const server of servers) {
185       const resAccounts = await getAccountsList(server.url, '-createdAt')
186
187       const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
188       expect(accountDeleted).to.be.undefined
189
190       const resVideoChannels = await getVideoChannelsList(server.url, 0, 10)
191       const videoChannelDeleted = resVideoChannels.body.data.find(a => {
192         return a.name === 'Default user1 channel' && a.host === 'localhost:9001'
193       }) as VideoChannel
194       expect(videoChannelDeleted).to.be.undefined
195     }
196   })
197
198   it('Should not have actor files', async () => {
199     for (const server of servers) {
200       await checkActorFilesWereRemoved(userAccountName, server.serverNumber)
201       await checkActorFilesWereRemoved(userVideoChannelUUID, server.serverNumber)
202     }
203   })
204
205   it('Should not have video files', async () => {
206     for (const server of servers) {
207       await checkVideoFilesWereRemoved(videoUUID, server.serverNumber)
208     }
209   })
210
211   after(async function () {
212     killallServers(servers)
213   })
214 })