Skip to content
Merged
6 changes: 3 additions & 3 deletions functions/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
});
};
Expand Down
2 changes: 1 addition & 1 deletion functions/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions functions/background/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
}
Expand Down Expand Up @@ -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`);
});
});
Expand Down
34 changes: 34 additions & 0 deletions functions/node8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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.
*
* @param {object} data The Cloud Functions event data.
* @returns {Promise}
*/
exports.helloPromise = (data) => {
return requestPromiseNative({
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]
4 changes: 4 additions & 0 deletions functions/node8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"ava": "^0.25.0",
"semistandard": "^12.0.1",
"uuid": "^3.3.2"
},
"dependencies": {
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
}
}
58 changes: 53 additions & 5 deletions functions/node8/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -38,14 +53,16 @@ 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']);
t.true(console.log.getCall(3).args[0].includes(dataId));
});

test.serial('should monitor Firestore', t => {
const sample = getSample();

const resourceId = uuid.v4();

const context = {
Expand All @@ -56,14 +73,16 @@ 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)));
t.true(console.log.calledWith(JSON.stringify(data.value, null, 2)));
});

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`;
Expand All @@ -76,14 +95,16 @@ 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));
t.true(console.log.thirdCall.args[0].includes(emailString));
});

test.serial('should monitor Analytics', t => {
const sample = getSample();

const date = new Date();
const data = {
eventDim: [{
Expand All @@ -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!`);
});