From ec96dcdae1e2bab6a1a0072b5418964403d08fe7 Mon Sep 17 00:00:00 2001 From: Lui de la Parra Date: Fri, 12 Oct 2018 19:03:55 -0700 Subject: [PATCH 1/2] Fixes Script String Parameter bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug with the client’s script method. The script method was parsing all values as json. The script method now only attempts to parse valid json. Signed-off-by: Lui de la Parra --- src/client.model.js | 7 ++++++- src/utilities/filemaker.utilities.js | 2 +- tests/script.test.js | 24 +++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/client.model.js b/src/client.model.js index 9ed2682..aa5680b 100644 --- a/src/client.model.js +++ b/src/client.model.js @@ -771,7 +771,12 @@ class Client extends Document { }, params: sanitizeParameters( Object.assign( - { script: name, 'script.param': stringify(parameters) }, + { + script: name, + 'script.param': isJson(parameters) + ? stringify(parameters) + : parameters.toString() + }, namespace({ limit: 1 }) ) ) diff --git a/src/utilities/filemaker.utilities.js b/src/utilities/filemaker.utilities.js index b3e09cd..65ed9ab 100644 --- a/src/utilities/filemaker.utilities.js +++ b/src/utilities/filemaker.utilities.js @@ -1,7 +1,7 @@ 'use strict'; const _ = require('lodash'); -const { stringify, isJson, parse } = require('./conversion.utilities'); +const { stringify, parse } = require('./conversion.utilities'); /** * @module Filemaker Utilities diff --git a/tests/script.test.js b/tests/script.test.js index 7d2489b..7333d3e 100644 --- a/tests/script.test.js +++ b/tests/script.test.js @@ -1,4 +1,4 @@ -/* global describe before beforeEach it */ +/* global describe before after it */ /* eslint-disable */ @@ -62,6 +62,24 @@ describe('Script Capabilities', () => { .that.has.all.keys('result'); }); + it('should allow you to trigger a script specifying a string as a parameter', () => { + return expect( + client.script(process.env.LAYOUT, 'FMS Triggered Script', 'string-here') + ) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + + it('should allow you to trigger a script specifying an object as a parameter', () => { + return expect( + client.script(process.env.LAYOUT, 'FMS Triggered Script', { + object: true + }) + ) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + it('should allow you to trigger a script in a find', () => { return expect( client.find( @@ -85,7 +103,7 @@ describe('Script Capabilities', () => { .that.has.all.keys('scriptResult', 'scriptError', 'data'); }); - it('should allow reject a script that does not exist', () => { + it('should reject a script that does not exist', () => { return expect( client .script(process.env.LAYOUT, { @@ -125,7 +143,7 @@ describe('Script Capabilities', () => { }); it('should not parse script results if the results are not json', () => { - return expect(client.script(process.env.LAYOUT,'Non JSON Script')) + return expect(client.script(process.env.LAYOUT, 'Non JSON Script')) .to.eventually.be.a('object') .that.has.all.keys('result') .and.property('result') From 922b585a1d19dc6789b7e5b4cd717a016ac9f286 Mon Sep 17 00:00:00 2001 From: Lui de la Parra Date: Fri, 12 Oct 2018 20:07:58 -0700 Subject: [PATCH 2/2] Updates isJson Utility This commit updates the isJson utility function to better identify null, strings, undefined, or a number. This commit also adds tests specific to the isJson utility. Signed-off-by: Lui de la Parra --- src/utilities/conversion.utilities.js | 11 +++++++-- tests/script.test.js | 6 +++++ tests/utilities.test.js | 33 +++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/utilities/conversion.utilities.js b/src/utilities/conversion.utilities.js index 72f3174..607c525 100644 --- a/src/utilities/conversion.utilities.js +++ b/src/utilities/conversion.utilities.js @@ -47,12 +47,19 @@ const toArray = data => (Array.isArray(data) ? data : [data]); */ const isJson = data => { + data = typeof data !== 'string' ? JSON.stringify(data) : data; + try { - JSON.parse(data); + data = JSON.parse(data); } catch (e) { return false; } - return true; + + if (typeof data === 'object' && data !== null) { + return true; + } + + return false; }; /** diff --git a/tests/script.test.js b/tests/script.test.js index 7333d3e..ab885c5 100644 --- a/tests/script.test.js +++ b/tests/script.test.js @@ -70,6 +70,12 @@ describe('Script Capabilities', () => { .that.has.all.keys('result'); }); + it('should allow you to trigger a script specifying a number as a parameter', () => { + return expect(client.script(process.env.LAYOUT, 'FMS Triggered Script', 1)) + .to.eventually.be.a('object') + .that.has.all.keys('result'); + }); + it('should allow you to trigger a script specifying an object as a parameter', () => { return expect( client.script(process.env.LAYOUT, 'FMS Triggered Script', { diff --git a/tests/utilities.test.js b/tests/utilities.test.js index 21a7194..7ac6112 100644 --- a/tests/utilities.test.js +++ b/tests/utilities.test.js @@ -1,4 +1,4 @@ -/* global describe before after it */ +/* global describe it */ /* eslint-disable */ @@ -9,9 +9,11 @@ const { expect, should } = require('chai'); const chai = require('chai'); const chaiAsPromised = require('chai-as-promised'); -const environment = require('dotenv'); -const varium = require('varium'); -const { omit, parse } = require('../src/utilities/conversion.utilities'); +const { + omit, + parse, + isJson +} = require('../src/utilities/conversion.utilities'); chai.use(chaiAsPromised); @@ -51,4 +53,27 @@ describe('Utility Capabilities', () => { .and.to.include.keys('name'); }); }); + describe('isJson Utility', () => { + it('it should return true for an object', () => { + return expect(isJson({ object: true })).to.equal(true); + }); + it('it should return true for an empty object', () => { + return expect(isJson({ object: true })).to.equal(true); + }); + it('it should return true for a stringified object', () => { + return expect(isJson({})).to.equal(true); + }); + it('it should return false for a number', () => { + return expect(isJson(1)).to.equal(false); + }); + it('it should return false for undefined', () => { + return expect(isJson()).to.equal(false); + }); + it('it should return false for a string', () => { + return expect(isJson('string')).to.equal(false); + }); + it('it should return false for null', () => { + return expect(isJson(null)).to.equal(false); + }); + }); });