diff options
-rw-r--r-- | client/tslint.json | 2 | ||||
-rw-r--r-- | config/test-1.yaml | 3 | ||||
-rw-r--r-- | server/middlewares/validators/videos.ts | 4 | ||||
-rw-r--r-- | server/models/user/user.ts | 7 | ||||
-rw-r--r-- | server/models/video/author.ts | 8 | ||||
-rw-r--r-- | server/tests/api/users.ts | 53 | ||||
-rw-r--r-- | server/tests/utils/users.ts | 44 |
7 files changed, 101 insertions, 20 deletions
diff --git a/client/tslint.json b/client/tslint.json index b1e211ee9..6438519a6 100644 --- a/client/tslint.json +++ b/client/tslint.json | |||
@@ -28,7 +28,7 @@ | |||
28 | "component-class-suffix": true, | 28 | "component-class-suffix": true, |
29 | "directive-class-suffix": true, | 29 | "directive-class-suffix": true, |
30 | "templates-use-public": true, | 30 | "templates-use-public": true, |
31 | "no-access-missing-member": true, | 31 | "no-access-missing-member": false, |
32 | "invoke-injectable": true | 32 | "invoke-injectable": true |
33 | } | 33 | } |
34 | } | 34 | } |
diff --git a/config/test-1.yaml b/config/test-1.yaml index d08a3bee6..f1514e9cc 100644 --- a/config/test-1.yaml +++ b/config/test-1.yaml | |||
@@ -21,5 +21,8 @@ storage: | |||
21 | admin: | 21 | admin: |
22 | email: 'admin1@example.com' | 22 | email: 'admin1@example.com' |
23 | 23 | ||
24 | user: | ||
25 | video_quota: 1024 * 1024 * 5 | ||
26 | |||
24 | signup: | 27 | signup: |
25 | limit: 4 | 28 | limit: 4 |
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 576814fcb..ba8c2d834 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts | |||
@@ -49,8 +49,8 @@ function videosAddValidator (req: express.Request, res: express.Response, next: | |||
49 | next() | 49 | next() |
50 | }) | 50 | }) |
51 | .catch(err => { | 51 | .catch(err => { |
52 | logger.error('Error in getting duration from file.', err) | 52 | logger.error('Error in video add validator', err) |
53 | res.status(400).send('Cannot retrieve metadata of the file.') | 53 | res.sendStatus(500) |
54 | }) | 54 | }) |
55 | }) | 55 | }) |
56 | } | 56 | } |
diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 12a7547f5..9bf13ad24 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts | |||
@@ -244,7 +244,7 @@ loadByUsernameOrEmail = function (username: string, email: string) { | |||
244 | function getOriginalVideoFileTotalFromUser (user: UserInstance) { | 244 | function getOriginalVideoFileTotalFromUser (user: UserInstance) { |
245 | const query = { | 245 | const query = { |
246 | attributes: [ | 246 | attributes: [ |
247 | Sequelize.fn('COUNT', Sequelize.col('VideoFile.size'), 'totalVideoBytes') | 247 | Sequelize.fn('COUNT', Sequelize.col('User.Author.Video.VideoFile.size'), 'totalVideoBytes') |
248 | ], | 248 | ], |
249 | where: { | 249 | where: { |
250 | id: user.id | 250 | id: user.id |
@@ -252,12 +252,15 @@ function getOriginalVideoFileTotalFromUser (user: UserInstance) { | |||
252 | include: [ | 252 | include: [ |
253 | { | 253 | { |
254 | model: User['sequelize'].models.Author, | 254 | model: User['sequelize'].models.Author, |
255 | required: true, | ||
255 | include: [ | 256 | include: [ |
256 | { | 257 | { |
257 | model: User['sequelize'].models.Video, | 258 | model: User['sequelize'].models.Video, |
259 | required: true, | ||
258 | include: [ | 260 | include: [ |
259 | { | 261 | { |
260 | model: User['sequelize'].models.VideoFile | 262 | model: User['sequelize'].models.VideoFile, |
263 | required: true | ||
261 | } | 264 | } |
262 | ] | 265 | ] |
263 | } | 266 | } |
diff --git a/server/models/video/author.ts b/server/models/video/author.ts index c30cc306d..fd0f44f6b 100644 --- a/server/models/video/author.ts +++ b/server/models/video/author.ts | |||
@@ -71,6 +71,14 @@ function associate (models) { | |||
71 | }, | 71 | }, |
72 | onDelete: 'cascade' | 72 | onDelete: 'cascade' |
73 | }) | 73 | }) |
74 | |||
75 | Author.hasMany(models.Video, { | ||
76 | foreignKey: { | ||
77 | name: 'authorId', | ||
78 | allowNull: false | ||
79 | }, | ||
80 | onDelete: 'cascade' | ||
81 | }) | ||
74 | } | 82 | } |
75 | 83 | ||
76 | findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) { | 84 | findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) { |
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' |
29 | import { killallServers } from '../utils/servers' | 30 | import { killallServers } from '../utils/servers' |
31 | import { getUserInformation } from '../utils/users' | ||
30 | 32 | ||
31 | describe('Test users', function () { | 33 | describe('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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | 2 | ||
3 | function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) { | 3 | function 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 | ||
34 | function getUserInformation (url: string, accessToken: string) { | 35 | function 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 | ||
46 | function 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 | |||
45 | function getUserVideoRating (url: string, accessToken: string, videoId: number) { | 57 | function 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 | ||
89 | function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) { | 101 | function 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 | |||
117 | function 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 | |||
106 | export { | 134 | export { |
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 | } |