|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const sinon = require('sinon'); |
| 4 | +const BbPromise = require('bluebird'); |
| 5 | + |
| 6 | +const GoogleProvider = require('../../provider/googleProvider'); |
| 7 | +const GoogleDeploy = require('../googleDeploy'); |
| 8 | +const Serverless = require('../../test/serverless'); |
| 9 | + |
| 10 | +describe('SetIamPolicy', () => { |
| 11 | + let serverless; |
| 12 | + let googleDeploy; |
| 13 | + let requestStub; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + serverless = new Serverless(); |
| 17 | + serverless.service.service = 'my-service'; |
| 18 | + serverless.service.provider = { |
| 19 | + project: 'my-project', |
| 20 | + }; |
| 21 | + serverless.config = { |
| 22 | + servicePath: 'tmp', |
| 23 | + }; |
| 24 | + serverless.setProvider('google', new GoogleProvider(serverless)); |
| 25 | + const options = { |
| 26 | + stage: 'dev', |
| 27 | + region: 'us-central1', |
| 28 | + }; |
| 29 | + googleDeploy = new GoogleDeploy(serverless, options); |
| 30 | + requestStub = sinon.stub(googleDeploy.provider, 'request'); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + googleDeploy.provider.request.restore(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('#setIamPolicy', () => { |
| 38 | + let getFunctionsStub; |
| 39 | + let setPoliciesStub; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + getFunctionsStub = sinon.stub(googleDeploy, 'getFunctions').returns(BbPromise.resolve()); |
| 43 | + setPoliciesStub = sinon.stub(googleDeploy, 'setPolicies').returns(BbPromise.resolve()); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + googleDeploy.getFunctions.restore(); |
| 48 | + googleDeploy.setPolicies.restore(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should run the promise chain', () => { |
| 52 | + googleDeploy.setIamPolicy().then(() => { |
| 53 | + expect(getFunctionsStub.calledOnce).toEqual(true); |
| 54 | + expect(setPoliciesStub.calledAfter(getFunctionsStub)); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('#getFunctions', () => { |
| 60 | + it('should return "undefined" if no functions are found', () => { |
| 61 | + requestStub.returns(BbPromise.resolve([])); |
| 62 | + |
| 63 | + return googleDeploy.getFunctions().then((foundFunctions) => { |
| 64 | + expect(foundFunctions).toEqual(undefined); |
| 65 | + expect( |
| 66 | + requestStub.calledWithExactly( |
| 67 | + 'cloudfunctions', |
| 68 | + 'projects', |
| 69 | + 'locations', |
| 70 | + 'functions', |
| 71 | + 'list', |
| 72 | + { |
| 73 | + parent: 'projects/my-project/locations/us-central1', |
| 74 | + } |
| 75 | + ) |
| 76 | + ).toEqual(true); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return all functions that are found', () => { |
| 81 | + const response = { |
| 82 | + functions: [{ name: 'cloud-function-1' }, { name: 'cloud-function-2' }], |
| 83 | + }; |
| 84 | + requestStub.returns(BbPromise.resolve(response)); |
| 85 | + |
| 86 | + return googleDeploy.getFunctions().then((foundFunctions) => { |
| 87 | + expect(foundFunctions).toEqual([ |
| 88 | + { name: 'cloud-function-1' }, |
| 89 | + { name: 'cloud-function-2' }, |
| 90 | + ]); |
| 91 | + expect( |
| 92 | + requestStub.calledWithExactly( |
| 93 | + 'cloudfunctions', |
| 94 | + 'projects', |
| 95 | + 'locations', |
| 96 | + 'functions', |
| 97 | + 'list', |
| 98 | + { |
| 99 | + parent: 'projects/my-project/locations/us-central1', |
| 100 | + } |
| 101 | + ) |
| 102 | + ).toEqual(true); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('#setPolicies', () => { |
| 108 | + let consoleLogStub; |
| 109 | + |
| 110 | + beforeEach(() => { |
| 111 | + consoleLogStub = sinon.stub(googleDeploy.serverless.cli, 'log').returns(); |
| 112 | + googleDeploy.serverless.service.provider.functionIamBindings = {}; |
| 113 | + }); |
| 114 | + |
| 115 | + afterEach(() => { |
| 116 | + googleDeploy.serverless.cli.log.restore(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should resolve if functionIamBindings is undefined', () => { |
| 120 | + const foundFunctions = [{ name: 'cloud-function-1' }, { name: 'cloud-function-2' }]; |
| 121 | + delete googleDeploy.serverless.service.provider.functionIamBindings; |
| 122 | + |
| 123 | + return googleDeploy.setPolicies(foundFunctions).then(() => { |
| 124 | + expect(consoleLogStub.calledOnce).toEqual(false); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should resolve if there are no IAM policies configured', () => { |
| 129 | + const foundFunctions = [{ name: 'cloud-function-1' }, { name: 'cloud-function-2' }]; |
| 130 | + |
| 131 | + return googleDeploy.setPolicies(foundFunctions).then(() => { |
| 132 | + expect(consoleLogStub.calledOnce).toEqual(false); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should error if there are no existing functions to apply configured IAM to', () => { |
| 137 | + const foundFunctions = []; |
| 138 | + googleDeploy.serverless.service.provider.functionIamBindings = { |
| 139 | + 'cloud-function-1': [{ role: 'roles/cloudfunctions.invoker', members: ['allUsers'] }], |
| 140 | + }; |
| 141 | + |
| 142 | + expect(() => googleDeploy.setPolicies(foundFunctions)).toThrow(Error); |
| 143 | + expect(consoleLogStub.calledOnce).toEqual(true); |
| 144 | + expect(requestStub.calledOnce).toEqual(false); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should error if a configured function is not found', () => { |
| 148 | + const foundFunctions = [{ name: 'cloud-function-2' }]; |
| 149 | + googleDeploy.serverless.service.provider.functionIamBindings = { |
| 150 | + 'cloud-function-1': [{ role: 'roles/cloudfunctions.invoker', members: ['allUsers'] }], |
| 151 | + }; |
| 152 | + |
| 153 | + expect(() => googleDeploy.setPolicies(foundFunctions)).toThrow(Error); |
| 154 | + expect(consoleLogStub.calledOnce).toEqual(true); |
| 155 | + expect(requestStub.calledOnce).toEqual(false); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should set the IAM policy for the configured functions', () => { |
| 159 | + const foundFunctions = [{ name: 'cloud-function-1' }, { name: 'cloud-function-2' }]; |
| 160 | + googleDeploy.serverless.service.provider.functionIamBindings = { |
| 161 | + 'cloud-function-2': [{ role: 'roles/cloudfunctions.invoker', members: ['allUsers'] }], |
| 162 | + }; |
| 163 | + requestStub.returns(BbPromise.resolve()); |
| 164 | + |
| 165 | + return googleDeploy.setPolicies(foundFunctions).then(() => { |
| 166 | + expect(consoleLogStub.calledOnce).toEqual(true); |
| 167 | + expect( |
| 168 | + requestStub.calledWithExactly( |
| 169 | + 'cloudfunctions', |
| 170 | + 'projects', |
| 171 | + 'locations', |
| 172 | + 'functions', |
| 173 | + 'setIamPolicy', |
| 174 | + { |
| 175 | + resource: 'cloud-function-2', |
| 176 | + requestBody: { |
| 177 | + policy: { |
| 178 | + bindings: [ |
| 179 | + { |
| 180 | + role: 'roles/cloudfunctions.invoker', |
| 181 | + members: ['allUsers'], |
| 182 | + }, |
| 183 | + ], |
| 184 | + }, |
| 185 | + }, |
| 186 | + } |
| 187 | + ) |
| 188 | + ).toEqual(true); |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments