|
| 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