Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
173 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getFirebaseProject } from "./management/projects"; | ||
import * as getProjectId from "./getProjectId"; | ||
|
||
/** | ||
* Fetches the project number. | ||
* @param options CLI options. | ||
* @return the project number, as a string. | ||
*/ | ||
export async function getProjectNumber(options: any): Promise<string> { | ||
if (options.projectNumber) { | ||
return options.projectNumber; | ||
} | ||
const projectId = getProjectId(options); | ||
const metadata = await getFirebaseProject(projectId); | ||
options.projectNumber = metadata.projectNumber; | ||
return options.projectNumber; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { expect } from "chai"; | ||
import * as nock from "nock"; | ||
import * as sinon from "sinon"; | ||
|
||
import { configstore } from "../configstore"; | ||
import { fetchWebSetup, getCachedWebSetup } from "../fetchWebSetup"; | ||
import { firebaseApiOrigin } from "../api"; | ||
import { FirebaseError } from "../error"; | ||
|
||
describe("fetchWebSetup module", () => { | ||
afterEach(() => { | ||
expect(nock.isDone()).to.be.true; | ||
}); | ||
|
||
describe("fetchWebSetup", () => { | ||
let configSetStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
sinon.stub(configstore, "get"); | ||
configSetStub = sinon.stub(configstore, "set").returns(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("should fetch the web app config", async () => { | ||
const projectId = "foo"; | ||
nock(firebaseApiOrigin) | ||
.get(`/v1beta1/projects/${projectId}/webApps/-/config`) | ||
.reply(200, { some: "config" }); | ||
|
||
const config = await fetchWebSetup({ project: projectId }); | ||
|
||
expect(config).to.deep.equal({ some: "config" }); | ||
}); | ||
|
||
it("should store the fetched config", async () => { | ||
const projectId = "projectId"; | ||
nock(firebaseApiOrigin) | ||
.get(`/v1beta1/projects/${projectId}/webApps/-/config`) | ||
.reply(200, { projectId, some: "config" }); | ||
|
||
await fetchWebSetup({ project: projectId }); | ||
|
||
expect(configSetStub).to.have.been.calledOnceWith("webconfig", { | ||
[projectId]: { | ||
projectId, | ||
some: "config", | ||
}, | ||
}); | ||
expect(nock.isDone()).to.be.true; | ||
}); | ||
|
||
it("should throw an error if the request fails", async () => { | ||
const projectId = "foo"; | ||
nock(firebaseApiOrigin) | ||
.get(`/v1beta1/projects/${projectId}/webApps/-/config`) | ||
.reply(404, { error: "Not Found" }); | ||
|
||
await expect(fetchWebSetup({ project: projectId })).to.eventually.be.rejectedWith( | ||
FirebaseError, | ||
"Not Found" | ||
); | ||
}); | ||
}); | ||
|
||
describe("getCachedWebSetup", () => { | ||
let configGetStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
sinon.stub(configstore, "set").returns(); | ||
configGetStub = sinon.stub(configstore, "get"); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("should return no config if none is cached", () => { | ||
configGetStub.returns(undefined); | ||
|
||
const config = getCachedWebSetup({ project: "foo" }); | ||
|
||
expect(config).to.be.undefined; | ||
}); | ||
|
||
it("should return a stored config", () => { | ||
const projectId = "projectId"; | ||
configGetStub.returns({ [projectId]: { project: projectId, some: "config" } }); | ||
|
||
const config = getCachedWebSetup({ project: projectId }); | ||
|
||
expect(config).to.be.deep.equal({ project: projectId, some: "config" }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
|
||
import { getProjectNumber } from "../getProjectNumber"; | ||
import * as projects from "../management/projects"; | ||
|
||
describe("getProjectNumber", () => { | ||
let getProjectStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
getProjectStub = sinon.stub(projects, "getFirebaseProject").throws(new Error("stubbed")); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("should return the project number from options, if present", async () => { | ||
const n = await getProjectNumber({ projectNumber: 1 }); | ||
|
||
expect(n).to.equal(1); | ||
expect(getProjectStub).to.not.have.been.called; | ||
}); | ||
|
||
it("should fetch the project number if necessary", async () => { | ||
getProjectStub.returns({ projectNumber: 2 }); | ||
|
||
const n = await getProjectNumber({ project: "foo" }); | ||
|
||
expect(n).to.equal(2); | ||
expect(getProjectStub).to.have.been.calledOnceWithExactly("foo"); | ||
}); | ||
|
||
it("should reject with an error on an error", async () => { | ||
getProjectStub.rejects(new Error("oh no")); | ||
|
||
await expect(getProjectNumber({ project: "foo" })).to.eventually.be.rejectedWith( | ||
Error, | ||
"oh no" | ||
); | ||
}); | ||
}); |