From 5c98d3bf078852043cbdd582c01e3dc4f4b5b79f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 5 Sep 2017 22:09:16 +0200 Subject: [PATCH] Begin tests for user quota --- client/tslint.json | 2 +- config/test-1.yaml | 3 ++ server/middlewares/validators/videos.ts | 4 +- server/models/user/user.ts | 7 +++- server/models/video/author.ts | 8 ++++ server/tests/api/users.ts | 53 +++++++++++++++++++++---- 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 @@ "component-class-suffix": true, "directive-class-suffix": true, "templates-use-public": true, - "no-access-missing-member": true, + "no-access-missing-member": false, "invoke-injectable": true } } 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: admin: email: 'admin1@example.com' +user: + video_quota: 1024 * 1024 * 5 + signup: 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: next() }) .catch(err => { - logger.error('Error in getting duration from file.', err) - res.status(400).send('Cannot retrieve metadata of the file.') + logger.error('Error in video add validator', err) + res.sendStatus(500) }) }) } 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) { function getOriginalVideoFileTotalFromUser (user: UserInstance) { const query = { attributes: [ - Sequelize.fn('COUNT', Sequelize.col('VideoFile.size'), 'totalVideoBytes') + Sequelize.fn('COUNT', Sequelize.col('User.Author.Video.VideoFile.size'), 'totalVideoBytes') ], where: { id: user.id @@ -252,12 +252,15 @@ function getOriginalVideoFileTotalFromUser (user: UserInstance) { include: [ { model: User['sequelize'].models.Author, + required: true, include: [ { model: User['sequelize'].models.Video, + required: true, include: [ { - model: User['sequelize'].models.VideoFile + model: User['sequelize'].models.VideoFile, + required: true } ] } 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) { }, onDelete: 'cascade' }) + + Author.hasMany(models.Video, { + foreignKey: { + name: 'authorId', + allowNull: false + }, + onDelete: 'cascade' + }) } 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 { makePutBodyRequest, createUser, loginAndGetAccessToken, - getUserInformation, + getMyUserInformation, getUsersList, getUsersListPaginationAndSort, updateUser, + updateMyUser, registerUser, removeUser } from '../utils' import { killallServers } from '../utils/servers' +import { getUserInformation } from '../utils/users' describe('Test users', function () { let server: ServerInfo @@ -166,7 +168,7 @@ describe('Test users', function () { it('Should be able to upload a video again') it('Should be able to create a new user', async function () { - await createUser(server.url, accessToken, 'user_1', 'super password') + await createUser(server.url, accessToken, 'user_1', 'super password', 2 * 1024 * 1024) }) it('Should be able to login with this user', async function () { @@ -179,12 +181,13 @@ describe('Test users', function () { }) it('Should be able to get the user information', async function () { - const res = await getUserInformation(server.url, accessTokenUser) + const res = await getMyUserInformation(server.url, accessTokenUser) const user = res.body expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') expect(user.displayNSFW).to.be.false + expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') }) @@ -282,22 +285,49 @@ describe('Test users', function () { expect(users[1].displayNSFW).to.be.false }) - it('Should update the user password', async function () { - await updateUser(server.url, userId, accessTokenUser, 'new password', null) + it('Should update my password', async function () { + await updateMyUser(server.url, accessTokenUser, 'new password') server.user.password = 'new password' await login(server.url, server.client, server.user, 200) }) it('Should be able to change the NSFW display attribute', async function () { - await updateUser(server.url, userId, accessTokenUser, null, true) + await updateMyUser(server.url, accessTokenUser, undefined, true) - const res = await getUserInformation(server.url, accessTokenUser) + const res = await getMyUserInformation(server.url, accessTokenUser) const user = res.body expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') expect(user.displayNSFW).to.be.ok + expect(user.videoQuota).to.equal(2 * 1024 * 1024) + expect(user.id).to.be.a('number') + }) + + it('Should be able to change the email display attribute', async function () { + await updateMyUser(server.url, accessTokenUser, undefined, undefined, 'updated@example.com') + + const res = await getMyUserInformation(server.url, accessTokenUser) + const user = res.body + + expect(user.username).to.equal('user_1') + expect(user.email).to.equal('updated@example.com') + expect(user.displayNSFW).to.be.ok + expect(user.videoQuota).to.equal(2 * 1024 * 1024) + expect(user.id).to.be.a('number') + }) + + it('Should be able to update another user', async function () { + await updateUser(server.url, userId, server.accessToken, 'updated2@example.com', 42 ) + + const res = await getUserInformation(server.url, server.accessToken, userId) + const user = res.body + + expect(user.username).to.equal('user_1') + expect(user.email).to.equal('updated2@example.com') + expect(user.displayNSFW).to.be.ok + expect(user.videoQuota).to.equal(42) expect(user.id).to.be.a('number') }) @@ -329,7 +359,14 @@ describe('Test users', function () { password: 'my super password' } - await loginAndGetAccessToken(server) + accessToken = await loginAndGetAccessToken(server) + }) + + it('Should have the correct video quota', async function () { + const res = await getMyUserInformation(server.url, accessToken) + const user = res.body + + expect(user.videoQuota).to.equal(5 * 1024 * 1024) }) 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 @@ import * as request from 'supertest' -function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) { +function createUser (url: string, accessToken: string, username: string, password: string, videoQuota = 1000000, specialStatus = 204) { const path = '/api/v1/users' const body = { username, password, - email: username + '@example.com' + email: username + '@example.com', + videoQuota } return request(url) @@ -31,7 +32,7 @@ function registerUser (url: string, username: string, password: string, specialS .expect(specialStatus) } -function getUserInformation (url: string, accessToken: string) { +function getMyUserInformation (url: string, accessToken: string) { const path = '/api/v1/users/me' return request(url) @@ -42,6 +43,17 @@ function getUserInformation (url: string, accessToken: string) { .expect('Content-Type', /json/) } +function getUserInformation (url: string, accessToken: string, userId: number) { + const path = '/api/v1/users/' + userId + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + function getUserVideoRating (url: string, accessToken: string, videoId: number) { const path = '/api/v1/users/me/videos/' + videoId + '/rating' @@ -86,12 +98,28 @@ function removeUser (url: string, userId: number, accessToken: string, expectedS .expect(expectedStatus) } -function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) { - const path = '/api/v1/users/' + userId +function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean, email?: string) { + const path = '/api/v1/users/me' const toSend = {} if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW + if (email !== undefined && email !== null) toSend['email'] = email + + return request(url) + .put(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send(toSend) + .expect(204) +} + +function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number) { + const path = '/api/v1/users/' + userId + + const toSend = {} + if (email !== undefined && email !== null) toSend['password'] = email + if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota return request(url) .put(path) @@ -106,10 +134,12 @@ function updateUser (url: string, userId: number, accessToken: string, newPasswo export { createUser, registerUser, - getUserInformation, + getMyUserInformation, getUserVideoRating, getUsersList, getUsersListPaginationAndSort, removeUser, - updateUser + updateUser, + updateMyUser, + getUserInformation } -- 2.41.0