1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { UserRole } from '../../../../shared/index'
7 createUser, flushTests, getBlacklistedVideosList, getMyUserInformation, getMyUserVideoRating, getUserInformation, getUsersList,
8 getUsersListPaginationAndSort, getVideosList, killallServers, login, makePutBodyRequest, rateVideo, registerUser, removeUser, removeVideo,
9 runServer, ServerInfo, serverLogin, updateMyUser, updateUser, uploadVideo
10 } from '../../utils/index'
11 import { follow } from '../../utils/server/follows'
12 import { setAccessTokensToServers } from '../../utils/users/login'
13 import { getMyVideos } from '../../utils/videos/videos'
15 const expect = chai.expect
17 describe('Test users', function () {
18 let server: ServerInfo
19 let accessToken: string
20 let accessTokenUser: string
24 before(async function () {
28 server = await runServer(1)
30 await setAccessTokensToServers([ server ])
33 it('Should create a new client')
35 it('Should return the first client')
37 it('Should remove the last client')
39 it('Should not login with an invalid client id', async function () {
40 const client = { id: 'client', secret: server.client.secret }
41 const res = await login(server.url, client, server.user, 400)
43 expect(res.body.error).to.equal('Authentication failed.')
46 it('Should not login with an invalid client secret', async function () {
47 const client = { id: server.client.id, secret: 'coucou' }
48 const res = await login(server.url, client, server.user, 400)
50 expect(res.body.error).to.equal('Authentication failed.')
53 it('Should not login with an invalid username', async function () {
54 const user = { username: 'captain crochet', password: server.user.password }
55 const res = await login(server.url, server.client, user, 400)
57 expect(res.body.error).to.equal('Authentication failed.')
60 it('Should not login with an invalid password', async function () {
61 const user = { username: server.user.username, password: 'mew_three' }
62 const res = await login(server.url, server.client, user, 400)
64 expect(res.body.error).to.equal('Authentication failed.')
67 it('Should not be able to upload a video', async function () {
68 accessToken = 'my_super_token'
70 const videoAttributes = {}
71 await uploadVideo(server.url, accessToken, videoAttributes, 401)
74 it('Should not be able to follow', async function () {
75 accessToken = 'my_super_token'
76 await follow(server.url, [ 'http://example.com' ], accessToken, 401)
79 it('Should not be able to unfollow')
81 it('Should be able to login', async function () {
82 const res = await login(server.url, server.client, server.user, 200)
84 accessToken = res.body.access_token
87 it('Should upload the video with the correct token', async function () {
88 const videoAttributes = {}
89 await uploadVideo(server.url, accessToken, videoAttributes)
90 const res = await getVideosList(server.url)
91 const video = res.body.data[ 0 ]
93 expect(video.accountName).to.equal('root')
97 it('Should upload the video again with the correct token', async function () {
98 const videoAttributes = {}
99 await uploadVideo(server.url, accessToken, videoAttributes)
102 it('Should retrieve a video rating', async function () {
103 await rateVideo(server.url, accessToken, videoId, 'like')
104 const res = await getMyUserVideoRating(server.url, accessToken, videoId)
105 const rating = res.body
107 expect(rating.videoId).to.equal(videoId)
108 expect(rating.rating).to.equal('like')
111 it('Should not be able to remove the video with an incorrect token', async function () {
112 await removeVideo(server.url, 'bad_token', videoId, 401)
115 it('Should not be able to remove the video with the token of another account')
117 it('Should be able to remove the video with the correct token', async function () {
118 await removeVideo(server.url, accessToken, videoId)
121 it('Should logout (revoke token)')
123 it('Should not be able to get the user information')
125 it('Should not be able to upload a video')
127 it('Should not be able to remove a video')
129 it('Should not be able to rate a video', async function () {
130 const path = '/api/v1/videos/'
137 path: path + videoId,
138 token: 'wrong token',
140 statusCodeExpected: 401
142 await makePutBodyRequest(options)
145 it('Should be able to login again')
147 it('Should have an expired access token')
149 it('Should refresh the token')
151 it('Should be able to upload a video again')
153 it('Should be able to create a new user', async function () {
154 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
157 it('Should be able to login with this user', async function () {
160 password: 'super password'
163 accessTokenUser = await serverLogin(server)
166 it('Should be able to get the user information', async function () {
167 const res = await getMyUserInformation(server.url, accessTokenUser)
168 const user = res.body
170 expect(user.username).to.equal('user_1')
171 expect(user.email).to.equal('user_1@example.com')
172 expect(user.displayNSFW).to.be.false
173 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
174 expect(user.roleLabel).to.equal('User')
175 expect(user.id).to.be.a('number')
178 it('Should be able to upload a video with this user', async function () {
181 const videoAttributes = {
182 name: 'super user video'
184 await uploadVideo(server.url, accessTokenUser, videoAttributes)
187 it('Should be able to list my videos', async function () {
188 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
189 expect(res.body.total).to.equal(1)
191 const videos = res.body.data
192 expect(videos).to.have.lengthOf(1)
194 expect(videos[ 0 ].name).to.equal('super user video')
197 it('Should list all the users', async function () {
198 const res = await getUsersList(server.url, server.accessToken)
199 const result = res.body
200 const total = result.total
201 const users = result.data
203 expect(total).to.equal(2)
204 expect(users).to.be.an('array')
205 expect(users.length).to.equal(2)
207 const user = users[ 0 ]
208 expect(user.username).to.equal('user_1')
209 expect(user.email).to.equal('user_1@example.com')
210 expect(user.displayNSFW).to.be.false
212 const rootUser = users[ 1 ]
213 expect(rootUser.username).to.equal('root')
214 expect(rootUser.email).to.equal('admin1@example.com')
215 expect(rootUser.displayNSFW).to.be.false
220 it('Should list only the first user by username asc', async function () {
221 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, 'username')
223 const result = res.body
224 const total = result.total
225 const users = result.data
227 expect(total).to.equal(2)
228 expect(users.length).to.equal(1)
230 const user = users[ 0 ]
231 expect(user.username).to.equal('root')
232 expect(user.email).to.equal('admin1@example.com')
233 expect(user.roleLabel).to.equal('Administrator')
234 expect(user.displayNSFW).to.be.false
237 it('Should list only the first user by username desc', async function () {
238 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-username')
239 const result = res.body
240 const total = result.total
241 const users = result.data
243 expect(total).to.equal(2)
244 expect(users.length).to.equal(1)
246 const user = users[ 0 ]
247 expect(user.username).to.equal('user_1')
248 expect(user.email).to.equal('user_1@example.com')
249 expect(user.displayNSFW).to.be.false
252 it('Should list only the second user by createdAt desc', async function () {
253 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 1, '-createdAt')
254 const result = res.body
255 const total = result.total
256 const users = result.data
258 expect(total).to.equal(2)
259 expect(users.length).to.equal(1)
261 const user = users[ 0 ]
262 expect(user.username).to.equal('user_1')
263 expect(user.email).to.equal('user_1@example.com')
264 expect(user.displayNSFW).to.be.false
267 it('Should list all the users by createdAt asc', async function () {
268 const res = await getUsersListPaginationAndSort(server.url, server.accessToken, 0, 2, 'createdAt')
269 const result = res.body
270 const total = result.total
271 const users = result.data
273 expect(total).to.equal(2)
274 expect(users.length).to.equal(2)
276 expect(users[ 0 ].username).to.equal('root')
277 expect(users[ 0 ].email).to.equal('admin1@example.com')
278 expect(users[ 0 ].displayNSFW).to.be.false
280 expect(users[ 1 ].username).to.equal('user_1')
281 expect(users[ 1 ].email).to.equal('user_1@example.com')
282 expect(users[ 1 ].displayNSFW).to.be.false
285 it('Should update my password', async function () {
288 accessToken: accessTokenUser,
289 newPassword: 'new password'
291 server.user.password = 'new password'
293 await login(server.url, server.client, server.user, 200)
296 it('Should be able to change the NSFW display attribute', async function () {
299 accessToken: accessTokenUser,
303 const res = await getMyUserInformation(server.url, accessTokenUser)
304 const user = res.body
306 expect(user.username).to.equal('user_1')
307 expect(user.email).to.equal('user_1@example.com')
308 expect(user.displayNSFW).to.be.ok
309 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
310 expect(user.id).to.be.a('number')
313 it('Should be able to change the autoPlayVideo attribute', async function () {
316 accessToken: accessTokenUser,
320 const res = await getMyUserInformation(server.url, accessTokenUser)
321 const user = res.body
323 expect(user.autoPlayVideo).to.be.false
326 it('Should be able to change the email display attribute', async function () {
329 accessToken: accessTokenUser,
330 email: 'updated@example.com'
333 const res = await getMyUserInformation(server.url, accessTokenUser)
334 const user = res.body
336 expect(user.username).to.equal('user_1')
337 expect(user.email).to.equal('updated@example.com')
338 expect(user.displayNSFW).to.be.ok
339 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
340 expect(user.id).to.be.a('number')
343 it('Should be able to update another user', async function () {
348 email: 'updated2@example.com',
350 role: UserRole.MODERATOR
353 const res = await getUserInformation(server.url, accessToken, userId)
354 const user = res.body
356 expect(user.username).to.equal('user_1')
357 expect(user.email).to.equal('updated2@example.com')
358 expect(user.displayNSFW).to.be.ok
359 expect(user.videoQuota).to.equal(42)
360 expect(user.roleLabel).to.equal('Moderator')
361 expect(user.id).to.be.a('number')
364 it('Should not be able to delete a user by a moderator', async function () {
365 await removeUser(server.url, 2, accessTokenUser, 403)
368 it('Should be able to list video blacklist by a moderator', async function () {
369 await getBlacklistedVideosList(server.url, accessTokenUser)
372 it('Should be able to remove this user', async function () {
373 await removeUser(server.url, userId, accessToken)
376 it('Should not be able to login with this user', async function () {
377 // server.user is already set to user 1
378 await login(server.url, server.client, server.user, 400)
381 it('Should not have videos of this user', async function () {
382 const res = await getVideosList(server.url)
384 expect(res.body.total).to.equal(1)
386 const video = res.body.data[ 0 ]
387 expect(video.accountName).to.equal('root')
390 it('Should register a new user', async function () {
391 await registerUser(server.url, 'user_15', 'my super password')
394 it('Should be able to login with this registered user', async function () {
397 password: 'my super password'
400 accessToken = await serverLogin(server)
403 it('Should have the correct video quota', async function () {
404 const res = await getMyUserInformation(server.url, accessToken)
405 const user = res.body
407 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
410 after(async function () {
411 killallServers([ server ])
413 // Keep the logs if the test failed