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.
- package/.mocharc.json +4 -0
- package/README.md +4 -2
- package/bus.js +169 -0
- package/middleware/arguments.js +50 -0
- package/middleware/attributes.js +38 -0
- package/middleware/roles.js +81 -0
- package/package.json +23 -4
- package/stream/sink.js +35 -0
- package/test/bootstrap.js +25 -0
- package/test/middleware/test-arguments.js +130 -0
- package/test/middleware/test-attributes.js +79 -0
- package/test/middleware/test-roles.js +176 -0
- package/test/resolver.js +26 -0
- package/test/stream/test-sink.js +112 -0
- package/test/test-bus.js +382 -0
- package/test/util/test-chain.js +164 -0
- package/test/util/test-error.js +33 -0
- package/test/util/test-restrict.js +73 -0
- package/util/chain.js +9 -0
- package/util/error.js +15 -0
- package/util/restrict.js +27 -0
package/.mocharc.json
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# gell-dispatch
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Event dispatch bus and middleware for gell.
|
|
4
|
+
|
|
5
|
+
Code is being migrated here from `gell-promote/gell-dispatch`.
|
package/bus.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
const { default: State } = require('gell-state');
|
|
5
|
+
|
|
6
|
+
const chain = require('./util/chain');
|
|
7
|
+
const { error } = require('./util/error');
|
|
8
|
+
|
|
9
|
+
const MESSAGE_MISSING_TRIGGER = 'route (name="%s") does not define trigger function (name="%s")';
|
|
10
|
+
|
|
11
|
+
function validateMiddleware(middleware) {
|
|
12
|
+
middleware.forEach(m => {
|
|
13
|
+
assert(_.isFunction(m));
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Trigger execution of an event. Result of the event is yielded
|
|
19
|
+
*
|
|
20
|
+
* NOTE: this is a rewrite of gell-session implementation
|
|
21
|
+
* - implements middleware concept now
|
|
22
|
+
*
|
|
23
|
+
* WIP: bus middleware
|
|
24
|
+
* - outstanding questions
|
|
25
|
+
* - should lookup be outside of dispatcher
|
|
26
|
+
* - middleware use cases
|
|
27
|
+
* - logging
|
|
28
|
+
* - transaction management
|
|
29
|
+
* - parameter validation
|
|
30
|
+
* - argument type wrangling
|
|
31
|
+
* - performance monitoring
|
|
32
|
+
* - audit logging
|
|
33
|
+
* - error handling
|
|
34
|
+
* - item lookups
|
|
35
|
+
* - batch persistence
|
|
36
|
+
* - rate limiting
|
|
37
|
+
* - event should be able to define their own middleware?
|
|
38
|
+
* - inject it in chain?
|
|
39
|
+
* - declarative middleware?
|
|
40
|
+
*
|
|
41
|
+
* WIP: middleware requirements
|
|
42
|
+
* - you are "wrapping" an invocation, so you must eventually call it
|
|
43
|
+
* - how to you "yield" to the chain
|
|
44
|
+
* - middleware will often need to maintain state or configuration
|
|
45
|
+
* - i.e. "batch size" or "log level"
|
|
46
|
+
* - implies we need a generator or object
|
|
47
|
+
* - testable individually
|
|
48
|
+
*
|
|
49
|
+
* WIP: declarative middleware
|
|
50
|
+
* - define functions for different touchpoints
|
|
51
|
+
* - initialize
|
|
52
|
+
* - before
|
|
53
|
+
* - after
|
|
54
|
+
* - error
|
|
55
|
+
*
|
|
56
|
+
* WIP: how to provide event catalog
|
|
57
|
+
* - unclear at this point, but this should probably not use the low level $resolver
|
|
58
|
+
* - use Directory instead
|
|
59
|
+
* - route name scheme?
|
|
60
|
+
* - for now
|
|
61
|
+
* - 'my.event#trigger'
|
|
62
|
+
* - seems like URL like names could work well
|
|
63
|
+
*
|
|
64
|
+
* WIP: implement dispatching features
|
|
65
|
+
* - validate user is authorized to make call
|
|
66
|
+
* - custom, event specific validation
|
|
67
|
+
* - for session update, validate attribute is modifiable
|
|
68
|
+
*
|
|
69
|
+
* WIP: consolidate with dispatcher implementations
|
|
70
|
+
*
|
|
71
|
+
* WIP: how to handle event State
|
|
72
|
+
* - could follow emitter pattern
|
|
73
|
+
* - node events are async tho which causes problem in Next environments
|
|
74
|
+
* - could follow custom emitter pattern
|
|
75
|
+
* - really this is just using the $bus to dispatch event effects
|
|
76
|
+
* - provide callback argument to next
|
|
77
|
+
* - middleware
|
|
78
|
+
*
|
|
79
|
+
* WIP: provide future support
|
|
80
|
+
* - options
|
|
81
|
+
* - short circuit/validation stack
|
|
82
|
+
* - trigger specific middlware
|
|
83
|
+
* - always do X for action triggers
|
|
84
|
+
* - argument to type wrangling
|
|
85
|
+
*/
|
|
86
|
+
module.exports = function*($resolver, middleware, deps) {
|
|
87
|
+
assert($resolver && _.isFunction($resolver.next), '$resolver is required');
|
|
88
|
+
assert(deps, 'deps is required');
|
|
89
|
+
|
|
90
|
+
validateMiddleware(middleware);
|
|
91
|
+
|
|
92
|
+
async function dispatch({ route, caller, source, context={}, params={}, deps, invoke=true }) {
|
|
93
|
+
assert(_.isString(route), 'event route is required');
|
|
94
|
+
|
|
95
|
+
const [eventModule, { f: triggerF }, { resource: eventName, f: triggerName }] = $resolver.next(route).value;
|
|
96
|
+
|
|
97
|
+
assert(eventName, `route (name=${route}) is invalid; missing event name`);
|
|
98
|
+
assert(triggerName && triggerName.length > 0, `route (name=${route}) is invalid; missing trigger name`);
|
|
99
|
+
assert(eventModule, `no event (route=${route})`);
|
|
100
|
+
|
|
101
|
+
const logger = deps.resolveSingle('logger');
|
|
102
|
+
|
|
103
|
+
logger.info(`dispatching event (name=${eventName}, trigger=${triggerName})`);
|
|
104
|
+
|
|
105
|
+
if (!_.isFunction(triggerF)) {
|
|
106
|
+
throw error('InvalidEventModule', MESSAGE_MISSING_TRIGGER, { route, eventName, trigger: triggerName }, eventName, triggerName);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* NOTE: each invocation gets its own dependency container, branched from the deps passed in (or the bus deps)
|
|
111
|
+
* - middleware and triggers can define invocation scoped dependencies (e.g. namespaced types) on it without
|
|
112
|
+
* affecting other invocations or the common container
|
|
113
|
+
* - a nested dispatch passing event.deps branches again, so it inherits (and can override) the outer
|
|
114
|
+
* invocation's dependencies
|
|
115
|
+
*
|
|
116
|
+
* WIP: gell-dependency builds a singleton with the container that first resolves it, so a root singleton first
|
|
117
|
+
* resolved during an invocation is built with that invocation's branch; singleton factories must not resolve
|
|
118
|
+
* (or keep) invocation scoped dependencies such as types
|
|
119
|
+
*/
|
|
120
|
+
const event = {
|
|
121
|
+
caller,
|
|
122
|
+
source,
|
|
123
|
+
params,
|
|
124
|
+
context,
|
|
125
|
+
deps: deps.branch()
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* NOTE: invocation spec is always provided so middleware can identify the event being invoked
|
|
130
|
+
* - triggerSpec is empty if the event module does not define metadata for the trigger
|
|
131
|
+
* - metadata is the event module's __metadata (empty if it does not define any)
|
|
132
|
+
*/
|
|
133
|
+
const { __metadata:md } = eventModule;
|
|
134
|
+
const { triggers={} } = md || {};
|
|
135
|
+
|
|
136
|
+
event.__invocationSpec = {
|
|
137
|
+
route,
|
|
138
|
+
eventName,
|
|
139
|
+
triggerName,
|
|
140
|
+
triggerSpec: triggers[triggerName] || {},
|
|
141
|
+
metadata: md || {},
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const chainFunctions = [...middleware];
|
|
145
|
+
|
|
146
|
+
if (invoke) chainFunctions.push(triggerF);
|
|
147
|
+
else chainFunctions.push(event => event);
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
return chain(...chainFunctions)(event);
|
|
151
|
+
} catch (ex) {
|
|
152
|
+
logger.error(`middleware threw an error`);
|
|
153
|
+
|
|
154
|
+
throw ex;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let args = yield new State();
|
|
159
|
+
|
|
160
|
+
while (true) {
|
|
161
|
+
const defaults = {
|
|
162
|
+
deps
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const t = Object.assign(defaults, _.isString(args) ? { route: args } : (args || {}));
|
|
166
|
+
|
|
167
|
+
args = yield dispatch(t);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
const { error } = require('../util/error');
|
|
4
|
+
|
|
5
|
+
const MESSAGE_REQUIRED_PARAMETER = 'call to event trigger (route="%s") does not provide required argument (name="%s")';
|
|
6
|
+
const MESSAGE_PARAMETER_MAXLENGTH = 'call to event trigger (route="%s") has argument (name="%s") that exceeds maximum allowed length';
|
|
7
|
+
const MESSAGE_PARAMETER_TYPE = 'call to event trigger (route="%s") has argument (name="%s") that is not of type (type="%s")';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* gell-dispatch middleware that validates event invocation parameters against the event metadata
|
|
11
|
+
*
|
|
12
|
+
* WIP: use gell-domain here to validate this correctly
|
|
13
|
+
*
|
|
14
|
+
* WIP: this isn't yet re-usable because of the error message
|
|
15
|
+
* - one approach would be to collect validation failures and throw at the end
|
|
16
|
+
* - short circuit
|
|
17
|
+
*/
|
|
18
|
+
module.exports = function(event, resume) {
|
|
19
|
+
const { params: values, __invocationSpec } = event;
|
|
20
|
+
|
|
21
|
+
if (!__invocationSpec) return resume(event);
|
|
22
|
+
|
|
23
|
+
const metadata = __invocationSpec.triggerSpec.params || {};
|
|
24
|
+
const route = __invocationSpec.route;
|
|
25
|
+
|
|
26
|
+
const keys = Object.keys(metadata || {});
|
|
27
|
+
keys.forEach(n => {
|
|
28
|
+
const param = metadata[n];
|
|
29
|
+
const val = values[n];
|
|
30
|
+
|
|
31
|
+
if (val === undefined && !param.optional) {
|
|
32
|
+
throw error('RequiredParameterError', MESSAGE_REQUIRED_PARAMETER, { route, parameter: n }, route, n);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// NOTE: omitted optional parameters are not type checked
|
|
36
|
+
if (val === undefined) return;
|
|
37
|
+
|
|
38
|
+
if (param.type === 'string') {
|
|
39
|
+
if (!_.isString(val)) {
|
|
40
|
+
throw error('ParameterValidationError', MESSAGE_PARAMETER_TYPE, { route, parameter: n, type: param.type }, route, n, param.type);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (param.maxLength && val.length > param.maxLength) {
|
|
44
|
+
throw error('ParameterValidationError', MESSAGE_PARAMETER_MAXLENGTH, { route, parameter: n }, route, n);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
return resume(event);
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { error } = require('../util/error');
|
|
2
|
+
|
|
3
|
+
const MESSAGE_REQUIRED_ATTRIBUTE = 'call to event trigger (route="%s") does not provide required context attribute (name="%s")';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* gell-dispatch middleware that validates event invocation context attributes against the event metadata
|
|
7
|
+
*
|
|
8
|
+
* WIP: use gell-domain here to validate this correctly
|
|
9
|
+
*
|
|
10
|
+
* WIP: this isn't yet re-usable because of the error message
|
|
11
|
+
* - one approach would be to collect validation failures and throw at the end
|
|
12
|
+
* - short circuit
|
|
13
|
+
*/
|
|
14
|
+
module.exports = function(event, resume) {
|
|
15
|
+
const { context: values, __invocationSpec } = event;
|
|
16
|
+
|
|
17
|
+
if (!__invocationSpec) return resume(event);
|
|
18
|
+
|
|
19
|
+
const metadata = __invocationSpec.triggerSpec.context || {};
|
|
20
|
+
const route = __invocationSpec.route;
|
|
21
|
+
|
|
22
|
+
const keys = Object.keys(metadata || {});
|
|
23
|
+
keys.forEach(n => {
|
|
24
|
+
const attr = metadata[n];
|
|
25
|
+
const val = values[n];
|
|
26
|
+
|
|
27
|
+
if (val === undefined && !attr.optional) {
|
|
28
|
+
/**
|
|
29
|
+
* WIP: idea here is that we would materialize "error" State
|
|
30
|
+
* - include metadata about the attribute being defined
|
|
31
|
+
* - possibly include the invalid attribute value
|
|
32
|
+
*/
|
|
33
|
+
throw error('RequiredAttributeError', MESSAGE_REQUIRED_ATTRIBUTE, { route, attribute: n }, route, n);
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
return resume(event);
|
|
38
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
const { error } = require('../util/error');
|
|
5
|
+
|
|
6
|
+
const MESSAGE_UNAUTHORIZED = 'call to event trigger (route="%s") requires an authorized caller';
|
|
7
|
+
const MESSAGE_FORBIDDEN = 'call to event trigger (route="%s") is not allowed for caller (roles="%s")';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns the roles of a caller
|
|
11
|
+
* - supports a single "role" (e.g. browser session) or a list of "roles" (e.g. USER)
|
|
12
|
+
*/
|
|
13
|
+
function callerRoles(caller) {
|
|
14
|
+
if (!caller) return [];
|
|
15
|
+
|
|
16
|
+
const attribute = name => _.isFunction(caller.snapshotAttribute) ? caller.snapshotAttribute(name) : caller[name];
|
|
17
|
+
|
|
18
|
+
const roles = attribute('roles');
|
|
19
|
+
if (_.isArray(roles)) return roles;
|
|
20
|
+
|
|
21
|
+
const role = attribute('role');
|
|
22
|
+
|
|
23
|
+
return role ? [role] : [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function throwError(name, status, route, message, roles) {
|
|
27
|
+
const attributes = { status, route };
|
|
28
|
+
const args = [route];
|
|
29
|
+
|
|
30
|
+
if (roles) {
|
|
31
|
+
attributes.roles = roles.join(',');
|
|
32
|
+
args.push(attributes.roles);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw error(name, message, attributes, ...args);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* gell-dispatch middleware that authorizes callers based on the roles allowed by the event trigger metadata
|
|
40
|
+
* - allowed roles are defined by the "roles" attribute of the trigger (e.g. __metadata.triggers.action.roles)
|
|
41
|
+
* - only checks triggers reachable by external callers (default: action, api)
|
|
42
|
+
* - triggers declaring "public: true" allow any caller, including anonymous ones
|
|
43
|
+
* - the event is responsible for authorizing the call itself (e.g. by validating a token param)
|
|
44
|
+
* - caller without a role is rejected (401)
|
|
45
|
+
* - triggers without "roles" allow any caller with a role
|
|
46
|
+
* - caller without an allowed role is rejected (403)
|
|
47
|
+
*
|
|
48
|
+
* WIP: supports roles only; authorization based on the caller's relationship to the data (e.g. "own" DRIVER) is up to the event
|
|
49
|
+
*
|
|
50
|
+
* @param {*} options
|
|
51
|
+
* - triggers: names of triggers to authorize
|
|
52
|
+
* - rolesOf: function returning the roles of a caller
|
|
53
|
+
*/
|
|
54
|
+
module.exports = function({ triggers=['action', 'api'], rolesOf=callerRoles }={}) {
|
|
55
|
+
assert(_.isArray(triggers), 'triggers must be an array');
|
|
56
|
+
assert(_.isFunction(rolesOf), 'rolesOf must be a function');
|
|
57
|
+
|
|
58
|
+
return function(event, resume) {
|
|
59
|
+
const { caller, __invocationSpec } = event;
|
|
60
|
+
|
|
61
|
+
assert(__invocationSpec, 'invocation spec is required');
|
|
62
|
+
|
|
63
|
+
const { route, triggerName, triggerSpec } = __invocationSpec;
|
|
64
|
+
|
|
65
|
+
if (!triggers.includes(triggerName)) return resume(event);
|
|
66
|
+
|
|
67
|
+
if (triggerSpec.public === true) return resume(event);
|
|
68
|
+
|
|
69
|
+
const roles = rolesOf(caller);
|
|
70
|
+
|
|
71
|
+
if (roles.length === 0) throwError('UnauthorizedError', 401, route, MESSAGE_UNAUTHORIZED);
|
|
72
|
+
|
|
73
|
+
const allowed = triggerSpec.roles;
|
|
74
|
+
|
|
75
|
+
if (allowed && !roles.some(r => allowed.includes(r))) throwError('ForbiddenError', 403, route, MESSAGE_FORBIDDEN, roles);
|
|
76
|
+
|
|
77
|
+
return resume(event);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports.callerRoles = callerRoles;
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gell-dispatch",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Event dispatch bus and middleware for gell",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "mocha"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"gell"
|
|
10
|
+
],
|
|
11
|
+
"author": "Daniel Gay",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"gell-dependency": "^1.0.0",
|
|
18
|
+
"gell-future": "^1.0.0",
|
|
19
|
+
"mocha": "^8.4.0"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"gell-state": "^1.0.0",
|
|
23
|
+
"lodash": "^4.18.1"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/stream/sink.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream sink that invokes an event effect. Event trigger is passed as arguments to iterator
|
|
3
|
+
*
|
|
4
|
+
* @param {*} event
|
|
5
|
+
* @param {*} deps
|
|
6
|
+
*/
|
|
7
|
+
exports.eventSink = function* (event, deps) {
|
|
8
|
+
const { logger, store } = deps.resolve('logger', 'store');
|
|
9
|
+
|
|
10
|
+
const { __metadata } = event;
|
|
11
|
+
|
|
12
|
+
async function invokeEvent(args={}) {
|
|
13
|
+
logger.info(`invoking event (name=${__metadata.name})`);
|
|
14
|
+
|
|
15
|
+
const $txn = store.write();
|
|
16
|
+
|
|
17
|
+
const { context={} } = args;
|
|
18
|
+
|
|
19
|
+
context.$txn = $txn;
|
|
20
|
+
context.deps = deps;
|
|
21
|
+
|
|
22
|
+
const ret_ = await event.effect({
|
|
23
|
+
context,
|
|
24
|
+
deps
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await $txn.return().value;
|
|
28
|
+
|
|
29
|
+
return ret_;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let args = yield;
|
|
33
|
+
|
|
34
|
+
while (true) args = yield invokeEvent(args);
|
|
35
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const { default: DependencyContainer } = require('gell-dependency/container');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Logger that records messages instead of writing them
|
|
5
|
+
*/
|
|
6
|
+
function testLogger() {
|
|
7
|
+
const messages = [];
|
|
8
|
+
const log = level => message => messages.push({ level, message });
|
|
9
|
+
|
|
10
|
+
return { messages, debug: log('debug'), info: log('info'), warn: log('warn'), error: log('error') };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function setupContext() {
|
|
14
|
+
this.logger = testLogger();
|
|
15
|
+
|
|
16
|
+
this.deps = new DependencyContainer({
|
|
17
|
+
logger: this.logger
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.mochaHooks = {
|
|
22
|
+
beforeEach: [
|
|
23
|
+
setupContext
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
|
|
4
|
+
const middleware = require('../../middleware/arguments');
|
|
5
|
+
|
|
6
|
+
describe('arguments middleware', function() {
|
|
7
|
+
let event, resume, resumed;
|
|
8
|
+
|
|
9
|
+
beforeEach(function() {
|
|
10
|
+
event = {
|
|
11
|
+
__invocationSpec: {
|
|
12
|
+
route: 'unittest.event',
|
|
13
|
+
triggerSpec: {
|
|
14
|
+
params: {}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
params: {}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
resume = function() {
|
|
21
|
+
resumed = true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
resumed = false;
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('no parameters', function() {
|
|
28
|
+
beforeEach(function() {
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('does not fail', async function() {
|
|
32
|
+
await middleware(event, resume);
|
|
33
|
+
|
|
34
|
+
assert(resumed);
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('string parameter', function() {
|
|
39
|
+
beforeEach(function() {
|
|
40
|
+
event.__invocationSpec.triggerSpec.params.message = {
|
|
41
|
+
type: 'string'
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('fails if argument is not a string', async function() {
|
|
46
|
+
event.params.message = 7;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
await middleware(event, resume);
|
|
50
|
+
|
|
51
|
+
assert.fail('should have thrown exception');
|
|
52
|
+
} catch (e) {
|
|
53
|
+
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
54
|
+
assert.strictEqual(e.parameter, 'message');
|
|
55
|
+
assert.strictEqual(e.message, 'call to event trigger (route="unittest.event") has argument (name="message") that is not of type (type="string")');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
assert(!resumed);
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('fails if required argument is not provided', async function() {
|
|
62
|
+
try {
|
|
63
|
+
await middleware(event, resume);
|
|
64
|
+
|
|
65
|
+
assert.fail('should have thrown exception');
|
|
66
|
+
} catch (e) {
|
|
67
|
+
assert.strictEqual(e.name, 'RequiredParameterError');
|
|
68
|
+
assert.strictEqual(e.route, 'unittest.event');
|
|
69
|
+
assert.strictEqual(e.parameter, 'message');
|
|
70
|
+
assert.strictEqual(e.message, 'call to event trigger (route="unittest.event") does not provide required argument (name="message")');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
assert(!resumed);
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('optional', function() {
|
|
77
|
+
beforeEach(function() {
|
|
78
|
+
event.__invocationSpec.triggerSpec.params.message.optional = true;
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('does not fail if argument is not provided', async function() {
|
|
82
|
+
await middleware(event, resume);
|
|
83
|
+
|
|
84
|
+
assert(resumed);
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('does not fail if argument is provided', async function() {
|
|
88
|
+
event.params.message = 'gello';
|
|
89
|
+
|
|
90
|
+
await middleware(event, resume);
|
|
91
|
+
|
|
92
|
+
assert(resumed);
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('fails if argument is not a string', async function() {
|
|
96
|
+
event.params.message = 7;
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
await middleware(event, resume);
|
|
100
|
+
|
|
101
|
+
assert.fail('should have thrown exception');
|
|
102
|
+
} catch (e) {
|
|
103
|
+
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
assert(!resumed);
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('max length', function() {
|
|
111
|
+
beforeEach(function() {
|
|
112
|
+
event.__invocationSpec.triggerSpec.params.message.maxLength = 10;
|
|
113
|
+
event.params.message = 'gello world!';
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('fails if argument is too long', async function() {
|
|
117
|
+
try {
|
|
118
|
+
await middleware(event, resume);
|
|
119
|
+
|
|
120
|
+
assert.fail('should have thrown exception');
|
|
121
|
+
} catch (e) {
|
|
122
|
+
assert.strictEqual(e.name, 'ParameterValidationError');
|
|
123
|
+
assert.strictEqual(e.message, 'call to event trigger (route="unittest.event") has argument (name="message") that exceeds maximum allowed length');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
assert(!resumed);
|
|
127
|
+
})
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
})
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
|
|
3
|
+
const middleware = require('../../middleware/attributes');
|
|
4
|
+
|
|
5
|
+
describe('attributes middleware', function() {
|
|
6
|
+
let event, resumed;
|
|
7
|
+
|
|
8
|
+
function resume() {
|
|
9
|
+
resumed = true;
|
|
10
|
+
|
|
11
|
+
return 'resumed';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
beforeEach(function() {
|
|
15
|
+
event = {
|
|
16
|
+
__invocationSpec: {
|
|
17
|
+
route: 'unittest.event#effect',
|
|
18
|
+
triggerSpec: {
|
|
19
|
+
context: {}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
context: {}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
resumed = false;
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('resumes if the trigger does not define context attributes', function() {
|
|
29
|
+
delete event.__invocationSpec.triggerSpec.context;
|
|
30
|
+
|
|
31
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
32
|
+
assert(resumed);
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('resumes if there is no invocation spec', function() {
|
|
36
|
+
delete event.__invocationSpec;
|
|
37
|
+
|
|
38
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
describe('required attribute', function() {
|
|
42
|
+
beforeEach(function() {
|
|
43
|
+
event.__invocationSpec.triggerSpec.context.session$ = 'complex';
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('fails if attribute is not provided', function() {
|
|
47
|
+
assert.throws(z => middleware(event, resume), {
|
|
48
|
+
name: 'RequiredAttributeError',
|
|
49
|
+
route: 'unittest.event#effect',
|
|
50
|
+
attribute: 'session$',
|
|
51
|
+
message: 'call to event trigger (route="unittest.event#effect") does not provide required context attribute (name="session$")'
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
assert(!resumed);
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('resumes if attribute is provided', function() {
|
|
58
|
+
event.context.session$ = {};
|
|
59
|
+
|
|
60
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('accepts falsy attribute values', function() {
|
|
64
|
+
event.context.session$ = null;
|
|
65
|
+
|
|
66
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('optional attribute', function() {
|
|
71
|
+
beforeEach(function() {
|
|
72
|
+
event.__invocationSpec.triggerSpec.context.user$ = { type: 'complex', optional: true };
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('resumes if attribute is not provided', function() {
|
|
76
|
+
assert.strictEqual(middleware(event, resume), 'resumed');
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
})
|