Skip to content
This repository
Browse code

Change calipsoHelper to be async using a finalize function.

  • Loading branch information...
commit 84f3af41cc45a652f7547484cc172e928b6999eb 1 parent ea5777d
Andreas Richter authored September 05, 2012
21  test/helpers/calipsoHelper.js
@@ -163,12 +163,15 @@ var requests = {
163 163
 /**
164 164
  * Initialise everything and then export
165 165
  */
166  
-new MockApp(function (app) {
167  
-	module.exports = {
168  
-		app: app,
169  
-		calipso: calipso,
170  
-		testPermit: calipso.permission.Helper.hasPermission("test:permission"),
171  
-		requests: requests,
172  
-		response: CreateResponse()
173  
-	}
174  
-})
  166
+module.exports.finalize = function (next) {
  167
+  if (module.exports.app)
  168
+    return next(null, module.exports);
  169
+  new MockApp(function (app) {
  170
+    module.exports.app = app;
  171
+    module.exports.calipso = calipso;
  172
+    module.exports.testPermit = calipso.permission.Helper.hasPermission("test:permission"),
  173
+    module.exports.requests = requests;
  174
+    module.exports.response = CreateResponse();
  175
+    next(null, module.exports);
  176
+  });
  177
+};
238  test/lib.calipso.js
@@ -5,133 +5,135 @@ var should = require('should'),
5 5
     rootpath = process.cwd() + '/',
6 6
     path = require('path'),
7 7
     fs = require('fs'),
8  
-    calipsoHelper = require('./helpers/calipsoHelper'),
9  
-    calipso = calipsoHelper.calipso,
  8
+    helper = require('./helpers/calipsoHelper'),
10 9
     mochaConfig = path.join(rootpath,'tmp','mocha.json');
11 10
 
12 11
     //calipso = require('./helpers/require')('calipso');
13 12
 
14  
-describe('Calipso', function(){
15  
-
16  
-  before(function(done){
17  
-      try { fs.unlinkSync(mochaConfig); } catch(ex) { /** ignore **/ }
18  
-      calipso.init(calipsoHelper.app, function(app) {
19  
-        done();
20  
-      })
21  
-  });
22  
-
23  
-  describe('Basic loading and initialisation', function(){
24  
-
25  
-    it('Calipso has loaded successfully', function(done) {
26  
-      calipso.loaded.should.equal(true);
27  
-      calipso.modules.should.exist;
28  
-      done();
29  
-    });
30  
-
31  
-    it('I can send a mock request through.', function(done) {
32  
-
33  
-      var req = calipsoHelper.requests.testUser,
34  
-          res = calipsoHelper.response,
35  
-          response = 0,
36  
-          routeFn = calipso.routingFn();
37  
-
38  
-      req.url = '/';
39  
-      res.outputStack = [];
40  
-
41  
-      // Over ride the res.end and increment our counter
42  
-      res.send = function(content) {
43  
-        response++;
44  
-      }
45  
-
46  
-      routeFn(req, res, function(err) {
47  
-        response.should.equal(1);
48  
-        res.outputStack.should.eql(['module_first','module_b','module_last']);
49  
-        done();
50  
-      })
51  
-
52  
-    });
53  
-
54  
-    it('I can send a mock request through to an invalid route and get a 404', function(done) {
55  
-
56  
-      var req = calipsoHelper.requests.testUser,
57  
-          res = calipsoHelper.response,
58  
-          response = 0,
59  
-          routeFn = calipso.routingFn();
60  
-
61  
-      req.url = '/invalid.html';
62  
-      res.outputStack = [];
63  
-
64  
-      // Over ride the res.end and increment our counter
65  
-      res.send = function(content) {
66  
-        response++;
67  
-      }
68  
-
69  
-      routeFn(req, res, function(err) {
70  
-        response.should.equal(1);
71  
-        res.statusCode.should.equal(404);
72  
-        res.outputStack.should.eql(['module_first','module_last']);
73  
-        done();
74  
-      })
75  
-
76  
-    });
77  
-
78  
-    it('I can send a mock admin request through as an admin user', function(done) {
79  
-
80  
-      var req = calipsoHelper.requests.adminUser,
81  
-          res = calipsoHelper.response,
82  
-          response = 0,
83  
-          responseContent = '',
84  
-          routeFn = calipso.routingFn();
85  
-
86  
-      req.url = '/secured';
87  
-      res.outputStack = [];
88  
-
89  
-      // Over ride the res.end and increment our counter
90  
-      res.send = function(content) {
91  
-        responseContent = content;
92  
-        response++;
93  
-      }
94  
-      routeFn(req, res, function(err) {
95  
-        response.should.equal(1);
96  
-        res.outputStack.should.eql(['module_first','module_a','module_last']);
97  
-        responseContent.should.match(/world/);
98  
-        done();
99  
-      })
100  
-    });
101  
-
102  
-    it('I can send a mock admin request through and fail as a test user.', function(done) {
103  
-      var req = calipsoHelper.requests.anonUser,
104  
-          res = calipsoHelper.response,
105  
-          response = 0,
106  
-          routeFn = calipso.routingFn();
107  
-
108  
-      req.cookies = {};
109  
-      req.url = '/secured';
110  
-      res.outputStack = [];
111  
-
112  
-      routeFn(req, res, function(err) {
113  
-        res.outputStack.should.eql(['module_first','module_last']);
114  
-        req.flashMsgs[0].type.should.equal('error');
115  
-        res.redirectQueue.length.should.equal(1);
116  
-        res.redirectQueue[0].should.equal('/');
117  
-        response.should.equal(0); // Don't match
118  
-        done();
119  
-      })
  13
+helper.finalize(function (err, calipsoHelper) {
  14
+  var calipso = calipsoHelper.calipso;
120 15
 
  16
+  describe('Calipso', function(){
  17
+  
  18
+    before(function(done){
  19
+        try { fs.unlinkSync(mochaConfig); } catch(ex) { /** ignore **/ }
  20
+        calipso.init(calipsoHelper.app, function(app) {
  21
+          done();
  22
+        })
121 23
     });
122  
-
123  
-    it('I can reload the configuration', function(done) {
124  
-      calipso.reloadConfig('RELOAD', {}, function(err) {
  24
+  
  25
+    describe('Basic loading and initialisation', function(){
  26
+  
  27
+      it('Calipso has loaded successfully', function(done) {
  28
+        calipso.loaded.should.equal(true);
125 29
         calipso.modules.should.exist;
126 30
         done();
127 31
       });
  32
+  
  33
+      it('I can send a mock request through.', function(done) {
  34
+  
  35
+        var req = calipsoHelper.requests.testUser,
  36
+            res = calipsoHelper.response,
  37
+            response = 0,
  38
+            routeFn = calipso.routingFn();
  39
+  
  40
+        req.url = '/';
  41
+        res.outputStack = [];
  42
+  
  43
+        // Over ride the res.end and increment our counter
  44
+        res.send = function(content) {
  45
+          response++;
  46
+        }
  47
+  
  48
+        routeFn(req, res, function(err) {
  49
+          response.should.equal(1);
  50
+          res.outputStack.should.eql(['module_first','module_b','module_last']);
  51
+          done();
  52
+        })
  53
+  
  54
+      });
  55
+  
  56
+      it('I can send a mock request through to an invalid route and get a 404', function(done) {
  57
+  
  58
+        var req = calipsoHelper.requests.testUser,
  59
+            res = calipsoHelper.response,
  60
+            response = 0,
  61
+            routeFn = calipso.routingFn();
  62
+  
  63
+        req.url = '/invalid.html';
  64
+        res.outputStack = [];
  65
+  
  66
+        // Over ride the res.end and increment our counter
  67
+        res.send = function(content) {
  68
+          response++;
  69
+        }
  70
+  
  71
+        routeFn(req, res, function(err) {
  72
+          response.should.equal(1);
  73
+          res.statusCode.should.equal(404);
  74
+          res.outputStack.should.eql(['module_first','module_last']);
  75
+          done();
  76
+        })
  77
+  
  78
+      });
  79
+  
  80
+      it('I can send a mock admin request through as an admin user', function(done) {
  81
+  
  82
+        var req = calipsoHelper.requests.adminUser,
  83
+            res = calipsoHelper.response,
  84
+            response = 0,
  85
+            responseContent = '',
  86
+            routeFn = calipso.routingFn();
  87
+  
  88
+        req.url = '/secured';
  89
+        res.outputStack = [];
  90
+  
  91
+        // Over ride the res.end and increment our counter
  92
+        res.send = function(content) {
  93
+          responseContent = content;
  94
+          response++;
  95
+        }
  96
+        routeFn(req, res, function(err) {
  97
+          response.should.equal(1);
  98
+          res.outputStack.should.eql(['module_first','module_a','module_last']);
  99
+          responseContent.should.match(/world/);
  100
+          done();
  101
+        })
  102
+      });
  103
+  
  104
+      it('I can send a mock admin request through and fail as a test user.', function(done) {
  105
+        var req = calipsoHelper.requests.anonUser,
  106
+            res = calipsoHelper.response,
  107
+            response = 0,
  108
+            routeFn = calipso.routingFn();
  109
+  
  110
+        req.cookies = {};
  111
+        req.url = '/secured';
  112
+        res.outputStack = [];
  113
+  
  114
+        routeFn(req, res, function(err) {
  115
+          res.outputStack.should.eql(['module_first','module_last']);
  116
+          req.flashMsgs[0].type.should.equal('error');
  117
+          res.redirectQueue.length.should.equal(1);
  118
+          res.redirectQueue[0].should.equal('/');
  119
+          response.should.equal(0); // Don't match
  120
+          done();
  121
+        })
  122
+  
  123
+      });
  124
+  
  125
+      it('I can reload the configuration', function(done) {
  126
+        calipso.reloadConfig('RELOAD', {}, function(err) {
  127
+          calipso.modules.should.exist;
  128
+          done();
  129
+        });
  130
+      });
  131
+  
128 132
     });
129  
-
  133
+  
  134
+    after(function() {
  135
+      try { fs.unlinkSync(mochaConfig); } catch(ex) { /** ignore **/ }
  136
+    })
  137
+  
130 138
   });
131  
-
132  
-  after(function() {
133  
-    try { fs.unlinkSync(mochaConfig); } catch(ex) { /** ignore **/ }
134  
-  })
135  
-
136 139
 });
137  
-
246  test/lib.client.client.js
@@ -5,134 +5,136 @@ var should = require('should'),
5 5
     fs = require('fs'),
6 6
     rootpath = process.cwd() + '/',
7 7
     path = require('path'),
8  
-    calipsoHelper = require('./helpers/calipsoHelper'),
9  
-    calipso = calipsoHelper.calipso,
10  
-    Client = require('./helpers/require')('client/Client');
11  
-
12  
-describe('Client', function(){
13  
-
14  
-  before(function(){
15  
-    //
16  
-  });
17  
-
18  
-  describe('Adding scripts', function(){
19  
-
20  
-    it('I can add a basic client script', function(done){
21  
-      var client = new Client(), script = {name:'test',url:'/test.js',weight:10};
22  
-      client.addScript(script);
23  
-      client.scripts.length.should.equal(1);
24  
-      client.scripts[0].should.equal(script);
25  
-      done();
26  
-    });
27  
-
28  
-    it('If there is no name, it assigns the url as the name', function(done){
29  
-      var client = new Client(), script = {url:'/test.js', weight:10}, scriptWithName = {url:'/test.js',weight:10, name:'/test.js'};
30  
-      client.addScript(script);
31  
-      client.scripts.length.should.equal(1);
32  
-      client.scripts[0].should.eql(scriptWithName);
33  
-      done();
34  
-    });
35  
-
36  
-    it('I can add core scripts just by name', function(done){
37  
-      var client = new Client(), script = 'calipso', scriptWithName = {key:'calipso', url:'calipso.js',weight:-50, name:'calipso.js'};
38  
-      client.addScript(script);
39  
-      client.scripts.length.should.equal(1);
40  
-      client.scripts[0].should.eql(scriptWithName);
41  
-      done();
42  
-    });
43  
-
44  
-    it('I can add a script just by name', function(done){
45  
-      var client = new Client(), script = '/myscript.js', scriptWithName = {url:'/myscript.js',weight:0, name:'/myscript.js'};
46  
-      client.addScript(script);
47  
-      client.scripts.length.should.equal(1);
48  
-      client.scripts[0].should.eql(scriptWithName);
49  
-      done();
50  
-    });
  8
+    helper = require('./helpers/calipsoHelper');
51 9
 
  10
+helper.finalize(function (err, calipsoHelper) {
  11
+  var calipso = calipsoHelper.calipso,
  12
+    Client = require('./helpers/require')('client/Client');
52 13
 
53  
-    it('Adding a script twice does not duplicate it', function(done){
54  
-      var client = new Client(), script = {name:'test',url:'/test.js',weight:10};
55  
-
56  
-      client.addScript(script);
57  
-      client.addScript(script);
58  
-
59  
-      client.scripts.length.should.equal(1);
60  
-      client.scripts[0].should.equal(script);
61  
-      done();
62  
-
63  
-    });
64  
-
65  
-
66  
-  });
67  
-
68  
-  describe('Adding styles', function(){
69  
-
70  
-    it('I can add a basic client style', function(done){
71  
-      var client = new Client(), style = {name:'test',url:'/test.css',weight:10};
72  
-      client.addStyle(style);
73  
-      client.styles.length.should.equal(1);
74  
-      client.styles[0].should.equal(style);
75  
-      done();
76  
-    });
77  
-
78  
-    it('If there is no name, it assigns the url as the name', function(done){
79  
-      var client = new Client(), style = {url:'/test.css', weight:10}, styleWithName = {url:'/test.css',weight:10, name:'/test.css'};
80  
-      client.addStyle(style);
81  
-      client.styles.length.should.equal(1);
82  
-      client.styles[0].should.eql(styleWithName);
83  
-      done();
84  
-    });
85  
-
86  
-    it('I can add a style by just passing in a url', function(done){
87  
-      var client = new Client(), style = '/test.css', styleWithName = {url:'/test.css',weight:0, name:'/test.css'};
88  
-      client.addStyle(style);
89  
-      client.styles.length.should.equal(1);
90  
-      client.styles[0].should.eql(styleWithName);
91  
-      done();
  14
+  describe('Client', function(){
  15
+  
  16
+    before(function(){
  17
+      //
92 18
     });
93  
-
94  
-
95  
-  });
96  
-
97  
-  describe('Listing scripts and styles', function(){
98  
-
99  
-     it('Listing scripts satisfies the ordering defined', function(done){
100  
-      var client = new Client(),
101  
-          s1 = {url:'/test1.js', weight:10},
102  
-          s2 = {url:'/test2.js', weight:-10};
103  
-
104  
-      client.addScript(s1);
105  
-      client.addScript(s2);
106  
-
107  
-      client.listScripts(function(err, scripts) {
108  
-        should.not.exist(err);
109  
-        scripts.should.match(/<script title=\"\/test2\.js\" src=\"\/test2\.js\"><\/script>\r\n<script title=\"\/test1\.js\" src=\"\/test1\.js\">/);
  19
+  
  20
+    describe('Adding scripts', function(){
  21
+  
  22
+      it('I can add a basic client script', function(done){
  23
+        var client = new Client(), script = {name:'test',url:'/test.js',weight:10};
  24
+        client.addScript(script);
  25
+        client.scripts.length.should.equal(1);
  26
+        client.scripts[0].should.equal(script);
110 27
         done();
111  
-      })
112  
-
  28
+      });
  29
+  
  30
+      it('If there is no name, it assigns the url as the name', function(done){
  31
+        var client = new Client(), script = {url:'/test.js', weight:10}, scriptWithName = {url:'/test.js',weight:10, name:'/test.js'};
  32
+        client.addScript(script);
  33
+        client.scripts.length.should.equal(1);
  34
+        client.scripts[0].should.eql(scriptWithName);
  35
+        done();
  36
+      });
  37
+  
  38
+      it('I can add core scripts just by name', function(done){
  39
+        var client = new Client(), script = 'calipso', scriptWithName = {key:'calipso', url:'calipso.js',weight:-50, name:'calipso.js'};
  40
+        client.addScript(script);
  41
+        client.scripts.length.should.equal(1);
  42
+        client.scripts[0].should.eql(scriptWithName);
  43
+        done();
  44
+      });
  45
+  
  46
+      it('I can add a script just by name', function(done){
  47
+        var client = new Client(), script = '/myscript.js', scriptWithName = {url:'/myscript.js',weight:0, name:'/myscript.js'};
  48
+        client.addScript(script);
  49
+        client.scripts.length.should.equal(1);
  50
+        client.scripts[0].should.eql(scriptWithName);
  51
+        done();
  52
+      });
  53
+  
  54
+  
  55
+      it('Adding a script twice does not duplicate it', function(done){
  56
+        var client = new Client(), script = {name:'test',url:'/test.js',weight:10};
  57
+  
  58
+        client.addScript(script);
  59
+        client.addScript(script);
  60
+  
  61
+        client.scripts.length.should.equal(1);
  62
+        client.scripts[0].should.equal(script);
  63
+        done();
  64
+  
  65
+      });
  66
+  
  67
+  
113 68
     });
114  
-
115  
-    it('Listing styles satisfies the ordering defined', function(done){
116  
-      var client = new Client(),
117  
-          s1 = {url:'/test1.css', weight:10},
118  
-          s2 = {url:'/test2.css', weight:-10};
119  
-
120  
-      client.addStyle(s1);
121  
-      client.addStyle(s2);
122  
-
123  
-      client.listStyles(function(err, styles) {
124  
-        should.not.exist(err);
125  
-        styles.should.match(/<link rel=\"stylesheet\" title=\"\/test2\.css\" href=\"\/test2\.css\"\/>\r\n<link rel=\"stylesheet\" title=\"\/test1\.css\" href=\"\/test1\.css\"\/>/);
  69
+  
  70
+    describe('Adding styles', function(){
  71
+  
  72
+      it('I can add a basic client style', function(done){
  73
+        var client = new Client(), style = {name:'test',url:'/test.css',weight:10};
  74
+        client.addStyle(style);
  75
+        client.styles.length.should.equal(1);
  76
+        client.styles[0].should.equal(style);
126 77
         done();
127  
-      })
128  
-
  78
+      });
  79
+  
  80
+      it('If there is no name, it assigns the url as the name', function(done){
  81
+        var client = new Client(), style = {url:'/test.css', weight:10}, styleWithName = {url:'/test.css',weight:10, name:'/test.css'};
  82
+        client.addStyle(style);
  83
+        client.styles.length.should.equal(1);
  84
+        client.styles[0].should.eql(styleWithName);
  85
+        done();
  86
+      });
  87
+  
  88
+      it('I can add a style by just passing in a url', function(done){
  89
+        var client = new Client(), style = '/test.css', styleWithName = {url:'/test.css',weight:0, name:'/test.css'};
  90
+        client.addStyle(style);
  91
+        client.styles.length.should.equal(1);
  92
+        client.styles[0].should.eql(styleWithName);
  93
+        done();
  94
+      });
  95
+  
  96
+  
129 97
     });
130  
-
  98
+  
  99
+    describe('Listing scripts and styles', function(){
  100
+  
  101
+       it('Listing scripts satisfies the ordering defined', function(done){
  102
+        var client = new Client(),
  103
+            s1 = {url:'/test1.js', weight:10},
  104
+            s2 = {url:'/test2.js', weight:-10};
  105
+  
  106
+        client.addScript(s1);
  107
+        client.addScript(s2);
  108
+  
  109
+        client.listScripts(function(err, scripts) {
  110
+          should.not.exist(err);
  111
+          scripts.should.match(/<script title=\"\/test2\.js\" src=\"\/test2\.js\"><\/script>\r\n<script title=\"\/test1\.js\" src=\"\/test1\.js\">/);
  112
+          done();
  113
+        })
  114
+  
  115
+      });
  116
+  
  117
+      it('Listing styles satisfies the ordering defined', function(done){
  118
+        var client = new Client(),
  119
+            s1 = {url:'/test1.css', weight:10},
  120
+            s2 = {url:'/test2.css', weight:-10};
  121
+  
  122
+        client.addStyle(s1);
  123
+        client.addStyle(s2);
  124
+  
  125
+        client.listStyles(function(err, styles) {
  126
+          should.not.exist(err);
  127
+          styles.should.match(/<link rel=\"stylesheet\" title=\"\/test2\.css\" href=\"\/test2\.css\"\/>\r\n<link rel=\"stylesheet\" title=\"\/test1\.css\" href=\"\/test1\.css\"\/>/);
  128
+          done();
  129
+        })
  130
+  
  131
+      });
  132
+  
  133
+    });
  134
+  
  135
+    after(function() {
  136
+  
  137
+    })
  138
+  
131 139
   });
132  
-
133  
-  after(function() {
134  
-
135  
-  })
136  
-
137 140
 });
138  
-
383  test/lib.core.event.js
@@ -5,204 +5,207 @@ var should = require('should'),
5 5
   fs = require('fs'),
6 6
   rootpath = process.cwd() + '/',
7 7
   path = require('path'),
8  
-  calipsoHelper = require('./helpers/calipsoHelper', true),
9  
-  calipso = calipsoHelper.calipso;
10  
-  Event = require('./helpers/require')('core/Event');
11  
-
12  
-describe('Events', function () {
13  
-
14  
-  before(function () {
15  
-    // 
16  
-  });
17  
-
18  
-  describe('General Events', function () {
19  
-
20  
-    it('I can create pre and post event emitters and emit an event, with no asynchronous callback', function (done) {
21  
-
22  
-      var ee = new Event.CalipsoEventEmitter();
23  
-      ee.init();
24  
-
25  
-      var eventCount = 0;
26  
-
27  
-      ee.addEvent('TEST');
28  
-
29  
-      ee.pre('TEST', 'myPreListener', function (event, data) {
30  
-        eventCount++;
31  
-      });
32  
-
33  
-      ee.post('TEST', 'myPostListener', function (event, data) {
34  
-        eventCount++;
35  
-      });
36  
-
37  
-      ee.pre_emit('TEST', {
38  
-        data: "data"
39  
-      });
40  
-      ee.post_emit('TEST', {
41  
-        data: "data"
42  
-      });
43  
-
44  
-      eventCount.should.equal(2);
45  
-
46  
-      done();
47  
-
  8
+  helper = require('./helpers/calipsoHelper', true);
  9
+  
  10
+helper.finalize(function (err, calipsoHelper) {
  11
+  var calipso = calipsoHelper.calipso;
  12
+    Event = require('./helpers/require')('core/Event');
  13
+
  14
+  describe('Events', function () {
  15
+  
  16
+    before(function () {
  17
+      // 
48 18
     });
49  
-
50  
-    it('I can create a pre and post event emitters and emit an event, with an asynchronous callback', function (done) {
51  
-
52  
-      var ee = new Event.CalipsoEventEmitter();
53  
-      ee.init();
54  
-
55  
-      var eventCount = 0;
56  
-
57  
-      ee.addEvent('TEST');
58  
-
59  
-      var callbackFn = function (err, data) {
  19
+  
  20
+    describe('General Events', function () {
  21
+  
  22
+      it('I can create pre and post event emitters and emit an event, with no asynchronous callback', function (done) {
  23
+  
  24
+        var ee = new Event.CalipsoEventEmitter();
  25
+        ee.init();
  26
+  
  27
+        var eventCount = 0;
  28
+  
  29
+        ee.addEvent('TEST');
  30
+  
  31
+        ee.pre('TEST', 'myPreListener', function (event, data) {
60 32
           eventCount++;
61  
-        }
62  
-
63  
-      ee.pre('TEST', 'myPreListener', function (event, data, next) {
64  
-        eventCount++;
65  
-        data.newData = "Pre Hello World";
66  
-        next(data);
  33
+        });
  34
+  
  35
+        ee.post('TEST', 'myPostListener', function (event, data) {
  36
+          eventCount++;
  37
+        });
  38
+  
  39
+        ee.pre_emit('TEST', {
  40
+          data: "data"
  41
+        });
  42
+        ee.post_emit('TEST', {
  43
+          data: "data"
  44
+        });
  45
+  
  46
+        eventCount.should.equal(2);
  47
+  
  48
+        done();
  49
+  
67 50
       });
68  
-
69  
-      ee.post('TEST', 'myPostListener', function (event, data, next) {
70  
-        eventCount++;
71  
-        data.newData = "Post Hello World";
72  
-        next(data);
  51
+  
  52
+      it('I can create a pre and post event emitters and emit an event, with an asynchronous callback', function (done) {
  53
+  
  54
+        var ee = new Event.CalipsoEventEmitter();
  55
+        ee.init();
  56
+  
  57
+        var eventCount = 0;
  58
+  
  59
+        ee.addEvent('TEST');
  60
+  
  61
+        var callbackFn = function (err, data) {
  62
+            eventCount++;
  63
+          }
  64
+  
  65
+        ee.pre('TEST', 'myPreListener', function (event, data, next) {
  66
+          eventCount++;
  67
+          data.newData = "Pre Hello World";
  68
+          next(data);
  69
+        });
  70
+  
  71
+        ee.post('TEST', 'myPostListener', function (event, data, next) {
  72
+          eventCount++;
  73
+          data.newData = "Post Hello World";
  74
+          next(data);
  75
+        });
  76
+  
  77
+        ee.pre_emit('TEST', {
  78
+          data: "data"
  79
+        }, function (data) {
  80
+          data.newData.should.equal("Pre Hello World");
  81
+        });
  82
+  
  83
+        ee.post_emit('TEST', {
  84
+          data: "data"
  85
+        }, function (data) {
  86
+          data.newData.should.equal("Post Hello World");
  87
+        });
  88
+  
  89
+        eventCount.should.equal(2);
  90
+  
  91
+        done();
  92
+  
73 93
       });
74  
-
75  
-      ee.pre_emit('TEST', {
76  
-        data: "data"
77  
-      }, function (data) {
78  
-        data.newData.should.equal("Pre Hello World");
  94
+  
  95
+      it('I can create custom events', function (done) {
  96
+  
  97
+        var ee = new Event.CalipsoEventEmitter();
  98
+        ee.init();
  99
+  
  100
+        var eventCount = 0;
  101
+  
  102
+        ee.addEvent('TEST');
  103
+  
  104
+        ee.custom('TEST', 'START', 'startListener', function (event, data, next) {
  105
+          eventCount++;
  106
+          data.start = "Started";
  107
+          next(data);
  108
+        });
  109
+  
  110
+        ee.custom('TEST', 'FINISH', 'finishListener', function (event, data, next) {
  111
+          eventCount++;
  112
+          data.finish = "Finished";
  113
+          next(data);
  114
+        });
  115
+  
  116
+        ee.custom_emit('TEST', 'START', {
  117
+          data: "data"
  118
+        }, function (data) {
  119
+          data.start.should.equal("Started");
  120
+        });
  121
+  
  122
+        ee.custom_emit('TEST', 'FINISH', {
  123
+          data: "data"
  124
+        }, function (data) {
  125
+          data.finish.should.equal("Finished");
  126
+        });
  127
+  
  128
+        eventCount.should.equal(2);
  129
+  
  130
+        done();
  131
+  
79 132
       });
80  
-
81  
-      ee.post_emit('TEST', {
82  
-        data: "data"
83  
-      }, function (data) {
84  
-        data.newData.should.equal("Post Hello World");
  133
+  
  134
+      it('I can reload events e.g. after a config reload', function (done) {
  135
+  
  136
+        var ee = new Event.CalipsoEventEmitter();
  137
+        ee.init();
  138
+  
  139
+        ee.addEvent('TEST2');
  140
+        ee.events['TEST2'].should.exist;
  141
+        ee.events['FORM'].should.exist;
  142
+  
  143
+        // Re-initialise
  144
+        ee.init();
  145
+        ee.events['TEST2'].should.not.exist;
  146
+        ee.events['FORM'].should.exist;
  147
+  
  148
+        done();
  149
+  
85 150
       });
86  
-
87  
-      eventCount.should.equal(2);
88  
-
89  
-      done();
90  
-
  151
+  
91 152
     });
92  
-
93  
-    it('I can create custom events', function (done) {
94  
-
95  
-      var ee = new Event.CalipsoEventEmitter();
96  
-      ee.init();
97  
-
98  
-      var eventCount = 0;
99  
-
100  
-      ee.addEvent('TEST');
101  
-
102  
-      ee.custom('TEST', 'START', 'startListener', function (event, data, next) {
103  
-        eventCount++;
104  
-        data.start = "Started";
105  
-        next(data);
106  
-      });
107  
-
108  
-      ee.custom('TEST', 'FINISH', 'finishListener', function (event, data, next) {
109  
-        eventCount++;
110  
-        data.finish = "Finished";
111  
-        next(data);
  153
+  
  154
+    describe('Module initialisation events', function () {
  155
+  
  156
+      it('I can add a module event listener to a module', function (done) {
  157
+  
  158
+        var module = {
  159
+          moduleName: 'test'
  160
+        };
  161
+  
  162
+        Event.addModuleEventListener(module);
  163
+  
  164
+        module.event._events.should.exist;
  165
+        module.event.init_start.should.exist;
  166
+        module.event.init_finish.should.exist;
  167
+  
  168
+        done();
  169
+  
112 170
       });
113  
-
114  
-      ee.custom_emit('TEST', 'START', {
115  
-        data: "data"
116  
-      }, function (data) {
117  
-        data.start.should.equal("Started");
118  
-      });
119  
-
120  
-      ee.custom_emit('TEST', 'FINISH', {
121  
-        data: "data"
122  
-      }, function (data) {
123  
-        data.finish.should.equal("Finished");
124  
-      });
125  
-
126  
-      eventCount.should.equal(2);
127  
-
128  
-      done();
129  
-
130  
-    });
131  
-
132  
-    it('I can reload events e.g. after a config reload', function (done) {
133  
-
134  
-      var ee = new Event.CalipsoEventEmitter();
135  
-      ee.init();
136  
-
137  
-      ee.addEvent('TEST2');
138  
-      ee.events['TEST2'].should.exist;
139  
-      ee.events['FORM'].should.exist;
140  
-
141  
-      // Re-initialise
142  
-      ee.init();
143  
-      ee.events['TEST2'].should.not.exist;
144  
-      ee.events['FORM'].should.exist;
145  
-
146  
-      done();
147  
-
  171
+  
148 172
     });
149  
-
150  
-  });
151  
-
152  
-  describe('Module initialisation events', function () {
153  
-
154  
-    it('I can add a module event listener to a module', function (done) {
155  
-
156  
-      var module = {
157  
-        moduleName: 'test'
158  
-      };
159  
-
160  
-      Event.addModuleEventListener(module);
161  
-
162  
-      module.event._events.should.exist;
163  
-      module.event.init_start.should.exist;
164  
-      module.event.init_finish.should.exist;
165  
-
166  
-      done();
167  
-
168  
-    });
169  
-
170  
-  });
171  
-
172  
-  describe('Module request events', function () {
173  
-
174  
-    it('I can add a module event listener to a request object', function (done) {
175  
-
176  
-      var notify = 0, register = 0, req = calipsoHelper.requests.testUser,
177  
-        res = calipsoHelper.response,
178  
-        re = new Event.RequestEventListener({
179  
-          notifyDependencyFn: function() { notify++ }, // Normally to calipso.module.notifyDependenciesOfRoute
180  
-          registerDependenciesFn: function() { register++ } // Normally to calipso.module.registerDependencies
181  
-        });
182  
-
183  
-      re.registerModule(req, res, 'module_a');
184  
-
185  
-      // We should be registered, no notifications.
186  
-      notify.should.equal(0);
187  
-      register.should.equal(1);
188  
-      re.modules.module_a.should.exist;
189  
-
190  
-      // Fire route start
191  
-      re.modules.module_a.route_start();
192  
-      re.modules.module_a.route_finish();
193  
-
194  
-      // Now have notifications
195  
-      notify.should.equal(1);
196  
-      re.modules.module_a.routed.should.equal(true);
197  
-
198  
-      done();
199  
-
  173
+  
  174
+    describe('Module request events', function () {
  175
+  
  176
+      it('I can add a module event listener to a request object', function (done) {
  177
+  
  178
+        var notify = 0, register = 0, req = calipsoHelper.requests.testUser,
  179
+          res = calipsoHelper.response,
  180
+          re = new Event.RequestEventListener({
  181
+            notifyDependencyFn: function() { notify++ }, // Normally to calipso.module.notifyDependenciesOfRoute
  182
+            registerDependenciesFn: function() { register++ } // Normally to calipso.module.registerDependencies
  183
+          });
  184
+  
  185
+        re.registerModule(req, res, 'module_a');
  186
+  
  187
+        // We should be registered, no notifications.
  188
+        notify.should.equal(0);
  189
+        register.should.equal(1);
  190
+        re.modules.module_a.should.exist;
  191
+  
  192
+        // Fire route start
  193
+        re.modules.module_a.route_start();
  194
+        re.modules.module_a.route_finish();
  195
+  
  196
+        // Now have notifications
  197
+        notify.should.equal(1);
  198
+        re.modules.module_a.routed.should.equal(true);
  199
+  
  200
+        done();
  201
+  
  202
+      });
  203
+  
200 204
     });
201  
-
  205
+  
  206
+    after(function () {
  207
+  
  208
+    })
  209
+  
202 210
   });
203  
-
204  
-  after(function () {
205  
-
206  
-  })
207  
-
208  
-});
  211
+});
496  test/lib.core.menu.js
@@ -4,268 +4,270 @@
4 4
 var should = require('should'),
5 5
     rootpath = process.cwd() + '/',
6 6
     path = require('path'),
7  
-    calipsoHelper = require('./helpers/calipsoHelper'),
8  
-    calipso = calipsoHelper.calipso,
9  
-    testPermit = calipsoHelper.testPermit,
10  
-    Menu = require('./helpers/require')('core/Menu');
11  
-
12  
-/**
13  
- * Test data
14  
- */
15  
-var anonMenuBasic = {name:'Public Menu Item',path:'publicpath',url:'/public'};
16  
-var simpleMenuBasic = {name:'Basic Menu Item',path:'simplepath',url:'/bob',permit:testPermit, order: 1};
17  
-var simpleMenuFull = {name:'Full Menu Item',path:'fullpath',url:'/bill',description:'This is a simple menu',permit:testPermit, order: 2}
18  
-var childMenuShort = {name:'Short Menu Item',path:'simplepath/child',url:'/bob/child',permit:testPermit};
19  
-var childMenuDeep = {name:'Deep Menu Item',path:'simplepath/a/b',url:'/bob/a/b',permit:testPermit};
20  
-var childMenuDeepLater = {name:'Later Menu Item',path:'simplepath/a/b/c',url:'/bob/a/b/c',permit:testPermit};
21  
-
22  
-describe('Menus', function(){
23  
-
24  
-  before(function(done){
25  
-    calipsoHelper.calipso.loaded.should.equal(true);
26  
-    done();
27  
-  });
28  
-
29  
-  describe('Creating items with correct permissons', function(){
30  
-
31  
-    it('I can create a menu, it has default sortby and is root.', function(done){
32  
-        var tm = new Menu('MyMenu');
33  
-        tm.name.should.equal('MyMenu');
34  
-        tm.type.should.equal('root');
35  
-        done();
36  
-    });
37  
-
38  
-    it('I can create a menu, with different sort by', function(done){
39  
-      var tm = new Menu('OtherMenu','key');
40  
-      tm.name.should.equal('OtherMenu');
41  
-      done();
42  
-    });
43  
-
44  
-    it('I can add a menu item with no permission and it is visible to everyone', function(done){
45  
-
46  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.anonUser;
47  
-      tm.addMenuItem(req, anonMenuBasic);
48  
-      should.exist(tm.children[anonMenuBasic.path])
49  
-      tm.children[anonMenuBasic.path].name.should.equal(anonMenuBasic.name);
50  
-      tm.children[anonMenuBasic.path].url.should.equal(anonMenuBasic.url);
51  
-      tm.children[anonMenuBasic.path].path.should.equal(anonMenuBasic.path);
52  
-
53  
-      done();
54  
-
55  
-    });
56  
-
57  
-    it('I can add a menu item and it is added to the menu correctly when user has the correct permission', function(done){
58  
-
59  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
60  
-      tm.addMenuItem(req, simpleMenuBasic);
61  
-      should.exist(tm.children[simpleMenuBasic.path])
62  
-      tm.children[simpleMenuBasic.path].name.should.equal(simpleMenuBasic.name);
63  
-      tm.children[simpleMenuBasic.path].url.should.equal(simpleMenuBasic.url);
64  
-      tm.children[simpleMenuBasic.path].path.should.equal(simpleMenuBasic.path);
65  
-      done();
66  
-
67  
-    });
68  
-
69  
-    it('Adding a menu item without the correct permission does not add the menu item', function(done){
70  
-
71  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.anonUser;
72  
-      tm.addMenuItem(req, simpleMenuBasic);
73  
-      should.not.exist(tm.children[simpleMenuBasic.path])
  7
+    helper = require('./helpers/calipsoHelper');
  8
+
  9
+helper.finalize(function (err, calipsoHelper) {
  10
+  var calipso = calipsoHelper.calipso,
  11
+      testPermit = calipsoHelper.testPermit,
  12
+      Menu = require('./helpers/require')('core/Menu');
  13
+  
  14
+  /**
  15
+   * Test data
  16
+   */
  17
+  var anonMenuBasic = {name:'Public Menu Item',path:'publicpath',url:'/public'};
  18
+  var simpleMenuBasic = {name:'Basic Menu Item',path:'simplepath',url:'/bob',permit:testPermit, order: 1};
  19
+  var simpleMenuFull = {name:'Full Menu Item',path:'fullpath',url:'/bill',description:'This is a simple menu',permit:testPermit, order: 2}
  20
+  var childMenuShort = {name:'Short Menu Item',path:'simplepath/child',url:'/bob/child',permit:testPermit};
  21
+  var childMenuDeep = {name:'Deep Menu Item',path:'simplepath/a/b',url:'/bob/a/b',permit:testPermit};
  22
+  var childMenuDeepLater = {name:'Later Menu Item',path:'simplepath/a/b/c',url:'/bob/a/b/c',permit:testPermit};
  23
+  
  24
+  describe('Menus', function(){
  25
+  
  26
+    before(function(done){
  27
+      calipsoHelper.calipso.loaded.should.equal(true);
74 28
       done();
75  
-
76  
-    });
77  
-
78  
-    it('Administrators can see all menu items', function(done){
79  
-
80  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.adminUser;
81  
-      tm.addMenuItem(req, simpleMenuBasic);
82  
-      should.exist(tm.children[simpleMenuBasic.path])
83  
-      done();
84  
-
85  
-    });
86  
-
87  
-    it('Admin menus are invisible by default if they have no permit fn defined', function(done){
88  
-
89  
-      var tm = new Menu('admin'), req = calipsoHelper.requests.testUser;
90  
-      tm.addMenuItem(req, anonMenuBasic);
91  
-      should.not.exist(tm.children[anonMenuBasic.path])
92  
-      done();
93  
-
94  
-    });
95  
-
96  
-    it('I can add the same menu twice without adverse effects', function(done){
97  
-
98  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
99  
-
100  
-      tm.addMenuItem(req, simpleMenuBasic);
101  
-      tm.addMenuItem(req, simpleMenuBasic);
102  
-
103  
-      tm.children[simpleMenuBasic.path].name.should.equal(simpleMenuBasic.name);
104  
-      tm.children[simpleMenuBasic.path].url.should.equal(simpleMenuBasic.url);
105  
-      tm.children[simpleMenuBasic.path].path.should.equal(simpleMenuBasic.path);
106  
-
107  
-      done();
108  
-
109  
-    });
110  
-
111  
-  });
112  
-
113  
-  describe('Creating hierarchical menus', function(){
114  
-
115  
-    it('I can add a menu with a heirarchical path and get a hierarchy', function(done){
116  
-
117  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
118  
-
119  
-      tm.addMenuItem(req, simpleMenuBasic);
120  
-      tm.addMenuItem(req, childMenuShort);
121  
-
122  
-      // Check created deep heirarchy
123  
-      tm.children.simplepath.children.child.name.should.equal(childMenuShort.name);
124  
-
125  
-      // Check parent still matches parent url
126  
-     tm.children.simplepath.url.should.equal(simpleMenuBasic.url);
127  
-
128  
-     done();
129  
-
130  
-    });
131  
-
132  
-
133  
-    it('I can add a menu with an overlapping heirarchical path and get a hierarchy', function(done){
134  
-
135  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
136  
-
137  
-      tm.addMenuItem(req, simpleMenuBasic);
138  
-      tm.addMenuItem(req, childMenuShort);
139  
-      tm.addMenuItem(req, childMenuDeep);
140  
-
141  
-      // Check created deep heirarchy
142  
-      tm.children.simplepath.children.child.name.should.equal(childMenuShort.name);
143  
-      tm.children.simplepath.children.a.children.b.name.should.equal(childMenuDeep.name);
144  
-
145  
-      // Check parent has still has first child url, it is not updated by second menu
146  
-      tm.children.simplepath.url.should.equal(simpleMenuBasic.url);
147  
-      done();
148  
-
149  
-    });
150  
-
151  
-
152  
-    it('Creating menus in the wrong order is ok, they get updated', function(done){
153  
-
154  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
155  
-
156  
-      tm.addMenuItem(req, childMenuShort);
157  
-      tm.addMenuItem(req, simpleMenuBasic);
158  
-
159  
-      // Check created deep heirarchy
160  
-      tm.children.simplepath.children.child.name.should.equal(childMenuShort.name);
161  
-
162  
-      // Check that parent url is set correctly
163  
-      tm.children.simplepath.url.should.equal(simpleMenuBasic.url);
164  
-
165  
-      done();
166  
-
167 29
     });
168  
-
169  
-    it('Creating menus with an invalid sort defaults to sorting by item name', function(done){
170  
-
171  
-      var tm = new Menu('MyMenu','invalid'), req = calipsoHelper.requests.testUser;
172  
-
173  
-      // Add in reverse order to what we expect
174  
-      tm.addMenuItem(req, simpleMenuFull);
175  
-      tm.addMenuItem(req, simpleMenuBasic);
176  
-
177  
-      tm.sortedChildren.should.eql([simpleMenuBasic.path, simpleMenuFull.path])
178  
-
179  
-      done();
180  
-
181  
-    });
182  
-
183  
-     it('Creating menus with a non string sort sorts by that directly', function(done){
184  
-
185  
-      var tm = new Menu('MyMenu','order'), req = calipsoHelper.requests.testUser;
186  
-
187  
-      // Add in reverse order to what we expect
188  
-      tm.addMenuItem(req, simpleMenuFull);
189  
-      tm.addMenuItem(req, simpleMenuBasic);
190  
-
191  
-      tm.sortedChildren.should.eql([simpleMenuBasic.path, simpleMenuFull.path])
192  
-
193  
-      done();
194  
-
195  
-    });
196  
-
197  
-
198  
-
199  
-
200  
-  });
201  
-
202  
-  describe('Recursing and displaying menus', function(){
203  
-
204  
-     it('I can recursively scan the menu tree', function(done){
205  
-
206  
-      var tm = new Menu('MyMenu'), req = calipsoHelper.requests.testUser;
207  
-      tm.addMenuItem(req, simpleMenuBasic);
208  
-      tm.addMenuItem(req, childMenuShort);
209  
-      tm.addMenuItem(req, childMenuDeep);
210  
-      tm.addMenuItem(req, childMenuDeepLater);
211  
-
212  
-      var output = [];
213  
-      var menuFn = function(menu) {
214  
-          return menu.path;
215  
-      }
216  
-      tm.fnRecurse(tm,menuFn,output);
217  
-
218  
-      output.length.should.equal(5);
219  
-      output[0].should.equal(simpleMenuBasic.path);
220  
-
221  
-      done();
222  
-
  30
+  
  31
+    describe('Creating items with correct permissons', function(){
  32
+  
  33
+      it('I can create a menu, it has default sortby and is root.', function(done){
  34
+          var tm = new Menu('MyMenu');
  35
+          tm.name.should.equal('MyMenu');