larvitcms 1.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,141 +0,0 @@
1
- 'use strict';
2
-
3
- const async = require('async');
4
- const uuid = require('uuid');
5
- const _ = require('lodash');
6
-
7
- exports.run = function (req, res, cb) {
8
- const data = {global: res.globalData};
9
- const tasks = [];
10
- const pageUuid = res.globalData.urlParsed.query.uuid || uuid.v1();
11
-
12
- data.global.menuControllerName = 'adminCmsPages';
13
- data.cmsTemplates = require(req.lfs.getPathSync('config/cmsTemplates.json'));
14
-
15
- // Make sure the user have the correct rights
16
- // This is set in larvitadmingui controllerGlobal
17
- if (!res.adminRights) {
18
- utils.deny(res);
19
-
20
- return cb(null, req, res, null);
21
- }
22
-
23
- // Save a POSTed form
24
- if (res.globalData.formFields.save !== undefined) {
25
- // Basic form validation
26
- tasks.push(function (cb) {
27
- if (!res.globalData.formFields.name) {
28
- res.globalData.errors = ['Page name is required'];
29
-
30
- return cb(new Error('Invalid fields'));
31
- }
32
-
33
- cb();
34
- });
35
-
36
- // Save the data
37
- tasks.push(function (cb) {
38
- const saveObj = {};
39
-
40
- saveObj.uuid = pageUuid;
41
- saveObj.name = res.globalData.formFields.name;
42
- saveObj.template = res.globalData.formFields.template;
43
- saveObj.langs = {};
44
-
45
- let fieldName;
46
- let field;
47
- let lang;
48
-
49
- if (res.globalData.formFields.published) {
50
- saveObj.published = true;
51
- } else {
52
- saveObj.published = false;
53
- }
54
-
55
- for (field in res.globalData.formFields) {
56
- if (field.split('.').length === 2) {
57
- fieldName = field.split('.')[0];
58
- lang = field.split('.')[1];
59
-
60
- if (saveObj.langs[lang] === undefined) {
61
- saveObj.langs[lang] = {};
62
- }
63
-
64
- if (!res.globalData.formFields[field]) {
65
- saveObj.langs[lang][fieldName] = null;
66
- } else {
67
- if (fieldName === 'slug') {
68
- res.globalData.formFields[field] = _.trimEnd(res.globalData.formFields[field], '/');
69
- }
70
-
71
- saveObj.langs[lang][fieldName] = res.globalData.formFields[field];
72
- }
73
- }
74
- }
75
-
76
- req.cms.savePage(saveObj, function (err) {
77
- if (err) return cb(err);
78
-
79
- if (res.globalData.urlParsed.query.uuid === undefined) {
80
- req.session.data.nextCallData = {global: {messages: ['New page created with uuid ' + pageUuid]}};
81
- } else {
82
- req.session.data.nextCallData = {global: {messages: ['Saved']}};
83
- }
84
-
85
- res.statusCode = 302;
86
- res.setHeader('Location', '/adminCmsPageEdit?uuid=' + pageUuid + '&langs=' + res.globalData.urlParsed.query.langs);
87
-
88
- cb();
89
- });
90
- });
91
- }
92
-
93
- // Delete a page
94
- if (res.globalData.formFields.delete !== undefined && res.globalData.urlParsed.query.uuid !== undefined) {
95
- tasks.push(function (cb) {
96
- req.cms.rmPage(uuid, function (err) {
97
- if (err) return cb(err);
98
-
99
- req.session.data.nextCallData = {global: {messages: ['Page with Uuid ' + uuid + ' deleted']}};
100
- res.statusCode = 302;
101
- res.setHeader('Location', '/adminCmsPages');
102
- cb();
103
- });
104
- });
105
-
106
- // Load data from database
107
- } else if (res.globalData.urlParsed.query.uuid !== undefined) {
108
- tasks.push(function (cb) {
109
- req.cms.getPages({uuids: pageUuid}, function (err, rows) {
110
- let lang;
111
-
112
- if (rows[0] !== undefined) {
113
- res.globalData.formFields = {
114
- name: rows[0].name,
115
- published: rows[0].published,
116
- template: rows[0].template
117
- };
118
-
119
- for (lang in rows[0].langs) {
120
- res.globalData.formFields['htmlTitle.' + lang] = rows[0].langs[lang].htmlTitle;
121
- res.globalData.formFields['slug.' + lang] = rows[0].langs[lang].slug;
122
- res.globalData.formFields['body1.' + lang] = rows[0].langs[lang].body1;
123
- res.globalData.formFields['body2.' + lang] = rows[0].langs[lang].body2;
124
- res.globalData.formFields['body3.' + lang] = rows[0].langs[lang].body3;
125
- res.globalData.formFields['body4.' + lang] = rows[0].langs[lang].body4;
126
- res.globalData.formFields['body5.' + lang] = rows[0].langs[lang].body5;
127
- res.globalData.formFields['body6.' + lang] = rows[0].langs[lang].body6;
128
- }
129
- } else {
130
- return cb(new Error('larvitcms: controllers/adminCmsPageEdit.js - Wrong pageId supplied'));
131
- }
132
-
133
- cb();
134
- });
135
- });
136
- }
137
-
138
- async.series(tasks, function () {
139
- cb(null, req, res, data);
140
- });
141
- };
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- exports.run = function (req, res, cb) {
4
- const data = {global: res.globalData};
5
-
6
- data.global.menuControllerName = 'adminCmsPages';
7
-
8
- // Make sure the user have the correct rights
9
- // This is set in larvitadmingui controllerGlobal
10
- if (!res.adminRights) {
11
- utils.deny(res);
12
-
13
- return cb(null, req, res, null);
14
- }
15
-
16
- if (res.langs) {
17
- data.global.langs = res.langs;
18
- }
19
-
20
- req.cms.getPages({limit: false}, function (err, rows) {
21
- data.cmsPages = rows;
22
- cb(null, req, res, data);
23
- });
24
- };