-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathrepo.spec.ts
325 lines (287 loc) · 10.5 KB
/
repo.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import * as sinon from "sinon";
import { expect } from "chai";
import * as gcb from "../gcp/cloudbuild";
import * as rm from "../gcp/resourceManager";
import * as prompt from "../prompt";
import * as poller from "../operation-poller";
import * as repo from "./repo";
import * as utils from "../utils";
import * as srcUtils from "../getProjectNumber";
import { FirebaseError } from "../error";
const projectId = "projectId";
const location = "us-central1";
function mockConn(id: string): gcb.Connection {
return {
name: `projects/${projectId}/locations/${location}/connections/${id}`,
disabled: false,
createTime: "0",
updateTime: "1",
installationState: {
stage: "COMPLETE",
message: "complete",
actionUri: "https://google.com",
},
reconciling: false,
};
}
function mockRepo(name: string): gcb.Repository {
return {
name: `${name}`,
remoteUri: `https://github.com/test/${name}.git`,
createTime: "",
updateTime: "",
};
}
function mockReposWithRandomUris(n: number): gcb.Repository[] {
const repos = [];
for (let i = 0; i < n; i++) {
const hash = Math.random().toString(36).slice(6);
repos.push(mockRepo(hash));
}
return repos;
}
describe("composer", () => {
describe("connect GitHub repo", () => {
const sandbox: sinon.SinonSandbox = sinon.createSandbox();
let promptOnceStub: sinon.SinonStub;
let pollOperationStub: sinon.SinonStub;
let getConnectionStub: sinon.SinonStub;
let getRepositoryStub: sinon.SinonStub;
let createConnectionStub: sinon.SinonStub;
let serviceAccountHasRolesStub: sinon.SinonStub;
let createRepositoryStub: sinon.SinonStub;
let fetchLinkableRepositoriesStub: sinon.SinonStub;
let getProjectNumberStub: sinon.SinonStub;
let openInBrowserPopupStub: sinon.SinonStub;
beforeEach(() => {
promptOnceStub = sandbox.stub(prompt, "promptOnce").throws("Unexpected promptOnce call");
pollOperationStub = sandbox
.stub(poller, "pollOperation")
.throws("Unexpected pollOperation call");
getConnectionStub = sandbox
.stub(gcb, "getConnection")
.throws("Unexpected getConnection call");
getRepositoryStub = sandbox
.stub(gcb, "getRepository")
.throws("Unexpected getRepository call");
createConnectionStub = sandbox
.stub(gcb, "createConnection")
.throws("Unexpected createConnection call");
serviceAccountHasRolesStub = sandbox.stub(rm, "serviceAccountHasRoles").resolves(true);
createRepositoryStub = sandbox
.stub(gcb, "createRepository")
.throws("Unexpected createRepository call");
fetchLinkableRepositoriesStub = sandbox
.stub(gcb, "fetchLinkableRepositories")
.throws("Unexpected fetchLinkableRepositories call");
sandbox.stub(utils, "openInBrowser").resolves();
openInBrowserPopupStub = sandbox
.stub(utils, "openInBrowserPopup")
.throws("Unexpected openInBrowserPopup call");
getProjectNumberStub = sandbox
.stub(srcUtils, "getProjectNumber")
.throws("Unexpected getProjectNumber call");
});
afterEach(() => {
sandbox.verifyAndRestore();
});
const projectId = "projectId";
const location = "us-central1";
const connectionId = `apphosting-${location}`;
const op = {
name: `projects/${projectId}/locations/${location}/connections/${connectionId}`,
done: true,
};
const pendingConn = {
name: `projects/${projectId}/locations/${location}/connections/${connectionId}`,
disabled: false,
createTime: "0",
updateTime: "1",
installationState: {
stage: "PENDING_USER_OAUTH",
message: "pending",
actionUri: "https://google.com",
},
reconciling: false,
};
const completeConn = {
name: `projects/${projectId}/locations/${location}/connections/${connectionId}`,
disabled: false,
createTime: "0",
updateTime: "1",
installationState: {
stage: "COMPLETE",
message: "complete",
actionUri: "https://google.com",
},
reconciling: false,
};
const repos = {
repositories: [
{
name: "repo0",
remoteUri: "https://github.com/test/repo0.git",
},
{
name: "repo1",
remoteUri: "https://github.com/test/repo1.git",
},
],
};
it("creates a connection if it doesn't exist", async () => {
getConnectionStub.onFirstCall().rejects(new FirebaseError("error", { status: 404 }));
getConnectionStub.onSecondCall().resolves(completeConn);
createConnectionStub.resolves(op);
pollOperationStub.resolves(pendingConn);
promptOnceStub.onFirstCall().resolves("any key");
await repo.getOrCreateConnection(projectId, location, connectionId);
expect(createConnectionStub).to.be.calledWith(projectId, location, connectionId);
});
it("checks if secret manager admin role is granted for cloud build P4SA when creating an oauth connection", async () => {
getConnectionStub.onFirstCall().rejects(new FirebaseError("error", { status: 404 }));
getConnectionStub.onSecondCall().resolves(completeConn);
createConnectionStub.resolves(op);
pollOperationStub.resolves(pendingConn);
promptOnceStub.resolves("any key");
getProjectNumberStub.onFirstCall().resolves(projectId);
openInBrowserPopupStub.resolves({ url: "", cleanup: sandbox.stub() });
await repo.getOrCreateOauthConnection(projectId, location);
expect(serviceAccountHasRolesStub).to.be.calledWith(
projectId,
`service-${projectId}@gcp-sa-cloudbuild.iam.gserviceaccount.com`,
["roles/secretmanager.admin"],
true,
);
});
it("creates repository if it doesn't exist", async () => {
getConnectionStub.resolves(completeConn);
fetchLinkableRepositoriesStub.resolves(repos);
promptOnceStub.onFirstCall().resolves(repos.repositories[0].remoteUri);
getRepositoryStub.rejects(new FirebaseError("error", { status: 404 }));
createRepositoryStub.resolves({ name: "op" });
pollOperationStub.resolves(repos.repositories[0]);
await repo.getOrCreateRepository(
projectId,
location,
connectionId,
repos.repositories[0].remoteUri,
);
expect(createRepositoryStub).to.be.calledWith(
projectId,
location,
connectionId,
"test-repo0",
repos.repositories[0].remoteUri,
);
});
it("re-uses existing repository it already exists", async () => {
getConnectionStub.resolves(completeConn);
fetchLinkableRepositoriesStub.resolves(repos);
promptOnceStub.onFirstCall().resolves(repos.repositories[0].remoteUri);
getRepositoryStub.resolves(repos.repositories[0]);
const r = await repo.getOrCreateRepository(
projectId,
location,
connectionId,
repos.repositories[0].remoteUri,
);
expect(r).to.be.deep.equal(repos.repositories[0]);
});
});
describe("parseConnectionName", () => {
it("should parse valid connection name", () => {
const str = "projects/my-project/locations/us-central1/connections/my-conn";
const expected = {
projectId: "my-project",
location: "us-central1",
id: "my-conn",
};
expect(repo.parseConnectionName(str)).to.deep.equal(expected);
});
it("should return undefined for invalid", () => {
expect(
repo.parseConnectionName(
"projects/my-project/locations/us-central1/connections/my-conn/repositories/repo",
),
).to.be.undefined;
expect(repo.parseConnectionName("foobar")).to.be.undefined;
});
});
describe("fetchAllRepositories", () => {
const sandbox: sinon.SinonSandbox = sinon.createSandbox();
let fetchLinkableRepositoriesStub: sinon.SinonStub;
beforeEach(() => {
fetchLinkableRepositoriesStub = sandbox
.stub(gcb, "fetchLinkableRepositories")
.throws("Unexpected fetchLinkableRepositories call");
});
afterEach(() => {
sandbox.verifyAndRestore();
});
it("should fetch all repositories from multiple pages", async () => {
fetchLinkableRepositoriesStub.onFirstCall().resolves({
repositories: mockReposWithRandomUris(10),
nextPageToken: "1234",
});
fetchLinkableRepositoriesStub.onSecondCall().resolves({
repositories: mockReposWithRandomUris(10),
});
const { repos, remoteUriToConnection } = await repo.fetchAllRepositories(projectId, [
mockConn("conn0"),
]);
expect(repos.length).to.equal(20);
expect(Object.keys(remoteUriToConnection).length).to.equal(20);
});
it("should fetch all linkable repositories from multiple connections", async () => {
const conn0 = mockConn("conn0");
const conn1 = mockConn("conn1");
const repo0 = mockRepo("repo-0");
const repo1 = mockRepo("repo-1");
fetchLinkableRepositoriesStub.onFirstCall().resolves({
repositories: [repo0],
});
fetchLinkableRepositoriesStub.onSecondCall().resolves({
repositories: [repo1],
});
const { repos, remoteUriToConnection } = await repo.fetchAllRepositories(projectId, [
conn0,
conn1,
]);
expect(repos.length).to.equal(2);
expect(remoteUriToConnection).to.deep.equal({
[repo0.remoteUri]: conn0,
[repo1.remoteUri]: conn1,
});
});
});
describe("listAppHostingConnections", () => {
const sandbox: sinon.SinonSandbox = sinon.createSandbox();
let listConnectionsStub: sinon.SinonStub;
function extractId(name: string): string {
const parts = name.split("/");
return parts.pop() ?? "";
}
beforeEach(() => {
listConnectionsStub = sandbox
.stub(gcb, "listConnections")
.throws("Unexpected getConnection call");
});
afterEach(() => {
sandbox.verifyAndRestore();
});
it("filters out non-apphosting connections", async () => {
listConnectionsStub.resolves([
mockConn("apphosting-github-conn-baddcafe"),
mockConn("hooray-conn"),
mockConn("apphosting-github-conn-deadbeef"),
mockConn("apphosting-github-oauth"),
]);
const conns = await repo.listAppHostingConnections(projectId);
expect(conns).to.have.length(2);
expect(conns.map((c) => extractId(c.name))).to.include.members([
"apphosting-github-conn-baddcafe",
"apphosting-github-conn-deadbeef",
]);
});
});
});