Skip to content

Commit

Permalink
Disallowing the use of API Model.create and Model.remove (#12)
Browse files Browse the repository at this point in the history
These two methods can't be protected by this plugin, so it's best to disallow them entirely. By doing so, it makes it safe to wrap your mongoose models with an Restify wrapper.

Also, this diff introduces Ava and the new testing engine. Very similar syntax as before, just a newer, modern, maintained library.
  • Loading branch information
makinde authored Jun 19, 2018
1 parent 982ddac commit 1654573
Show file tree
Hide file tree
Showing 7 changed files with 3,003 additions and 731 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 2.0.0

- Removing the ability to call Model.remove() and Model.create() since those aren't compatible with how this library works.
-
27 changes: 27 additions & 0 deletions __tests__/restrictedMethods.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const test = require('ava');
const mongoose = require('mongoose');
const authz = require('../');
const IncompatibleMethodError = require('../lib/IncompatibleMethodError');

test.before((t) => {
const schema = new mongoose.Schema({ friend: String });
schema.plugin(authz);
t.context.MyModel = mongoose.model('MyModel', schema);
});

test('Model.create should not be callable', (t) => {
const { MyModel } = t.context;
t.throws(
() => MyModel.create({ friend: 'bar' }),
IncompatibleMethodError,
);
});

test('Model.remove should not be callable', (t) => {
const { MyModel } = t.context;
t.throws(
() => MyModel.remove({}),
IncompatibleMethodError,
);
});

9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
} = require('./lib/helpers');

const PermissionDeniedError = require('./lib/PermissionDeniedError');
const IncompatibleMethodError = require('./lib/IncompatibleMethodError');

module.exports = (schema) => {
async function save(doc, options) {
Expand Down Expand Up @@ -143,4 +144,12 @@ module.exports = (schema) => {
const authLevels = await resolveAuthLevel(schema, options, {});
return hasPermission(this.schema, authLevels, 'create');
};

schema.statics.create = function cannotCreate() {
throw new IncompatibleMethodError('Model.create');
};

schema.statics.remove = function cannotRemove() {
throw new IncompatibleMethodError('Model.remove');
};
};
9 changes: 9 additions & 0 deletions lib/IncompatibleMethodError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = class IncompatibleMethodError extends Error {
constructor(method) {
const message = `[${method}] is not compatable with mongoose-authz. ` +
`Please see https://www.npmjs.com/package/mongoose-authz#${method} for more details.`;

super(message);
this.name = 'IncompatibleMethod';
}
};
Loading

0 comments on commit 1654573

Please sign in to comment.