]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/users.js
Server: refractoring upload/update video test utils
[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 87
b4c5ac97
C
88 const videoAttributes = {}
89 videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 401, done)
0c1cbbfe
C
90 })
91
b3b92647
C
92 it('Should not be able to make friends', function (done) {
93 accessToken = 'mysupertoken'
8d309058 94 podsUtils.makeFriends(server.url, accessToken, 401, done)
b3b92647
C
95 })
96
97 it('Should not be able to quit friends', function (done) {
98 accessToken = 'mysupertoken'
8d309058 99 podsUtils.quitFriends(server.url, accessToken, 401, done)
b3b92647
C
100 })
101
0c1cbbfe 102 it('Should be able to login', function (done) {
8d309058 103 loginUtils.login(server.url, server.client, server.user, 200, function (err, res) {
0c1cbbfe
C
104 if (err) throw err
105
bc503c2a 106 accessToken = res.body.access_token
0c1cbbfe
C
107 done()
108 })
109 })
110
111 it('Should upload the video with the correct token', function (done) {
b4c5ac97
C
112 const videoAttributes = {}
113 videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, function (err, res) {
0c1cbbfe
C
114 if (err) throw err
115
8d309058 116 videosUtils.getVideosList(server.url, function (err, res) {
0c1cbbfe
C
117 if (err) throw err
118
68ce3ae0 119 const video = res.body.data[0]
6d8ada5f
C
120 expect(video.author).to.equal('root')
121
bc503c2a 122 videoId = video.id
0c1cbbfe
C
123 done()
124 })
125 })
126 })
127
128 it('Should upload the video again with the correct token', function (done) {
b4c5ac97
C
129 const videoAttributes = {}
130 videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, done)
0c1cbbfe
C
131 })
132
d38b8281
C
133 it('Should retrieve a video rating', function (done) {
134 videosUtils.rateVideo(server.url, accessToken, videoId, 'like', function (err) {
135 if (err) throw err
136
137 usersUtils.getUserVideoRating(server.url, accessToken, videoId, function (err, res) {
138 if (err) throw err
139
140 const rating = res.body
141
142 expect(rating.videoId).to.equal(videoId)
143 expect(rating.rating).to.equal('like')
144
145 done()
146 })
147 })
148 })
149
0c1cbbfe 150 it('Should not be able to remove the video with an incorrect token', function (done) {
8d309058 151 videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done)
0c1cbbfe
C
152 })
153
154 it('Should not be able to remove the video with the token of another account')
155
156 it('Should be able to remove the video with the correct token', function (done) {
8d309058 157 videosUtils.removeVideo(server.url, accessToken, videoId, done)
0c1cbbfe
C
158 })
159
2f372a86 160 it('Should logout (revoke token)')
0c1cbbfe 161
d38b8281
C
162 it('Should not be able to get the user informations')
163
0c1cbbfe
C
164 it('Should not be able to upload a video')
165
166 it('Should not be able to remove a video')
167
d38b8281
C
168 it('Should not be able to rate a video', function (done) {
169 const path = '/api/v1/videos/'
170 const data = {
171 rating: 'likes'
172 }
173
174 requestsUtils.makePutBodyRequest(server.url, path + videoId, 'wrong token', data, done, 401)
175 })
176
0c1cbbfe
C
177 it('Should be able to login again')
178
2f372a86
C
179 it('Should have an expired access token')
180
181 it('Should refresh the token')
182
183 it('Should be able to upload a video again')
184
9bd26629 185 it('Should be able to create a new user', function (done) {
8d309058 186 usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done)
9bd26629
C
187 })
188
189 it('Should be able to login with this user', function (done) {
190 server.user = {
191 username: 'user_1',
192 password: 'super password'
193 }
194
8d309058 195 loginUtils.loginAndGetAccessToken(server, function (err, token) {
9bd26629
C
196 if (err) throw err
197
198 accessTokenUser = token
199
200 done()
201 })
202 })
203
99a64bfe 204 it('Should be able to get the user informations', function (done) {
8d309058 205 usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) {
99a64bfe
C
206 if (err) throw err
207
208 const user = res.body
209
210 expect(user.username).to.equal('user_1')
ad4a8a1c 211 expect(user.email).to.equal('user_1@example.com')
99a64bfe
C
212 expect(user.id).to.exist
213
214 done()
215 })
216 })
217
9bd26629
C
218 it('Should be able to upload a video with this user', function (done) {
219 this.timeout(5000)
220
b4c5ac97
C
221 const videoAttributes = {}
222 videosUtils.uploadVideo(server.url, accessTokenUser, videoAttributes, done)
9bd26629
C
223 })
224
225 it('Should list all the users', function (done) {
8d309058 226 usersUtils.getUsersList(server.url, function (err, res) {
9bd26629
C
227 if (err) throw err
228
5c39adb7
C
229 const result = res.body
230 const total = result.total
231 const users = result.data
9bd26629 232
5c39adb7 233 expect(total).to.equal(2)
9bd26629
C
234 expect(users).to.be.an('array')
235 expect(users.length).to.equal(2)
236
5c39adb7
C
237 const user = users[0]
238 expect(user.username).to.equal('user_1')
ad4a8a1c 239 expect(user.email).to.equal('user_1@example.com')
5c39adb7
C
240
241 const rootUser = users[1]
9bd26629 242 expect(rootUser.username).to.equal('root')
ad4a8a1c 243 expect(rootUser.email).to.equal('admin1@example.com')
5c39adb7
C
244 userId = user.id
245
246 done()
247 })
248 })
249
250 it('Should list only the first user by username asc', function (done) {
251 usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, 'username', function (err, res) {
252 if (err) throw err
253
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)
9bd26629 260
5c39adb7
C
261 const user = users[0]
262 expect(user.username).to.equal('root')
ad4a8a1c 263 expect(user.email).to.equal('admin1@example.com')
5c39adb7
C
264
265 done()
266 })
267 })
268
269 it('Should list only the first user by username desc', function (done) {
270 usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-username', function (err, res) {
271 if (err) throw err
272
273 const result = res.body
274 const total = result.total
275 const users = result.data
276
277 expect(total).to.equal(2)
278 expect(users.length).to.equal(1)
279
280 const user = users[0]
9bd26629 281 expect(user.username).to.equal('user_1')
ad4a8a1c 282 expect(user.email).to.equal('user_1@example.com')
5c39adb7
C
283
284 done()
285 })
286 })
287
feb4bdfd
C
288 it('Should list only the second user by createdAt desc', function (done) {
289 usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt', function (err, res) {
5c39adb7
C
290 if (err) throw err
291
292 const result = res.body
293 const total = result.total
294 const users = result.data
295
296 expect(total).to.equal(2)
297 expect(users.length).to.equal(1)
298
299 const user = users[0]
300 expect(user.username).to.equal('user_1')
ad4a8a1c 301 expect(user.email).to.equal('user_1@example.com')
5c39adb7
C
302
303 done()
304 })
305 })
306
feb4bdfd
C
307 it('Should list all the users by createdAt asc', function (done) {
308 usersUtils.getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt', function (err, res) {
5c39adb7
C
309 if (err) throw err
310
311 const result = res.body
312 const total = result.total
313 const users = result.data
314
315 expect(total).to.equal(2)
316 expect(users.length).to.equal(2)
317
318 expect(users[0].username).to.equal('root')
ad4a8a1c 319 expect(users[0].email).to.equal('admin1@example.com')
5c39adb7 320 expect(users[1].username).to.equal('user_1')
ad4a8a1c 321 expect(users[1].email).to.equal('user_1@example.com')
9bd26629
C
322
323 done()
324 })
325 })
326
327 it('Should update the user password', function (done) {
8d309058 328 usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) {
9bd26629
C
329 if (err) throw err
330
331 server.user.password = 'new password'
8d309058 332 loginUtils.login(server.url, server.client, server.user, 200, done)
9bd26629
C
333 })
334 })
335
336 it('Should be able to remove this user', function (done) {
68a3b9f2 337 usersUtils.removeUser(server.url, userId, accessToken, done)
9bd26629
C
338 })
339
340 it('Should not be able to login with this user', function (done) {
341 // server.user is already set to user 1
8d309058 342 loginUtils.login(server.url, server.client, server.user, 400, done)
9bd26629
C
343 })
344
345 it('Should not have videos of this user', function (done) {
8d309058 346 videosUtils.getVideosList(server.url, function (err, res) {
9bd26629
C
347 if (err) throw err
348
349 expect(res.body.total).to.equal(1)
350 const video = res.body.data[0]
351 expect(video.author).to.equal('root')
352
353 done()
354 })
355 })
356
0c1cbbfe
C
357 after(function (done) {
358 process.kill(-server.app.pid)
359
360 // Keep the logs if the test failed
361 if (this.ok) {
8d309058 362 serversUtils.flushTests(done)
0c1cbbfe
C
363 } else {
364 done()
365 }
366 })
367})