Skip to content
Open
Changes from 1 commit
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
120 changes: 120 additions & 0 deletions src/commands/apphosting-backends-create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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;
let needProjectIdStub: sinon.SinonStub;

Check failure on line 19 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'needProjectIdStub' is assigned a value but never used

Check failure on line 19 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'needProjectIdStub' is assigned a value but never used

Check failure on line 19 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'needProjectIdStub' is assigned a value but never used
let requireAuthStub: sinon.SinonStub;

Check failure on line 20 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'requireAuthStub' is assigned a value but never used

Check failure on line 20 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

'requireAuthStub' is assigned a value but never used

Check failure on line 20 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'requireAuthStub' is assigned a value but never used

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

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

beforeEach(() => {
doSetupStub = sinon.stub(backend, "doSetup").resolves();
needProjectIdStub = sinon.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
requireAuthStub = 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: [{

Check failure on line 46 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `⏎··········`

Check failure on line 46 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `⏎··········`

Check failure on line 46 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `⏎··········`
tosId: "APP_HOSTING_TOS",

Check failure on line 47 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `··`

Check failure on line 47 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `··`

Check failure on line 47 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `··`
serviceStatus: {

Check failure on line 48 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `··`

Check failure on line 48 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `··`

Check failure on line 48 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `··`
status: "ACCEPTED"

Check failure on line 49 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `status:·"ACCEPTED"` with `··status:·"ACCEPTED",`

Check failure on line 49 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `status:·"ACCEPTED"` with `··status:·"ACCEPTED",`

Check failure on line 49 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `status:·"ACCEPTED"` with `··status:·"ACCEPTED",`
}

Check failure on line 50 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `··········}` with `············},`

Check failure on line 50 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `··········}` with `············},`

Check failure on line 50 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `··········}` with `············},`
}]

Check failure on line 51 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `········}]` with `··········},⏎········],`

Check failure on line 51 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `········}]` with `··········},⏎········],`

Check failure on line 51 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `········}]` with `··········},⏎········],`
});
});

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

it("should throw error if non-interactive but missing required options", async () => {
const options = { nonInteractive: true };
await expect(command.runner()(options)).to.be.rejectedWith(
FirebaseError,
"requires --backend and --primary-region"

Check failure on line 64 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`

Check failure on line 64 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 64 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`
);
});

it("should throw error if non-interactive and just backend provided", async () => {
const options = { nonInteractive: true, backend: BACKEND_ID };
await expect(command.runner()(options)).to.be.rejectedWith(
FirebaseError,
"requires --backend and --primary-region"

Check failure on line 72 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`

Check failure on line 72 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Insert `,`

Check failure on line 72 in src/commands/apphosting-backends-create.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Insert `,`
);
});

it("should throw error if non-interactive and just region provided", async () => {
const options = { nonInteractive: true, primaryRegion: REGION };
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
);
});
});
Loading