Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ace-n committed Apr 3, 2020
1 parent 916d68d commit 5c10bf1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion functions/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const metadataServerURL =
'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=';
const tokenUrl = metadataServerURL + functionURL;

exports.bearerToken = async (req, res) => {
exports.callingFunction = async (req, res) => {
// Fetch the token
const tokenResponse = await get(tokenUrl, {
headers: {
Expand Down
27 changes: 27 additions & 0 deletions functions/security/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nodejs-docs-samples-functions-security",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha test/*.test.js"
},
"dependencies": {
"axios": "^0.19.2",
"eslint-plugin-node": "^11.1.0",
"mocha": "^7.1.1"
},
"devDependencies": {
"assert": "^2.0.0",
"proxyquire": "^2.1.3",
"sinon": "^9.0.1"
}
}
59 changes: 59 additions & 0 deletions functions/security/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2017 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 assert = require('assert');

const getSample = () => {
const getMock = sinon
.stub()
.onFirstCall()
.resolves({data: 'some-token'})
.onSecondCall()
.resolves({data: 'function-response'});

const axiosMock = {get: getMock};

const resMock = {};
resMock.status = sinon.stub().returns(resMock);
resMock.send = sinon.stub().returns(resMock);

return {
sample: proxyquire('../', {
axios: axiosMock,
}),
mocks: {
res: resMock,
axios: axiosMock,
},
};
};

describe('functions_bearer_token', () => {
it('should run', async () => {
const {sample, mocks} = getSample();

await sample.callingFunction(null, mocks.res);

assert(mocks.axios.get.calledTwice);
assert.deepEqual(mocks.axios.get.firstCall.args[1], {
headers: {'Metadata-Flavor': 'Google'},
});

assert(mocks.res.send.calledWith('function-response'));
});
});

0 comments on commit 5c10bf1

Please sign in to comment.