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