]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users-multiple-servers.ts
Introduce login command
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users-multiple-servers.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 checkActorFilesWereRemoved,
7 checkTmpIsEmpty,
8 checkVideoFilesWereRemoved,
9 cleanupTests,
10 createUser,
11 doubleFollow,
12 flushAndRunMultipleServers,
13 getAccountVideos,
14 getMyUserInformation,
15 removeUser,
16 ServerInfo,
17 setAccessTokensToServers,
18 testImage,
19 updateMyAvatar,
20 updateMyUser,
21 uploadVideo,
22 waitJobs
23 } from '@shared/extra-utils'
24 import { User } from '@shared/models'
25
26 const expect = chai.expect
27
28 describe('Test users with multiple servers', function () {
29 let servers: ServerInfo[] = []
30 let user: User
31 let userId: number
32 let videoUUID: string
33 let userAccessToken: string
34 let userAvatarFilename: string
35
36 before(async function () {
37 this.timeout(120_000)
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({
60 url: servers[0].url,
61 accessToken: servers[0].accessToken,
62 username: user.username,
63 password: user.password
64 })
65 userId = res.body.user.id
66 userAccessToken = await servers[0].loginCommand.getAccessToken(user)
67 }
68
69 {
70 const resVideo = await uploadVideo(servers[0].url, userAccessToken, {})
71 videoUUID = resVideo.body.video.uuid
72 }
73
74 await waitJobs(servers)
75 })
76
77 it('Should be able to update my display name', async function () {
78 this.timeout(10000)
79
80 await updateMyUser({
81 url: servers[0].url,
82 accessToken: servers[0].accessToken,
83 displayName: 'my super display name'
84 })
85
86 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
87 user = res.body
88
89 expect(user.account.displayName).to.equal('my super display name')
90
91 await waitJobs(servers)
92 })
93
94 it('Should be able to update my description', async function () {
95 this.timeout(10_000)
96
97 await updateMyUser({
98 url: servers[0].url,
99 accessToken: servers[0].accessToken,
100 description: 'my super description updated'
101 })
102
103 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
104 user = res.body
105 expect(user.account.displayName).to.equal('my super display name')
106 expect(user.account.description).to.equal('my super description updated')
107
108 await waitJobs(servers)
109 })
110
111 it('Should be able to update my avatar', async function () {
112 this.timeout(10_000)
113
114 const fixture = 'avatar2.png'
115
116 await updateMyAvatar({
117 url: servers[0].url,
118 accessToken: servers[0].accessToken,
119 fixture
120 })
121
122 const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
123 user = res.body
124
125 userAvatarFilename = user.account.avatar.path
126
127 await testImage(servers[0].url, 'avatar2-resized', userAvatarFilename, '.png')
128
129 await waitJobs(servers)
130 })
131
132 it('Should have updated my profile on other servers too', async function () {
133 let createdAt: string | Date
134
135 for (const server of servers) {
136 const body = await server.accountsCommand.list({ sort: '-createdAt' })
137
138 const resList = body.data.find(a => a.name === 'root' && a.host === 'localhost:' + servers[0].port)
139 expect(resList).not.to.be.undefined
140
141 const account = await server.accountsCommand.get({ accountName: resList.name + '@' + resList.host })
142
143 if (!createdAt) createdAt = account.createdAt
144
145 expect(account.name).to.equal('root')
146 expect(account.host).to.equal('localhost:' + servers[0].port)
147 expect(account.displayName).to.equal('my super display name')
148 expect(account.description).to.equal('my super description updated')
149 expect(createdAt).to.equal(account.createdAt)
150
151 if (server.serverNumber === 1) {
152 expect(account.userId).to.be.a('number')
153 } else {
154 expect(account.userId).to.be.undefined
155 }
156
157 await testImage(server.url, 'avatar2-resized', account.avatar.path, '.png')
158 }
159 })
160
161 it('Should list account videos', async function () {
162 for (const server of servers) {
163 const res = await getAccountVideos(server.url, server.accessToken, 'user1@localhost:' + servers[0].port, 0, 5)
164
165 expect(res.body.total).to.equal(1)
166 expect(res.body.data).to.be.an('array')
167 expect(res.body.data).to.have.lengthOf(1)
168 expect(res.body.data[0].uuid).to.equal(videoUUID)
169 }
170 })
171
172 it('Should search through account videos', async function () {
173 this.timeout(10_000)
174
175 const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'Kami no chikara' })
176
177 await waitJobs(servers)
178
179 for (const server of servers) {
180 const res = await getAccountVideos(server.url, server.accessToken, 'user1@localhost:' + servers[0].port, 0, 5, undefined, {
181 search: 'Kami'
182 })
183
184 expect(res.body.total).to.equal(1)
185 expect(res.body.data).to.be.an('array')
186 expect(res.body.data).to.have.lengthOf(1)
187 expect(res.body.data[0].uuid).to.equal(resVideo.body.video.uuid)
188 }
189 })
190
191 it('Should remove the user', async function () {
192 this.timeout(10_000)
193
194 for (const server of servers) {
195 const body = await server.accountsCommand.list({ sort: '-createdAt' })
196
197 const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port)
198 expect(accountDeleted).not.to.be.undefined
199
200 const { data } = await server.channelsCommand.list()
201 const videoChannelDeleted = data.find(a => a.displayName === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port)
202 expect(videoChannelDeleted).not.to.be.undefined
203 }
204
205 await removeUser(servers[0].url, userId, servers[0].accessToken)
206
207 await waitJobs(servers)
208
209 for (const server of servers) {
210 const body = await server.accountsCommand.list({ sort: '-createdAt' })
211
212 const accountDeleted = body.data.find(a => a.name === 'user1' && a.host === 'localhost:' + servers[0].port)
213 expect(accountDeleted).to.be.undefined
214
215 const { data } = await server.channelsCommand.list()
216 const videoChannelDeleted = data.find(a => a.name === 'Main user1 channel' && a.host === 'localhost:' + servers[0].port)
217 expect(videoChannelDeleted).to.be.undefined
218 }
219 })
220
221 it('Should not have actor files', async () => {
222 for (const server of servers) {
223 await checkActorFilesWereRemoved(userAvatarFilename, server.internalServerNumber)
224 }
225 })
226
227 it('Should not have video files', async () => {
228 for (const server of servers) {
229 await checkVideoFilesWereRemoved(videoUUID, server)
230 }
231 })
232
233 it('Should have an empty tmp directory', async function () {
234 for (const server of servers) {
235 await checkTmpIsEmpty(server)
236 }
237 })
238
239 after(async function () {
240 await cleanupTests(servers)
241 })
242 })