-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
databasemock.js
239 lines (201 loc) · 7.49 KB
/
databasemock.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict';
/**
* Database Mock - wrapper for database.js, makes system use separate test db, instead of production
* ATTENTION: testing db is flushed before every use!
*/
require('../../require-main');
const path = require('path');
const nconf = require('nconf');
const url = require('url');
const util = require('util');
process.env.NODE_ENV = process.env.TEST_ENV || 'production';
global.env = process.env.NODE_ENV || 'production';
const winston = require('winston');
const packageInfo = require('../../package');
winston.add(new winston.transports.Console({
format: winston.format.combine(
winston.format.splat(),
winston.format.simple()
),
}));
nconf.file({ file: path.join(__dirname, '../../config.json') });
nconf.defaults({
base_dir: path.join(__dirname, '../..'),
themes_path: path.join(__dirname, '../../node_modules'),
upload_path: 'test/uploads',
views_dir: path.join(__dirname, '../../build/public/templates'),
relative_path: '',
});
const urlObject = url.parse(nconf.get('url'));
const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
nconf.set('relative_path', relativePath);
if (nconf.get('isCluster') === undefined) {
nconf.set('isPrimary', true);
nconf.set('isCluster', true);
}
const dbType = nconf.get('database');
const testDbConfig = nconf.get('test_database');
const productionDbConfig = nconf.get(dbType);
if (!testDbConfig) {
const errorText = 'test_database is not defined';
winston.info(
'\n===========================================================\n' +
'Please, add parameters for test database in config.json\n' +
'For example (redis):\n' +
'"test_database": {\n' +
' "host": "127.0.0.1",\n' +
' "port": "6379",\n' +
' "password": "",\n' +
' "database": "1"\n' +
'}\n' +
' or (mongo):\n' +
'"test_database": {\n' +
' "host": "127.0.0.1",\n' +
' "port": "27017",\n' +
' "password": "",\n' +
' "database": "1"\n' +
'}\n' +
' or (mongo) in a replicaset\n' +
'"test_database": {\n' +
' "host": "127.0.0.1,127.0.0.1,127.0.0.1",\n' +
' "port": "27017,27018,27019",\n' +
' "username": "",\n' +
' "password": "",\n' +
' "database": "nodebb_test"\n' +
'}\n' +
' or (postgres):\n' +
'"test_database": {\n' +
' "host": "127.0.0.1",\n' +
' "port": "5432",\n' +
' "username": "postgres",\n' +
' "password": "",\n' +
' "database": "nodebb_test"\n' +
'}\n' +
'==========================================================='
);
winston.error(errorText);
throw new Error(errorText);
}
if (testDbConfig.database === productionDbConfig.database &&
testDbConfig.host === productionDbConfig.host &&
testDbConfig.port === productionDbConfig.port) {
const errorText = 'test_database has the same config as production db';
winston.error(errorText);
throw new Error(errorText);
}
nconf.set(dbType, testDbConfig);
winston.info('database config %s', dbType, testDbConfig);
winston.info('environment ' + global.env);
const db = require('../../src/database');
module.exports = db;
before(async function () {
this.timeout(30000);
await db.init();
await db.createIndices();
await setupMockDefaults();
await db.initSessionStore();
const meta = require('../../src/meta');
// nconf defaults, if not set in config
if (!nconf.get('sessionKey')) {
nconf.set('sessionKey', 'express.sid');
}
// Parse out the relative_url and other goodies from the configured URL
const urlObject = url.parse(nconf.get('url'));
const relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
nconf.set('url_parsed', urlObject);
nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
nconf.set('secure', urlObject.protocol === 'https:');
nconf.set('use_port', !!urlObject.port);
nconf.set('relative_path', relativePath);
nconf.set('port', urlObject.port || nconf.get('port') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path')));
nconf.set('upload_url', '/assets/uploads');
nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
nconf.set('bcrypt_rounds', 1);
nconf.set('socket.io:origins', '*:*');
nconf.set('version', packageInfo.version);
nconf.set('runJobs', false);
nconf.set('jobsDisabled', true);
await meta.dependencies.check();
const webserver = require('../../src/webserver');
const sockets = require('../../src/socket.io');
sockets.init(webserver.server);
require('../../src/notifications').startJobs();
require('../../src/user').startJobs();
await webserver.listen();
// Iterate over all of the test suites/contexts
this.test.parent.suites.forEach(function (suite) {
// Attach an afterAll listener that resets the defaults
suite.afterAll(async function () {
await setupMockDefaults();
});
});
});
async function setupMockDefaults() {
const meta = require('../../src/meta');
await db.emptydb();
require('../../src/groups').resetCache();
require('../../src/posts/cache').reset();
require('../../src/cache').reset();
winston.info('test_database flushed');
await setupDefaultConfigs(meta);
await giveDefaultGlobalPrivileges();
await meta.configs.init();
meta.config.postDelay = 0;
meta.config.initialPostDelay = 0;
meta.config.newbiePostDelay = 0;
meta.config.autoDetectLang = 0;
await enableDefaultPlugins();
await meta.themes.set({
type: 'local',
id: 'nodebb-theme-persona',
});
const rimraf = util.promisify(require('rimraf'));
await rimraf('test/uploads');
const mkdirp = require('mkdirp');
const folders = [
'test/uploads',
'test/uploads/category',
'test/uploads/files',
'test/uploads/system',
'test/uploads/sounds',
'test/uploads/profile',
];
for (const folder of folders) {
/* eslint-disable no-await-in-loop */
await mkdirp(folder);
}
}
db.setupMockDefaults = setupMockDefaults;
async function setupDefaultConfigs(meta) {
winston.info('Populating database with default configs, if not already set...\n');
const defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json'));
defaults.eventLoopCheckEnabled = 0;
defaults.minimumPasswordStrength = 0;
await meta.configs.setOnEmpty(defaults);
}
async function giveDefaultGlobalPrivileges() {
const privileges = require('../../src/privileges');
await privileges.global.give([
'groups:chat', 'groups:upload:post:image', 'groups:signature', 'groups:search:content',
'groups:search:users', 'groups:search:tags', 'groups:local:login', 'groups:view:users',
'groups:view:tags', 'groups:view:groups',
], 'registered-users');
await privileges.global.give([
'groups:view:users', 'groups:view:tags', 'groups:view:groups',
], 'guests');
}
async function enableDefaultPlugins() {
winston.info('Enabling default plugins\n');
const testPlugins = Array.isArray(nconf.get('test_plugins')) ? nconf.get('test_plugins') : [];
const defaultEnabled = [
'nodebb-plugin-dbsearch',
'nodebb-plugin-soundpack-default',
'nodebb-widget-essentials',
].concat(testPlugins);
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
await db.sortedSetAdd('plugins:active', Object.keys(defaultEnabled), defaultEnabled);
}