1 /* tslint:disable:no-unused-expression */
4 import * as chai from 'chai'
5 const expect = chai.expect
21 loginAndGetAccessToken,
24 getUsersListPaginationAndSort,
31 getBlacklistedVideosList
33 import { UserRole } from '../../../shared'
34 import { getMyVideos } from '../utils/videos'
36 describe('Test users', function () {
37 let server: ServerInfo
38 let accessToken: string
39 let accessTokenUser: string
43 before(async function () {
47 server = await runServer(1)
50 it('Should create a new client')
52 it('Should return the first client')
54 it('Should remove the last client')
56 it('Should not login with an invalid client id', async function () {
57 const client = { id: 'client', secret: server.client.secret }
58 const res = await login(server.url, client, server.user, 400)
60 expect(res.body.error).to.equal('invalid_client')
63 it('Should not login with an invalid client secret', async function () {
64 const client = { id: server.client.id, secret: 'coucou' }
65 const res = await login(server.url, client, server.user, 400)
67 expect(res.body.error).to.equal('invalid_client')
70 it('Should not login with an invalid username', async function () {
71 const user = { username: 'captain crochet', password: server.user.password }
72 const res = await login(server.url, server.client, user, 400)
74 expect(res.body.error).to.equal('invalid_grant')
77 it('Should not login with an invalid password', async function () {
78 const user = { username: server.user.username, password: 'mew_three' }
79 const res = await login(server.url, server.client, user, 400)
81 expect(res.body.error).to.equal('invalid_grant')
84 it('Should not be able to upload a video', async function () {
85 accessToken = 'my_super_token'
87 const videoAttributes = {}
88 await uploadVideo(server.url, accessToken, videoAttributes, 401)
91 it('Should not be able to make friends', async function () {
92 accessToken = 'my_super_token'
93 await makeFriends(server.url, accessToken, 401)
96 it('Should not be able to quit friends', async function () {
97 accessToken = 'my_super_token'
98 await quitFriends(server.url, accessToken, 401)
101 it('Should be able to login', async function () {
102 const res = await login(server.url, server.client, server.user, 200)
104 accessToken = res.body.access_token
107 it('Should upload the video with the correct token', async function () {
108 const videoAttributes = {}
109 await uploadVideo(server.url, accessToken, videoAttributes, 204)
110 const res = await getVideosList(server.url)
111 const video = res.body.data[0]
113 expect(video.author).to.equal('root')
117 it('Should upload the video again with the correct token', async function () {
118 const videoAttributes = {}
119 await uploadVideo(server.url, accessToken, videoAttributes, 204)
122 it('Should retrieve a video rating', async function () {
123 await rateVideo(server.url, accessToken, videoId, 'like')
124 const res = await getUserVideoRating(server.url, accessToken, videoId)
125 const rating = res.body
127 expect(rating.videoId).to.equal(videoId)
128 expect(rating.rating).to.equal('like')
131 it('Should not be able to remove the video with an incorrect token', async function () {
132 await removeVideo(server.url, 'bad_token', videoId, 401)
135 it('Should not be able to remove the video with the token of another account')
137 it('Should be able to remove the video with the correct token', async function () {
138 await removeVideo(server.url, accessToken, videoId)
141 it('Should logout (revoke token)')
143 it('Should not be able to get the user information')
145 it('Should not be able to upload a video')
147 it('Should not be able to remove a video')
149 it('Should not be able to rate a video', async function () {
150 const path = '/api/v1/videos/'
157 path: path + videoId,
158 token: 'wrong token',
160 statusCodeExpected: 401
162 await makePutBodyRequest(options)
165 it('Should be able to login again')
167 it('Should have an expired access token')
169 it('Should refresh the token')
171 it('Should be able to upload a video again')
173 it('Should be able to create a new user', async function () {
174 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
177 it('Should be able to login with this user', async function () {
180 password: 'super password'
183 accessTokenUser = await loginAndGetAccessToken(server)
186 it('Should be able to get the user information', async function () {
187 const res = await getMyUserInformation(server.url, accessTokenUser)
188 const user = res.body
190 expect(user.username).to.equal('user_1')
191 expect(user.email).to.equal('user_1@example.com')
192 expect(user.displayNSFW).to.be.false
193 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
194 expect(user.roleLabel).to.equal('User')
195 expect(user.id).to.be.a('number')
198 it('Should be able to upload a video with this user', async function () {
201 const videoAttributes = {
202 name: 'super user video'
204 await uploadVideo(server.url, accessTokenUser, videoAttributes)
207 it('Should be able to list my videos', async function () {
208 const res = await getMyVideos(server.url, accessTokenUser, 0, 5)
209 expect(res.body.total).to.equal(1)
211 const videos = res.body.data
212 expect(videos).to.have.lengthOf(1)
214 expect(videos[0].name).to.equal('super user video')
217 it('Should list all the users', async function () {
218 const res = await getUsersList(server.url)
219 const result = res.body
220 const total = result.total
221 const users = result.data
223 expect(total).to.equal(2)
224 expect(users).to.be.an('array')
225 expect(users.length).to.equal(2)
227 const user = users[0]
228 expect(user.username).to.equal('user_1')
229 expect(user.email).to.equal('user_1@example.com')
230 expect(user.displayNSFW).to.be.false
232 const rootUser = users[1]
233 expect(rootUser.username).to.equal('root')
234 expect(rootUser.email).to.equal('admin1@example.com')
235 expect(rootUser.displayNSFW).to.be.false
240 it('Should list only the first user by username asc', async function () {
241 const res = await getUsersListPaginationAndSort(server.url, 0, 1, 'username')
243 const result = res.body
244 const total = result.total
245 const users = result.data
247 expect(total).to.equal(2)
248 expect(users.length).to.equal(1)
250 const user = users[0]
251 expect(user.username).to.equal('root')
252 expect(user.email).to.equal('admin1@example.com')
253 expect(user.roleLabel).to.equal('Administrator')
254 expect(user.displayNSFW).to.be.false
257 it('Should list only the first user by username desc', async function () {
258 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-username')
259 const result = res.body
260 const total = result.total
261 const users = result.data
263 expect(total).to.equal(2)
264 expect(users.length).to.equal(1)
266 const user = users[0]
267 expect(user.username).to.equal('user_1')
268 expect(user.email).to.equal('user_1@example.com')
269 expect(user.displayNSFW).to.be.false
272 it('Should list only the second user by createdAt desc', async function () {
273 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt')
274 const result = res.body
275 const total = result.total
276 const users = result.data
278 expect(total).to.equal(2)
279 expect(users.length).to.equal(1)
281 const user = users[0]
282 expect(user.username).to.equal('user_1')
283 expect(user.email).to.equal('user_1@example.com')
284 expect(user.displayNSFW).to.be.false
287 it('Should list all the users by createdAt asc', async function () {
288 const res = await getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt')
289 const result = res.body
290 const total = result.total
291 const users = result.data
293 expect(total).to.equal(2)
294 expect(users.length).to.equal(2)
296 expect(users[0].username).to.equal('root')
297 expect(users[0].email).to.equal('admin1@example.com')
298 expect(users[0].displayNSFW).to.be.false
300 expect(users[1].username).to.equal('user_1')
301 expect(users[1].email).to.equal('user_1@example.com')
302 expect(users[1].displayNSFW).to.be.false
305 it('Should update my password', async function () {
306 await updateMyUser(server.url, accessTokenUser, 'new password')
307 server.user.password = 'new password'
309 await login(server.url, server.client, server.user, 200)
312 it('Should be able to change the NSFW display attribute', async function () {
313 await updateMyUser(server.url, accessTokenUser, undefined, true)
315 const res = await getMyUserInformation(server.url, accessTokenUser)
316 const user = res.body
318 expect(user.username).to.equal('user_1')
319 expect(user.email).to.equal('user_1@example.com')
320 expect(user.displayNSFW).to.be.ok
321 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
322 expect(user.id).to.be.a('number')
325 it('Should be able to change the email display attribute', async function () {
326 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
328 const res = await getMyUserInformation(server.url, accessTokenUser)
329 const user = res.body
331 expect(user.username).to.equal('user_1')
332 expect(user.email).to.equal('updated@example.com')
333 expect(user.displayNSFW).to.be.ok
334 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
335 expect(user.id).to.be.a('number')
338 it('Should be able to update another user', async function () {
339 await updateUser(server.url, userId, accessToken, 'updated2@example.com', 42, UserRole.MODERATOR)
341 const res = await getUserInformation(server.url, accessToken, userId)
342 const user = res.body
344 expect(user.username).to.equal('user_1')
345 expect(user.email).to.equal('updated2@example.com')
346 expect(user.displayNSFW).to.be.ok
347 expect(user.videoQuota).to.equal(42)
348 expect(user.roleLabel).to.equal('Moderator')
349 expect(user.id).to.be.a('number')
352 it('Should not be able to delete a user by a moderator', async function () {
353 await removeUser(server.url, 2, accessTokenUser, 403)
356 it('Should be able to list video blacklist by a moderator', async function () {
357 await getBlacklistedVideosList(server.url, accessTokenUser)
360 it('Should be able to remove this user', async function () {
361 await removeUser(server.url, userId, accessToken)
364 it('Should not be able to login with this user', async function () {
365 // server.user is already set to user 1
366 await login(server.url, server.client, server.user, 400)
369 it('Should not have videos of this user', async function () {
370 const res = await getVideosList(server.url)
372 expect(res.body.total).to.equal(1)
374 const video = res.body.data[0]
375 expect(video.author).to.equal('root')
378 it('Should register a new user', async function () {
379 await registerUser(server.url, 'user_15', 'my super password')
382 it('Should be able to login with this registered user', async function () {
385 password: 'my super password'
388 accessToken = await loginAndGetAccessToken(server)
391 it('Should have the correct video quota', async function () {
392 const res = await getMyUserInformation(server.url, accessToken)
393 const user = res.body
395 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
398 after(async function () {
399 killallServers([ server ])
401 // Keep the logs if the test failed