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