aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/users.js
blob: 2b6956baadcc75701772dc158e804522171805e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* eslint-disable no-unused-expressions */

'use strict'

const chai = require('chai')
const expect = chai.expect
const series = require('async/series')

const loginUtils = require('../utils/login')
const podsUtils = require('../utils/pods')
const serversUtils = require('../utils/servers')
const usersUtils = require('../utils/users')
const requestsUtils = require('../utils/requests')
const videosUtils = require('../utils/videos')

describe('Test users', function () {
  let server = null
  let accessToken = null
  let accessTokenUser = null
  let videoId = null
  let userId = null

  before(function (done) {
    this.timeout(20000)

    series([
      function (next) {
        serversUtils.flushTests(next)
      },
      function (next) {
        serversUtils.runServer(1, function (server1) {
          server = server1
          next()
        })
      }
    ], done)
  })

  it('Should create a new client')

  it('Should return the first client')

  it('Should remove the last client')

  it('Should not login with an invalid client id', function (done) {
    const client = { id: 'client', password: server.client.secret }
    loginUtils.login(server.url, client, server.user, 400, function (err, res) {
      if (err) throw err

      expect(res.body.error).to.equal('invalid_client')
      done()
    })
  })

  it('Should not login with an invalid client password', function (done) {
    const client = { id: server.client.id, password: 'coucou' }
    loginUtils.login(server.url, client, server.user, 400, function (err, res) {
      if (err) throw err

      expect(res.body.error).to.equal('invalid_client')
      done()
    })
  })

  it('Should not login with an invalid username', function (done) {
    const user = { username: 'captain crochet', password: server.user.password }
    loginUtils.login(server.url, server.client, user, 400, function (err, res) {
      if (err) throw err

      expect(res.body.error).to.equal('invalid_grant')
      done()
    })
  })

  it('Should not login with an invalid password', function (done) {
    const user = { username: server.user.username, password: 'mewthree' }
    loginUtils.login(server.url, server.client, user, 400, function (err, res) {
      if (err) throw err

      expect(res.body.error).to.equal('invalid_grant')
      done()
    })
  })

  it('Should not be able to upload a video', function (done) {
    accessToken = 'mysupertoken'

    const videoAttributes = {}
    videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 401, done)
  })

  it('Should not be able to make friends', function (done) {
    accessToken = 'mysupertoken'
    podsUtils.makeFriends(server.url, accessToken, 401, done)
  })

  it('Should not be able to quit friends', function (done) {
    accessToken = 'mysupertoken'
    podsUtils.quitFriends(server.url, accessToken, 401, done)
  })

  it('Should be able to login', function (done) {
    loginUtils.login(server.url, server.client, server.user, 200, function (err, res) {
      if (err) throw err

      accessToken = res.body.access_token
      done()
    })
  })

  it('Should upload the video with the correct token', function (done) {
    const videoAttributes = {}
    videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, function (err, res) {
      if (err) throw err

      videosUtils.getVideosList(server.url, function (err, res) {
        if (err) throw err

        const video = res.body.data[0]
        expect(video.author).to.equal('root')

        videoId = video.id
        done()
      })
    })
  })

  it('Should upload the video again with the correct token', function (done) {
    const videoAttributes = {}
    videosUtils.uploadVideo(server.url, accessToken, videoAttributes, 204, done)
  })

  it('Should retrieve a video rating', function (done) {
    videosUtils.rateVideo(server.url, accessToken, videoId, 'like', function (err) {
      if (err) throw err

      usersUtils.getUserVideoRating(server.url, accessToken, videoId, function (err, res) {
        if (err) throw err

        const rating = res.body

        expect(rating.videoId).to.equal(videoId)
        expect(rating.rating).to.equal('like')

        done()
      })
    })
  })

  it('Should not be able to remove the video with an incorrect token', function (done) {
    videosUtils.removeVideo(server.url, 'bad_token', videoId, 401, done)
  })

  it('Should not be able to remove the video with the token of another account')

  it('Should be able to remove the video with the correct token', function (done) {
    videosUtils.removeVideo(server.url, accessToken, videoId, done)
  })

  it('Should logout (revoke token)')

  it('Should not be able to get the user informations')

  it('Should not be able to upload a video')

  it('Should not be able to remove a video')

  it('Should not be able to rate a video', function (done) {
    const path = '/api/v1/videos/'
    const data = {
      rating: 'likes'
    }

    requestsUtils.makePutBodyRequest(server.url, path + videoId, 'wrong token', data, done, 401)
  })

  it('Should be able to login again')

  it('Should have an expired access token')

  it('Should refresh the token')

  it('Should be able to upload a video again')

  it('Should be able to create a new user', function (done) {
    usersUtils.createUser(server.url, accessToken, 'user_1', 'super password', done)
  })

  it('Should be able to login with this user', function (done) {
    server.user = {
      username: 'user_1',
      password: 'super password'
    }

    loginUtils.loginAndGetAccessToken(server, function (err, token) {
      if (err) throw err

      accessTokenUser = token

      done()
    })
  })

  it('Should be able to get the user informations', function (done) {
    usersUtils.getUserInformation(server.url, accessTokenUser, function (err, res) {
      if (err) throw err

      const user = res.body

      expect(user.username).to.equal('user_1')
      expect(user.email).to.equal('user_1@example.com')
      expect(user.id).to.exist

      done()
    })
  })

  it('Should be able to upload a video with this user', function (done) {
    this.timeout(5000)

    const videoAttributes = {}
    videosUtils.uploadVideo(server.url, accessTokenUser, videoAttributes, done)
  })

  it('Should list all the users', function (done) {
    usersUtils.getUsersList(server.url, function (err, res) {
      if (err) throw err

      const result = res.body
      const total = result.total
      const users = result.data

      expect(total).to.equal(2)
      expect(users).to.be.an('array')
      expect(users.length).to.equal(2)

      const user = users[0]
      expect(user.username).to.equal('user_1')
      expect(user.email).to.equal('user_1@example.com')

      const rootUser = users[1]
      expect(rootUser.username).to.equal('root')
      expect(rootUser.email).to.equal('admin1@example.com')
      userId = user.id

      done()
    })
  })

  it('Should list only the first user by username asc', function (done) {
    usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, 'username', function (err, res) {
      if (err) throw err

      const result = res.body
      const total = result.total
      const users = result.data

      expect(total).to.equal(2)
      expect(users.length).to.equal(1)

      const user = users[0]
      expect(user.username).to.equal('root')
      expect(user.email).to.equal('admin1@example.com')

      done()
    })
  })

  it('Should list only the first user by username desc', function (done) {
    usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-username', function (err, res) {
      if (err) throw err

      const result = res.body
      const total = result.total
      const users = result.data

      expect(total).to.equal(2)
      expect(users.length).to.equal(1)

      const user = users[0]
      expect(user.username).to.equal('user_1')
      expect(user.email).to.equal('user_1@example.com')

      done()
    })
  })

  it('Should list only the second user by createdAt desc', function (done) {
    usersUtils.getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt', function (err, res) {
      if (err) throw err

      const result = res.body
      const total = result.total
      const users = result.data

      expect(total).to.equal(2)
      expect(users.length).to.equal(1)

      const user = users[0]
      expect(user.username).to.equal('user_1')
      expect(user.email).to.equal('user_1@example.com')

      done()
    })
  })

  it('Should list all the users by createdAt asc', function (done) {
    usersUtils.getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt', function (err, res) {
      if (err) throw err

      const result = res.body
      const total = result.total
      const users = result.data

      expect(total).to.equal(2)
      expect(users.length).to.equal(2)

      expect(users[0].username).to.equal('root')
      expect(users[0].email).to.equal('admin1@example.com')
      expect(users[1].username).to.equal('user_1')
      expect(users[1].email).to.equal('user_1@example.com')

      done()
    })
  })

  it('Should update the user password', function (done) {
    usersUtils.updateUser(server.url, userId, accessTokenUser, 'new password', function (err, res) {
      if (err) throw err

      server.user.password = 'new password'
      loginUtils.login(server.url, server.client, server.user, 200, done)
    })
  })

  it('Should be able to remove this user', function (done) {
    usersUtils.removeUser(server.url, userId, accessToken, done)
  })

  it('Should not be able to login with this user', function (done) {
    // server.user is already set to user 1
    loginUtils.login(server.url, server.client, server.user, 400, done)
  })

  it('Should not have videos of this user', function (done) {
    videosUtils.getVideosList(server.url, function (err, res) {
      if (err) throw err

      expect(res.body.total).to.equal(1)
      const video = res.body.data[0]
      expect(video.author).to.equal('root')

      done()
    })
  })

  after(function (done) {
    process.kill(-server.app.pid)

    // Keep the logs if the test failed
    if (this.ok) {
      serversUtils.flushTests(done)
    } else {
      done()
    }
  })
})