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