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