Skip to content
Open
Show file tree
Hide file tree
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
118 changes: 118 additions & 0 deletions src/commands/apphosting-backends-create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as nock from "nock";
import { command } from "./apphosting-backends-create";
import * as backend from "../apphosting/backend";
import * as projectUtils from "../projectUtils";
import * as requireAuthModule from "../requireAuth";
import { FirebaseError } from "../error";

describe("apphosting:backends:create", () => {
const PROJECT_ID = "test-project";
const WEB_APP_ID = "test-web-app";
const BACKEND_ID = "test-backend";
const REGION = "us-central1";
const SERVICE_ACCOUNT = "test-sa";
const ROOT_DIR = ".";

let doSetupStub: sinon.SinonStub;

before(() => {
nock.disableNetConnect();
});

after(() => {
nock.enableNetConnect();
});

beforeEach(() => {
doSetupStub = sinon.stub(backend, "doSetup").resolves();
sinon.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
sinon.stub(requireAuthModule, "requireAuth").resolves();

// Stub ensureApiEnabled calls
nock("https://serviceusage.googleapis.com")
.get(`/v1/projects/${PROJECT_ID}/services/firebaseapphosting.googleapis.com`)
.query(true) // match any query params
.reply(200, { state: "ENABLED" });

// Stub TOS acceptance check
nock("https://mobilesdk-pa.googleapis.com")
.get("/v1/accessmanagement/tos:getStatus")
.query(true)
.reply(200, {
perServiceStatus: [
{
tosId: "APP_HOSTING_TOS",
serviceStatus: {
status: "ACCEPTED",
},
},
],
});
});

afterEach(() => {
sinon.restore();
nock.cleanAll();
});

[
{
description: "missing required options",
options: { nonInteractive: true },
},
{
description: "just backend provided",
options: { nonInteractive: true, backend: BACKEND_ID },
},
{
description: "just region provided",
options: { nonInteractive: true, primaryRegion: REGION },
},
].forEach(({ description, options }) => {
it(`should throw error if non-interactive and ${description}`, async () => {
await expect(command.runner()(options)).to.be.rejectedWith(
FirebaseError,
"requires --backend and --primary-region",
);
});
});

it("should call doSetup with correct arguments in interactive mode", async () => {
const options = {};
await command.runner()(options);

expect(doSetupStub).to.have.been.calledWith(
PROJECT_ID,
undefined, // nonInteractive
undefined, // webAppId
undefined, // backendId
undefined, // serviceAccount
undefined, // primaryRegion
undefined, // rootDir
);
});

it("should call doSetup with passed options in non-interactive mode", async () => {
const options = {
nonInteractive: true,
backend: BACKEND_ID,
primaryRegion: REGION,
app: WEB_APP_ID,
serviceAccount: SERVICE_ACCOUNT,
rootDir: ROOT_DIR,
};
await command.runner()(options);

expect(doSetupStub).to.have.been.calledWith(
PROJECT_ID,
true,
WEB_APP_ID,
BACKEND_ID,
SERVICE_ACCOUNT,
REGION,
ROOT_DIR,
);
});
});
2 changes: 1 addition & 1 deletion src/commands/apphosting-backends-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const command = new Command("apphosting:backends:create")
"specify the primary region for the backend. Required with --non-interactive.",
)
.option("--root-dir <rootDir>", "specify the root directory for the backend.")
.before(requireAuth)
.before((options: Options) => requireAuth(options))
.before(ensureApiEnabled)
.before(requireTosAcceptance(APPHOSTING_TOS_ID))
.action(async (options: Options) => {
Expand Down
Loading