aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/users.ts53
-rw-r--r--server/tests/utils/users.ts44
2 files changed, 82 insertions, 15 deletions
diff --git a/server/tests/api/users.ts b/server/tests/api/users.ts
index fd3b51123..104d783bb 100644
--- a/server/tests/api/users.ts
+++ b/server/tests/api/users.ts
@@ -19,14 +19,16 @@ import {
19 makePutBodyRequest, 19 makePutBodyRequest,
20 createUser, 20 createUser,
21 loginAndGetAccessToken, 21 loginAndGetAccessToken,
22 getUserInformation, 22 getMyUserInformation,
23 getUsersList, 23 getUsersList,
24 getUsersListPaginationAndSort, 24 getUsersListPaginationAndSort,
25 updateUser, 25 updateUser,
26 updateMyUser,
26 registerUser, 27 registerUser,
27 removeUser 28 removeUser
28} from '../utils' 29} from '../utils'
29import { killallServers } from '../utils/servers' 30import { killallServers } from '../utils/servers'
31import { getUserInformation } from '../utils/users'
30 32
31describe('Test users', function () { 33describe('Test users', function () {
32 let server: ServerInfo 34 let server: ServerInfo
@@ -166,7 +168,7 @@ describe('Test users', function () {
166 it('Should be able to upload a video again') 168 it('Should be able to upload a video again')
167 169
168 it('Should be able to create a new user', async function () { 170 it('Should be able to create a new user', async function () {
169 await createUser(server.url, accessToken, 'user_1', 'super password') 171 await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024)
170 }) 172 })
171 173
172 it('Should be able to login with this user', async function () { 174 it('Should be able to login with this user', async function () {
@@ -179,12 +181,13 @@ describe('Test users', function () {
179 }) 181 })
180 182
181 it('Should be able to get the user information', async function () { 183 it('Should be able to get the user information', async function () {
182 const res = await getUserInformation(server.url, accessTokenUser) 184 const res = await getMyUserInformation(server.url, accessTokenUser)
183 const user = res.body 185 const user = res.body
184 186
185 expect(user.username).to.equal('user_1') 187 expect(user.username).to.equal('user_1')
186 expect(user.email).to.equal('user_1@example.com') 188 expect(user.email).to.equal('user_1@example.com')
187 expect(user.displayNSFW).to.be.false 189 expect(user.displayNSFW).to.be.false
190 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
188 expect(user.id).to.be.a('number') 191 expect(user.id).to.be.a('number')
189 }) 192 })
190 193
@@ -282,22 +285,49 @@ describe('Test users', function () {
282 expect(users[1].displayNSFW).to.be.false 285 expect(users[1].displayNSFW).to.be.false
283 }) 286 })
284 287
285 it('Should update the user password', async function () { 288 it('Should update my password', async function () {
286 await updateUser(server.url, userId, accessTokenUser, 'new password', null) 289 await updateMyUser(server.url, accessTokenUser, 'new password')
287 server.user.password = 'new password' 290 server.user.password = 'new password'
288 291
289 await login(server.url, server.client, server.user, 200) 292 await login(server.url, server.client, server.user, 200)
290 }) 293 })
291 294
292 it('Should be able to change the NSFW display attribute', async function () { 295 it('Should be able to change the NSFW display attribute', async function () {
293 await updateUser(server.url, userId, accessTokenUser, null, true) 296 await updateMyUser(server.url, accessTokenUser, undefined, true)
294 297
295 const res = await getUserInformation(server.url, accessTokenUser) 298 const res = await getMyUserInformation(server.url, accessTokenUser)
296 const user = res.body 299 const user = res.body
297 300
298 expect(user.username).to.equal('user_1') 301 expect(user.username).to.equal('user_1')
299 expect(user.email).to.equal('user_1@example.com') 302 expect(user.email).to.equal('user_1@example.com')
300 expect(user.displayNSFW).to.be.ok 303 expect(user.displayNSFW).to.be.ok
304 expect(user.videoQuota).to.equal(2 * 1024 * 1024)
305 expect(user.id).to.be.a('number')
306 })
307
308 it('Should be able to change the email display attribute', async function () {
309 await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com')
310
311 const res = await getMyUserInformation(server.url, accessTokenUser)
312 const user = res.body
313
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')
319 })
320
321 it('Should be able to update another user', async function () {
322 await updateUser(server.url, userId, server.accessToken, 'updated2@example.com', 42 )
323
324 const res = await getUserInformation(server.url, server.accessToken, userId)
325 const user = res.body
326
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)
301 expect(user.id).to.be.a('number') 331 expect(user.id).to.be.a('number')
302 }) 332 })
303 333
@@ -329,7 +359,14 @@ describe('Test users', function () {
329 password: 'my super password' 359 password: 'my super password'
330 } 360 }
331 361
332 await loginAndGetAccessToken(server) 362 accessToken = await loginAndGetAccessToken(server)
363 })
364
365 it('Should have the correct video quota', async function () {
366 const res = await getMyUserInformation(server.url, accessToken)
367 const user = res.body
368
369 expect(user.videoQuota).to.equal(5 * 1024 * 1024)
333 }) 370 })
334 371
335 after(async function () { 372 after(async function () {
diff --git a/server/tests/utils/users.ts b/server/tests/utils/users.ts
index 6e4b5f2f5..1c3f6826e 100644
--- a/server/tests/utils/users.ts
+++ b/server/tests/utils/users.ts
@@ -1,11 +1,12 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) { 3function createUser (url: string, accessToken: string, username: string, password: string, videoQuota = 1000000, specialStatus = 204) {
4 const path = '/api/v1/users' 4 const path = '/api/v1/users'
5 const body = { 5 const body = {
6 username, 6 username,
7 password, 7 password,
8 email: username + '@example.com' 8 email: username + '@example.com',
9 videoQuota
9 } 10 }
10 11
11 return request(url) 12 return request(url)
@@ -31,7 +32,7 @@ function registerUser (url: string, username: string, password: string, specialS
31 .expect(specialStatus) 32 .expect(specialStatus)
32} 33}
33 34
34function getUserInformation (url: string, accessToken: string) { 35function getMyUserInformation (url: string, accessToken: string) {
35 const path = '/api/v1/users/me' 36 const path = '/api/v1/users/me'
36 37
37 return request(url) 38 return request(url)
@@ -42,6 +43,17 @@ function getUserInformation (url: string, accessToken: string) {
42 .expect('Content-Type', /json/) 43 .expect('Content-Type', /json/)
43} 44}
44 45
46function getUserInformation (url: string, accessToken: string, userId: number) {
47 const path = '/api/v1/users/' + userId
48
49 return request(url)
50 .get(path)
51 .set('Accept', 'application/json')
52 .set('Authorization', 'Bearer ' + accessToken)
53 .expect(200)
54 .expect('Content-Type', /json/)
55}
56
45function getUserVideoRating (url: string, accessToken: string, videoId: number) { 57function getUserVideoRating (url: string, accessToken: string, videoId: number) {
46 const path = '/api/v1/users/me/videos/' + videoId + '/rating' 58 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
47 59
@@ -86,12 +98,28 @@ function removeUser (url: string, userId: number, accessToken: string, expectedS
86 .expect(expectedStatus) 98 .expect(expectedStatus)
87} 99}
88 100
89function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) { 101function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean, email?: string) {
90 const path = '/api/v1/users/' + userId 102 const path = '/api/v1/users/me'
91 103
92 const toSend = {} 104 const toSend = {}
93 if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword 105 if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword
94 if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW 106 if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW
107 if (email !== undefined && email !== null) toSend['email'] = email
108
109 return request(url)
110 .put(path)
111 .set('Accept', 'application/json')
112 .set('Authorization', 'Bearer ' + accessToken)
113 .send(toSend)
114 .expect(204)
115}
116
117function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number) {
118 const path = '/api/v1/users/' + userId
119
120 const toSend = {}
121 if (email !== undefined && email !== null) toSend['password'] = email
122 if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota
95 123
96 return request(url) 124 return request(url)
97 .put(path) 125 .put(path)
@@ -106,10 +134,12 @@ function updateUser (url: string, userId: number, accessToken: string, newPasswo
106export { 134export {
107 createUser, 135 createUser,
108 registerUser, 136 registerUser,
109 getUserInformation, 137 getMyUserInformation,
110 getUserVideoRating, 138 getUserVideoRating,
111 getUsersList, 139 getUsersList,
112 getUsersListPaginationAndSort, 140 getUsersListPaginationAndSort,
113 removeUser, 141 removeUser,
114 updateUser 142 updateUser,
143 updateMyUser,
144 getUserInformation
115} 145}