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