larvitcms 2.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 async = require('async');
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 db = require('larvitdb');
8
+ const Db = require('larvitdb');
13
9
  const fs = require('fs');
14
- const _ = require('lodash');
15
10
 
16
- let cmsLib;
17
-
18
- before(function (done) {
19
- const tasks = [];
11
+ const lUtils = new Utils();
12
+ const log = new Log('info');
20
13
 
21
- this.timeout(10000);
14
+ let cmsLib;
15
+ let db;
22
16
 
17
+ before(async () => {
23
18
  // Run DB Setup
24
- tasks.push(function (cb) {
25
- let confFile;
26
-
27
- if (process.env.TRAVIS) {
28
- confFile = __dirname + '/../config/db_travis.json';
29
- } else {
30
- confFile = __dirname + '/../config/db_test.json';
31
- }
32
-
33
- log.verbose('DB config file: "' + confFile + '"');
34
-
35
- // First look for absolute path
36
- fs.stat(confFile, function (err) {
37
- if (err) {
38
- // Then look for this string in the config folder
39
- confFile = __dirname + '/../config/' + confFile;
40
- fs.stat(confFile, function (err) {
41
- if (err) throw err;
42
- log.verbose('DB config: ' + JSON.stringify(require(confFile)));
43
- db.setup(require(confFile), cb);
44
- });
45
-
46
- return;
47
- }
48
-
49
- log.verbose('DB config: ' + JSON.stringify(require(confFile)));
50
- db.setup(require(confFile), cb);
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
- tasks.push(function (cb) {
56
- db.query('SHOW TABLES', function (err, rows) {
57
- if (err) throw err;
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
- tasks.push(function (cb) {
69
- cmsLib = new Cms({
70
- mode: 'noSync',
71
- intercom: new Intercom('loopback interface'),
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
- async.series(tasks, function (err) {
81
- done(err);
82
- });
62
+ await cmsLib.runDbMigrations();
83
63
  });
84
64
 
85
- after(function (done) {
86
- db.removeAllTables(done);
65
+ after(async () => {
66
+ await db.removeAllTables();
87
67
  });
88
68
 
89
69
  describe('Sanity test', function () {
90
- it('Get pages of empty database', function (done) {
91
- cmsLib.getPages({}, function (err, pages) {
92
- assert.strictEqual(err, null);
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', function (done) {
139
- const tasks = [];
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
- tasks.push(function (cb) {
150
- cmsLib.getPages(function (err, pages) {
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
- assert.strictEqual(pages[0].uuid, cmsPage.uuid);
155
- assert.strictEqual(pages[0].name, 'foo');
156
- assert.strictEqual(Object.keys(pages[0].langs).length, 2);
157
- assert.strictEqual(pages[0].langs.en.htmlTitle, 'foobar');
158
- assert.strictEqual(pages[0].langs.sv.htmlTitle, 'sv_foobar');
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
- assert.strictEqual(pages[1].uuid, cmsPage2.uuid);
161
- assert.strictEqual(pages[1].name, 'foo2');
162
- assert.strictEqual(Object.keys(pages[1].langs).length, 2);
163
- assert.strictEqual(pages[1].langs.en.htmlTitle, 'foobar2');
164
- assert.strictEqual(pages[1].langs.sv.htmlTitle, 'sv_foobar2');
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', function (cb) {
174
- cmsLib.getPages({uuids: cmsPage.uuid}, function (err, pages) {
175
- const page = pages[0];
176
-
177
- assert.strictEqual(pages.length, 1);
178
- assert.strictEqual(err, null);
179
- assert.strictEqual(page.uuid, cmsPage.uuid);
180
- assert.strictEqual(page.name, 'foo');
181
- assert.strictEqual(Object.keys(page.langs).length, 2);
182
- assert.strictEqual(page.langs.en.htmlTitle, 'foobar');
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', function (cb) {
189
- cmsLib.getPages({limit: 1}, function (err, pages) {
190
- assert.strictEqual(err, null);
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', function (cb) {
197
- cmsLib.getPages({slugs: 'sv_bar'}, function (err, pages) {
198
- assert.strictEqual(err, null);
199
- assert.strictEqual(pages.length, 1);
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', function (cb) {
206
- cmsLib.getPages({published: true}, function (err, pages) {
207
- assert.strictEqual(err, null);
208
- assert.strictEqual(pages.length, 1);
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', function (cb) {
215
- cmsLib.getPages({uuids: cmsPage.uuid, langs: 'en'}, function (err, pages) {
216
- assert.strictEqual(err, null);
217
- assert.strictEqual(pages.length, 1);
218
- assert.strictEqual(pages[0].uuid, cmsPage.uuid);
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', function (cb) {
225
- const updatePage = _.cloneDeep(cmsPage);
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
- tasks.push(function (cb) {
231
- cmsLib.savePage(updatePage, cb);
232
- });
176
+ await cmsLib.savePage(updatePage);
233
177
 
234
- tasks.push(function (cb) {
235
- cmsLib.getPages({uuids: cmsPage.uuid}, function (err, pages) {
236
- assert.strictEqual(err, null);
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', function (cb) {
247
- const tasks = [];
248
-
249
- tasks.push(function (cb) {
250
- cmsLib.rmPage(cmsPage2.uuid, cb);
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', function (cb) {
280
- const tasks = [];
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
- tasks.push(function (cb) {
291
- cmsLib.getSnippets(function (err, snippets) {
292
- assert.strictEqual(err, null);
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
- async.series(tasks, cb);
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', function (cb) {
302
- cmsLib.getSnippets({names: slugify('body 1 en')}, function (err, snippets) {
303
- assert.strictEqual(err, null);
304
- assert.strictEqual(snippets.length, 1);
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('Remove snippet by name', function (cb) {
311
- cmsLib.rmSnippet(slugify('body 1 en'), function (err) {
312
- if (err) throw err;
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
- cmsLib.getSnippets({names: slugify('body 1 en')}, function (err, snippets) {
315
- assert.strictEqual(err, null);
316
- assert.strictEqual(snippets.length, 0);
317
- cb();
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
- });
package/test/99close.js DELETED
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- after(function () {
4
- setTimeout(function () {
5
- process.exit();
6
- }, 1000);
7
- });