- Removed test data from db files, cleaned structures, and fix other things

This commit is contained in:
Alexis
2020-06-22 21:36:51 +02:00
parent 49bddf0939
commit 96d13ec6e9
15 changed files with 186 additions and 227 deletions

View File

@@ -10,7 +10,8 @@ const IngredientValidation = require("../validations/IngredientValidation")
v.addSchema(IngredientValidation, "/IngredientValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling
const { HttpError } = require('../validations/Errors')
@@ -68,11 +69,11 @@ class IngredientRepository {
addOne(igr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(igr)) {
if (isEmptyObject(igr)) {
reject(new HttpError(403, "Error: Ingredient cannot be nothing !"))
} else if (!v.validate(igr, IngredientValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors))
} else if (this.isXSSAttempt(igr.description)) {
} else if (isXSSAttempt(igr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
bookshelf.transaction(t => {
@@ -103,11 +104,11 @@ class IngredientRepository {
updateOne(id, igr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(igr)) {
if (isEmptyObject(igr)) {
reject(new HttpError(403, "Error: Ingredient cannot be nothing !"))
} else if (!v.validate(igr, IngredientValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors))
} else if (this.isXSSAttempt(igr.description)) {
} else if (isXSSAttempt(igr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
model.forge({id: id})
@@ -165,24 +166,6 @@ class IngredientRepository {
})
})
}
// Check if object is null
isEmptyObject(obj) {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt(string) {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = IngredientRepository

View File

@@ -10,7 +10,8 @@ const SchoolValidation = require("../validations/SchoolValidation")
v.addSchema(SchoolValidation, "/SchoolValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling
const { HttpError } = require('../validations/Errors')
@@ -67,11 +68,11 @@ class SchoolRepository {
addOne(s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) {
if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: School cannot be nothing !"))
} else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) {
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
bookshelf.transaction(t => {
@@ -103,11 +104,11 @@ class SchoolRepository {
updateOne(id, s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) {
if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: School cannot be nothing !"))
} else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) {
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
model.forge({id: id})
@@ -166,24 +167,6 @@ class SchoolRepository {
})
})
}
// Check if object is null
isEmptyObject(obj) {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt(string) {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = SchoolRepository

View File

@@ -10,7 +10,8 @@ const SpellValidation = require("../validations/SpellValidation")
v.addSchema(SpellValidation, "/SpellValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling
const { HttpError } = require('../validations/Errors')
@@ -34,6 +35,40 @@ class SpellRepository {
})
}
getAllPublic() {
return new Promise((resolve, reject) => {
model.forge()
.where({ 'public' : 1 })
.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get public spells"))
})
})
}
getPage(page) {
return new Promise((resolve, reject) => {
model.forge()
.where({ 'public' : 1 })
.fetchPage({
pageSize: 20,
page: page,
withRelated: ['schools.meta_schools', 'variables', 'ingredients'],
})
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get public spells"))
})
})
}
getOne(id) {
return new Promise((resolve, reject) => {
model.forge()
@@ -52,11 +87,11 @@ class SpellRepository {
addOne(s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) {
if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: Spell cannot be nothing !"))
} else if (!v.validate(s, SpellValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description) || this.isXSSAttempt(s.cost)) {
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
bookshelf.transaction(t => {
@@ -112,11 +147,11 @@ class SpellRepository {
updateOne(id, s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) {
if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: Spell cannot be nothing !"))
} else if (!v.validate(s, SpellValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description) || this.isXSSAttempt(s.cost)) {
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
model.forge({id: id})
@@ -222,24 +257,6 @@ class SpellRepository {
})
})
}
// Check if object is null
isEmptyObject(obj) {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt(string) {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = SpellRepository

View File

@@ -13,7 +13,8 @@ const UserValidation = require("../validations/UserValidation")
v.addSchema(UserValidation, "/UserValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling
const { HttpError } = require('../validations/Errors')

View File

@@ -10,7 +10,8 @@ const VariableValidation = require("../validations/VariableValidation")
v.addSchema(VariableValidation, "/VariableValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling
const { HttpError } = require('../validations/Errors')
@@ -67,11 +68,11 @@ class VariableRepository {
addOne(vr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(vr)) {
if (isEmptyObject(vr)) {
reject(new HttpError(403, "Error: Variable cannot be nothing !"))
} else if (!v.validate(vr, VariableValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors))
} else if (this.isXSSAttempt(vr.description)) {
} else if (isXSSAttempt(vr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
bookshelf.transaction(t => {
@@ -101,11 +102,11 @@ class VariableRepository {
updateOne(id, vr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(vr)) {
if (isEmptyObject(vr)) {
reject(new HttpError(403, "Error: Variable cannot be nothing !"))
} else if (!v.validate(vr, VariableValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors))
} else if (this.isXSSAttempt(vr.description)) {
} else if (isXSSAttempt(vr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
model.forge({id: id})
@@ -162,24 +163,6 @@ class VariableRepository {
})
})
}
// Check if object is null
isEmptyObject(obj) {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt(string) {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = VariableRepository