Skip to content

Commit 7c3b419

Browse files
author
ace-n
committed
Add tests
1 parent 716fec0 commit 7c3b419

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

functions/security/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const metadataServerURL =
2929
'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=';
3030
const tokenUrl = metadataServerURL + functionURL;
3131

32-
exports.bearerToken = async (req, res) => {
32+
exports.callingFunction = async (req, res) => {
3333
// Fetch the token
3434
const tokenResponse = await get(tokenUrl, {
3535
headers: {

functions/security/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "nodejs-docs-samples-functions-security",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=8.0.0"
13+
},
14+
"scripts": {
15+
"test": "mocha test/*.test.js"
16+
},
17+
"dependencies": {
18+
"axios": "^0.19.2",
19+
"eslint-plugin-node": "^11.1.0",
20+
"mocha": "^7.1.1"
21+
},
22+
"devDependencies": {
23+
"assert": "^2.0.0",
24+
"proxyquire": "^2.1.3",
25+
"sinon": "^9.0.1"
26+
}
27+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2017 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const proxyquire = require('proxyquire').noCallThru();
18+
const sinon = require('sinon');
19+
const assert = require('assert');
20+
21+
const getSample = () => {
22+
const getMock = sinon
23+
.stub()
24+
.onFirstCall()
25+
.resolves({data: 'some-token'})
26+
.onSecondCall()
27+
.resolves({data: 'function-response'});
28+
29+
const axiosMock = {get: getMock};
30+
31+
const resMock = {};
32+
resMock.status = sinon.stub().returns(resMock);
33+
resMock.send = sinon.stub().returns(resMock);
34+
35+
return {
36+
sample: proxyquire('../', {
37+
axios: axiosMock,
38+
}),
39+
mocks: {
40+
res: resMock,
41+
axios: axiosMock,
42+
},
43+
};
44+
};
45+
46+
describe('functions_bearer_token', () => {
47+
it('should run', async () => {
48+
const {sample, mocks} = getSample();
49+
50+
await sample.callingFunction(null, mocks.res);
51+
52+
assert(mocks.axios.get.calledTwice);
53+
assert.deepEqual(mocks.axios.get.firstCall.args[1], {
54+
headers: {'Metadata-Flavor': 'Google'},
55+
});
56+
57+
assert(mocks.res.send.calledWith('function-response'));
58+
});
59+
});

0 commit comments

Comments
 (0)