ti2 1.0.7 → 1.0.11

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/utils.js CHANGED
@@ -1,111 +1,110 @@
1
+ /* global afterAll */
2
+
1
3
  const chance = require('chance').Chance();
2
4
  const request = require('supertest');
3
5
  const assert = require('assert');
4
-
5
- const app = require('../index');
6
- const pluginMock = require('../controllers/__mocks__/plugin');
6
+ const appReq = require('../index');
7
+ const Plugin = require('./plugin');
8
+ const slugify = require('./slugify');
9
+ const sqldb = require('../models/db');
7
10
 
8
11
  const { env: { adminKey } } = process;
9
12
 
10
13
  const timeout = ms => (new Promise(resolve => setTimeout(resolve, ms)));
11
14
 
12
- const getRequireMocks = ({ jest, app: { name } }) => {
13
- // create the mock dependencies
14
- const mock = jest.mock(`ti2-${name}`, () => ({
15
- ...pluginMock(),
16
- }), { virtual: true });
17
- return require(`ti2-${name}`);
18
- };
19
-
20
- // source : https://gist.github.com/codeguy/6684588
21
- const slugify = strParam => {
22
- let str = strParam.replace(/^\s+|\s+$/g, ''); // trim
23
- str = str.toLowerCase();
24
-
25
- // remove accents, swap ñ for n, etc
26
- const from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;';
27
- const to = 'aaaaeeeeiiiioooouuuunc------';
28
- for (let i = 0, l = from.length; i < l; i += 1) {
29
- str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
30
- }
15
+ afterAll(async () => {
16
+ await sqldb.connectionManager.close();
17
+ });
18
+ module.exports = (appParams = {}) => {
19
+ const plugins = (() => {
20
+ if (Array.isArray(appParams.plugins)) {
21
+ const retVal = {};
22
+ appParams.plugins.forEach(pluginName => {
23
+ retVal[pluginName] = Plugin;
24
+ });
25
+ return retVal;
26
+ }
27
+ return {
28
+ mockName: Plugin,
29
+ };
30
+ })();
31
+ const app = appReq({
32
+ startServer: false,
33
+ ...appParams,
34
+ plugins,
35
+ });
31
36
 
32
- str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
33
- .replace(/\s+/g, '-') // collapse whitespace and replace by -
34
- .replace(/-+/g, '-'); // collapse dashes
35
-
36
- return str;
37
- };
38
-
39
- const appSetup = async ({ userId: userIdParam } = {}) => {
40
- const userId = userIdParam || chance.guid();
41
- const appName1 = slugify(
42
- chance.company(),
43
- ).toLowerCase();
44
- const newApp = {
45
- name: appName1,
46
- packageName: `ti2-${appName1}`,
47
- adminEmail: chance.email(),
48
- };
49
- const apiKey1 = chance.guid();
50
- const token = {
51
- endpoint: chance.url(),
52
- apiKey: apiKey1,
53
- };
54
- const { body: { value: appKey } } = await request(app)
55
- .post('/app')
56
- .set('Authorization', `Bearer ${adminKey}`)
57
- .send(newApp);
58
- await request(app)
59
- .post(`/${newApp.name}/${userId}`)
60
- .set('Authorization', `Bearer ${appKey}`)
61
- .send({
62
- tokenHint: token.apiKey.split('-')[0],
63
- token,
64
- });
65
- return {
66
- newApp,
37
+ const doApi = async ({
38
+ query,
39
+ verb,
40
+ payload,
41
+ url,
67
42
  token,
68
- appKey,
69
- userId,
43
+ expectStatusCode = 200,
44
+ rawResponse = false,
45
+ }) => {
46
+ let resp;
47
+ if (token) {
48
+ resp = await request(app)[verb](url)
49
+ .set({ Authorization: `Bearer ${token}` })
50
+ .query(query)
51
+ .send(payload);
52
+ } else {
53
+ resp = await request(app)[verb](url).query(query).send(payload);
54
+ }
55
+ // if (resp.statusCode !== 200) assert.strictEqual(resp, `${url}-${expectStatusCode}`);
56
+ assert.strictEqual(`${url}-${resp.statusCode}`, `${url}-${expectStatusCode}`);
57
+ if (rawResponse) return resp;
58
+ return resp.body;
70
59
  };
71
- };
72
60
 
73
- const doApi = async ({
74
- query,
75
- verb,
76
- payload,
77
- url,
78
- token,
79
- expectStatusCode = 200,
80
- rawResponse = false,
81
- }) => {
82
- let resp;
83
- if (token) {
84
- resp = await request(app)[verb](url)
85
- .set({ Authorization: `Bearer ${token}` })
86
- .query(query)
87
- .send(payload);
88
- } else {
89
- resp = await request(app)[verb](url).query(query).send(payload);
90
- }
91
- // if (resp.statusCode !== 200) assert.strictEqual(resp, `${url}-${expectStatusCode}`);
92
- assert.strictEqual(`${url}-${resp.statusCode}`, `${url}-${expectStatusCode}`);
93
- if (rawResponse) return resp;
94
- return resp.body;
95
- };
61
+ const doApiGet = params => doApi({ ...params, verb: 'get' });
62
+ const doApiPost = params => doApi({ ...params, verb: 'post' });
63
+ const doApiPut = params => doApi({ ...params, verb: 'put' });
64
+ const doApiDelete = params => doApi({ ...params, verb: 'delete' });
96
65
 
97
- const doApiGet = params => doApi({ ...params, verb: 'get' });
98
- const doApiPost = params => doApi({ ...params, verb: 'post' });
99
- const doApiPut = params => doApi({ ...params, verb: 'put' });
100
- const doApiDelete = params => doApi({ ...params, verb: 'delete' });
66
+ const appSetup = async ({ userId: userIdParam } = {}) => {
67
+ const userId = userIdParam || chance.guid();
68
+ const appName1 = slugify(
69
+ chance.company(),
70
+ ).toLowerCase();
71
+ const newApp = {
72
+ name: appName1,
73
+ packageName: `ti2-${appName1}`,
74
+ adminEmail: chance.email(),
75
+ };
76
+ const apiKey1 = chance.guid();
77
+ const token = {
78
+ endpoint: chance.url(),
79
+ apiKey: apiKey1,
80
+ };
81
+ const { body: { value: appKey } } = await request(app)
82
+ .post('/app')
83
+ .set('Authorization', `Bearer ${adminKey}`)
84
+ .send(newApp);
85
+ await request(app)
86
+ .post(`/${newApp.name}/${userId}`)
87
+ .set('Authorization', `Bearer ${appKey}`)
88
+ .send({
89
+ tokenHint: token.apiKey.split('-')[0],
90
+ token,
91
+ });
92
+ return {
93
+ newApp,
94
+ token,
95
+ appKey,
96
+ userId,
97
+ };
98
+ };
101
99
 
102
- module.exports = {
103
- appSetup,
104
- doApiDelete,
105
- doApiGet,
106
- doApiPost,
107
- doApiPut,
108
- getRequireMocks,
109
- slugify,
110
- timeout,
100
+ return {
101
+ appSetup,
102
+ doApiDelete,
103
+ doApiGet,
104
+ doApiPost,
105
+ doApiPut,
106
+ slugify,
107
+ timeout,
108
+ plugins: app.plugins,
109
+ };
111
110
  };