Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling to composer GCF trigger sample #732

Merged
merged 4 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions functions/composer-storage-trigger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function authorizeIap (clientId, projectId, userAgent) {
})
.then(res => res.json())
.then(function obtainAccessTokenCallback (tokenResponse) {
if (tokenResponse.error) {
return Promise.reject(tokenResponse.error);
}
var accessToken = tokenResponse.access_token;
var iat = Math.floor(new Date().getTime() / 1000);
var claims = {
Expand All @@ -104,6 +107,9 @@ function authorizeIap (clientId, projectId, userAgent) {
})
.then(res => res.json())
.then(function signJsonClaimCallback (body) {
if (body.error) {
return Promise.reject(body.error);
}
// Request service account signature on header and claimset
var jwtSignature = body.signature;
jwt = [JWT_HEADER, jwtClaimset, jwtSignature].join('.');
Expand All @@ -118,6 +124,9 @@ function authorizeIap (clientId, projectId, userAgent) {
})
.then(res => res.json())
.then(function returnJwt (body) {
if (body.error) {
return Promise.reject(body.error);
}
return {
jwt: jwt,
idToken: body.id_token
Expand Down
9 changes: 7 additions & 2 deletions functions/composer-storage-trigger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.2.5"
"@google-cloud/nodejs-repo-tools": "^2.2.5",
"ava": "0.25.0",
"proxyquire": "2.0.0",
"semistandard": "^12.0.1",
"sinon": "4.4.2"
},
"scripts": {
"lint": "repo-tools lint"
"lint": "repo-tools lint",
"test": "ava -T 20s --verbose test/*.test.js"
}
}
56 changes: 56 additions & 0 deletions functions/composer-storage-trigger/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);

function getSample () {
const bodyJson = {};
const body = {
json: sinon.stub().resolves(bodyJson)
};
const FetchMock = sinon.stub().resolves(body);

return {
program: proxyquire(`../`, {
'node-fetch': FetchMock
}),
mocks: {
fetch: FetchMock,
body: body,
bodyJson: bodyJson
}
};
}

test.cb.serial(`Does nothing for deleted files`, (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove serial?

const event = {
data: {
file: `some-file`
}
};
const expectedMsg = `Something bad happened.`;
const sample = getSample();
sample.mocks.bodyJson.error = expectedMsg;

sample.program.triggerDag(event, (err, message) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we switch to promises at some point? (In a separate PR, of course.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize we could return promises with GCF. Yeah, that'd be ideal.

t.regex(err, /Something bad happened/);
t.is(message, undefined);
t.end();
});
});