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