Permalink
Browse files

Make the acl object emit grant and revoke events.

  • Loading branch information...
1 parent 72f19d6 commit 92ee8a1f22db20c88186be3aa30dd909be61e022 Chakrit Wichian committed Sep 5, 2012
Showing with 67 additions and 7 deletions.
  1. +7 −5 index.js
  2. +53 −0 test/events.js
  3. +7 −2 test/index.js
View
@@ -2,18 +2,20 @@
// index.js - Main interface file
module.exports = (function() {
- var acl = { };
+ var EventEmitter = require('events').EventEmitter
+ , acl = new EventEmitter();
acl.MemoryStore = require('./memory-store');
acl.RedisStore = require('./redis-store');
acl.MockStore = require('./mock-store');
// function shim
- var shim = function(obj, funcName) {
- var func = obj[funcName];
+ var shim = function(obj, action) {
+ var func = obj[action];
- return function() {
- func.apply(obj, arguments);
+ return function(grantee, resource, callback) {
+ acl.emit(action, grantee, resource);
+ func.call(obj, grantee, resource, callback);
};
};
View
@@ -0,0 +1,53 @@
+
+// events.js - Test that the acl module properly emit events.
+(function() {
+
+ var assert = require('assert')
+ , acl = require('..');
+
+ // helper for testing EventEmitter
+ var fuse = function() {
+ this.triggered = false;
+ this.trigger = function() { this.triggered = true; };
+ this.assert = function() { assert(this.triggered); };
+ };
+
+ var G = 'grantee', R = 'resource'
+ , events = 'grant,revoke'.split(',');
+
+ // tests
+ describe('acl events', function() {
+
+ for (var i in events) (function(event) {
+ describe('when ' + event + '()-ing', function() {
+ before(function(done) { var me = this;
+ me.emitted = false;
+ me.listener = function(grantee, resource) {
+ me.grantee = grantee;
+ me.resource = resource;
+ me.emitted = true;
+ };
+
+ acl.on(event, me.listener);
+ acl[event].call(me.acl, G, R, done);
+ });
+
+ after(function() {
+ acl.removeListener(event, this.listener);
+ delete this['listener'];
+ delete this['grantee'];
+ delete this['resource'];
+ delete this['emitted'];
+ });
+
+ it('the ' + event + ' event is emitted with the same params', function() {
+ assert(this.emitted);
+ assert(this.grantee === G);
+ assert(this.resource === R);
+ });
+ });
+ })(events[i]);
+
+ });
+
+})();
View
@@ -2,15 +2,20 @@
// index.js - Test main simple-acl export
(function() {
- var assert = require('assert');
+ var assert = require('assert')
+ , EventEmitter = require('events').EventEmitter;
// interface info
var stores = 'MemoryStore,RedisStore,MockStore'.split(',')
, funcs = 'grant,revoke,assert'.split(',');
// tests
describe('acl', function() {
- before(function() { this.acl = require('./'); });
+ before(function() { this.acl = require('..'); });
+
+ it('should be an instance of EventEmitter', function() {
+ assert(this.acl instanceof EventEmitter);
+ });
for (var i in stores) (function(store) {
it('should exports the ' + store, function() {

0 comments on commit 92ee8a1

Please sign in to comment.