larvitcms 2.0.0 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc +85 -87
- package/.github/workflows/ci.yml +14 -0
- package/README.md +2 -0
- package/cms.js +82 -181
- package/dataWriter.js +60 -312
- package/dbmigration/5.js +11 -21
- package/package.json +14 -19
- package/renovate.json +2 -7
- package/test/00test.js +135 -221
- package/.travis.yml +0 -36
- package/test/98lint.js +0 -9
- package/test/99close.js +0 -7
package/test/00test.js
CHANGED
@@ -1,98 +1,75 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const Intercom = require('larvitamintercom');
|
4
3
|
const uuidLib = require('uuid');
|
5
|
-
const slugify = require('larvitslugify');
|
4
|
+
const { slugify } = require('larvitslugify');
|
6
5
|
const assert = require('assert');
|
7
|
-
const
|
8
|
-
const LUtils = require('larvitutils');
|
9
|
-
const lUtils = new LUtils();
|
10
|
-
const log = new lUtils.Log('info');
|
6
|
+
const { Utils, Log } = require('larvitutils');
|
11
7
|
const Cms = require(__dirname + '/../cms.js');
|
12
|
-
const
|
8
|
+
const Db = require('larvitdb');
|
13
9
|
const fs = require('fs');
|
14
|
-
const _ = require('lodash');
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
before(function (done) {
|
19
|
-
const tasks = [];
|
11
|
+
const lUtils = new Utils();
|
12
|
+
const log = new Log('info');
|
20
13
|
|
21
|
-
|
14
|
+
let cmsLib;
|
15
|
+
let db;
|
22
16
|
|
17
|
+
before(async () => {
|
23
18
|
// Run DB Setup
|
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
|
-
|
19
|
+
let confFile;
|
20
|
+
|
21
|
+
if (process.env.TRAVIS) {
|
22
|
+
confFile = __dirname + '/../config/db_travis.json';
|
23
|
+
} else {
|
24
|
+
confFile = __dirname + '/../config/db_test.json';
|
25
|
+
}
|
26
|
+
|
27
|
+
log.verbose('DB config file: "' + confFile + '"');
|
28
|
+
|
29
|
+
// First look for absolute path
|
30
|
+
let conf;
|
31
|
+
try {
|
32
|
+
await fs.promises.stat(confFile);
|
33
|
+
log.verbose('DB config: ' + JSON.stringify(require(confFile)));
|
34
|
+
conf = require(confFile);
|
35
|
+
// eslint-disable-next-line no-unused-vars
|
36
|
+
} catch (err) {
|
37
|
+
// Then look for this string in the config folder
|
38
|
+
confFile = __dirname + '/../config/' + confFile;
|
39
|
+
await fs.promises.stat(confFile);
|
40
|
+
log.verbose('DB config: ' + JSON.stringify(require(confFile)));
|
41
|
+
conf = require(confFile);
|
42
|
+
}
|
43
|
+
|
44
|
+
db = new Db({
|
45
|
+
...conf,
|
46
|
+
log,
|
52
47
|
});
|
53
48
|
|
54
49
|
// Check for empty db
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
if (rows.length) {
|
60
|
-
throw new Error('Database is not empty. To make a test, you must supply an empty database!');
|
61
|
-
}
|
62
|
-
|
63
|
-
cb();
|
64
|
-
});
|
65
|
-
});
|
50
|
+
const { rows } = await db.query('SHOW TABLES');
|
51
|
+
if (rows.length) {
|
52
|
+
throw new Error('Database is not empty. To make a test, you must supply an empty database!');
|
53
|
+
}
|
66
54
|
|
67
55
|
// Load lib
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
db,
|
73
|
-
log,
|
74
|
-
lUtils
|
75
|
-
});
|
76
|
-
|
77
|
-
cmsLib.ready(cb);
|
56
|
+
cmsLib = new Cms({
|
57
|
+
db,
|
58
|
+
log,
|
59
|
+
lUtils,
|
78
60
|
});
|
79
61
|
|
80
|
-
|
81
|
-
done(err);
|
82
|
-
});
|
62
|
+
await cmsLib.runDbMigrations();
|
83
63
|
});
|
84
64
|
|
85
|
-
after(
|
86
|
-
db.removeAllTables(
|
65
|
+
after(async () => {
|
66
|
+
await db.removeAllTables();
|
87
67
|
});
|
88
68
|
|
89
69
|
describe('Sanity test', function () {
|
90
|
-
it('Get pages of empty database',
|
91
|
-
cmsLib.getPages({}
|
92
|
-
|
93
|
-
assert.deepEqual(pages, []);
|
94
|
-
done();
|
95
|
-
});
|
70
|
+
it('Get pages of empty database', async () => {
|
71
|
+
const pages = await cmsLib.getPages({});
|
72
|
+
assert.deepEqual(pages, []);
|
96
73
|
});
|
97
74
|
});
|
98
75
|
|
@@ -106,14 +83,14 @@ describe('Cms page CRUD test', function () {
|
|
106
83
|
en: {
|
107
84
|
htmlTitle: 'foobar',
|
108
85
|
slug: 'bar',
|
109
|
-
body1: 'lots of foo and bars'
|
86
|
+
body1: 'lots of foo and bars',
|
110
87
|
},
|
111
88
|
sv: {
|
112
89
|
htmlTitle: 'sv_foobar',
|
113
90
|
slug: 'sv_bar',
|
114
|
-
body1: 'sv_lots of foo and bars'
|
115
|
-
}
|
116
|
-
}
|
91
|
+
body1: 'sv_lots of foo and bars',
|
92
|
+
},
|
93
|
+
},
|
117
94
|
};
|
118
95
|
|
119
96
|
const cmsPage2 = {
|
@@ -125,141 +102,89 @@ describe('Cms page CRUD test', function () {
|
|
125
102
|
en: {
|
126
103
|
htmlTitle: 'foobar2',
|
127
104
|
slug: 'bar2',
|
128
|
-
body1: 'lots of foo and bars2'
|
105
|
+
body1: 'lots of foo and bars2',
|
129
106
|
},
|
130
107
|
sv: {
|
131
108
|
htmlTitle: 'sv_foobar2',
|
132
109
|
slug: 'sv_ba??r2',
|
133
|
-
body1: 'sv_lots of foo and bars2'
|
134
|
-
}
|
135
|
-
}
|
110
|
+
body1: 'sv_lots of foo and bars2',
|
111
|
+
},
|
112
|
+
},
|
136
113
|
};
|
137
114
|
|
138
|
-
it('Create 2 new pages',
|
139
|
-
|
140
|
-
|
141
|
-
tasks.push(function (cb) {
|
142
|
-
cmsLib.savePage(cmsPage, cb);
|
143
|
-
});
|
144
|
-
|
145
|
-
tasks.push(function (cb) {
|
146
|
-
cmsLib.savePage(cmsPage2, cb);
|
147
|
-
});
|
115
|
+
it('Create 2 new pages', async () => {
|
116
|
+
await cmsLib.savePage(cmsPage);
|
117
|
+
await cmsLib.savePage(cmsPage2);
|
148
118
|
|
149
|
-
|
150
|
-
|
151
|
-
assert.strictEqual(pages.length, 2);
|
152
|
-
assert.strictEqual(err, null);
|
119
|
+
const pages = await cmsLib.getPages();
|
120
|
+
assert.strictEqual(pages.length, 2);
|
153
121
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
122
|
+
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
123
|
+
assert.strictEqual(pages[0].name, 'foo');
|
124
|
+
assert.strictEqual(Object.keys(pages[0].langs).length, 2);
|
125
|
+
assert.strictEqual(pages[0].langs.en.htmlTitle, 'foobar');
|
126
|
+
assert.strictEqual(pages[0].langs.sv.htmlTitle, 'sv_foobar');
|
159
127
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
cb();
|
167
|
-
});
|
168
|
-
});
|
169
|
-
|
170
|
-
async.series(tasks, done);
|
128
|
+
assert.strictEqual(pages[1].uuid, cmsPage2.uuid);
|
129
|
+
assert.strictEqual(pages[1].name, 'foo2');
|
130
|
+
assert.strictEqual(Object.keys(pages[1].langs).length, 2);
|
131
|
+
assert.strictEqual(pages[1].langs.en.htmlTitle, 'foobar2');
|
132
|
+
assert.strictEqual(pages[1].langs.sv.htmlTitle, 'sv_foobar2');
|
171
133
|
});
|
172
134
|
|
173
|
-
it('Get page by uuid',
|
174
|
-
cmsLib.getPages({uuids: cmsPage.uuid}
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
assert.strictEqual(page.langs.sv.htmlTitle, 'sv_foobar');
|
184
|
-
cb();
|
185
|
-
});
|
135
|
+
it('Get page by uuid', async () => {
|
136
|
+
const pages = await cmsLib.getPages({uuids: cmsPage.uuid});
|
137
|
+
const page = pages[0];
|
138
|
+
|
139
|
+
assert.strictEqual(pages.length, 1);
|
140
|
+
assert.strictEqual(page.uuid, cmsPage.uuid);
|
141
|
+
assert.strictEqual(page.name, 'foo');
|
142
|
+
assert.strictEqual(Object.keys(page.langs).length, 2);
|
143
|
+
assert.strictEqual(page.langs.en.htmlTitle, 'foobar');
|
144
|
+
assert.strictEqual(page.langs.sv.htmlTitle, 'sv_foobar');
|
186
145
|
});
|
187
146
|
|
188
|
-
it('Get pages with limit',
|
189
|
-
cmsLib.getPages({limit: 1}
|
190
|
-
|
191
|
-
assert.strictEqual(pages.length, 1);
|
192
|
-
cb();
|
193
|
-
});
|
147
|
+
it('Get pages with limit', async () => {
|
148
|
+
const pages = await cmsLib.getPages({limit: 1});
|
149
|
+
assert.strictEqual(pages.length, 1);
|
194
150
|
});
|
195
151
|
|
196
|
-
it('Get page by slug',
|
197
|
-
cmsLib.getPages({slugs: 'sv_bar'}
|
198
|
-
|
199
|
-
|
200
|
-
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
201
|
-
cb();
|
202
|
-
});
|
152
|
+
it('Get page by slug', async () => {
|
153
|
+
const pages = await cmsLib.getPages({slugs: 'sv_bar'});
|
154
|
+
assert.strictEqual(pages.length, 1);
|
155
|
+
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
203
156
|
});
|
204
157
|
|
205
|
-
it('Only get published pages',
|
206
|
-
cmsLib.getPages({published: true}
|
207
|
-
|
208
|
-
|
209
|
-
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
210
|
-
cb();
|
211
|
-
});
|
158
|
+
it('Only get published pages', async () => {
|
159
|
+
const pages = await cmsLib.getPages({published: true});
|
160
|
+
assert.strictEqual(pages.length, 1);
|
161
|
+
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
212
162
|
});
|
213
163
|
|
214
|
-
it('Get by uuid and only one lang',
|
215
|
-
cmsLib.getPages({uuids: cmsPage.uuid, langs: 'en'}
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
assert.strictEqual(Object.keys(pages[0].langs).length, 1);
|
220
|
-
cb();
|
221
|
-
});
|
164
|
+
it('Get by uuid and only one lang', async () => {
|
165
|
+
const pages = await cmsLib.getPages({uuids: cmsPage.uuid, langs: 'en'});
|
166
|
+
assert.strictEqual(pages.length, 1);
|
167
|
+
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
168
|
+
assert.strictEqual(Object.keys(pages[0].langs).length, 1);
|
222
169
|
});
|
223
170
|
|
224
|
-
it('Update cms page',
|
225
|
-
const updatePage =
|
226
|
-
const tasks = [];
|
171
|
+
it('Update cms page', async () => {
|
172
|
+
const updatePage = JSON.parse(JSON.stringify(cmsPage));
|
227
173
|
|
228
174
|
updatePage.langs.en.body1 += ' and other stuff';
|
229
175
|
|
230
|
-
|
231
|
-
cmsLib.savePage(updatePage, cb);
|
232
|
-
});
|
176
|
+
await cmsLib.savePage(updatePage);
|
233
177
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
assert.strictEqual(pages.length, 1);
|
238
|
-
assert.strictEqual(pages[0].langs.en.body1, 'lots of foo and bars and other stuff');
|
239
|
-
cb();
|
240
|
-
});
|
241
|
-
});
|
242
|
-
|
243
|
-
async.series(tasks, cb);
|
178
|
+
const pages = await cmsLib.getPages({uuids: cmsPage.uuid});
|
179
|
+
assert.strictEqual(pages.length, 1);
|
180
|
+
assert.strictEqual(pages[0].langs.en.body1, 'lots of foo and bars and other stuff');
|
244
181
|
});
|
245
182
|
|
246
|
-
it('Remove cms page',
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
});
|
252
|
-
|
253
|
-
tasks.push(function (cb) {
|
254
|
-
cmsLib.getPages(function (err, pages) {
|
255
|
-
assert.strictEqual(err, null);
|
256
|
-
assert.strictEqual(pages.length, 1);
|
257
|
-
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
258
|
-
cb();
|
259
|
-
});
|
260
|
-
});
|
261
|
-
|
262
|
-
async.series(tasks, cb);
|
183
|
+
it('Remove cms page', async () => {
|
184
|
+
await cmsLib.rmPage(cmsPage2.uuid);
|
185
|
+
const pages = await cmsLib.getPages();
|
186
|
+
assert.strictEqual(pages.length, 1);
|
187
|
+
assert.strictEqual(pages[0].uuid, cmsPage.uuid);
|
263
188
|
});
|
264
189
|
});
|
265
190
|
|
@@ -267,55 +192,44 @@ describe('Snippets CRUD', function () {
|
|
267
192
|
const snippet1 = {
|
268
193
|
body: 'body 1 en',
|
269
194
|
name: slugify('body 1 en'),
|
270
|
-
lang: 'en'
|
195
|
+
lang: 'en',
|
271
196
|
};
|
272
197
|
|
273
198
|
const snippet2 = {
|
274
199
|
body: 'body 2 sv',
|
275
200
|
name: slugify('body 2 sv'),
|
276
|
-
lang: 'sv'
|
201
|
+
lang: 'sv',
|
277
202
|
};
|
278
203
|
|
279
|
-
it('Create snippets',
|
280
|
-
|
281
|
-
|
282
|
-
tasks.push(function (cb) {
|
283
|
-
cmsLib.saveSnippet(snippet1, cb);
|
284
|
-
});
|
285
|
-
|
286
|
-
tasks.push(function (cb) {
|
287
|
-
cmsLib.saveSnippet(snippet2, cb);
|
288
|
-
});
|
204
|
+
it('Create snippets', async () => {
|
205
|
+
await cmsLib.saveSnippet(snippet1);
|
206
|
+
await cmsLib.saveSnippet(snippet2);
|
289
207
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
assert.strictEqual(snippets.length, 2);
|
294
|
-
cb();
|
295
|
-
});
|
296
|
-
});
|
208
|
+
const snippets = await cmsLib.getSnippets();
|
209
|
+
assert.strictEqual(snippets.length, 2);
|
210
|
+
});
|
297
211
|
|
298
|
-
|
212
|
+
it('Get snippet names', async () => {
|
213
|
+
const snippets = await cmsLib.getSnippets({ onlyNames: true });
|
214
|
+
assert.strictEqual(snippets.length, 2);
|
215
|
+
assert.deepStrictEqual(snippets[0], { name: slugify('body 1 en') });
|
216
|
+
assert.deepStrictEqual(snippets[1], { name: slugify('body 2 sv') });
|
299
217
|
});
|
300
218
|
|
301
|
-
it('Get snippet by name',
|
302
|
-
cmsLib.getSnippets({names: slugify('body 1 en')}
|
303
|
-
|
304
|
-
|
305
|
-
assert.strictEqual(snippets[0].langs.en, 'body 1 en');
|
306
|
-
cb();
|
307
|
-
});
|
219
|
+
it('Get snippet by name', async () => {
|
220
|
+
const snippets = await cmsLib.getSnippets({names: slugify('body 1 en')});
|
221
|
+
assert.strictEqual(snippets.length, 1);
|
222
|
+
assert.strictEqual(snippets[0].langs.en, 'body 1 en');
|
308
223
|
});
|
309
224
|
|
310
|
-
it('
|
311
|
-
|
312
|
-
|
225
|
+
it('Get snippet by no names should give no result', async () => {
|
226
|
+
const snippets = await cmsLib.getSnippets({names: []});
|
227
|
+
assert.strictEqual(snippets.length, 0);
|
228
|
+
});
|
313
229
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
});
|
319
|
-
});
|
230
|
+
it('Remove snippet by name', async () => {
|
231
|
+
await cmsLib.rmSnippet(slugify('body 1 en'));
|
232
|
+
const snippets = await cmsLib.getSnippets({names: slugify('body 1 en')});
|
233
|
+
assert.strictEqual(snippets.length, 0);
|
320
234
|
});
|
321
235
|
});
|
package/.travis.yml
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
language: node_js
|
2
|
-
|
3
|
-
env:
|
4
|
-
- CXX=g++-4.8
|
5
|
-
|
6
|
-
node_js:
|
7
|
-
- 6
|
8
|
-
- 8
|
9
|
-
- 10
|
10
|
-
- 11
|
11
|
-
|
12
|
-
notifications:
|
13
|
-
email:
|
14
|
-
- lilleman@larvit.se
|
15
|
-
|
16
|
-
before_install:
|
17
|
-
- $CXX --version
|
18
|
-
|
19
|
-
install:
|
20
|
-
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then export CXX=g++-4.8; fi
|
21
|
-
- npm i
|
22
|
-
|
23
|
-
before_script:
|
24
|
-
- mysql -e 'CREATE DATABASE test'
|
25
|
-
|
26
|
-
script: npm test
|
27
|
-
|
28
|
-
services:
|
29
|
-
- mysql
|
30
|
-
|
31
|
-
addons:
|
32
|
-
apt:
|
33
|
-
sources:
|
34
|
-
- ubuntu-toolchain-r-test
|
35
|
-
packages:
|
36
|
-
- g++-4.8
|
package/test/98lint.js
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
require('mocha-eslint')([__dirname + '/..'], {
|
4
|
-
// Increase the timeout of the test if linting takes to long
|
5
|
-
timeout: 5000, // Defaults to the global mocha `timeout` option
|
6
|
-
|
7
|
-
// Increase the time until a test is marked as slow
|
8
|
-
slow: 1000 // Defaults to the global mocha `slow` option
|
9
|
-
});
|