Skip to content

Commit

Permalink
Add error handling to composer GCF trigger sample (#732)
Browse files Browse the repository at this point in the history
* Add error handling to composer GCF trigger sample

* Fix lint

* Remove serial from test definition.
  • Loading branch information
tswast authored Sep 7, 2018
1 parent 0322c5d commit 058f033
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
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(`Handles error in JSON body`, (t) => {
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) => {
t.regex(err, /Something bad happened/);
t.is(message, undefined);
t.end();
});
});

0 comments on commit 058f033

Please sign in to comment.