Begin tests for user quota

This commit is contained in:
Chocobozzz 2017-09-05 22:09:16 +02:00
parent 8094a89802
commit 5c98d3bf07
7 changed files with 101 additions and 20 deletions

View File

@ -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
}
}

View File

@ -21,5 +21,8 @@ storage:
admin:
email: 'admin1@example.com'
user:
video_quota: 1024 * 1024 * 5
signup:
limit: 4

View File

@ -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)
})
})
}

View File

@ -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
}
]
}

View File

@ -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) {

View File

@ -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 () {

View File

@ -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
}