From 3a871d956996ce0526cfdc4a639cbbf8043a2755 Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Mon, 22 Oct 2018 16:33:19 -0700 Subject: [PATCH 1/6] GCF: Add Node 8 background function samples --- functions/node8/index.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/functions/node8/index.js b/functions/node8/index.js index 58f41f14c7..3e7f1e456f 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -207,3 +207,37 @@ exports.helloAnalytics = (data, context) => { console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`); }; // [END functions_firebase_analytics_node8] + +// [START functions_background_promise_node8] +/** + * Background Cloud Function that returns a Promise. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + * @returns {Promise} + */ +exports.helloPromise = (data) => { + const request = require('request-promise'); + + return request({ + uri: data.endpoint + }); +}; +// [END functions_background_promise_node8] + +// [START functions_background_synchronous_node8] +/** + * Background Cloud Function that returns synchronously. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + */ +exports.helloSynchronous = (data) => { + // This function returns synchronously + if (data.something === true) { + return 'Something is true!'; + } else { + throw new Error('Something was not true!'); + } +}; +// [END functions_background_synchronous_node8] From ffeb4ba25148f2c550ce12dcc057dca245884060 Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Mon, 22 Oct 2018 16:41:55 -0700 Subject: [PATCH 2/6] Add necessary dependencies to package.json --- functions/node8/package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/node8/package.json b/functions/node8/package.json index d9e5ffaa4e..82d32b1cd1 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -23,5 +23,9 @@ "ava": "^0.25.0", "semistandard": "^12.0.1", "uuid": "^3.3.2" + }, + "dependencies": { + "request": "^2.88.0", + "request-promise": "^4.2.2" } } From c9f7becac3a4f0e2920618e48346f0b8385c388c Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Mon, 22 Oct 2018 16:33:19 -0700 Subject: [PATCH 3/6] GCF: Add Node 8 background function samples --- functions/node8/index.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/functions/node8/index.js b/functions/node8/index.js index 58f41f14c7..3e7f1e456f 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -207,3 +207,37 @@ exports.helloAnalytics = (data, context) => { console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`); }; // [END functions_firebase_analytics_node8] + +// [START functions_background_promise_node8] +/** + * Background Cloud Function that returns a Promise. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + * @returns {Promise} + */ +exports.helloPromise = (data) => { + const request = require('request-promise'); + + return request({ + uri: data.endpoint + }); +}; +// [END functions_background_promise_node8] + +// [START functions_background_synchronous_node8] +/** + * Background Cloud Function that returns synchronously. Note that we don't pass + * a "callback" argument to the function. + * + * @param {object} data The Cloud Functions event data. + */ +exports.helloSynchronous = (data) => { + // This function returns synchronously + if (data.something === true) { + return 'Something is true!'; + } else { + throw new Error('Something was not true!'); + } +}; +// [END functions_background_synchronous_node8] From 555632828860f2318b44213b169e5c6f1e663a72 Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Mon, 22 Oct 2018 16:41:55 -0700 Subject: [PATCH 4/6] Add necessary dependencies to package.json --- functions/node8/package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/node8/package.json b/functions/node8/package.json index d9e5ffaa4e..82d32b1cd1 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -23,5 +23,9 @@ "ava": "^0.25.0", "semistandard": "^12.0.1", "uuid": "^3.3.2" + }, + "dependencies": { + "request": "^2.88.0", + "request-promise": "^4.2.2" } } From fe76a0c15f5bec4d9b5c6f18c8db23308425c9ea Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Thu, 1 Nov 2018 15:53:25 -0700 Subject: [PATCH 5/6] Add tests + switch to requestPromiseNative/mocking --- functions/background/index.js | 6 +-- functions/background/package.json | 2 +- functions/background/test/index.test.js | 8 ++-- functions/node8/index.js | 6 +-- functions/node8/package.json | 2 +- functions/node8/test/index.test.js | 58 ++++++++++++++++++++++--- 6 files changed, 65 insertions(+), 17 deletions(-) diff --git a/functions/background/index.js b/functions/background/index.js index 46fe9e82cf..2d195d8d6b 100644 --- a/functions/background/index.js +++ b/functions/background/index.js @@ -36,6 +36,8 @@ exports.helloWorld = (event, callback) => { // [END functions_background_helloworld] // [START functions_background_promise] +const requestPromiseNative = require('request-promise-native'); + /** * Background Cloud Function that returns a Promise. Note that we don't pass * a "callback" argument to the function. @@ -45,9 +47,7 @@ exports.helloWorld = (event, callback) => { * @returns {Promise} */ exports.helloPromise = (event) => { - const request = require('request-promise'); - - return request({ + return requestPromiseNative({ uri: event.data.endpoint }); }; diff --git a/functions/background/package.json b/functions/background/package.json index dc792028dd..7d526b8ba2 100644 --- a/functions/background/package.json +++ b/functions/background/package.json @@ -18,7 +18,7 @@ }, "dependencies": { "request": "2.83.0", - "request-promise": "4.2.2" + "request-promise-native": "^1.0.5" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "2.2.1", diff --git a/functions/background/test/index.test.js b/functions/background/test/index.test.js index 6f7e73b9dd..a3654b4228 100644 --- a/functions/background/test/index.test.js +++ b/functions/background/test/index.test.js @@ -21,14 +21,14 @@ const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); function getSample () { - const requestPromise = sinon.stub().returns(Promise.resolve(`test`)); + const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`)); return { program: proxyquire(`../`, { - 'request-promise': requestPromise + 'request-promise-native': requestPromiseNative }), mocks: { - requestPromise: requestPromise + requestPromiseNative: requestPromiseNative } }; } @@ -73,7 +73,7 @@ test.serial(`should make a promise request`, (t) => { return sample.program.helloPromise(event) .then((result) => { - t.deepEqual(sample.mocks.requestPromise.firstCall.args, [{ uri: `foo.com` }]); + t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]); t.is(result, `test`); }); }); diff --git a/functions/node8/index.js b/functions/node8/index.js index 3e7f1e456f..0809c409d9 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -209,6 +209,8 @@ exports.helloAnalytics = (data, context) => { // [END functions_firebase_analytics_node8] // [START functions_background_promise_node8] +const requestPromiseNative = require('request-promise-native'); + /** * Background Cloud Function that returns a Promise. Note that we don't pass * a "callback" argument to the function. @@ -217,9 +219,7 @@ exports.helloAnalytics = (data, context) => { * @returns {Promise} */ exports.helloPromise = (data) => { - const request = require('request-promise'); - - return request({ + return requestPromiseNative({ uri: data.endpoint }); }; diff --git a/functions/node8/package.json b/functions/node8/package.json index 82d32b1cd1..44f8a4e7d0 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -26,6 +26,6 @@ }, "dependencies": { "request": "^2.88.0", - "request-promise": "^4.2.2" + "request-promise-native": "^1.0.5" } } diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js index c7e3d40d90..8480d2f92b 100644 --- a/functions/node8/test/index.test.js +++ b/functions/node8/test/index.test.js @@ -15,16 +15,31 @@ 'use strict'; +const proxyquire = require(`proxyquire`).noCallThru(); +const sinon = require(`sinon`); const uuid = require('uuid'); const test = require('ava'); const utils = require('@google-cloud/nodejs-repo-tools'); -const program = require('../'); +function getSample () { + const requestPromiseNative = sinon.stub().returns(Promise.resolve(`test`)); + + return { + program: proxyquire(`../`, { + 'request-promise-native': requestPromiseNative + }), + mocks: { + requestPromiseNative: requestPromiseNative + } + }; +} test.beforeEach(utils.stubConsole); test.afterEach.always(utils.restoreConsole); test.serial('should monitor Firebase RTDB', t => { + const sample = getSample(); + const dataId = uuid.v4(); const resourceId = uuid.v4(); @@ -38,7 +53,7 @@ test.serial('should monitor Firebase RTDB', t => { resource: resourceId }; - program.helloRTDB(data, context); + sample.program.helloRTDB(data, context); t.true(console.log.firstCall.args[0].includes(resourceId)); t.deepEqual(console.log.secondCall.args, ['Admin?: true']); @@ -46,6 +61,8 @@ test.serial('should monitor Firebase RTDB', t => { }); test.serial('should monitor Firestore', t => { + const sample = getSample(); + const resourceId = uuid.v4(); const context = { @@ -56,7 +73,7 @@ test.serial('should monitor Firestore', t => { value: { uuid: uuid.v4() } }; - program.helloFirestore(data, context); + sample.program.helloFirestore(data, context); t.true(console.log.firstCall.args[0].includes(resourceId)); t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2))); @@ -64,6 +81,8 @@ test.serial('should monitor Firestore', t => { }); test.serial('should monitor Auth', t => { + const sample = getSample(); + const userId = uuid.v4(); const dateString = (new Date()).toISOString(); const emailString = `${uuid.v4()}@${uuid.v4()}.com`; @@ -76,7 +95,7 @@ test.serial('should monitor Auth', t => { email: emailString }; - program.helloAuth(data, null); + sample.program.helloAuth(data, null); t.true(console.log.firstCall.args[0].includes(userId)); t.true(console.log.secondCall.args[0].includes(dateString)); @@ -84,6 +103,8 @@ test.serial('should monitor Auth', t => { }); test.serial('should monitor Analytics', t => { + const sample = getSample(); + const date = new Date(); const data = { eventDim: [{ @@ -105,10 +126,37 @@ test.serial('should monitor Analytics', t => { resource: 'my-resource' }; - program.helloAnalytics(data, context); + sample.program.helloAnalytics(data, context); t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`); t.is(console.log.args[1][0], `Name: my-event`); t.is(console.log.args[2][0], `Timestamp: ${date}`); t.is(console.log.args[3][0], `Device Model: Pixel`); t.is(console.log.args[4][0], `Location: London, UK`); }); + +test.serial(`should make a promise request`, (t) => { + const sample = getSample(); + const data = { + endpoint: `foo.com` + }; + + return sample.program.helloPromise(data) + .then((result) => { + t.deepEqual(sample.mocks.requestPromiseNative.firstCall.args, [{ uri: `foo.com` }]); + t.is(result, `test`); + }); +}); + +test.serial(`should return synchronously`, (t) => { + t.is(getSample().program.helloSynchronous({ + something: true + }), `Something is true!`); +}); + +test.serial(`should throw an error`, (t) => { + t.throws(() => { + getSample().program.helloSynchronous({ + something: false + }); + }, Error, `Something was not true!`); +}); From ad4b8fcf35d5c952bcff86a4d61340bf4918c7f1 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 12 Nov 2018 16:32:51 -0800 Subject: [PATCH 6/6] Fix lint bugs caused by merge --- functions/node8/index.js | 1 - functions/node8/test/index.test.js | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/functions/node8/index.js b/functions/node8/index.js index a9007b4a87..47191eba9d 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -279,4 +279,3 @@ exports.makeUpperCase = (data, context) => { }); }; // [END functions_firebase_reactive_node8] - diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js index ae3d8db967..92d4a69420 100644 --- a/functions/node8/test/index.test.js +++ b/functions/node8/test/index.test.js @@ -15,7 +15,6 @@ 'use strict'; -const proxyquire = require(`proxyquire`).noCallThru(); const sinon = require(`sinon`); const uuid = require('uuid'); const test = require('ava'); @@ -29,7 +28,7 @@ function getSample () { doc: sinon.stub().returnsThis(), set: sinon.stub() }; - + return { program: proxyquire(`../`, { 'request-promise-native': requestPromiseNative,