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