]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
86d13ec2 | 2 | |
afffe988 | 3 | import 'mocha' |
4f32032f | 4 | import * as chai from 'chai' |
c55e3d72 | 5 | import { testImage } from '@server/tests/shared' |
58c44687 | 6 | import { AbuseState, HttpStatusCode, OAuth2ErrorCode, UserAdminFlag, UserRole, VideoPlaylistType } from '@shared/models' |
0e1dc3e7 | 7 | import { |
7c3b7976 | 8 | cleanupTests, |
254d3579 | 9 | createSingleServer, |
f43db2f4 | 10 | killallServers, |
a890d1e0 | 11 | makePutBodyRequest, |
254d3579 | 12 | PeerTubeServer, |
58c44687 | 13 | setAccessTokensToServers |
bf54587a | 14 | } from '@shared/server-commands' |
0e1dc3e7 | 15 | |
afffe988 C |
16 | const expect = chai.expect |
17 | ||
0e1dc3e7 | 18 | describe('Test users', function () { |
254d3579 | 19 | let server: PeerTubeServer |
d23dd9fb C |
20 | let token: string |
21 | let userToken: string | |
0e1dc3e7 C |
22 | let videoId: number |
23 | let userId: number | |
f8b8c36b C |
24 | const user = { |
25 | username: 'user_1', | |
26 | password: 'super password' | |
27 | } | |
0e1dc3e7 C |
28 | |
29 | before(async function () { | |
e212f887 | 30 | this.timeout(30000) |
e1c55031 | 31 | |
254d3579 | 32 | server = await createSingleServer(1, { |
e1c55031 C |
33 | rates_limit: { |
34 | login: { | |
35 | max: 30 | |
36 | } | |
37 | } | |
38 | }) | |
86d13ec2 C |
39 | |
40 | await setAccessTokensToServers([ server ]) | |
9b474844 | 41 | |
89d241a7 | 42 | await server.plugins.install({ npmName: 'peertube-theme-background-red' }) |
0e1dc3e7 C |
43 | }) |
44 | ||
1eddc9a7 C |
45 | describe('OAuth client', function () { |
46 | it('Should create a new client') | |
0e1dc3e7 | 47 | |
1eddc9a7 | 48 | it('Should return the first client') |
0e1dc3e7 | 49 | |
1eddc9a7 | 50 | it('Should remove the last client') |
0e1dc3e7 | 51 | |
1eddc9a7 | 52 | it('Should not login with an invalid client id', async function () { |
89d241a7 C |
53 | const client = { id: 'client', secret: server.store.client.secret } |
54 | const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
0e1dc3e7 | 55 | |
41d1d075 C |
56 | expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT) |
57 | expect(body.error).to.contain('client is invalid') | |
58 | expect(body.type.startsWith('https://')).to.be.true | |
59 | expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT) | |
1eddc9a7 | 60 | }) |
0e1dc3e7 | 61 | |
1eddc9a7 | 62 | it('Should not login with an invalid client secret', async function () { |
89d241a7 C |
63 | const client = { id: server.store.client.id, secret: 'coucou' } |
64 | const body = await server.login.login({ client, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
0e1dc3e7 | 65 | |
41d1d075 C |
66 | expect(body.code).to.equal(OAuth2ErrorCode.INVALID_CLIENT) |
67 | expect(body.error).to.contain('client is invalid') | |
68 | expect(body.type.startsWith('https://')).to.be.true | |
69 | expect(body.type).to.contain(OAuth2ErrorCode.INVALID_CLIENT) | |
1eddc9a7 | 70 | }) |
0e1dc3e7 C |
71 | }) |
72 | ||
1eddc9a7 | 73 | describe('Login', function () { |
0e1dc3e7 | 74 | |
1eddc9a7 | 75 | it('Should not login with an invalid username', async function () { |
89d241a7 C |
76 | const user = { username: 'captain crochet', password: server.store.user.password } |
77 | const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
0e1dc3e7 | 78 | |
41d1d075 C |
79 | expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT) |
80 | expect(body.error).to.contain('credentials are invalid') | |
81 | expect(body.type.startsWith('https://')).to.be.true | |
82 | expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT) | |
1eddc9a7 | 83 | }) |
0e1dc3e7 | 84 | |
1eddc9a7 | 85 | it('Should not login with an invalid password', async function () { |
89d241a7 C |
86 | const user = { username: server.store.user.username, password: 'mew_three' } |
87 | const body = await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
0e1dc3e7 | 88 | |
41d1d075 C |
89 | expect(body.code).to.equal(OAuth2ErrorCode.INVALID_GRANT) |
90 | expect(body.error).to.contain('credentials are invalid') | |
91 | expect(body.type.startsWith('https://')).to.be.true | |
92 | expect(body.type).to.contain(OAuth2ErrorCode.INVALID_GRANT) | |
1eddc9a7 | 93 | }) |
0e1dc3e7 | 94 | |
1eddc9a7 | 95 | it('Should not be able to upload a video', async function () { |
d23dd9fb | 96 | token = 'my_super_token' |
0e1dc3e7 | 97 | |
89d241a7 | 98 | await server.videos.upload({ token, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
1eddc9a7 | 99 | }) |
0e1dc3e7 | 100 | |
1eddc9a7 | 101 | it('Should not be able to follow', async function () { |
d23dd9fb | 102 | token = 'my_super_token' |
c3d29f69 | 103 | |
89d241a7 | 104 | await server.follows.follow({ |
4d029ef8 | 105 | hosts: [ 'http://example.com' ], |
d23dd9fb | 106 | token, |
c3d29f69 C |
107 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
108 | }) | |
1eddc9a7 | 109 | }) |
0e1dc3e7 | 110 | |
1eddc9a7 | 111 | it('Should not be able to unfollow') |
0e1dc3e7 | 112 | |
1eddc9a7 | 113 | it('Should be able to login', async function () { |
89d241a7 | 114 | const body = await server.login.login({ expectedStatus: HttpStatusCode.OK_200 }) |
0e1dc3e7 | 115 | |
d23dd9fb | 116 | token = body.access_token |
1eddc9a7 | 117 | }) |
50b4dcce NB |
118 | |
119 | it('Should be able to login with an insensitive username', async function () { | |
89d241a7 C |
120 | const user = { username: 'RoOt', password: server.store.user.password } |
121 | await server.login.login({ user, expectedStatus: HttpStatusCode.OK_200 }) | |
50b4dcce | 122 | |
89d241a7 C |
123 | const user2 = { username: 'rOoT', password: server.store.user.password } |
124 | await server.login.login({ user: user2, expectedStatus: HttpStatusCode.OK_200 }) | |
50b4dcce | 125 | |
89d241a7 C |
126 | const user3 = { username: 'ROOt', password: server.store.user.password } |
127 | await server.login.login({ user: user3, expectedStatus: HttpStatusCode.OK_200 }) | |
50b4dcce | 128 | }) |
0e1dc3e7 C |
129 | }) |
130 | ||
1eddc9a7 | 131 | describe('Logout', function () { |
7fed6375 | 132 | it('Should logout (revoke token)', async function () { |
89d241a7 | 133 | await server.login.logout({ token: server.accessToken }) |
7fed6375 | 134 | }) |
0e1dc3e7 | 135 | |
7fed6375 | 136 | it('Should not be able to get the user information', async function () { |
89d241a7 | 137 | await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
7fed6375 | 138 | }) |
0e1dc3e7 | 139 | |
7fed6375 | 140 | it('Should not be able to upload a video', async function () { |
89d241a7 | 141 | await server.videos.upload({ attributes: { name: 'video' }, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
7fed6375 | 142 | }) |
0e1dc3e7 | 143 | |
1eddc9a7 C |
144 | it('Should not be able to rate a video', async function () { |
145 | const path = '/api/v1/videos/' | |
146 | const data = { | |
147 | rating: 'likes' | |
148 | } | |
0e1dc3e7 | 149 | |
1eddc9a7 C |
150 | const options = { |
151 | url: server.url, | |
152 | path: path + videoId, | |
153 | token: 'wrong token', | |
154 | fields: data, | |
c0e8b12e | 155 | expectedStatus: HttpStatusCode.UNAUTHORIZED_401 |
1eddc9a7 C |
156 | } |
157 | await makePutBodyRequest(options) | |
158 | }) | |
0e1dc3e7 | 159 | |
e1c55031 | 160 | it('Should be able to login again', async function () { |
89d241a7 | 161 | const body = await server.login.login() |
41d1d075 C |
162 | server.accessToken = body.access_token |
163 | server.refreshToken = body.refresh_token | |
f43db2f4 C |
164 | }) |
165 | ||
166 | it('Should be able to get my user information again', async function () { | |
89d241a7 | 167 | await server.users.getMyInfo() |
f43db2f4 C |
168 | }) |
169 | ||
170 | it('Should have an expired access token', async function () { | |
81d02aac | 171 | this.timeout(60000) |
f43db2f4 | 172 | |
89d241a7 C |
173 | await server.sql.setTokenField(server.accessToken, 'accessTokenExpiresAt', new Date().toISOString()) |
174 | await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', new Date().toISOString()) | |
f43db2f4 | 175 | |
9293139f | 176 | await killallServers([ server ]) |
254d3579 | 177 | await server.run() |
f43db2f4 | 178 | |
89d241a7 | 179 | await server.users.getMyInfo({ expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
f43db2f4 C |
180 | }) |
181 | ||
182 | it('Should not be able to refresh an access token with an expired refresh token', async function () { | |
89d241a7 | 183 | await server.login.refreshToken({ refreshToken: server.refreshToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
e1c55031 | 184 | }) |
0e1dc3e7 | 185 | |
f43db2f4 C |
186 | it('Should refresh the token', async function () { |
187 | this.timeout(15000) | |
188 | ||
189 | const futureDate = new Date(new Date().getTime() + 1000 * 60).toISOString() | |
89d241a7 | 190 | await server.sql.setTokenField(server.accessToken, 'refreshTokenExpiresAt', futureDate) |
0e1dc3e7 | 191 | |
9293139f | 192 | await killallServers([ server ]) |
254d3579 | 193 | await server.run() |
f43db2f4 | 194 | |
89d241a7 | 195 | const res = await server.login.refreshToken({ refreshToken: server.refreshToken }) |
f43db2f4 C |
196 | server.accessToken = res.body.access_token |
197 | server.refreshToken = res.body.refresh_token | |
198 | }) | |
1eddc9a7 | 199 | |
e1c55031 | 200 | it('Should be able to get my user information again', async function () { |
89d241a7 | 201 | await server.users.getMyInfo() |
e1c55031 | 202 | }) |
0e1dc3e7 C |
203 | }) |
204 | ||
1eddc9a7 | 205 | describe('Creating a user', function () { |
ce5496d6 | 206 | |
1eddc9a7 | 207 | it('Should be able to create a new user', async function () { |
89d241a7 | 208 | await server.users.create({ ...user, videoQuota: 2 * 1024 * 1024, adminFlags: UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST }) |
1eddc9a7 | 209 | }) |
a76138ff | 210 | |
1eddc9a7 | 211 | it('Should be able to login with this user', async function () { |
89d241a7 | 212 | userToken = await server.login.getAccessToken(user) |
1eddc9a7 | 213 | }) |
a76138ff | 214 | |
1eddc9a7 | 215 | it('Should be able to get user information', async function () { |
89d241a7 | 216 | const userMe = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 | 217 | |
89d241a7 | 218 | const userGet = await server.users.get({ userId: userMe.id, withStats: true }) |
1eddc9a7 C |
219 | |
220 | for (const user of [ userMe, userGet ]) { | |
221 | expect(user.username).to.equal('user_1') | |
222 | expect(user.email).to.equal('user_1@example.com') | |
223 | expect(user.nsfwPolicy).to.equal('display') | |
224 | expect(user.videoQuota).to.equal(2 * 1024 * 1024) | |
225 | expect(user.roleLabel).to.equal('User') | |
226 | expect(user.id).to.be.a('number') | |
227 | expect(user.account.displayName).to.equal('user_1') | |
228 | expect(user.account.description).to.be.null | |
229 | } | |
230 | ||
4e1592da | 231 | expect(userMe.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST) |
3487330d | 232 | expect(userGet.adminFlags).to.equal(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST) |
29128b2f RK |
233 | |
234 | expect(userMe.specialPlaylists).to.have.lengthOf(1) | |
ac0868bc | 235 | expect(userMe.specialPlaylists[0].type).to.equal(VideoPlaylistType.WATCH_LATER) |
76314386 RK |
236 | |
237 | // Check stats are included with withStats | |
238 | expect(userGet.videosCount).to.be.a('number') | |
239 | expect(userGet.videosCount).to.equal(0) | |
240 | expect(userGet.videoCommentsCount).to.be.a('number') | |
241 | expect(userGet.videoCommentsCount).to.equal(0) | |
4f32032f C |
242 | expect(userGet.abusesCount).to.be.a('number') |
243 | expect(userGet.abusesCount).to.equal(0) | |
244 | expect(userGet.abusesAcceptedCount).to.be.a('number') | |
245 | expect(userGet.abusesAcceptedCount).to.equal(0) | |
1eddc9a7 | 246 | }) |
ce5496d6 C |
247 | }) |
248 | ||
1eddc9a7 | 249 | describe('Users listing', function () { |
0e1dc3e7 | 250 | |
1eddc9a7 | 251 | it('Should list all the users', async function () { |
89d241a7 | 252 | const { data, total } = await server.users.list() |
afffe988 | 253 | |
1eddc9a7 | 254 | expect(total).to.equal(2) |
7926c5f9 C |
255 | expect(data).to.be.an('array') |
256 | expect(data.length).to.equal(2) | |
0e1dc3e7 | 257 | |
7926c5f9 | 258 | const user = data[0] |
1eddc9a7 C |
259 | expect(user.username).to.equal('user_1') |
260 | expect(user.email).to.equal('user_1@example.com') | |
261 | expect(user.nsfwPolicy).to.equal('display') | |
0e1dc3e7 | 262 | |
7926c5f9 | 263 | const rootUser = data[1] |
1eddc9a7 | 264 | expect(rootUser.username).to.equal('root') |
48f07b4a | 265 | expect(rootUser.email).to.equal('admin' + server.internalServerNumber + '@example.com') |
1eddc9a7 | 266 | expect(user.nsfwPolicy).to.equal('display') |
afffe988 | 267 | |
3cc665f4 C |
268 | expect(rootUser.lastLoginDate).to.exist |
269 | expect(user.lastLoginDate).to.exist | |
270 | ||
1eddc9a7 C |
271 | userId = user.id |
272 | }) | |
0e1dc3e7 | 273 | |
1eddc9a7 | 274 | it('Should list only the first user by username asc', async function () { |
89d241a7 | 275 | const { total, data } = await server.users.list({ start: 0, count: 1, sort: 'username' }) |
a7ba16b6 | 276 | |
1eddc9a7 | 277 | expect(total).to.equal(2) |
7926c5f9 | 278 | expect(data.length).to.equal(1) |
afffe988 | 279 | |
7926c5f9 | 280 | const user = data[0] |
1eddc9a7 | 281 | expect(user.username).to.equal('root') |
48f07b4a | 282 | expect(user.email).to.equal('admin' + server.internalServerNumber + '@example.com') |
1eddc9a7 C |
283 | expect(user.roleLabel).to.equal('Administrator') |
284 | expect(user.nsfwPolicy).to.equal('display') | |
285 | }) | |
0e1dc3e7 | 286 | |
1eddc9a7 | 287 | it('Should list only the first user by username desc', async function () { |
89d241a7 | 288 | const { total, data } = await server.users.list({ start: 0, count: 1, sort: '-username' }) |
24b9417c | 289 | |
1eddc9a7 | 290 | expect(total).to.equal(2) |
7926c5f9 | 291 | expect(data.length).to.equal(1) |
24b9417c | 292 | |
7926c5f9 | 293 | const user = data[0] |
1eddc9a7 C |
294 | expect(user.username).to.equal('user_1') |
295 | expect(user.email).to.equal('user_1@example.com') | |
296 | expect(user.nsfwPolicy).to.equal('display') | |
297 | }) | |
24b9417c | 298 | |
1eddc9a7 | 299 | it('Should list only the second user by createdAt desc', async function () { |
89d241a7 | 300 | const { data, total } = await server.users.list({ start: 0, count: 1, sort: '-createdAt' }) |
1eddc9a7 | 301 | expect(total).to.equal(2) |
24b9417c | 302 | |
7926c5f9 C |
303 | expect(data.length).to.equal(1) |
304 | ||
305 | const user = data[0] | |
1eddc9a7 C |
306 | expect(user.username).to.equal('user_1') |
307 | expect(user.email).to.equal('user_1@example.com') | |
308 | expect(user.nsfwPolicy).to.equal('display') | |
309 | }) | |
24b9417c | 310 | |
1eddc9a7 | 311 | it('Should list all the users by createdAt asc', async function () { |
89d241a7 | 312 | const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt' }) |
24b9417c | 313 | |
1eddc9a7 | 314 | expect(total).to.equal(2) |
7926c5f9 | 315 | expect(data.length).to.equal(2) |
24b9417c | 316 | |
7926c5f9 C |
317 | expect(data[0].username).to.equal('root') |
318 | expect(data[0].email).to.equal('admin' + server.internalServerNumber + '@example.com') | |
319 | expect(data[0].nsfwPolicy).to.equal('display') | |
24b9417c | 320 | |
7926c5f9 C |
321 | expect(data[1].username).to.equal('user_1') |
322 | expect(data[1].email).to.equal('user_1@example.com') | |
323 | expect(data[1].nsfwPolicy).to.equal('display') | |
11ba2ab3 | 324 | }) |
0e1dc3e7 | 325 | |
1eddc9a7 | 326 | it('Should search user by username', async function () { |
89d241a7 | 327 | const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'oot' }) |
7926c5f9 C |
328 | expect(total).to.equal(1) |
329 | expect(data.length).to.equal(1) | |
330 | expect(data[0].username).to.equal('root') | |
11ba2ab3 | 331 | }) |
0e1dc3e7 | 332 | |
1eddc9a7 C |
333 | it('Should search user by email', async function () { |
334 | { | |
89d241a7 | 335 | const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'r_1@exam' }) |
7926c5f9 C |
336 | expect(total).to.equal(1) |
337 | expect(data.length).to.equal(1) | |
338 | expect(data[0].username).to.equal('user_1') | |
339 | expect(data[0].email).to.equal('user_1@example.com') | |
1eddc9a7 | 340 | } |
7efe153b | 341 | |
1eddc9a7 | 342 | { |
89d241a7 | 343 | const { total, data } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', search: 'example' }) |
7926c5f9 C |
344 | expect(total).to.equal(2) |
345 | expect(data.length).to.equal(2) | |
346 | expect(data[0].username).to.equal('root') | |
347 | expect(data[1].username).to.equal('user_1') | |
1eddc9a7 | 348 | } |
11ba2ab3 | 349 | }) |
5c98d3bf C |
350 | }) |
351 | ||
1eddc9a7 | 352 | describe('Update my account', function () { |
41d1d075 | 353 | |
1eddc9a7 | 354 | it('Should update my password', async function () { |
89d241a7 | 355 | await server.users.updateMe({ |
d23dd9fb | 356 | token: userToken, |
1eddc9a7 | 357 | currentPassword: 'super password', |
43d0ea7f | 358 | password: 'new password' |
1eddc9a7 C |
359 | }) |
360 | user.password = 'new password' | |
c5911fd3 | 361 | |
89d241a7 | 362 | await server.login.login({ user }) |
c5911fd3 C |
363 | }) |
364 | ||
1eddc9a7 | 365 | it('Should be able to change the NSFW display attribute', async function () { |
89d241a7 | 366 | await server.users.updateMe({ |
d23dd9fb | 367 | token: userToken, |
1eddc9a7 C |
368 | nsfwPolicy: 'do_not_list' |
369 | }) | |
370 | ||
89d241a7 | 371 | const user = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 C |
372 | expect(user.username).to.equal('user_1') |
373 | expect(user.email).to.equal('user_1@example.com') | |
374 | expect(user.nsfwPolicy).to.equal('do_not_list') | |
375 | expect(user.videoQuota).to.equal(2 * 1024 * 1024) | |
376 | expect(user.id).to.be.a('number') | |
377 | expect(user.account.displayName).to.equal('user_1') | |
378 | expect(user.account.description).to.be.null | |
379 | }) | |
c5911fd3 | 380 | |
1eddc9a7 | 381 | it('Should be able to change the autoPlayVideo attribute', async function () { |
89d241a7 | 382 | await server.users.updateMe({ |
d23dd9fb | 383 | token: userToken, |
1eddc9a7 C |
384 | autoPlayVideo: false |
385 | }) | |
c5911fd3 | 386 | |
89d241a7 | 387 | const user = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 | 388 | expect(user.autoPlayVideo).to.be.false |
ed56ad11 C |
389 | }) |
390 | ||
6aa54148 | 391 | it('Should be able to change the autoPlayNextVideo attribute', async function () { |
89d241a7 | 392 | await server.users.updateMe({ |
d23dd9fb | 393 | token: userToken, |
6aa54148 L |
394 | autoPlayNextVideo: true |
395 | }) | |
396 | ||
89d241a7 | 397 | const user = await server.users.getMyInfo({ token: userToken }) |
6aa54148 L |
398 | expect(user.autoPlayNextVideo).to.be.true |
399 | }) | |
400 | ||
a9bfa85d C |
401 | it('Should be able to change the p2p attribute', async function () { |
402 | { | |
403 | await server.users.updateMe({ | |
404 | token: userToken, | |
405 | webTorrentEnabled: false | |
406 | }) | |
407 | ||
408 | const user = await server.users.getMyInfo({ token: userToken }) | |
409 | expect(user.p2pEnabled).to.be.false | |
410 | } | |
411 | ||
412 | { | |
413 | await server.users.updateMe({ | |
414 | token: userToken, | |
415 | p2pEnabled: true | |
416 | }) | |
417 | ||
418 | const user = await server.users.getMyInfo({ token: userToken }) | |
419 | expect(user.p2pEnabled).to.be.true | |
420 | } | |
421 | }) | |
422 | ||
675a8fc7 | 423 | it('Should be able to change the email attribute', async function () { |
89d241a7 | 424 | await server.users.updateMe({ |
d23dd9fb | 425 | token: userToken, |
675a8fc7 | 426 | currentPassword: 'new password', |
1eddc9a7 C |
427 | email: 'updated@example.com' |
428 | }) | |
429 | ||
89d241a7 | 430 | const user = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 C |
431 | expect(user.username).to.equal('user_1') |
432 | expect(user.email).to.equal('updated@example.com') | |
433 | expect(user.nsfwPolicy).to.equal('do_not_list') | |
434 | expect(user.videoQuota).to.equal(2 * 1024 * 1024) | |
435 | expect(user.id).to.be.a('number') | |
436 | expect(user.account.displayName).to.equal('user_1') | |
437 | expect(user.account.description).to.be.null | |
438 | }) | |
ed56ad11 | 439 | |
f619de0e C |
440 | it('Should be able to update my avatar with a gif', async function () { |
441 | const fixture = 'avatar.gif' | |
ed56ad11 | 442 | |
89d241a7 | 443 | await server.users.updateMyAvatar({ token: userToken, fixture }) |
2422c46b | 444 | |
89d241a7 | 445 | const user = await server.users.getMyInfo({ token: userToken }) |
d0800f76 | 446 | for (const avatar of user.account.avatars) { |
447 | await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.gif') | |
448 | } | |
f619de0e C |
449 | }) |
450 | ||
451 | it('Should be able to update my avatar with a gif, and then a png', async function () { | |
452 | for (const extension of [ '.png', '.gif' ]) { | |
453 | const fixture = 'avatar' + extension | |
454 | ||
89d241a7 | 455 | await server.users.updateMyAvatar({ token: userToken, fixture }) |
f619de0e | 456 | |
89d241a7 | 457 | const user = await server.users.getMyInfo({ token: userToken }) |
d0800f76 | 458 | for (const avatar of user.account.avatars) { |
459 | await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, extension) | |
460 | } | |
f619de0e | 461 | } |
1eddc9a7 C |
462 | }) |
463 | ||
464 | it('Should be able to update my display name', async function () { | |
89d241a7 | 465 | await server.users.updateMe({ token: userToken, displayName: 'new display name' }) |
1eddc9a7 | 466 | |
89d241a7 | 467 | const user = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 C |
468 | expect(user.username).to.equal('user_1') |
469 | expect(user.email).to.equal('updated@example.com') | |
470 | expect(user.nsfwPolicy).to.equal('do_not_list') | |
471 | expect(user.videoQuota).to.equal(2 * 1024 * 1024) | |
472 | expect(user.id).to.be.a('number') | |
473 | expect(user.account.displayName).to.equal('new display name') | |
474 | expect(user.account.description).to.be.null | |
475 | }) | |
2422c46b | 476 | |
1eddc9a7 | 477 | it('Should be able to update my description', async function () { |
89d241a7 | 478 | await server.users.updateMe({ token: userToken, description: 'my super description updated' }) |
1eddc9a7 | 479 | |
89d241a7 | 480 | const user = await server.users.getMyInfo({ token: userToken }) |
1eddc9a7 C |
481 | expect(user.username).to.equal('user_1') |
482 | expect(user.email).to.equal('updated@example.com') | |
483 | expect(user.nsfwPolicy).to.equal('do_not_list') | |
484 | expect(user.videoQuota).to.equal(2 * 1024 * 1024) | |
485 | expect(user.id).to.be.a('number') | |
486 | expect(user.account.displayName).to.equal('new display name') | |
487 | expect(user.account.description).to.equal('my super description updated') | |
43d0ea7f C |
488 | expect(user.noWelcomeModal).to.be.false |
489 | expect(user.noInstanceConfigWarningModal).to.be.false | |
8f581725 | 490 | expect(user.noAccountSetupWarningModal).to.be.false |
1eddc9a7 | 491 | }) |
9b474844 C |
492 | |
493 | it('Should be able to update my theme', async function () { | |
494 | for (const theme of [ 'background-red', 'default', 'instance-default' ]) { | |
89d241a7 | 495 | await server.users.updateMe({ token: userToken, theme }) |
9b474844 | 496 | |
89d241a7 | 497 | const user = await server.users.getMyInfo({ token: userToken }) |
7926c5f9 | 498 | expect(user.theme).to.equal(theme) |
9b474844 C |
499 | } |
500 | }) | |
43d0ea7f C |
501 | |
502 | it('Should be able to update my modal preferences', async function () { | |
89d241a7 | 503 | await server.users.updateMe({ |
d23dd9fb | 504 | token: userToken, |
43d0ea7f | 505 | noInstanceConfigWarningModal: true, |
8f581725 C |
506 | noWelcomeModal: true, |
507 | noAccountSetupWarningModal: true | |
43d0ea7f C |
508 | }) |
509 | ||
89d241a7 | 510 | const user = await server.users.getMyInfo({ token: userToken }) |
43d0ea7f C |
511 | expect(user.noWelcomeModal).to.be.true |
512 | expect(user.noInstanceConfigWarningModal).to.be.true | |
8f581725 | 513 | expect(user.noAccountSetupWarningModal).to.be.true |
43d0ea7f | 514 | }) |
0e1dc3e7 C |
515 | }) |
516 | ||
1eddc9a7 | 517 | describe('Updating another user', function () { |
1eddc9a7 | 518 | it('Should be able to update another user', async function () { |
89d241a7 | 519 | await server.users.update({ |
1eddc9a7 | 520 | userId, |
d23dd9fb | 521 | token, |
1eddc9a7 C |
522 | email: 'updated2@example.com', |
523 | emailVerified: true, | |
524 | videoQuota: 42, | |
525 | role: UserRole.MODERATOR, | |
6d989edc C |
526 | adminFlags: UserAdminFlag.NONE, |
527 | pluginAuth: 'toto' | |
1eddc9a7 C |
528 | }) |
529 | ||
89d241a7 | 530 | const user = await server.users.get({ token, userId }) |
1eddc9a7 C |
531 | |
532 | expect(user.username).to.equal('user_1') | |
533 | expect(user.email).to.equal('updated2@example.com') | |
534 | expect(user.emailVerified).to.be.true | |
535 | expect(user.nsfwPolicy).to.equal('do_not_list') | |
536 | expect(user.videoQuota).to.equal(42) | |
537 | expect(user.roleLabel).to.equal('Moderator') | |
538 | expect(user.id).to.be.a('number') | |
539 | expect(user.adminFlags).to.equal(UserAdminFlag.NONE) | |
6d989edc C |
540 | expect(user.pluginAuth).to.equal('toto') |
541 | }) | |
542 | ||
543 | it('Should reset the auth plugin', async function () { | |
89d241a7 | 544 | await server.users.update({ userId, token, pluginAuth: null }) |
6d989edc | 545 | |
89d241a7 | 546 | const user = await server.users.get({ token, userId }) |
6d989edc | 547 | expect(user.pluginAuth).to.be.null |
1eddc9a7 | 548 | }) |
f8b8c36b | 549 | |
1eddc9a7 | 550 | it('Should have removed the user token', async function () { |
89d241a7 | 551 | await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
f8b8c36b | 552 | |
89d241a7 | 553 | userToken = await server.login.getAccessToken(user) |
b426edd4 C |
554 | }) |
555 | ||
1eddc9a7 | 556 | it('Should be able to update another user password', async function () { |
89d241a7 | 557 | await server.users.update({ userId, token, password: 'password updated' }) |
b426edd4 | 558 | |
89d241a7 | 559 | await server.users.getMyQuotaUsed({ token: userToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
b426edd4 | 560 | |
89d241a7 | 561 | await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
b426edd4 | 562 | |
1eddc9a7 | 563 | user.password = 'password updated' |
89d241a7 | 564 | userToken = await server.login.getAccessToken(user) |
1eddc9a7 | 565 | }) |
757f0da3 C |
566 | }) |
567 | ||
1eddc9a7 | 568 | describe('Video blacklists', function () { |
58c44687 C |
569 | |
570 | it('Should be able to list my video blacklist', async function () { | |
89d241a7 | 571 | await server.blacklist.list({ token: userToken }) |
1eddc9a7 | 572 | }) |
0e1dc3e7 C |
573 | }) |
574 | ||
1eddc9a7 | 575 | describe('Remove a user', function () { |
58c44687 C |
576 | |
577 | before(async function () { | |
578 | await server.users.update({ | |
579 | userId, | |
580 | token, | |
581 | videoQuota: 2 * 1024 * 1024 | |
582 | }) | |
583 | ||
584 | await server.videos.quickUpload({ name: 'user video', token: userToken, fixture: 'video_short.webm' }) | |
585 | await server.videos.quickUpload({ name: 'root video' }) | |
586 | ||
587 | const { total } = await server.videos.list() | |
588 | expect(total).to.equal(2) | |
589 | }) | |
590 | ||
1eddc9a7 | 591 | it('Should be able to remove this user', async function () { |
89d241a7 | 592 | await server.users.remove({ userId, token }) |
1eddc9a7 | 593 | }) |
0e1dc3e7 | 594 | |
1eddc9a7 | 595 | it('Should not be able to login with this user', async function () { |
89d241a7 | 596 | await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
1eddc9a7 | 597 | }) |
0e1dc3e7 | 598 | |
1eddc9a7 | 599 | it('Should not have videos of this user', async function () { |
89d241a7 | 600 | const { data, total } = await server.videos.list() |
d23dd9fb | 601 | expect(total).to.equal(1) |
0e1dc3e7 | 602 | |
d23dd9fb | 603 | const video = data[0] |
1eddc9a7 C |
604 | expect(video.account.name).to.equal('root') |
605 | }) | |
0e1dc3e7 C |
606 | }) |
607 | ||
1eddc9a7 | 608 | describe('Registering a new user', function () { |
58c44687 | 609 | let user15AccessToken: string |
76314386 | 610 | |
1eddc9a7 | 611 | it('Should register a new user', async function () { |
1f20622f | 612 | const user = { displayName: 'super user 15', username: 'user_15', password: 'my super password' } |
e590b4a5 C |
613 | const channel = { name: 'my_user_15_channel', displayName: 'my channel rocks' } |
614 | ||
89d241a7 | 615 | await server.users.register({ ...user, channel }) |
1eddc9a7 | 616 | }) |
0e1dc3e7 | 617 | |
1eddc9a7 C |
618 | it('Should be able to login with this registered user', async function () { |
619 | const user15 = { | |
620 | username: 'user_15', | |
621 | password: 'my super password' | |
622 | } | |
5c98d3bf | 623 | |
89d241a7 | 624 | user15AccessToken = await server.login.getAccessToken(user15) |
1eddc9a7 | 625 | }) |
5c98d3bf | 626 | |
1f20622f | 627 | it('Should have the correct display name', async function () { |
89d241a7 | 628 | const user = await server.users.getMyInfo({ token: user15AccessToken }) |
1f20622f C |
629 | expect(user.account.displayName).to.equal('super user 15') |
630 | }) | |
631 | ||
1eddc9a7 | 632 | it('Should have the correct video quota', async function () { |
89d241a7 | 633 | const user = await server.users.getMyInfo({ token: user15AccessToken }) |
1eddc9a7 C |
634 | expect(user.videoQuota).to.equal(5 * 1024 * 1024) |
635 | }) | |
92b9d60c | 636 | |
e590b4a5 | 637 | it('Should have created the channel', async function () { |
89d241a7 | 638 | const { displayName } = await server.channels.get({ channelName: 'my_user_15_channel' }) |
e590b4a5 | 639 | |
a5461888 | 640 | expect(displayName).to.equal('my channel rocks') |
e590b4a5 C |
641 | }) |
642 | ||
1eddc9a7 C |
643 | it('Should remove me', async function () { |
644 | { | |
89d241a7 | 645 | const { data } = await server.users.list() |
7926c5f9 | 646 | expect(data.find(u => u.username === 'user_15')).to.not.be.undefined |
1eddc9a7 | 647 | } |
92b9d60c | 648 | |
89d241a7 | 649 | await server.users.deleteMe({ token: user15AccessToken }) |
1eddc9a7 C |
650 | |
651 | { | |
89d241a7 | 652 | const { data } = await server.users.list() |
7926c5f9 | 653 | expect(data.find(u => u.username === 'user_15')).to.be.undefined |
1eddc9a7 C |
654 | } |
655 | }) | |
92b9d60c C |
656 | }) |
657 | ||
1eddc9a7 | 658 | describe('User blocking', function () { |
76314386 RK |
659 | let user16Id |
660 | let user16AccessToken | |
8491293b RK |
661 | const user16 = { |
662 | username: 'user_16', | |
663 | password: 'my super password' | |
664 | } | |
76314386 | 665 | |
8491293b | 666 | it('Should block a user', async function () { |
89d241a7 | 667 | const user = await server.users.create({ ...user16 }) |
7926c5f9 | 668 | user16Id = user.id |
e6921918 | 669 | |
89d241a7 | 670 | user16AccessToken = await server.login.getAccessToken(user16) |
e6921918 | 671 | |
89d241a7 C |
672 | await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 }) |
673 | await server.users.banUser({ userId: user16Id }) | |
e6921918 | 674 | |
89d241a7 C |
675 | await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) |
676 | await server.login.login({ user: user16, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
8491293b RK |
677 | }) |
678 | ||
679 | it('Should search user by banned status', async function () { | |
680 | { | |
89d241a7 | 681 | const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: true }) |
7926c5f9 C |
682 | expect(total).to.equal(1) |
683 | expect(data.length).to.equal(1) | |
8491293b | 684 | |
7926c5f9 | 685 | expect(data[0].username).to.equal(user16.username) |
8491293b RK |
686 | } |
687 | ||
688 | { | |
89d241a7 | 689 | const { data, total } = await server.users.list({ start: 0, count: 2, sort: 'createdAt', blocked: false }) |
7926c5f9 C |
690 | expect(total).to.equal(1) |
691 | expect(data.length).to.equal(1) | |
8491293b | 692 | |
7926c5f9 | 693 | expect(data[0].username).to.not.equal(user16.username) |
8491293b RK |
694 | } |
695 | }) | |
e6921918 | 696 | |
8491293b | 697 | it('Should unblock a user', async function () { |
89d241a7 C |
698 | await server.users.unbanUser({ userId: user16Id }) |
699 | user16AccessToken = await server.login.getAccessToken(user16) | |
700 | await server.users.getMyInfo({ token: user16AccessToken, expectedStatus: HttpStatusCode.OK_200 }) | |
76314386 RK |
701 | }) |
702 | }) | |
703 | ||
704 | describe('User stats', function () { | |
58c44687 C |
705 | let user17Id: number |
706 | let user17AccessToken: string | |
76314386 RK |
707 | |
708 | it('Should report correct initial statistics about a user', async function () { | |
709 | const user17 = { | |
710 | username: 'user_17', | |
711 | password: 'my super password' | |
712 | } | |
89d241a7 | 713 | const created = await server.users.create({ ...user17 }) |
76314386 | 714 | |
7926c5f9 | 715 | user17Id = created.id |
89d241a7 | 716 | user17AccessToken = await server.login.getAccessToken(user17) |
76314386 | 717 | |
89d241a7 | 718 | const user = await server.users.get({ userId: user17Id, withStats: true }) |
76314386 RK |
719 | expect(user.videosCount).to.equal(0) |
720 | expect(user.videoCommentsCount).to.equal(0) | |
4f32032f C |
721 | expect(user.abusesCount).to.equal(0) |
722 | expect(user.abusesCreatedCount).to.equal(0) | |
723 | expect(user.abusesAcceptedCount).to.equal(0) | |
76314386 RK |
724 | }) |
725 | ||
726 | it('Should report correct videos count', async function () { | |
d23dd9fb | 727 | const attributes = { name: 'video to test user stats' } |
89d241a7 | 728 | await server.videos.upload({ token: user17AccessToken, attributes }) |
d23dd9fb | 729 | |
89d241a7 | 730 | const { data } = await server.videos.list() |
d23dd9fb | 731 | videoId = data.find(video => video.name === attributes.name).id |
76314386 | 732 | |
89d241a7 | 733 | const user = await server.users.get({ userId: user17Id, withStats: true }) |
76314386 RK |
734 | expect(user.videosCount).to.equal(1) |
735 | }) | |
736 | ||
737 | it('Should report correct video comments for user', async function () { | |
738 | const text = 'super comment' | |
89d241a7 | 739 | await server.comments.createThread({ token: user17AccessToken, videoId, text }) |
76314386 | 740 | |
89d241a7 | 741 | const user = await server.users.get({ userId: user17Id, withStats: true }) |
76314386 RK |
742 | expect(user.videoCommentsCount).to.equal(1) |
743 | }) | |
744 | ||
4f32032f | 745 | it('Should report correct abuses counts', async function () { |
76314386 | 746 | const reason = 'my super bad reason' |
89d241a7 | 747 | await server.abuses.report({ token: user17AccessToken, videoId, reason }) |
76314386 | 748 | |
89d241a7 | 749 | const body1 = await server.abuses.getAdminList() |
0c1a77e9 | 750 | const abuseId = body1.data[0].id |
76314386 | 751 | |
89d241a7 | 752 | const user2 = await server.users.get({ userId: user17Id, withStats: true }) |
4f32032f C |
753 | expect(user2.abusesCount).to.equal(1) // number of incriminations |
754 | expect(user2.abusesCreatedCount).to.equal(1) // number of reports created | |
76314386 | 755 | |
89d241a7 | 756 | await server.abuses.update({ abuseId, body: { state: AbuseState.ACCEPTED } }) |
76314386 | 757 | |
89d241a7 | 758 | const user3 = await server.users.get({ userId: user17Id, withStats: true }) |
4f32032f | 759 | expect(user3.abusesAcceptedCount).to.equal(1) // number of reports created accepted |
1eddc9a7 | 760 | }) |
e6921918 C |
761 | }) |
762 | ||
7c3b7976 C |
763 | after(async function () { |
764 | await cleanupTests([ server ]) | |
0e1dc3e7 C |
765 | }) |
766 | }) |