Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions cloud-tasks/tutorial-gcf/function/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');

describe('tasks/function', () => {
let key;

const getSample = function () {
const requestPromise = sinon
.stub()
.returns(new Promise(resolve => resolve('test')));
const sendGridStub = {
setApiKey: sinon.stub(),
send: sinon.stub().resolves([{statusCode: 200}]),
};
Comment thread
angelcaamal marked this conversation as resolved.

return {
program: proxyquire('../', {
'request-promise': requestPromise,
'@sendgrid/mail': sendGridStub,
}),
mocks: {
requestPromise: requestPromise,
sendGridStub: sendGridStub,
},
Comment thread
angelcaamal marked this conversation as resolved.
};
};
Expand All @@ -54,14 +54,7 @@ describe('tasks/function', () => {
};

before(async () => {
const secrets = new SecretManagerServiceClient();
const projectId = await secrets.getProjectId();
const secretName = 'sendgrid-api-key';
const secretVersion = 1;
const [version] = await secrets.accessSecretVersion({
name: secrets.secretVersionPath(projectId, secretName, secretVersion),
});
key = version.payload.data.toString();
key = 'SG.dummy_key_for_testing';
process.env.SENDGRID_API_KEY = key;
});

Expand Down Expand Up @@ -167,5 +160,11 @@ describe('tasks/function', () => {
assert.strictEqual(mocks.res.status.callCount, 1);
assert.deepStrictEqual(mocks.res.status.firstCall.args, [200]);
assert.strictEqual(mocks.res.send.callCount, 1);
sinon.assert.calledOnceWithExactly(sample.mocks.sendGridStub.send, {
to: 'to@gmail.com',
from: 'postcard@example.com',
subject: 'A Postcard Just for You!',
html: sinon.match.string,
});
});
});
Loading