1 /* tslint:disable:no-unused-expression */
4 import * as chai from 'chai'
5 const expect = chai.expect
21 loginAndGetAccessToken,
24 getUsersListPaginationAndSort,
30 import { killallServers } from '../utils/servers'
31 import { getUserInformation } from '../utils/users'
33 describe('Test users', function () {
34 let server: ServerInfo
35 let accessToken: string
36 let accessTokenUser: string
40 before(async function () {
44 server = await runServer(1)
47 it('Should create a new client')
49 it('Should return the first client')
51 it('Should remove the last client')
53 it('Should not login with an invalid client id', async function () {
54 const client = { id: 'client', secret: server.client.secret }
55 const res = await login(server.url, client, server.user, 400)
57 expect(res.body.error).to.equal('invalid_client')
60 it('Should not login with an invalid client secret', async function () {
61 const client = { id: server.client.id, secret: 'coucou' }
62 const res = await login(server.url, client, server.user, 400)
64 expect(res.body.error).to.equal('invalid_client')
67 it('Should not login with an invalid username', async function () {
68 const user = { username: 'captain crochet', password: server.user.password }
69 const res = await login(server.url, server.client, user, 400)
71 expect(res.body.error).to.equal('invalid_grant')
74 it('Should not login with an invalid password', async function () {
75 const user = { username: server.user.username, password: 'mewthree' }
76 const res = await login(server.url, server.client, user, 400)
78 expect(res.body.error).to.equal('invalid_grant')
81 it('Should not be able to upload a video', async function () {
82 accessToken = 'my_super_token'
84 const videoAttributes = {}
85 await uploadVideo(server.url, accessToken, videoAttributes, 401)
88 it('Should not be able to make friends', async function () {
89 accessToken = 'my_super_token'
90 await makeFriends(server.url, accessToken, 401)
93 it('Should not be able to quit friends', async function () {
94 accessToken = 'my_super_token'
95 await quitFriends(server.url, accessToken, 401)
98 it('Should be able to login', async function () {
99 const res = await login(server.url, server.client, server.user, 200)
101 accessToken = res.body.access_token
104 it('Should upload the video with the correct token', async function () {
105 const videoAttributes = {}
106 await uploadVideo(server.url, accessToken, videoAttributes, 204)
107 const res = await getVideosList(server.url)
108 const video = res.body.data[0]
110 expect(video.author).to.equal('root')
114 it('Should upload the video again with the correct token', async function () {
115 const videoAttributes = {}
116 await uploadVideo(server.url, accessToken, videoAttributes, 204)
119 it('Should retrieve a video rating', async function () {
120 await rateVideo(server.url, accessToken, videoId, 'like')
121 const res = await getUserVideoRating(server.url, accessToken, videoId)
122 const rating = res.body
124 expect(rating.videoId).to.equal(videoId)
125 expect(rating.rating).to.equal('like')
128 it('Should not be able to remove the video with an incorrect token', async function () {
129 await removeVideo(server.url, 'bad_token', videoId, 401)
132 it('Should not be able to remove the video with the token of another account')
134 it('Should be able to remove the video with the correct token', async function () {
135 await removeVideo(server.url, accessToken, videoId)
138 it('Should logout (revoke token)')
140 it('Should not be able to get the user information')
142 it('Should not be able to upload a video')
144 it('Should not be able to remove a video')
146 it('Should not be able to rate a video', async function () {
147 const path = '/api/v1/videos/'
154 path: path + videoId,
155 token: 'wrong token',
157 statusCodeExpected: 401
159 await makePutBodyRequest(options)
162 it('Should be able to login again')
164 it('Should have an expired access token')
166 it('Should refresh the token')
168 it('Should be able to upload a video again')
170 it('Should be able to create a new user', async function () {
171 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
174 it('Should be able to login with this user', async function () {
177 password: 'super password'
180 accessTokenUser = await loginAndGetAccessToken(server)
183 it('Should be able to get the user information', async function () {
184 const res = await getMyUserInformation(server.url, accessTokenUser)
185 const user = res.body
187 expect(user.username).to.equal('user_1')
188 expect(user.email).to.equal('user_1@example.com')
189 expect(user.displayNSFW).to.be.false
190 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
191 expect(user.id).to.be.a('number')
194 it('Should be able to upload a video with this user', async function () {
197 const videoAttributes = {}
198 await uploadVideo(server.url, accessTokenUser, videoAttributes)
201 it('Should list all the users', async function () {
202 const res = await getUsersList(server.url)
203 const result = res.body
204 const total = result.total
205 const users = result.data
207 expect(total).to.equal(2)
208 expect(users).to.be.an('array')
209 expect(users.length).to.equal(2)
211 const user = users[0]
212 expect(user.username).to.equal('user_1')
213 expect(user.email).to.equal('user_1@example.com')
214 expect(user.displayNSFW).to.be.false
216 const rootUser = users[1]
217 expect(rootUser.username).to.equal('root')
218 expect(rootUser.email).to.equal('admin1@example.com')
219 expect(rootUser.displayNSFW).to.be.false
224 it('Should list only the first user by username asc', async function () {
225 const res = await getUsersListPaginationAndSort(server.url, 0, 1, 'username')
227 const result = res.body
228 const total = result.total
229 const users = result.data
231 expect(total).to.equal(2)
232 expect(users.length).to.equal(1)
234 const user = users[0]
235 expect(user.username).to.equal('root')
236 expect(user.email).to.equal('admin1@example.com')
237 expect(user.displayNSFW).to.be.false
240 it('Should list only the first user by username desc', async function () {
241 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-username')
242 const result = res.body
243 const total = result.total
244 const users = result.data
246 expect(total).to.equal(2)
247 expect(users.length).to.equal(1)
249 const user = users[0]
250 expect(user.username).to.equal('user_1')
251 expect(user.email).to.equal('user_1@example.com')
252 expect(user.displayNSFW).to.be.false
255 it('Should list only the second user by createdAt desc', async function () {
256 const res = await getUsersListPaginationAndSort(server.url, 0, 1, '-createdAt')
257 const result = res.body
258 const total = result.total
259 const users = result.data
261 expect(total).to.equal(2)
262 expect(users.length).to.equal(1)
264 const user = users[0]
265 expect(user.username).to.equal('user_1')
266 expect(user.email).to.equal('user_1@example.com')
267 expect(user.displayNSFW).to.be.false
270 it('Should list all the users by createdAt asc', async function () {
271 const res = await getUsersListPaginationAndSort(server.url, 0, 2, 'createdAt')
272 const result = res.body
273 const total = result.total
274 const users = result.data
276 expect(total).to.equal(2)
277 expect(users.length).to.equal(2)
279 expect(users[0].username).to.equal('root')
280 expect(users[0].email).to.equal('admin1@example.com')
281 expect(users[0].displayNSFW).to.be.false
283 expect(users[1].username).to.equal('user_1')
284 expect(users[1].email).to.equal('user_1@example.com')
285 expect(users[1].displayNSFW).to.be.false
288 it('Should update my password', async function () {
289 await updateMyUser(server.url, accessTokenUser, 'new password')
290 server.user.password = 'new password'
292 await login(server.url, server.client, server.user, 200)
295 it('Should be able to change the NSFW display attribute', async function () {
296 await updateMyUser(server.url, accessTokenUser, undefined, true)
298 const res = await getMyUserInformation(server.url, accessTokenUser)
299 const user = res.body
301 expect(user.username).to.equal('user_1')
302 expect(user.email).to.equal('user_1@example.com')
303 expect(user.displayNSFW).to.be.ok
304 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
305 expect(user.id).to.be.a('number')
308 it('Should be able to change the email display attribute', async function () {
309 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
311 const res = await getMyUserInformation(server.url, accessTokenUser)
312 const user = res.body
314 expect(user.username).to.equal('user_1')
315 expect(user.email).to.equal('updated@example.com')
316 expect(user.displayNSFW).to.be.ok
317 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
318 expect(user.id).to.be.a('number')
321 it('Should be able to update another user', async function () {
322 await updateUser(server.url, userId, server.accessToken, 'updated2@example.com', 42 )
324 const res = await getUserInformation(server.url, server.accessToken, userId)
325 const user = res.body
327 expect(user.username).to.equal('user_1')
328 expect(user.email).to.equal('updated2@example.com')
329 expect(user.displayNSFW).to.be.ok
330 expect(user.videoQuota).to.equal(42)
331 expect(user.id).to.be.a('number')
334 it('Should be able to remove this user', async function () {
335 await removeUser(server.url, userId, accessToken)
338 it('Should not be able to login with this user', async function () {
339 // server.user is already set to user 1
340 await login(server.url, server.client, server.user, 400)
343 it('Should not have videos of this user', async function () {
344 const res = await getVideosList(server.url)
346 expect(res.body.total).to.equal(1)
348 const video = res.body.data[0]
349 expect(video.author).to.equal('root')
352 it('Should register a new user', async function () {
353 await registerUser(server.url, 'user_15', 'my super password')
356 it('Should be able to login with this registered user', async function () {
359 password: 'my super password'
362 accessToken = await loginAndGetAccessToken(server)
365 it('Should have the correct video quota', async function () {
366 const res = await getMyUserInformation(server.url, accessToken)
367 const user = res.body
369 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
372 after(async function () {
373 killallServers([ server ])
375 // Keep the logs if the test failed