1 /* tslint:disable:no-unused-expression */
4 import * as chai from 'chai'
5 const expect = chai.expect
21 loginAndGetAccessToken,
24 getUsersListPaginationAndSort,
29 import { killallServers } from '../utils/servers'
31 describe('Test users', function () {
32 let server: ServerInfo
33 let accessToken: string
34 let accessTokenUser: string
38 before(async function () {
42 server = await runServer(1)
45 it('Should create a new client')
47 it('Should return the first client')
49 it('Should remove the last client')
51 it('Should not login with an invalid client id', async function () {
52 const client = { id: 'client', secret: server.client.secret }
53 const res = await login(server.url, client, server.user, 400)
55 expect(res.body.error).to.equal('invalid_client')
58 it('Should not login with an invalid client secret', async function () {
59 const client = { id: server.client.id, secret: 'coucou' }
60 const res = await login(server.url, client, server.user, 400)
62 expect(res.body.error).to.equal('invalid_client')
65 it('Should not login with an invalid username', async function () {
66 const user = { username: 'captain crochet', password: server.user.password }
67 const res = await login(server.url, server.client, user, 400)
69 expect(res.body.error).to.equal('invalid_grant')
72 it('Should not login with an invalid password', async function () {
73 const user = { username: server.user.username, password: 'mewthree' }
74 const res = await login(server.url, server.client, user, 400)
76 expect(res.body.error).to.equal('invalid_grant')
79 it('Should not be able to upload a video', async function () {
80 accessToken = 'my_super_token'
82 const videoAttributes = {}
83 await uploadVideo(server.url, accessToken, videoAttributes, 401)
86 it('Should not be able to make friends', async function () {
87 accessToken = 'my_super_token'
88 await makeFriends(server.url, accessToken, 401)
91 it('Should not be able to quit friends', async function () {
92 accessToken = 'my_super_token'
93 await quitFriends(server.url, accessToken, 401)
96 it('Should be able to login', async function () {
97 const res = await login(server.url, server.client, server.user, 200)
99 accessToken = res.body.access_token
102 it('Should upload the video with the correct token', async function () {
103 const videoAttributes = {}
104 await uploadVideo(server.url, accessToken, videoAttributes, 204)
105 const res = await getVideosList(server.url)
106 const video = res.body.data[0]
108 expect(video.author).to.equal('root')
112 it('Should upload the video again with the correct token', async function () {
113 const videoAttributes = {}
114 await uploadVideo(server.url, accessToken, videoAttributes, 204)
117 it('Should retrieve a video rating', async function () {
118 await rateVideo(server.url, accessToken, videoId, 'like')
119 const res = await getUserVideoRating(server.url, accessToken, videoId)
120 const rating = res.body
122 expect(rating.videoId).to.equal(videoId)
123 expect(rating.rating).to.equal('like')
126 it('Should not be able to remove the video with an incorrect token', async function () {
127 await removeVideo(server.url, 'bad_token', videoId, 401)
130 it('Should not be able to remove the video with the token of another account')
132 it('Should be able to remove the video with the correct token', async function () {
133 await removeVideo(server.url, accessToken, videoId)
136 it('Should logout (revoke token)')
138 it('Should not be able to get the user information')
140 it('Should not be able to upload a video')
142 it('Should not be able to remove a video')
144 it('Should not be able to rate a video', async function () {
145 const path = '/api/v1/videos/'
152 path: path + videoId,
153 token: 'wrong token',
155 statusCodeExpected: 401
157 await makePutBodyRequest(options)
160 it('Should be able to login again')
162 it('Should have an expired access token')
164 it('Should refresh the token')
166 it('Should be able to upload a video again')
168 it('Should be able to create a new user', async function () {
169 await createUser(server.url, accessToken, 'user_1', 'super password')
172 it('Should be able to login with this user', async function () {
175 password: 'super password'
178 accessTokenUser = await loginAndGetAccessToken(server)
181 it('Should be able to get the user information', async function () {
182 const res = await getUserInformation(server.url, accessTokenUser)
183 const user = res.body
185 expect(user.username).to.equal('user_1')
186 expect(user.email).to.equal('user_1@example.com')
187 expect(user.displayNSFW).to.be.false
188 expect(user.id).to.be.a('number')
191 it('Should be able to upload a video with this user', async function () {
194 const videoAttributes = {}
195 await uploadVideo(server.url, accessTokenUser, videoAttributes)
198 it('Should list all the users', async function () {
199 const res = await getUsersList(server.url)
200 const result = res.body
201 const total = result.total
202 const users = result.data
204 expect(total).to.equal(2)
205 expect(users).to.be.an('array')
206 expect(users.length).to.equal(2)
208 const user = users[0]
209 expect(user.username).to.equal('user_1')
210 expect(user.email).to.equal('user_1@example.com')
211 expect(user.displayNSFW).to.be.false
213 const rootUser = users[1]
214 expect(rootUser.username).to.equal('root')
215 expect(rootUser.email).to.equal('admin1@example.com')
216 expect(rootUser.displayNSFW).to.be.false
221 it('Should list only the first user by username asc', async function () {
222 const res = await getUsersListPaginationAndSort(server.url, 0, 1, 'username')
224 const result = res.body
225 const total = result.total
226 const users = result.data
228 expect(total).to.equal(2)
229 expect(users.length).to.equal(1)
231 const user = users[0]
232 expect(user.username).to.equal('root')
233 expect(user.email).to.equal('admin1@example.com')
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, 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, 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, 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 the user password', async function () {
286 await updateUser(server.url, userId, accessTokenUser, 'new password', null)
287 server.user.password = 'new password'
289 await login(server.url, server.client, server.user, 200)
292 it('Should be able to change the NSFW display attribute', async function () {
293 await updateUser(server.url, userId, accessTokenUser, null, true)
295 const res = await getUserInformation(server.url, accessTokenUser)
296 const user = res.body
298 expect(user.username).to.equal('user_1')
299 expect(user.email).to.equal('user_1@example.com')
300 expect(user.displayNSFW).to.be.ok
301 expect(user.id).to.be.a('number')
304 it('Should be able to remove this user', async function () {
305 await removeUser(server.url, userId, accessToken)
308 it('Should not be able to login with this user', async function () {
309 // server.user is already set to user 1
310 await login(server.url, server.client, server.user, 400)
313 it('Should not have videos of this user', async function () {
314 const res = await getVideosList(server.url)
316 expect(res.body.total).to.equal(1)
318 const video = res.body.data[0]
319 expect(video.author).to.equal('root')
322 it('Should register a new user', async function () {
323 await registerUser(server.url, 'user_15', 'my super password')
326 it('Should be able to login with this registered user', async function () {
329 password: 'my super password'
332 await loginAndGetAccessToken(server)
335 after(async function () {
336 killallServers([ server ])
338 // Keep the logs if the test failed