Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/apphosting/backend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ import {
chooseBackends,
getBackendForAmbiguousLocation,
getBackend,
ensureRequiredApisEnabled,
createGitRepoLink,
} from "./backend";
import * as deploymentTool from "../deploymentTool";
import { FirebaseError } from "../error";
import * as experiments from "../experiments";
import * as ensureApiEnabled from "../ensureApiEnabled";
import * as githubConnections from "./githubConnections";
import {
developerConnectOrigin,
secretManagerOrigin,
cloudbuildOrigin,
cloudRunApiOrigin,
artifactRegistryDomain,
iamOrigin,
} from "../api";

describe("apphosting setup functions", () => {
const projectId = "projectId";
Expand All @@ -35,6 +47,7 @@ describe("apphosting setup functions", () => {
let createServiceAccountStub: sinon.SinonStub;
let addServiceAccountToRolesStub: sinon.SinonStub;
let testResourceIamPermissionsStub: sinon.SinonStub;
let ensureStub: sinon.SinonStub;

beforeEach(() => {
promptStub = sinon.stub(promptImport);
Expand Down Expand Up @@ -67,6 +80,7 @@ describe("apphosting setup functions", () => {
testResourceIamPermissionsStub = sinon
.stub(iam, "testResourceIamPermissions")
.throws("Unexpected testResourceIamPermissions call");
ensureStub = sinon.stub(ensureApiEnabled, "ensure").resolves();
sinon.stub(experiments, "isEnabled").returns(false).withArgs("abiu").returns(true);
});

Expand Down Expand Up @@ -509,4 +523,68 @@ describe("apphosting setup functions", () => {
await expect(getBackend(projectId, "cow")).to.eventually.equal(backendCow);
});
});

describe("ensureRequiredApisEnabled", () => {
it("should enable all required APIs including developerconnect and secretmanager", async () => {
await ensureRequiredApisEnabled(projectId);
expect(ensureStub).to.have.been.calledWith(
projectId,
developerConnectOrigin(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(
projectId,
secretManagerOrigin(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(projectId, cloudbuildOrigin(), "apphosting", true);
expect(ensureStub).to.have.been.calledWith(
projectId,
cloudRunApiOrigin(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(
projectId,
artifactRegistryDomain(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(projectId, iamOrigin(), "apphosting", true);
});
});

describe("createGitRepoLink", () => {
it("should enable developerconnect, secretmanager and iam APIs", async () => {
const linkGitHubRepositoryStub = sinon
.stub(githubConnections, "linkGitHubRepository")
.resolves();
listLocationsStub.resolves([{ name: location, locationId: location }]);
try {
await createGitRepoLink(projectId, location, "connectionId");
expect(ensureStub).to.have.been.calledWith(
projectId,
developerConnectOrigin(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(
projectId,
secretManagerOrigin(),
"apphosting",
true,
);
expect(ensureStub).to.have.been.calledWith(projectId, iamOrigin(), "apphosting", true);
expect(linkGitHubRepositoryStub).to.have.been.calledWith(
projectId,
location,
"connectionId",
);
} finally {
linkGitHubRepositoryStub.restore();
}
Comment on lines +565 to +587

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The try-finally block is redundant here because sinon.verifyAndRestore() is already called in the afterEach hook (line 88), which automatically restores all stubs created on the default sandbox. Removing the try-finally block reduces unnecessary nesting and improves readability, adhering to the repository style guide's recommendation to reduce nesting as much as possible.

      await createGitRepoLink(projectId, location, "connectionId");
      expect(ensureStub).to.have.been.calledWith(
        projectId,
        developerConnectOrigin(),
        "apphosting",
        true,
      );
      expect(ensureStub).to.have.been.calledWith(
        projectId,
        secretManagerOrigin(),
        "apphosting",
        true,
      );
      expect(ensureStub).to.have.been.calledWith(projectId, iamOrigin(), "apphosting", true);
      expect(linkGitHubRepositoryStub).to.have.been.calledWith(
        projectId,
        location,
        "connectionId",
      );
References
  1. Reduce nesting as much as possible. Code should avoid unnecessarily deep nesting or long periods of nesting. (link)

});
});
});
Loading