]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users/users.ts
Fix real world script
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
CommitLineData
0e1dc3e7 1/* tslint:disable:no-unused-expression */
86d13ec2 2
0e1dc3e7 3import * as chai from 'chai'
afffe988 4import 'mocha'
c5d31dba 5import { UserRole } from '../../../../shared/index'
0e1dc3e7 6import {
11ba2ab3
C
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList,
8 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
9 runServer, ServerInfo, serverLogin, updateMyUser, updateUser, uploadVideo
c5d31dba
C
10} from '../../utils/index'
11import { follow } from '../../utils/server/follows'
c5d31dba 12import { setAccessTokensToServers } from '../../utils/users/login'
11ba2ab3 13import { getMyVideos } from '../../utils/videos/videos'
0e1dc3e7 14
afffe988
C
15const expect = chai.expect
16
0e1dc3e7
C
17describe('Test users', function () {
18 let server: ServerInfo
19 let accessToken: string
20 let accessTokenUser: string
21 let videoId: number
22 let userId: number
23
24 before(async function () {
572f8d3d 25 this.timeout(10000)
0e1dc3e7
C
26
27 await flushTests()
28 server = await runServer(1)
86d13ec2
C
29
30 await setAccessTokensToServers([ server ])
0e1dc3e7
C
31 })
32
33 it('Should create a new client')
34
35 it('Should return the first client')
36
37 it('Should remove the last client')
38
39 it('Should not login with an invalid client id', async function () {
40 const client = { id: 'client', secret: server.client.secret }
41 const res = await login(server.url, client, server.user, 400)
42
a7ba16b6 43 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
44 })
45
46 it('Should not login with an invalid client secret', async function () {
47 const client = { id: server.client.id, secret: 'coucou' }
48 const res = await login(server.url, client, server.user, 400)
49
a7ba16b6 50 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
51 })
52
53 it('Should not login with an invalid username', async function () {
54 const user = { username: 'captain crochet', password: server.user.password }
55 const res = await login(server.url, server.client, user, 400)
56
a7ba16b6 57 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
58 })
59
60 it('Should not login with an invalid password', async function () {
5f04dd2f 61 const user = { username: server.user.username, password: 'mew_three' }
0e1dc3e7
C
62 const res = await login(server.url, server.client, user, 400)
63
a7ba16b6 64 expect(res.body.error).to.equal('Authentication failed.')
0e1dc3e7
C
65 })
66
67 it('Should not be able to upload a video', async function () {
68 accessToken = 'my_super_token'
69
70 const videoAttributes = {}
71 await uploadVideo(server.url, accessToken, videoAttributes, 401)
72 })
73
afffe988 74 it('Should not be able to follow', async function () {
0e1dc3e7 75 accessToken = 'my_super_token'
afffe988 76 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
0e1dc3e7
C
77 })
78
afffe988 79 it('Should not be able to unfollow')
0e1dc3e7
C
80
81 it('Should be able to login', async function () {
82 const res = await login(server.url, server.client, server.user, 200)
83
84 accessToken = res.body.access_token
85 })
86
87 it('Should upload the video with the correct token', async function () {
88 const videoAttributes = {}
04e0fc48 89 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7 90 const res = await getVideosList(server.url)
afffe988 91 const video = res.body.data[ 0 ]
0e1dc3e7 92
a7ba16b6 93 expect(video.accountName).to.equal('root')
0e1dc3e7
C
94 videoId = video.id
95 })
96
97 it('Should upload the video again with the correct token', async function () {
98 const videoAttributes = {}
04e0fc48 99 await uploadVideo(server.url, accessToken, videoAttributes)
0e1dc3e7
C
100 })
101
102 it('Should retrieve a video rating', async function () {
103 await rateVideo(server.url, accessToken, videoId, 'like')
26d21b78 104 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
0e1dc3e7
C
105 const rating = res.body
106
a7ba16b6
C
107 expect(rating.videoId).to.equal(videoId)
108 expect(rating.rating).to.equal('like')
0e1dc3e7
C
109 })
110
111 it('Should not be able to remove the video with an incorrect token', async function () {
112 await removeVideo(server.url, 'bad_token', videoId, 401)
113 })
114
115 it('Should not be able to remove the video with the token of another account')
116
117 it('Should be able to remove the video with the correct token', async function () {
118 await removeVideo(server.url, accessToken, videoId)
119 })
120
121 it('Should logout (revoke token)')
122
123 it('Should not be able to get the user information')
124
125 it('Should not be able to upload a video')
126
127 it('Should not be able to remove a video')
128
129 it('Should not be able to rate a video', async function () {
130 const path = '/api/v1/videos/'
131 const data = {
132 rating: 'likes'
133 }
134
135 const options = {
136 url: server.url,
137 path: path + videoId,
138 token: 'wrong token',
139 fields: data,
140 statusCodeExpected: 401
141 }
142 await makePutBodyRequest(options)
143 })
144
145 it('Should be able to login again')
146
147 it('Should have an expired access token')
148
149 it('Should refresh the token')
150
151 it('Should be able to upload a video again')
152
153 it('Should be able to create a new user', async function () {
5c98d3bf 154 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
0e1dc3e7
C
155 })
156
157 it('Should be able to login with this user', async function () {
158 server.user = {
159 username: 'user_1',
160 password: 'super password'
161 }
162
eec63bbc 163 accessTokenUser = await serverLogin(server)
0e1dc3e7
C
164 })
165
166 it('Should be able to get the user information', async function () {
5c98d3bf 167 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
168 const user = res.body
169
a7ba16b6
C
170 expect(user.username).to.equal('user_1')
171 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7 172 expect(user.displayNSFW).to.be.false
a7ba16b6
C
173 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
174 expect(user.roleLabel).to.equal('User')
175 expect(user.id).to.be.a('number')
0e1dc3e7
C
176 })
177
178 it('Should be able to upload a video with this user', async function () {
179 this.timeout(5000)
180
11474c3c
C
181 const videoAttributes = {
182 name: 'super user video'
183 }
0e1dc3e7
C
184 await uploadVideo(server.url, accessTokenUser, videoAttributes)
185 })
186
11474c3c
C
187 it('Should be able to list my videos', async function () {
188 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
a7ba16b6 189 expect(res.body.total).to.equal(1)
11474c3c
C
190
191 const videos = res.body.data
a7ba16b6 192 expect(videos).to.have.lengthOf(1)
11474c3c 193
a7ba16b6 194 expect(videos[ 0 ].name).to.equal('super user video')
11474c3c
C
195 })
196
0e1dc3e7 197 it('Should list all the users', async function () {
86d13ec2 198 const res = await getUsersList(server.url, server.accessToken)
0e1dc3e7
C
199 const result = res.body
200 const total = result.total
201 const users = result.data
202
a7ba16b6
C
203 expect(total).to.equal(2)
204 expect(users).to.be.an('array')
205 expect(users.length).to.equal(2)
afffe988
C
206
207 const user = users[ 0 ]
a7ba16b6
C
208 expect(user.username).to.equal('user_1')
209 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
210 expect(user.displayNSFW).to.be.false
211
afffe988 212 const rootUser = users[ 1 ]
a7ba16b6
C
213 expect(rootUser.username).to.equal('root')
214 expect(rootUser.email).to.equal('admin1@example.com')
0e1dc3e7
C
215 expect(rootUser.displayNSFW).to.be.false
216
217 userId = user.id
218 })
219
220 it('Should list only the first user by username asc', async function () {
86d13ec2 221 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
0e1dc3e7
C
222
223 const result = res.body
224 const total = result.total
225 const users = result.data
226
a7ba16b6
C
227 expect(total).to.equal(2)
228 expect(users.length).to.equal(1)
afffe988
C
229
230 const user = users[ 0 ]
a7ba16b6
C
231 expect(user.username).to.equal('root')
232 expect(user.email).to.equal('admin1@example.com')
233 expect(user.roleLabel).to.equal('Administrator')
0e1dc3e7
C
234 expect(user.displayNSFW).to.be.false
235 })
236
237 it('Should list only the first user by username desc', async function () {
86d13ec2 238 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
0e1dc3e7
C
239 const result = res.body
240 const total = result.total
241 const users = result.data
242
a7ba16b6
C
243 expect(total).to.equal(2)
244 expect(users.length).to.equal(1)
afffe988
C
245
246 const user = users[ 0 ]
a7ba16b6
C
247 expect(user.username).to.equal('user_1')
248 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
249 expect(user.displayNSFW).to.be.false
250 })
251
252 it('Should list only the second user by createdAt desc', async function () {
86d13ec2 253 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
0e1dc3e7
C
254 const result = res.body
255 const total = result.total
256 const users = result.data
257
a7ba16b6
C
258 expect(total).to.equal(2)
259 expect(users.length).to.equal(1)
afffe988
C
260
261 const user = users[ 0 ]
a7ba16b6
C
262 expect(user.username).to.equal('user_1')
263 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7
C
264 expect(user.displayNSFW).to.be.false
265 })
266
267 it('Should list all the users by createdAt asc', async function () {
86d13ec2 268 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
0e1dc3e7
C
269 const result = res.body
270 const total = result.total
271 const users = result.data
272
a7ba16b6
C
273 expect(total).to.equal(2)
274 expect(users.length).to.equal(2)
275
276 expect(users[ 0 ].username).to.equal('root')
277 expect(users[ 0 ].email).to.equal('admin1@example.com')
afffe988
C
278 expect(users[ 0 ].displayNSFW).to.be.false
279
a7ba16b6
C
280 expect(users[ 1 ].username).to.equal('user_1')
281 expect(users[ 1 ].email).to.equal('user_1@example.com')
afffe988 282 expect(users[ 1 ].displayNSFW).to.be.false
0e1dc3e7
C
283 })
284
5c98d3bf 285 it('Should update my password', async function () {
11ba2ab3
C
286 await updateMyUser({
287 url: server.url,
288 accessToken: accessTokenUser,
289 newPassword: 'new password'
290 })
0e1dc3e7
C
291 server.user.password = 'new password'
292
293 await login(server.url, server.client, server.user, 200)
294 })
295
296 it('Should be able to change the NSFW display attribute', async function () {
11ba2ab3
C
297 await updateMyUser({
298 url: server.url,
299 accessToken: accessTokenUser,
300 displayNSFW: true
301 })
0e1dc3e7 302
5c98d3bf 303 const res = await getMyUserInformation(server.url, accessTokenUser)
0e1dc3e7
C
304 const user = res.body
305
a7ba16b6
C
306 expect(user.username).to.equal('user_1')
307 expect(user.email).to.equal('user_1@example.com')
0e1dc3e7 308 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
309 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
310 expect(user.id).to.be.a('number')
5c98d3bf
C
311 })
312
7efe153b 313 it('Should be able to change the autoPlayVideo attribute', async function () {
11ba2ab3
C
314 await updateMyUser({
315 url: server.url,
316 accessToken: accessTokenUser,
317 autoPlayVideo: false
318 })
7efe153b
AL
319
320 const res = await getMyUserInformation(server.url, accessTokenUser)
321 const user = res.body
322
323 expect(user.autoPlayVideo).to.be.false
324 })
325
5c98d3bf 326 it('Should be able to change the email display attribute', async function () {
11ba2ab3
C
327 await updateMyUser({
328 url: server.url,
329 accessToken: accessTokenUser,
330 email: 'updated@example.com'
331 })
5c98d3bf
C
332
333 const res = await getMyUserInformation(server.url, accessTokenUser)
334 const user = res.body
335
a7ba16b6
C
336 expect(user.username).to.equal('user_1')
337 expect(user.email).to.equal('updated@example.com')
5c98d3bf 338 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
339 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
340 expect(user.id).to.be.a('number')
5c98d3bf
C
341 })
342
343 it('Should be able to update another user', async function () {
11ba2ab3
C
344 await updateUser({
345 url: server.url,
346 userId,
347 accessToken,
348 email: 'updated2@example.com',
349 videoQuota: 42,
350 role: UserRole.MODERATOR
351 })
5c98d3bf 352
77a5501f 353 const res = await getUserInformation(server.url, accessToken, userId)
5c98d3bf
C
354 const user = res.body
355
a7ba16b6
C
356 expect(user.username).to.equal('user_1')
357 expect(user.email).to.equal('updated2@example.com')
5c98d3bf 358 expect(user.displayNSFW).to.be.ok
a7ba16b6
C
359 expect(user.videoQuota).to.equal(42)
360 expect(user.roleLabel).to.equal('Moderator')
361 expect(user.id).to.be.a('number')
0e1dc3e7
C
362 })
363
757f0da3
C
364 it('Should not be able to delete a user by a moderator', async function () {
365 await removeUser(server.url, 2, accessTokenUser, 403)
366 })
367
368 it('Should be able to list video blacklist by a moderator', async function () {
369 await getBlacklistedVideosList(server.url, accessTokenUser)
370 })
371
0e1dc3e7
C
372 it('Should be able to remove this user', async function () {
373 await removeUser(server.url, userId, accessToken)
374 })
375
376 it('Should not be able to login with this user', async function () {
377 // server.user is already set to user 1
378 await login(server.url, server.client, server.user, 400)
379 })
380
381 it('Should not have videos of this user', async function () {
382 const res = await getVideosList(server.url)
383
a7ba16b6 384 expect(res.body.total).to.equal(1)
0e1dc3e7 385
afffe988 386 const video = res.body.data[ 0 ]
a7ba16b6 387 expect(video.accountName).to.equal('root')
0e1dc3e7
C
388 })
389
390 it('Should register a new user', async function () {
391 await registerUser(server.url, 'user_15', 'my super password')
392 })
393
394 it('Should be able to login with this registered user', async function () {
395 server.user = {
396 username: 'user_15',
397 password: 'my super password'
398 }
399
eec63bbc 400 accessToken = await serverLogin(server)
5c98d3bf
C
401 })
402
403 it('Should have the correct video quota', async function () {
404 const res = await getMyUserInformation(server.url, accessToken)
405 const user = res.body
406
a7ba16b6 407 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
0e1dc3e7
C
408 })
409
410 after(async function () {
411 killallServers([ server ])
412
413 // Keep the logs if the test failed
afffe988 414 if (this[ 'ok' ]) {
0e1dc3e7
C
415 await flushTests()
416 }
417 })
418})