Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_js:
- 5
- 6
- 7
- 8
before_install: if [[ `npm -v` != 3* ]]; then npm i -g npm@3; fi
notifications:
email: false
Expand Down
31 changes: 31 additions & 0 deletions api/policies/FootprintPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ const Policy = require('trails/policy')
* @see http://expressjs.com/en/4x/api.html#req
*/
module.exports = class Footprint extends Policy {
_isModelIgnored(req) {
const ignoredModels = _.get(this.app.config, 'footprints.models.ignore', []).map(value => {
return value.toLowerCase()
})
return req.params.model ? ignoredModels.indexOf(req.params.model.toLowerCase()) != -1 :
ignoredModels.indexOf(req.params.parentModel.toLowerCase()) != -1
}

/**
* Create Policy.
* @see FootprintController.create
*/
create(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (!_.isPlainObject(req.body) && !_.isArray(req.body)) {
return res.boom.preconditionFailed(this.__('errors.footprints.body'))
}
Expand All @@ -31,6 +41,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.find
*/
find(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
const criteria = this.app.packs.express.getCriteriaFromQuery(req.query)

if (req.params.id && !_.isEmpty(criteria)) {
Expand All @@ -45,6 +58,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.update
*/
update(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (req.params.id && !_.isEmpty(req.query)) {
return res.boom.preconditionFailed(this.__('errors.footprints.update.mutex'))
}
Expand All @@ -57,6 +73,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.destroy
*/
destroy(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (req.params.id && !_.isEmpty(req.query)) {
return res.boom.preconditionFailed(this.__('errors.footprints.destroy.mutex'))
}
Expand All @@ -69,6 +88,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.createAssociation
*/
createAssociation(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (!_.isPlainObject(req.body)) {
return res.boom.preconditionFailed(this.__('errors.footprints.body'))
}
Expand All @@ -81,6 +103,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.findAssociation
*/
findAssociation(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
const criteria = this.app.packs.express.getCriteriaFromQuery(req.query)
if (req.params.childId && !_.isEmpty(criteria)) {
return res.boom.preconditionFailed(this.__('errors.footprints.find.mutex'))
Expand All @@ -94,6 +119,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.updateAssociation
*/
updateAssociation(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (req.params.childId && !_.isEmpty(req.query)) {
return res.boom.preconditionFailed(this.__('errors.footprints.update.mutex'))
}
Expand All @@ -106,6 +134,9 @@ module.exports = class Footprint extends Policy {
* @see FootprintController.destroyAssociation
*/
destroyAssociation(req, res, next) {
if (this._isModelIgnored(req)) {
return res.boom.forbidden(this.__('errors.footprints.forbidden'))
}
if (req.params.childId && !_.isEmpty(req.query)) {
return res.boom.preconditionFailed(this.__('errors.footprints.destroy.mutex'))
}
Expand Down
23 changes: 23 additions & 0 deletions test/api/models/Ignored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const Model = require('trails/model')

/**
* Ignored
*
* @description A Ignored model
*/
module.exports = class User extends Model {
static config(app, Sequelize) {
return {}
}

static schema(app, Sequelize) {
return {
name: {
type: Sequelize.STRING,
allowNull: false
}
}
}
}
1 change: 1 addition & 0 deletions test/api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exports.User = require('./User')
exports.Ignored = require('./Ignored')
exports.Role = require('./Role')
2 changes: 1 addition & 1 deletion test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const App = {
ignore: ['DefaultController', 'ViewController', 'StandardController']
},
models: {

ignore: ['Ignored'],
actions: {
create: true,
createWithId: true,
Expand Down
62 changes: 62 additions & 0 deletions test/integration/footprint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,68 @@ describe('FootprintController', () => {
request = supertest('http://localhost:3000/api/v1')
})

describe('#ignored model', () => {
it('should not insert a record', done => {
request
.post('/ignored')
.send({
name: 'createtest1'
})
.expect(403)
.end((err, res) => {
assert.equal(res.body.message, 'errors.footprints.forbidden')
assert.equal(res.body.error, 'Forbidden')
done(err)
})
})
it('should not find', done => {
request
.get('/ignored/' + userId)
.expect(403)
.end((err, res) => {
assert.equal(res.body.message, 'errors.footprints.forbidden')
assert.equal(res.body.error, 'Forbidden')
done(err)
})
})
it('should not update', done => {
request
.put('/ignored/' + userId)
.send({
name: 'updatetest2'
})
.expect(403)
.end((err, res) => {
assert.equal(res.body.message, 'errors.footprints.forbidden')
assert.equal(res.body.error, 'Forbidden')
done(err)
})
})
it('should not destroy', done => {
request
.del('/ignored/' + userId)
.expect(403)
.end((err, res) => {
assert.equal(res.body.message, 'errors.footprints.forbidden')
assert.equal(res.body.error, 'Forbidden')
done(err)
})
})
it('should not insert an associated record', done => {
request
.post('/ignored/' + userId + '/roles')
.send({
name: 'associatedroletest1'
})
.expect(403)
.end((err, res) => {
assert.equal(res.body.message, 'errors.footprints.forbidden')
assert.equal(res.body.error, 'Forbidden')
done(err)
})
})
})

describe('#create', () => {
it('should insert a record', done => {
request
Expand Down