From 30e77aa355cc8d3172d1eea3a2ab31dfe936d819 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Wed, 22 Aug 2018 21:09:38 -0700 Subject: [PATCH 1/5] Add node8 firebase snippets --- functions/node8/index.js | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/functions/node8/index.js b/functions/node8/index.js index 44cae23d57..8ef615fe2a 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -123,3 +123,61 @@ exports.helloGCSGeneric = (data, context) => { console.log(` Updated: ${file.updated}`); }; // [END functions_helloworld_storage_generic_node8] + +// [START functions_firebase_rtdb_node8] +/** + * Triggered by a change to a Firebase RTDB reference. + * + * @param {object} data The event payload. + * @param {object} context The event metadata. + */ +exports.helloRTDB = (data, context) => { + const triggerResource = context.resource; + + console.log(`Function triggered by change to: ${triggerResource}`); + console.log(`Admin?: ${!!data.admin}`); + console.log(`Delta:`); + console.log(JSON.stringify(data.delta, null, 2)); +}; +// [END functions_firebase_rtdb_node8] + +// [START functions_firebase_firestore_node8] +/** + * Triggered by a change to a Firestore document. + * + * @param {object} data The event payload. + * @param {object} context The event metadata. + */ +exports.helloFirestore = (data, context) => { + const triggerResource = context.resource; + + console.log(`Function triggered by change to: ${triggerResource}`); + + console.log(`\nOld value:`); + console.log(JSON.stringify(data.oldValue, null, 2)); + + console.log(`\nNew value:`); + console.log(JSON.stringify(data.value, null, 2)); +}; +// [END functions_firebase_firestore_node8] + +// [START functions_firebase_auth_node8] +/** + * Triggered by a change to a Firebase Auth user object. + * + * @param {object} data The event payload. + * @param {object} context The event metadata. + */ +exports.helloAuth = (data, context) => { + try { + console.log(`Function triggered by change to user: ${data.uid}`); + console.log(`Created at: ${data.metadata.createdAt}`); + + if (data.email) { + console.log(`Email: ${data.email}`); + } + } catch (err) { + console.error(err); + } +}; +// [END functions_firebase_auth_node8] From e9755312458f7331f8bd6569f81a094b1c0e165a Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Thu, 23 Aug 2018 10:52:29 -0700 Subject: [PATCH 2/5] Address sam's comments --- functions/node8/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/node8/index.js b/functions/node8/index.js index 8ef615fe2a..70d182a1df 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -163,14 +163,14 @@ exports.helloFirestore = (data, context) => { // [START functions_firebase_auth_node8] /** - * Triggered by a change to a Firebase Auth user object. + * Triggered by creation or deletion of a Firebase Auth user object. * * @param {object} data The event payload. * @param {object} context The event metadata. */ exports.helloAuth = (data, context) => { try { - console.log(`Function triggered by change to user: ${data.uid}`); + console.log(`Function triggered by creation or deletion of user: ${data.uid}`); console.log(`Created at: ${data.metadata.createdAt}`); if (data.email) { From b0ebdb1dfafd2234459fb18f4dd8d31d60069b91 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 27 Aug 2018 11:48:44 -0700 Subject: [PATCH 3/5] Add (unit) tests for Firebase snippets --- functions/node8/package.json | 4 +- functions/node8/test/index.test.js | 84 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 functions/node8/test/index.test.js diff --git a/functions/node8/package.json b/functions/node8/package.json index 665830e98e..4fb6a8a904 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -19,7 +19,9 @@ "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues" }, "dependencies": { - "ava": "^0.25.0" + "@google-cloud/nodejs-repo-tools": "^2.3.3", + "ava": "^0.25.0", + "uuid": "^3.3.2" }, "devDependencies": { "semistandard": "^12.0.1" diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js new file mode 100644 index 0000000000..58fc1229e0 --- /dev/null +++ b/functions/node8/test/index.test.js @@ -0,0 +1,84 @@ +/** + * Copyright 2018, Google LLC. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const uuid = require('uuid'); +const test = require(`ava`); +const utils = require('@google-cloud/nodejs-repo-tools'); + +const program = require('../'); + +test.beforeEach(utils.stubConsole); +test.afterEach.always(utils.restoreConsole); + +test.serial('should monitor Firebase RTDB', t => { + const dataId = uuid.v4(); + const resourceId = uuid.v4(); + + const data = { + admin: true, + delta: { + id: dataId + } + }; + const context = { + resource: resourceId + }; + + program.helloRTDB(data, context); + + t.true(console.log.firstCall.args[0].includes(resourceId)); + t.deepEqual(console.log.secondCall.args, ['Admin?: true']); + t.true(console.log.getCall(3).args[0].includes(dataId)); +}); + +test.serial('should monitor Firestore', t => { + const resourceId = uuid.v4(); + + const context = { + resource: resourceId + }; + const data = { + oldValue: { uuid: uuid.v4() }, + value: { uuid: uuid.v4() } + }; + + program.helloFirestore(data, context); + + t.true(console.log.firstCall.args[0].includes(resourceId)); + t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2))); + t.true(console.log.calledWith(JSON.stringify(data.value, null, 2))); +}); + +test.serial('should monitor Auth', t => { + const userId = uuid.v4(); + const dateString = (new Date()).toISOString(); + const emailString = `${uuid.v4()}@${uuid.v4()}.com`; + + const data = { + uid: userId, + metadata: { + createdAt: dateString + }, + email: emailString + }; + + program.helloAuth(data, null); + + t.true(console.log.firstCall.args[0].includes(userId)); + t.true(console.log.secondCall.args[0].includes(dateString)); + t.true(console.log.thirdCall.args[0].includes(emailString)); +}); From 6416c33efc879acf15133d41523241ea6e40fa46 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 27 Aug 2018 13:03:25 -0700 Subject: [PATCH 4/5] Move deps to devDeps --- functions/node8/package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/functions/node8/package.json b/functions/node8/package.json index 4fb6a8a904..d9e5ffaa4e 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -18,12 +18,10 @@ "bugs": { "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues" }, - "dependencies": { + "devDependencies": { "@google-cloud/nodejs-repo-tools": "^2.3.3", "ava": "^0.25.0", + "semistandard": "^12.0.1", "uuid": "^3.3.2" - }, - "devDependencies": { - "semistandard": "^12.0.1" } } From 328689fc0dbd3921839227aaf6c9d099816d5208 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 27 Aug 2018 15:17:07 -0700 Subject: [PATCH 5/5] Fix quotes --- functions/node8/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/node8/test/index.test.js b/functions/node8/test/index.test.js index 58fc1229e0..618bc84e97 100644 --- a/functions/node8/test/index.test.js +++ b/functions/node8/test/index.test.js @@ -16,7 +16,7 @@ 'use strict'; const uuid = require('uuid'); -const test = require(`ava`); +const test = require('ava'); const utils = require('@google-cloud/nodejs-repo-tools'); const program = require('../');