|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import * as resourceManager from "../../../gcp/resourceManager"; |
| 4 | +import * as pn from "../../../getProjectNumber"; |
| 5 | +import * as v2FunctionHelper from "../../../deploy/extensions/v2FunctionHelper"; |
| 6 | +import * as ensureApiEnabled from "../../../ensureApiEnabled"; |
| 7 | +import * as projectUtils from "../../../projectUtils"; |
| 8 | + |
| 9 | +const GOOD_BINDING = { |
| 10 | + role: "roles/eventarc.eventReceiver", |
| 11 | + members: ["serviceAccount:[email protected]"], |
| 12 | +}; |
| 13 | + |
| 14 | +describe("ensureNecessaryV2ApisAndRoles", () => { |
| 15 | + let getIamStub: sinon.SinonStub; |
| 16 | + let setIamStub: sinon.SinonStub; |
| 17 | + let needProjectIdStub: sinon.SinonStub; |
| 18 | + let getProjectNumberStub: sinon.SinonStub; |
| 19 | + let ensureApiEnabledStub: sinon.SinonStub; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + getIamStub = sinon |
| 23 | + .stub(resourceManager, "getIamPolicy") |
| 24 | + .throws("unexpected call to resourceManager.getIamStub"); |
| 25 | + setIamStub = sinon |
| 26 | + .stub(resourceManager, "setIamPolicy") |
| 27 | + .throws("unexpected call to resourceManager.setIamPolicy"); |
| 28 | + needProjectIdStub = sinon |
| 29 | + .stub(projectUtils, "needProjectId") |
| 30 | + .throws("unexpected call to pn.getProjectNumber"); |
| 31 | + getProjectNumberStub = sinon |
| 32 | + .stub(pn, "getProjectNumber") |
| 33 | + .throws("unexpected call to pn.getProjectNumber"); |
| 34 | + ensureApiEnabledStub = sinon |
| 35 | + .stub(ensureApiEnabled, "ensure") |
| 36 | + .throws("unexpected call to ensureApiEnabled.ensure"); |
| 37 | + |
| 38 | + getProjectNumberStub.resolves(123456); |
| 39 | + needProjectIdStub.returns("project_id"); |
| 40 | + ensureApiEnabledStub.resolves(undefined); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + sinon.verifyAndRestore(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should succeed when IAM policy is correct", async () => { |
| 48 | + getIamStub.resolves({ |
| 49 | + etag: "etag", |
| 50 | + version: 3, |
| 51 | + bindings: [GOOD_BINDING], |
| 52 | + }); |
| 53 | + |
| 54 | + expect(await v2FunctionHelper.ensureNecessaryV2ApisAndRoles({ projectId: "project_id" })).to.not |
| 55 | + .throw; |
| 56 | + |
| 57 | + expect(getIamStub).to.have.been.calledWith("project_id"); |
| 58 | + expect(setIamStub).to.not.have.been.called; |
| 59 | + }); |
| 60 | + |
| 61 | + it("should fix the IAM policy by adding missing bindings", async () => { |
| 62 | + getIamStub.resolves({ |
| 63 | + etag: "etag", |
| 64 | + version: 3, |
| 65 | + bindings: [], |
| 66 | + }); |
| 67 | + setIamStub.resolves(); |
| 68 | + |
| 69 | + expect(await v2FunctionHelper.ensureNecessaryV2ApisAndRoles({ projectId: "project_id" })).to.not |
| 70 | + .throw; |
| 71 | + |
| 72 | + expect(getIamStub).to.have.been.calledWith("project_id"); |
| 73 | + expect(setIamStub).to.have.been.calledWith( |
| 74 | + "project_id", |
| 75 | + { |
| 76 | + etag: "etag", |
| 77 | + version: 3, |
| 78 | + bindings: [GOOD_BINDING], |
| 79 | + }, |
| 80 | + "bindings" |
| 81 | + ); |
| 82 | + }); |
| 83 | +}); |
0 commit comments