]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/users/users.ts
Fix real world script
[github/Chocobozzz/PeerTube.git] / server / tests / api / users / users.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { UserRole } from '../../../../shared/index'
6 import {
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
10 } from '../../utils/index'
11 import { follow } from '../../utils/server/follows'
12 import { setAccessTokensToServers } from '../../utils/users/login'
13 import { getMyVideos } from '../../utils/videos/videos'
14
15 const expect = chai.expect
16
17 describe('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 () {
25 this.timeout(10000)
26
27 await flushTests()
28 server = await runServer(1)
29
30 await setAccessTokensToServers([ server ])
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
43 expect(res.body.error).to.equal('Authentication failed.')
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
50 expect(res.body.error).to.equal('Authentication failed.')
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
57 expect(res.body.error).to.equal('Authentication failed.')
58 })
59
60 it('Should not login with an invalid password', async function () {
61 const user = { username: server.user.username, password: 'mew_three' }
62 const res = await login(server.url, server.client, user, 400)
63
64 expect(res.body.error).to.equal('Authentication failed.')
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
74 it('Should not be able to follow', async function () {
75 accessToken = 'my_super_token'
76 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
77 })
78
79 it('Should not be able to unfollow')
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 = {}
89 await uploadVideo(server.url, accessToken, videoAttributes)
90 const res = await getVideosList(server.url)
91 const video = res.body.data[ 0 ]
92
93 expect(video.accountName).to.equal('root')
94 videoId = video.id
95 })
96
97 it('Should upload the video again with the correct token', async function () {
98 const videoAttributes = {}
99 await uploadVideo(server.url, accessToken, videoAttributes)
100 })
101
102 it('Should retrieve a video rating', async function () {
103 await rateVideo(server.url, accessToken, videoId, 'like')
104 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
105 const rating = res.body
106
107 expect(rating.videoId).to.equal(videoId)
108 expect(rating.rating).to.equal('like')
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 () {
154 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
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
163 accessTokenUser = await serverLogin(server)
164 })
165
166 it('Should be able to get the user information', async function () {
167 const res = await getMyUserInformation(server.url, accessTokenUser)
168 const user = res.body
169
170 expect(user.username).to.equal('user_1')
171 expect(user.email).to.equal('user_1@example.com')
172 expect(user.displayNSFW).to.be.false
173 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
174 expect(user.roleLabel).to.equal('User')
175 expect(user.id).to.be.a('number')
176 })
177
178 it('Should be able to upload a video with this user', async function () {
179 this.timeout(5000)
180
181 const videoAttributes = {
182 name: 'super user video'
183 }
184 await uploadVideo(server.url, accessTokenUser, videoAttributes)
185 })
186
187 it('Should be able to list my videos', async function () {
188 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
189 expect(res.body.total).to.equal(1)
190
191 const videos = res.body.data
192 expect(videos).to.have.lengthOf(1)
193
194 expect(videos[ 0 ].name).to.equal('super user video')
195 })
196
197 it('Should list all the users', async function () {
198 const res = await getUsersList(server.url, server.accessToken)
199 const result = res.body
200 const total = result.total
201 const users = result.data
202
203 expect(total).to.equal(2)
204 expect(users).to.be.an('array')
205 expect(users.length).to.equal(2)
206
207 const user = users[ 0 ]
208 expect(user.username).to.equal('user_1')
209 expect(user.email).to.equal('user_1@example.com')
210 expect(user.displayNSFW).to.be.false
211
212 const rootUser = users[ 1 ]
213 expect(rootUser.username).to.equal('root')
214 expect(rootUser.email).to.equal('admin1@example.com')
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 () {
221 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
222
223 const result = res.body
224 const total = result.total
225 const users = result.data
226
227 expect(total).to.equal(2)
228 expect(users.length).to.equal(1)
229
230 const user = users[ 0 ]
231 expect(user.username).to.equal('root')
232 expect(user.email).to.equal('admin1@example.com')
233 expect(user.roleLabel).to.equal('Administrator')
234 expect(user.displayNSFW).to.be.false
235 })
236
237 it('Should list only the first user by username desc', async function () {
238 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
239 const result = res.body
240 const total = result.total
241 const users = result.data
242
243 expect(total).to.equal(2)
244 expect(users.length).to.equal(1)
245
246 const user = users[ 0 ]
247 expect(user.username).to.equal('user_1')
248 expect(user.email).to.equal('user_1@example.com')
249 expect(user.displayNSFW).to.be.false
250 })
251
252 it('Should list only the second user by createdAt desc', async function () {
253 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
254 const result = res.body
255 const total = result.total
256 const users = result.data
257
258 expect(total).to.equal(2)
259 expect(users.length).to.equal(1)
260
261 const user = users[ 0 ]
262 expect(user.username).to.equal('user_1')
263 expect(user.email).to.equal('user_1@example.com')
264 expect(user.displayNSFW).to.be.false
265 })
266
267 it('Should list all the users by createdAt asc', async function () {
268 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
269 const result = res.body
270 const total = result.total
271 const users = result.data
272
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')
278 expect(users[ 0 ].displayNSFW).to.be.false
279
280 expect(users[ 1 ].username).to.equal('user_1')
281 expect(users[ 1 ].email).to.equal('user_1@example.com')
282 expect(users[ 1 ].displayNSFW).to.be.false
283 })
284
285 it('Should update my password', async function () {
286 await updateMyUser({
287 url: server.url,
288 accessToken: accessTokenUser,
289 newPassword: 'new password'
290 })
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 () {
297 await updateMyUser({
298 url: server.url,
299 accessToken: accessTokenUser,
300 displayNSFW: true
301 })
302
303 const res = await getMyUserInformation(server.url, accessTokenUser)
304 const user = res.body
305
306 expect(user.username).to.equal('user_1')
307 expect(user.email).to.equal('user_1@example.com')
308 expect(user.displayNSFW).to.be.ok
309 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
310 expect(user.id).to.be.a('number')
311 })
312
313 it('Should be able to change the autoPlayVideo attribute', async function () {
314 await updateMyUser({
315 url: server.url,
316 accessToken: accessTokenUser,
317 autoPlayVideo: false
318 })
319
320 const res = await getMyUserInformation(server.url, accessTokenUser)
321 const user = res.body
322
323 expect(user.autoPlayVideo).to.be.false
324 })
325
326 it('Should be able to change the email display attribute', async function () {
327 await updateMyUser({
328 url: server.url,
329 accessToken: accessTokenUser,
330 email: 'updated@example.com'
331 })
332
333 const res = await getMyUserInformation(server.url, accessTokenUser)
334 const user = res.body
335
336 expect(user.username).to.equal('user_1')
337 expect(user.email).to.equal('updated@example.com')
338 expect(user.displayNSFW).to.be.ok
339 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
340 expect(user.id).to.be.a('number')
341 })
342
343 it('Should be able to update another user', async function () {
344 await updateUser({
345 url: server.url,
346 userId,
347 accessToken,
348 email: 'updated2@example.com',
349 videoQuota: 42,
350 role: UserRole.MODERATOR
351 })
352
353 const res = await getUserInformation(server.url, accessToken, userId)
354 const user = res.body
355
356 expect(user.username).to.equal('user_1')
357 expect(user.email).to.equal('updated2@example.com')
358 expect(user.displayNSFW).to.be.ok
359 expect(user.videoQuota).to.equal(42)
360 expect(user.roleLabel).to.equal('Moderator')
361 expect(user.id).to.be.a('number')
362 })
363
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
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
384 expect(res.body.total).to.equal(1)
385
386 const video = res.body.data[ 0 ]
387 expect(video.accountName).to.equal('root')
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
400 accessToken = await serverLogin(server)
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
407 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
408 })
409
410 after(async function () {
411 killallServers([ server ])
412
413 // Keep the logs if the test failed
414 if (this[ 'ok' ]) {
415 await flushTests()
416 }
417 })
418 })