diff --git a/functions/composer-storage-trigger/index.js b/functions/composer-storage-trigger/index.js index 455837c880..84d223d924 100644 --- a/functions/composer-storage-trigger/index.js +++ b/functions/composer-storage-trigger/index.js @@ -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 = { @@ -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('.'); @@ -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 diff --git a/functions/composer-storage-trigger/package.json b/functions/composer-storage-trigger/package.json index c2dcd939b3..5a8cf7569b 100644 --- a/functions/composer-storage-trigger/package.json +++ b/functions/composer-storage-trigger/package.json @@ -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" } } diff --git a/functions/composer-storage-trigger/test/index.test.js b/functions/composer-storage-trigger/test/index.test.js new file mode 100644 index 0000000000..db4586d17c --- /dev/null +++ b/functions/composer-storage-trigger/test/index.test.js @@ -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(); + }); +});