Check current password on server side
[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 } from '../../utils'
17 import { getMyUserInformation, killallServers, ServerInfo, testImage, updateMyAvatar, uploadVideo } from '../../utils/index'
18 import { checkActorFilesWereRemoved, getAccount, getAccountsList } from '../../utils/users/accounts'
19 import { setAccessTokensToServers } from '../../utils/users/login'
20 import { User } from '../../../../shared/models/users'
21 import { VideoChannel } from '../../../../shared/models/videos'
22 import { waitJobs } from '../../utils/server/jobs'
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 userAccountUUID: 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       const account: Account = res.body.account
67       userAccountName = account.name + '@' + account.host
68       userAccountUUID = account.uuid
69     }
70
71     {
72       const res = await getMyUserInformation(servers[ 0 ].url, servers[ 0 ].accessToken)
73       const user: User = res.body
74       userVideoChannelUUID = user.videoChannels[0].uuid
75     }
76
77     {
78       const resVideo = await uploadVideo(servers[ 0 ].url, userAccessToken, {})
79       videoUUID = resVideo.body.video.uuid
80     }
81
82     await waitJobs(servers)
83   })
84
85   it('Should be able to update my display name', async function () {
86     this.timeout(10000)
87
88     await updateMyUser({
89       url: servers[0].url,
90       accessToken: servers[0].accessToken,
91       displayName: 'my super display name'
92     })
93
94     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
95     user = res.body
96     expect(user.account.displayName).to.equal('my super display name')
97
98     await waitJobs(servers)
99   })
100
101   it('Should be able to update my description', async function () {
102     this.timeout(10000)
103
104     await updateMyUser({
105       url: servers[0].url,
106       accessToken: servers[0].accessToken,
107       description: 'my super description updated'
108     })
109
110     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
111     user = res.body
112     expect(user.account.displayName).to.equal('my super display name')
113     expect(user.account.description).to.equal('my super description updated')
114
115     await waitJobs(servers)
116   })
117
118   it('Should be able to update my avatar', async function () {
119     this.timeout(10000)
120
121     const fixture = 'avatar2.png'
122
123     await updateMyAvatar({
124       url: servers[0].url,
125       accessToken: servers[0].accessToken,
126       fixture
127     })
128
129     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
130     user = res.body
131
132     await testImage(servers[0].url, 'avatar2-resized', user.account.avatar.path, '.png')
133
134     await waitJobs(servers)
135   })
136
137   it('Should have updated my profile on other servers too', async function () {
138     for (const server of servers) {
139       const resAccounts = await getAccountsList(server.url, '-createdAt')
140
141       const rootServer1List = resAccounts.body.data.find(a => a.name === 'root' && a.host === 'localhost:9001') as Account
142       expect(rootServer1List).not.to.be.undefined
143
144       const resAccount = await getAccount(server.url, rootServer1List.name + '@' + rootServer1List.host)
145       const rootServer1Get = resAccount.body as Account
146       expect(rootServer1Get.name).to.equal('root')
147       expect(rootServer1Get.host).to.equal('localhost:9001')
148       expect(rootServer1Get.displayName).to.equal('my super display name')
149       expect(rootServer1Get.description).to.equal('my super description updated')
150
151       await testImage(server.url, 'avatar2-resized', rootServer1Get.avatar.path, '.png')
152     }
153   })
154
155   it('Should list account videos', async function () {
156     for (const server of servers) {
157       const res = await getAccountVideos(server.url, server.accessToken, userAccountName, 0, 5)
158
159       expect(res.body.total).to.equal(1)
160       expect(res.body.data).to.be.an('array')
161       expect(res.body.data).to.have.lengthOf(1)
162       expect(res.body.data[0].uuid).to.equal(videoUUID)
163     }
164   })
165
166   it('Should remove the user', async function () {
167     this.timeout(10000)
168
169     for (const server of servers) {
170       const resAccounts = await getAccountsList(server.url, '-createdAt')
171
172       const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
173       expect(accountDeleted).not.to.be.undefined
174
175       const resVideoChannels = await getVideoChannelsList(server.url, 0, 10)
176       const videoChannelDeleted = resVideoChannels.body.data.find(a => {
177         return a.displayName === 'Main user1 channel' && a.host === 'localhost:9001'
178       }) as VideoChannel
179       expect(videoChannelDeleted).not.to.be.undefined
180     }
181
182     await removeUser(servers[0].url, userId, servers[0].accessToken)
183
184     await waitJobs(servers)
185
186     for (const server of servers) {
187       const resAccounts = await getAccountsList(server.url, '-createdAt')
188
189       const accountDeleted = resAccounts.body.data.find(a => a.name === 'user1' && a.host === 'localhost:9001') as Account
190       expect(accountDeleted).to.be.undefined
191
192       const resVideoChannels = await getVideoChannelsList(server.url, 0, 10)
193       const videoChannelDeleted = resVideoChannels.body.data.find(a => {
194         return a.name === 'Main user1 channel' && a.host === 'localhost:9001'
195       }) as VideoChannel
196       expect(videoChannelDeleted).to.be.undefined
197     }
198   })
199
200   it('Should not have actor files', async () => {
201     for (const server of servers) {
202       await checkActorFilesWereRemoved(userAccountUUID, server.serverNumber)
203       await checkActorFilesWereRemoved(userVideoChannelUUID, server.serverNumber)
204     }
205   })
206
207   it('Should not have video files', async () => {
208     for (const server of servers) {
209       await checkVideoFilesWereRemoved(videoUUID, server.serverNumber)
210     }
211   })
212
213   after(async function () {
214     killallServers(servers)
215   })
216 })