123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- var assert = require('assert');
- var util = require('util');
- var _ = require('@sailshq/lodash');
- var M = require('../lib/Machine.constructor');
- // ██╗ ██╗ █████╗ ██████╗ ███╗ ██╗██╗███╗ ██╗ ██████╗
- // ▄ ██╗▄▄ ██╗▄██║ ██║██╔══██╗██╔══██╗████╗ ██║██║████╗ ██║██╔════╝ ▄ ██╗▄▄ ██╗▄
- // ████╗ ████╗██║ █╗ ██║███████║██████╔╝██╔██╗ ██║██║██╔██╗ ██║██║ ███╗ ████╗ ████╗
- // ▀╚██╔▀▀╚██╔▀██║███╗██║██╔══██║██╔══██╗██║╚██╗██║██║██║╚██╗██║██║ ██║▀╚██╔▀▀╚██╔▀
- // ╚═╝ ╚═╝ ╚███╔███╔╝██║ ██║██║ ██║██║ ╚████║██║██║ ╚████║╚██████╔╝ ╚═╝ ╚═╝
- // ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═══╝ ╚═════╝
- //
- // █████╗ ██╗ ██╗ ██╗ ██╗███████╗███████╗██████╗ ██╗ █████╗ ███╗ ██╗██████╗
- // ██╔══██╗██║ ██║ ██║ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔══██╗████╗ ██║██╔══██╗
- // ███████║██║ ██║ ██║ ██║███████╗█████╗ ██████╔╝██║ ███████║██╔██╗ ██║██║ ██║
- // ██╔══██║██║ ██║ ██║ ██║╚════██║██╔══╝ ██╔══██╗██║ ██╔══██║██║╚██╗██║██║ ██║
- // ██║ ██║███████╗███████╗ ╚██████╔╝███████║███████╗██║ ██║███████╗██║ ██║██║ ╚████║██████╔╝
- // ╚═╝ ╚═╝╚══════╝╚══════╝ ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝
- //
- // ████████╗██╗ ██╗██████╗ ███████╗ ███████╗ █████╗ ███████╗███████╗████████╗██╗ ██╗
- // ╚══██╔══╝╚██╗ ██╔╝██╔══██╗██╔════╝ ██╔════╝██╔══██╗██╔════╝██╔════╝╚══██╔══╝╚██╗ ██╔╝
- // ██║ ╚████╔╝ ██████╔╝█████╗█████╗███████╗███████║█████╗ █████╗ ██║ ╚████╔╝
- // ██║ ╚██╔╝ ██╔═══╝ ██╔══╝╚════╝╚════██║██╔══██║██╔══╝ ██╔══╝ ██║ ╚██╔╝
- // ██║ ██║ ██║ ███████╗ ███████║██║ ██║██║ ███████╗ ██║ ██║
- // ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═╝ ╚═╝
- //
- // ██████╗ ██████╗ ███╗ ██╗████████╗██████╗ ██████╗ ██╗ ███████╗ █████╗ ██████╗ ███████╗
- // ██╔════╝██╔═══██╗████╗ ██║╚══██╔══╝██╔══██╗██╔═══██╗██║ ██╔════╝ ██╔══██╗██╔══██╗██╔════╝
- // ██║ ██║ ██║██╔██╗ ██║ ██║ ██████╔╝██║ ██║██║ ███████╗ ███████║██████╔╝█████╗
- // ██║ ██║ ██║██║╚██╗██║ ██║ ██╔══██╗██║ ██║██║ ╚════██║ ██╔══██║██╔══██╗██╔══╝
- // ╚██████╗╚██████╔╝██║ ╚████║ ██║ ██║ ██║╚██████╔╝███████╗███████║ ██║ ██║██║ ██║███████╗
- // ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
- //
- // ███████╗██╗ ██╗██████╗ ███████╗██████╗ ██╗███╗ ███╗███████╗███╗ ██╗████████╗ █████╗ ██╗ ██╗██╗
- // ██╔════╝╚██╗██╔╝██╔══██╗██╔════╝██╔══██╗██║████╗ ████║██╔════╝████╗ ██║╚══██╔══╝██╔══██╗██║ ██║██║
- // █████╗ ╚███╔╝ ██████╔╝█████╗ ██████╔╝██║██╔████╔██║█████╗ ██╔██╗ ██║ ██║ ███████║██║ ██║██║
- // ██╔══╝ ██╔██╗ ██╔═══╝ ██╔══╝ ██╔══██╗██║██║╚██╔╝██║██╔══╝ ██║╚██╗██║ ██║ ██╔══██║██║ ╚═╝╚═╝
- // ███████╗██╔╝ ██╗██║ ███████╗██║ ██║██║██║ ╚═╝ ██║███████╗██║ ╚████║ ██║ ██║ ██║███████╗██╗██╗
- // ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝╚═╝
- //
- describe('Unsafe mode', function() {
- it('should not coerce input data into proper types', function(done) {
- var _inputs;
- var sampleMachineDef = {
- inputs: {
- foo: {
- example: 100
- },
- bar: {
- example: 'foobar'
- }
- },
- exits: {
- success: {
- example: 'hello'
- },
- error: {
- example: 'world'
- }
- },
- fn: function (inputs, exits, deps) {
- _inputs = inputs;
- exits(null, 'foo');
- }
- };
- try {
- var live = M.build(sampleMachineDef)
- .configure({
- foo: '20',
- bar: 20
- });
- // Set experimental flags:
- live._unsafeMode = true;
- live._runTimeTypeCheck = false;
- live._inputCoercion = false;
- live._exitCoercion = false;
- live.exec(function(err, result) {
- if(err) { return done(err); }
- assert.strictEqual(_inputs.foo,'20');
- assert.strictEqual(_inputs.bar,20);
- done();
- });
- }
- catch (e){
- assert(false, 'Should not throw');
- }
- });
- it('should not fail if input expects string, but empty object ({}) is provided', function(done) {
- testInputConfiguration({
- example: 'asdf',
- actual: {}
- }, function (err, result){
- if (!err) {
- return done();
- }
- return done(new Error('Unexpected error: ', err));
- });
- });
- it('should fail if input expects string, but empty array ([]) is provided', function(done) {
- testInputConfiguration({
- example: 'asdf',
- actual: []
- }, function (err, result){
- if (!err) {
- return done();
- }
- return done(new Error('Unexpected error: ', err));
- });
- });
- it('should fail if input expects string, but `{foo:"bar"}` is provided', function(done) {
- testInputConfiguration({
- example: 'asdf',
- actual: {foo: 'bar'}
- }, function (err, result){
- if (!err) {
- return done();
- }
- return done(new Error('Unexpected error: ', err));
- });
- });
- it('should fail if input expects string, but `{foo:{bar: "baz"}}` is provided', function(done) {
- testInputConfiguration({
- example: 'asdf',
- actual: {foo:{bar: 'baz'}}
- }, function (err, result){
- if (!err) {
- return done();
- }
- return done(new Error('Unexpected error: ', err));
- });
- });
- it('should fail if input expects number, but `{foo:{bar: "baz"}}` is provided', function(done) {
- testInputConfiguration({
- example: 1234,
- actual: {foo:{bar: 'baz'}}
- }, function (err, result){
- if (!err) {
- return done();
- }
- return done(new Error('Unexpected error: ', err));
- });
- });
- });
- describe('_inputCoercion flag (+assorted sanity checks)', function() {
- var machine = {
- inputs: {
- foo: {
- example: 'foo bar'
- },
- bar: {
- example: 2
- }
- },
- exits: {
- success: {},
- error: {}
- },
- fn: function (inputs, exits, deps) {
- exits();
- }
- };
- it('should run with valid inputs', function(done) {
- M.build(machine)
- .configure({
- foo: 'hello',
- bar: 4
- })
- .exec(function(err, result) {
- if(err) { return done(err); }
- done();
- });
- });
- it('should error with mildly-invalid inputs when input coercion is off', function(done) {
- var live = M.build(machine)
- .configure({
- foo: 'hello',
- bar: '4'
- });
- live._inputCoercion = false;
- live.exec(function(err, result) {
- assert(err, 'expected error providing `"4"` to an input with example === `4`');
- done();
- });
- });
- it('should not error with mildly-invalid inputs when input coercion is on', function(done) {
- M.build(machine)
- .configure({
- foo: 'hello',
- bar: '4'
- })
- .exec(function(err, result) {
- if(err) { return done(err); }
- done();
- });
- });
- it('should error when undeclared exits are configured', function(done) {
- try {
- var live = M.build(machine)
- .configure({
- foo: 'hello',
- bar: 4
- });
- live._inputCoercion = false;
- live.exec({
- success: function(){},
- error: function() {},
- baz: function() {},
- boop: function() {}
- });
- return done(new Error('Expected an error regarding undeclared exits `baz, boop`.'));
- } catch (e) {
- return done();
- }
- });
- });
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////// HELPERS ///////////////
- function testInputConfiguration(options, cb){
- var _inputsInFn;
- var outputValue;
- var live = M.build({
- inputs: {
- x: (function _determineInputDefinition(){
- var def = {};
- if (!_.isUndefined(options.example)) {
- def.example = options.example;
- }
- return def;
- })()
- },
- exits: {
- error: {},
- success: {}
- },
- fn: function (inputs, exits) {
- _inputsInFn = inputs;
- exits(null, outputValue);
- }
- })
- .configure({
- x: options.actual
- });
- // Set experimental flags:
- live._unsafeMode = true;
- live._runTimeTypeCheck = false;
- live._inputCoercion = false;
- live._exitCoercion = false;
- live.exec(function (err, result){
- return cb(err, result);
- });
- }
|