gell-dispatch 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,164 @@
1
+ const assert = require('assert');
2
+ const _ = require('lodash');
3
+
4
+ const build = require('../../util/chain');
5
+
6
+ describe('function chain', function() {
7
+
8
+ describe('no functions', function() {
9
+ it('no args returns undefined', function() {
10
+ const chain = build();
11
+
12
+ const result = chain();
13
+
14
+ assert(_.isUndefined(result));
15
+ })
16
+
17
+ it('single arg returns undefined', function() {
18
+ const chain = build();
19
+
20
+ const result = chain('gello!');
21
+
22
+ assert(_.isUndefined(result));
23
+ })
24
+ })
25
+
26
+ describe('single function', function() {
27
+ let chain;
28
+
29
+ describe('noop function', function() {
30
+ beforeEach(function() {
31
+ chain = build(z => {});
32
+ })
33
+
34
+ it('no args returns undefined', function() {
35
+ const result = chain();
36
+
37
+ assert(_.isUndefined(result));
38
+ })
39
+
40
+ it('single arg returns undefined', function() {
41
+ const result = chain('gello!');
42
+
43
+ assert(_.isUndefined(result));
44
+ })
45
+ })
46
+
47
+ describe('simple function', function() {
48
+ beforeEach(function() {
49
+ chain = build(z => 'world!');
50
+ })
51
+
52
+ it('no args returns last result', function() {
53
+ const result = chain();
54
+
55
+ assert.strictEqual(result, 'world!');
56
+ })
57
+
58
+ it('single arg returns last result', function() {
59
+ const result = chain('gello!');
60
+
61
+ assert.strictEqual(result, 'world!');
62
+ })
63
+ })
64
+ })
65
+
66
+ describe('two functions', function() {
67
+ let chain, firstExecuted;
68
+
69
+ describe('noop function', function() {
70
+ beforeEach(function() {
71
+ chain = build(
72
+ z => { firstExecuted = true },
73
+ z => {}
74
+ );
75
+ })
76
+
77
+ it('chain invoked', async function() {
78
+ chain();
79
+
80
+ assert(firstExecuted);
81
+ })
82
+
83
+ it('no args returns undefined', function() {
84
+ const result = chain();
85
+
86
+ assert(_.isUndefined(result));
87
+ })
88
+
89
+ it('single arg returns undefined', function() {
90
+ const result = chain('gello!');
91
+
92
+ assert(_.isUndefined(result));
93
+ })
94
+ })
95
+
96
+ describe('simple function', function() {
97
+ beforeEach(function() {
98
+ chain = build(
99
+ (z, resume) => { firstExecuted = true; return resume(z); },
100
+ z => 'world!'
101
+ );
102
+ })
103
+
104
+ it('chain invoked', async function() {
105
+ chain();
106
+
107
+ assert(firstExecuted);
108
+ })
109
+
110
+ it('no args returns last result', function() {
111
+ const result = chain();
112
+
113
+ assert.strictEqual(result, 'world!');
114
+ })
115
+
116
+ it('single arg returns last result', function() {
117
+ const result = chain('gello!');
118
+
119
+ assert.strictEqual(result, 'world!');
120
+ })
121
+ })
122
+ })
123
+
124
+ it('building call chain', async function() {
125
+ var result = "";
126
+
127
+ async function e(arg) {
128
+ // console.debug('e', arg);
129
+
130
+ return arg + "4";
131
+ }
132
+ async function one(arg, resume) {
133
+ // console.debug('one-before', arg);
134
+
135
+ const a = arg + '1';
136
+
137
+ const b = await resume(a);
138
+
139
+ // console.debug('one-after', b);
140
+
141
+ return b + "6";
142
+ }
143
+ function two(arg, resume) {
144
+ // console.debug('two-before', arg);
145
+
146
+ result += "2";
147
+
148
+ return resume(arg + '2');
149
+ }
150
+ async function three(arg, resume) {
151
+ const a = await resume(arg);
152
+
153
+ return a + "5";
154
+ }
155
+
156
+ assert.strictEqual(await build(e)('0'), '04');
157
+ // assert.strictEqual(await build(one)('0'), '016');
158
+ assert.strictEqual(await build(two, e)('0'), '024');
159
+ assert.strictEqual(await build(one, e)('0'), '0146');
160
+ assert.strictEqual(await build(one, two, e)('0'), '01246');
161
+ assert.strictEqual(await build(one, two, three, e)('0'), '012456');
162
+ })
163
+
164
+ })
@@ -0,0 +1,33 @@
1
+ const assert = require('assert');
2
+
3
+ const { error } = require('../../util/error');
4
+
5
+ describe('error', function() {
6
+ it('creates an Error with a name and message', function() {
7
+ const e = error('RequiredParameterError', 'gello!');
8
+
9
+ assert(e instanceof Error);
10
+ assert.strictEqual(e.name, 'RequiredParameterError');
11
+ assert.strictEqual(e.message, 'gello!');
12
+ })
13
+
14
+ it('formats the message with arguments', function() {
15
+ const e = error('RequiredParameterError', 'route (name="%s") argument (name="%s")', {}, 'my.event#action', 'message');
16
+
17
+ assert.strictEqual(e.message, 'route (name="my.event#action") argument (name="message")');
18
+ })
19
+
20
+ it('assigns attributes to the error', function() {
21
+ const e = error('ForbiddenError', 'gello!', { status: 403, route: 'my.event#action' });
22
+
23
+ assert.strictEqual(e.status, 403);
24
+ assert.strictEqual(e.route, 'my.event#action');
25
+ })
26
+
27
+ it('does not let attributes replace the name or message', function() {
28
+ const e = error('ForbiddenError', 'gello!', { name: 'Other', message: 'other' });
29
+
30
+ assert.strictEqual(e.name, 'ForbiddenError');
31
+ assert.strictEqual(e.message, 'gello!');
32
+ })
33
+ })
@@ -0,0 +1,73 @@
1
+ const assert = require('assert');
2
+
3
+ const restrict = require('../../util/restrict');
4
+
5
+ describe('restrict', function() {
6
+ let module;
7
+
8
+ beforeEach(function() {
9
+ module = {
10
+ __metadata: {
11
+ name: 'unittest.event',
12
+ triggers: {
13
+ effect: {},
14
+ action: {
15
+ params: { id: 'string' },
16
+ transaction: 'NEW'
17
+ }
18
+ }
19
+ },
20
+
21
+ effect: z => 'effect',
22
+ action: z => 'action'
23
+ };
24
+ })
25
+
26
+ it('defines roles on action trigger', function() {
27
+ const restricted = restrict(module, ['administrator']);
28
+
29
+ assert.deepStrictEqual(restricted.__metadata.triggers.action, {
30
+ params: { id: 'string' },
31
+ transaction: 'NEW',
32
+ roles: ['administrator']
33
+ });
34
+ })
35
+
36
+ it('does not restrict effect trigger', function() {
37
+ const restricted = restrict(module, ['administrator']);
38
+
39
+ assert.deepStrictEqual(restricted.__metadata.triggers.effect, {});
40
+ })
41
+
42
+ it('does not add triggers the module does not implement', function() {
43
+ const restricted = restrict(module, ['administrator']);
44
+
45
+ assert(!restricted.__metadata.triggers.api);
46
+ })
47
+
48
+ it('defines trigger metadata if missing', function() {
49
+ delete module.__metadata.triggers;
50
+
51
+ const restricted = restrict(module, ['administrator']);
52
+
53
+ assert.deepStrictEqual(restricted.__metadata.triggers.action, { roles: ['administrator'] });
54
+ })
55
+
56
+ it('keeps trigger functions', function() {
57
+ const restricted = restrict(module, ['administrator']);
58
+
59
+ assert.strictEqual(restricted.action, module.action);
60
+ assert.strictEqual(restricted.effect, module.effect);
61
+ assert.strictEqual(restricted.__metadata.name, 'unittest.event');
62
+ })
63
+
64
+ it('does not modify the original module', function() {
65
+ restrict(module, ['administrator']);
66
+
67
+ assert(!module.__metadata.triggers.action.roles);
68
+ })
69
+
70
+ it('requires roles', function() {
71
+ assert.throws(z => restrict(module, []));
72
+ })
73
+ })
package/util/chain.js ADDED
@@ -0,0 +1,9 @@
1
+ function build(...m) {
2
+ if (m.length === 0) return z => {};
3
+
4
+ const first = m.shift();
5
+
6
+ return z => first(z, build(...m));
7
+ }
8
+
9
+ module.exports = build;
package/util/error.js ADDED
@@ -0,0 +1,15 @@
1
+ const { format } = require('util');
2
+
3
+ /**
4
+ * Creates an Error describing a failed event invocation
5
+ *
6
+ * @param {*} name - error name (e.g. RequiredParameterError)
7
+ * @param {*} message - printf style message (see util.format)
8
+ * @param {*} attributes - additional properties assigned to the error (e.g. route, status)
9
+ * @param {...any} args - message arguments
10
+ */
11
+ function error(name, message, attributes={}, ...args) {
12
+ return Object.assign(new Error(), attributes, { name, message: format(message, ...args) });
13
+ }
14
+
15
+ module.exports = { error };
@@ -0,0 +1,27 @@
1
+ const assert = require('assert');
2
+ const _ = require('lodash');
3
+
4
+ /**
5
+ * Returns a copy of an event/snapshot module with allowed roles defined on its externally reachable triggers
6
+ * - used to restrict framework (gell) modules, which don't define application roles, when registering them with an application
7
+ * - the original module is not modified
8
+ *
9
+ * @param {*} module event/snapshot module with __metadata
10
+ * @param {*} roles allowed roles (see gell-dispatch/middleware/roles)
11
+ * @param {*} triggers names of triggers to restrict (only those defined by the module are restricted)
12
+ */
13
+ module.exports = function restrict(module, roles, triggers=['action', 'api']) {
14
+ assert(module && module.__metadata, 'module with __metadata is required');
15
+ assert(_.isArray(roles) && roles.length > 0, 'roles are required');
16
+
17
+ const __metadata = _.cloneDeep(module.__metadata);
18
+ const definedTriggers = __metadata.triggers || {};
19
+
20
+ triggers.forEach(name => {
21
+ if (_.isFunction(module[name])) definedTriggers[name] = { ...definedTriggers[name], roles: [...roles] };
22
+ });
23
+
24
+ __metadata.triggers = definedTriggers;
25
+
26
+ return { ...module, __metadata };
27
+ }