-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystone.js
203 lines (172 loc) · 4.49 KB
/
keystone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var travis = !(process.argv.indexOf('--travis') === -1);
if (!travis) {
require('dotenv').load();
}
// Require keystone
var keystone = require('keystone');
var handlebars = require('express-handlebars');
var Nightwatch = require('nightwatch/lib/index.js');
var child_process = require('child_process');
var path = require('path');
var selenium = null;
var async = require('async');
var request = require('superagent');
var testing = !(process.argv.indexOf('--test') === -1);
var host = process.env.IP || 'localhost';
var port = process.env.PORT || '3000';
keystone.init({
'name': 'Students4Students',
'brand': 'Students4Students',
'less': 'public',
'static': 'public',
'static options': { maxAge: '30d' },
'favicon': 'public/favicon.ico',
'views': 'templates/views',
'view engine': 'hbs',
'custom engine': handlebars.create({
layoutsDir: 'templates/views/layouts',
partialsDir: 'templates/views/partials',
defaultLayout: 'default',
helpers: require('./templates/views/helpers')(),
extname: '.hbs',
}).engine,
'auto update': true,
'session': true,
'auth': true,
'user model': 'User',
'model prefix': 's4s',
'mongo options': { server: { keepAlive: 1 } },
});
if (keystone.get('env') === 'production') {
keystone.set('session store', 'connect-mongo');
}
keystone.import('models');
keystone.set('locals', {
_: require('underscore'),
env: keystone.get('env'),
utils: keystone.utils,
editable: keystone.content.editable,
});
keystone.set('routes', require('./routes'));
keystone.set('nav', {
posts: ['posts', 'post-categories'],
users: 'users',
people: ['oxford-committee-members', 'oxford-tutors', 'durham-committee-members', 'durham-tutors', 'bristol-committee-members', 'bristol-tutors', 'trustees'],
files: ['files', 'folders'],
faqs: ['tutor-faqs', 'school-faqs'],
applications: 'applications',
});
keystone.set('signin redirect', '/');
keystone.set('cloudinary secure', true);
if (!testing) {
keystone.start();
} else {
test();
}
function checkKeystoneReady (done) {
async.retry({
times: 10,
interval: 3000,
}, function (done, result) {
console.log('Checking if KeystoneJS ready for request');
console.log('http://' + host + ':' + port + '/keystone');
request
.get('http://' + host + ':' + port + '/keystone')
.end(done);
}, function (err, result) {
if (!err) {
console.log('tests: KeystoneJS Ready!');
done();
} else {
console.log('tests: KeystoneJS does not appear ready!');
done(err);
}
});
}
/*
On some machines, selenium fails with a timeout error when nightwatch tries to connect due to a
deadlock situation. The following is a temporary workaround that starts selenium without a pipe
from stdin until this issue is fixed in nightwatch:
https://github.com/nightwatchjs/nightwatch/issues/470
*/
function runSelenium (done) {
console.log('tests: starting selenium server in background...');
selenium = child_process.spawn('java',
[
'-jar',
path.join(__dirname, 'tests/bin/selenium-server-standalone-2.53.0.jar'),
],
{
stdio: ['ignore', 'pipe', 'pipe'],
});
var running = false;
selenium.stderr.on('data', function (buffer)
{
var line = buffer.toString();
if (line.search(/Selenium Server is up and running/g) !== -1) {
running = true;
done();
}
});
selenium.on('close', function (code) {
if (!running) {
done(new Error('Selenium exited with error code ' + code));
}
});
}
function runNightwatch (done) {
console.log('tests: starting tests...');
try {
Nightwatch.cli(function (argv) {
Nightwatch.runner(argv, function () {
console.log('tests: finished tests...');
done();
});
});
} catch (ex) {
console.error('\nThere was an error while starting the nightwatch test runner:\n\n');
process.stderr.write(ex.stack + '\n');
done('failed to run nightwatch!');
}
}
function runKeystone (cb) {
console.log('tests: starting KeystoneJS...');
keystone.start({
onMount: function () {
console.log('tests: KeystoneJS mounted Successfuly');
},
onStart: function () {
console.log('tests: KeystoneJS Started Successfully');
cb();
},
});
}
function test () {
async.series([
function (cb) {
runKeystone(cb);
},
function (cb) {
checkKeystoneReady(cb);
},
function (cb) {
runSelenium(cb);
},
function (cb) {
runNightwatch(cb);
},
], function (err) {
var exitProcess = false;
if (err) {
console.error('tests: ' + err);
exitProcess = true;
}
if (selenium) {
selenium.kill('SIGHUP');
exitProcess = true;
}
if (exitProcess) {
process.exit();
}
});
}