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
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require:
- ts-node/register
- source-map-support/register
- src/test/helpers/mocha-bootstrap.ts
- src/test/helpers/mocha-bootstrap.js
file:
- src/test/helpers/global-mock-auth.ts
timeout: 2000
Expand Down
2 changes: 1 addition & 1 deletion src/accountExporter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { expect } from "chai";
import * as nock from "nock";
import nock from "./test/helpers/nock";
import * as os from "os";
import * as sinon from "sinon";

Expand Down
2 changes: 1 addition & 1 deletion src/accountImporter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as nock from "nock";
import nock from "./test/helpers/nock";
import { expect } from "chai";

import { googleOrigin } from "./api";
Expand Down
62 changes: 51 additions & 11 deletions src/apiv2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import { createServer, Server } from "http";
import { expect } from "chai";
import * as nock from "nock";
import AbortController from "abort-controller";
import * as sinon from "sinon";
import * as FormData from "form-data";
import * as auth from "./auth";
import nock from "./test/helpers/nock";
const proxySetup = require("proxy");

import { Client, CLI_OAUTH_PROJECT_NUMBER } from "./apiv2";
import { FirebaseError } from "./error";
import { streamToString, stringToStream } from "./utils";

describe("apiv2", () => {
let authStub: sinon.SinonStub | undefined;
before(() => {
if (typeof (auth.getAccessToken as any).restore !== "function") {
authStub = sinon.stub(auth, "getAccessToken").resolves({ access_token: "owner" } as any);
}
});
after(() => {
if (authStub) {
authStub.restore();
}
});

beforeEach(() => {
// The api module has package variables that we don't want sticking around.
delete require.cache[require.resolve("./apiv2")];
Expand Down Expand Up @@ -150,17 +163,39 @@ describe("apiv2", () => {
});

it("should resend a multipart body when retrying after a premature close", async () => {
const sentBodies: string[] = [];
const capture = (b: unknown): boolean => {
sentBodies.push(typeof b === "string" ? b : JSON.stringify(b));
let body1: string | undefined;
let body2: string | undefined;

const bodyToStr = (b: unknown): string => {
if (b instanceof Uint8Array || Buffer.isBuffer(b)) {
return Buffer.from(b).toString("utf8");
} else if (typeof b === "string") {
return b;
} else {
return JSON.stringify(b);
}
};

const capture1 = (b: unknown): boolean => {
if (body1 === undefined) {
body1 = bodyToStr(b);
}
return true;
};
nock("https://example.com").post("/upload", capture).once().replyWithError({

const capture2 = (b: unknown): boolean => {
if (body2 === undefined) {
body2 = bodyToStr(b);
}
return true;
};

nock("https://example.com").post("/upload", capture1).once().replyWithError({
message:
"Invalid response body while trying to fetch https://example.com/upload: Premature close",
code: "ERR_STREAM_PREMATURE_CLOSE",
});
nock("https://example.com").post("/upload", capture).once().reply(200, { ok: true });
nock("https://example.com").post("/upload", capture2).once().reply(200, { ok: true });

const form = new FormData();
form.append("code", "secret-code-123");
Expand All @@ -177,9 +212,10 @@ describe("apiv2", () => {
});
expect(r.status).to.equal(200);
// Both the original attempt and the retry must carry the full body.
expect(sentBodies).to.have.length(2);
expect(sentBodies[0]).to.contain("secret-code-123");
expect(sentBodies[1]).to.contain("secret-code-123");
expect(body1).to.not.be.undefined;
expect(body2).to.not.be.undefined;
expect(body1).to.contain("secret-code-123");
expect(body2).to.contain("secret-code-123");
expect(nock.isDone()).to.be.true;
});

Expand Down Expand Up @@ -584,7 +620,11 @@ describe("apiv2", () => {
new Promise((resolve) => proxyServer.close(resolve)),
new Promise((resolve) => targetServer.close(resolve)),
]);
process.env.HTTP_PROXY = oldProxy;
if (oldProxy === undefined) {
delete process.env.HTTP_PROXY;
} else {
process.env.HTTP_PROXY = oldProxy;
}
});

it("should be able to make a basic GET request", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/appdistribution/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { join } from "path";
import * as fs from "fs-extra";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import { rmSync } from "node:fs";
import * as sinon from "sinon";
import * as tmp from "tmp";
Expand Down
2 changes: 1 addition & 1 deletion src/apptesting/invokeTests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import { appTestingOrigin } from "../api";
import { invokeTests, pollInvocationStatus } from "./invokeTests";
import { FirebaseError } from "../error";
Expand Down
2 changes: 1 addition & 1 deletion src/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as rc from "./rc";
import * as nock from "nock";
import nock from "./test/helpers/nock";
import { configstore } from "./configstore";

import { Command, validateProjectId } from "./command";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/crashlytics-sourcemap-upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("crashlytics:sourcemap:upload", () => {
execSyncStub.withArgs("git rev-parse HEAD").returns(Buffer.from("a".repeat(40)));
clientPatchStub = sandbox.stub(Client.prototype, "patch").resolves({
status: 200,
response: {} as unknown as import("node-fetch").Response,
response: {} as unknown as Response,
body: {},
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/crashlytics/events.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as chaiAsPromised from "chai-as-promised";

import { listEvents, batchGetEvents } from "./events";
Expand Down
2 changes: 1 addition & 1 deletion src/crashlytics/issues.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as chaiAsPromised from "chai-as-promised";

import { getIssue, updateIssue } from "./issues";
Expand Down
2 changes: 1 addition & 1 deletion src/crashlytics/notes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as chaiAsPromised from "chai-as-promised";

import { createNote, deleteNote, listNotes } from "./notes";
Expand Down
2 changes: 1 addition & 1 deletion src/crashlytics/reports.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as chaiAsPromised from "chai-as-promised";

import { CrashlyticsReport, getReport, simplifyReport } from "./reports";
Expand Down
18 changes: 15 additions & 3 deletions src/database/import.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as stream from "stream";
import * as sinon from "sinon";
import * as auth from "../auth";
import * as utils from "../utils";
import { expect } from "chai";

import DatabaseImporter from "./import";
import { FirebaseError } from "../error";
import { FetchError } from "node-fetch";

const dbUrl = new URL("https://test-db.firebaseio.com/foo");
const payloadSize = 1024 * 1024 * 10;
const concurrencyLimit = 5;

describe("DatabaseImporter", () => {
let authStub: sinon.SinonStub | undefined;
before(() => {
if (typeof (auth.getAccessToken as any).restore !== "function") {
authStub = sinon.stub(auth, "getAccessToken").resolves({ access_token: "owner" } as any);
}
});
after(() => {
if (authStub) {
authStub.restore();
}
});
const DATA = { a: 100, b: [true, "bar", { f: { g: 0, h: 1 }, i: "baz" }], c: { d: false } };
let DATA_STREAM: stream.Readable;

Expand Down Expand Up @@ -164,7 +176,7 @@ describe("DatabaseImporter", () => {
});

it("retries non-fatal connection timeout error", async () => {
const timeoutErr = new FetchError("connect ETIMEDOUT", "system");
const timeoutErr = new Error("connect ETIMEDOUT") as any;
timeoutErr.code = "ETIMEDOUT";

nock("https://test-db.firebaseio.com").get("/foo.json?shallow=true").reply(200);
Expand Down
2 changes: 1 addition & 1 deletion src/database/listRemote.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";

import * as utils from "../utils";
import { realtimeOrigin } from "../api";
Expand Down
2 changes: 1 addition & 1 deletion src/database/removeRemote.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";

import * as utils from "../utils";
import { RTDBRemoveRemote } from "./removeRemote";
Expand Down
2 changes: 1 addition & 1 deletion src/dataconnect/dataplaneClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../test/helpers/nock";
import * as chai from "chai";
import {
dataconnectDataplaneClient,
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/dataconnect/deploy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import * as deploy from "./deploy";
import * as utils from "../../utils";
import * as projectUtils from "../../projectUtils";
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/dataconnect/prepare.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import * as prepare from "./prepare";
import * as load from "../../dataconnect/load";
import * as utils from "../../utils";
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/dataconnect/release.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import * as release from "./release";
import * as utils from "../../utils";
import * as projectUtils from "../../projectUtils";
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/functions/ensure.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import * as sinon from "sinon";
import * as nock from "nock";
import nock from "../../test/helpers/nock";

import { FirebaseError } from "../../error";
import { logger } from "../../logger";
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/functions/runtimes/discovery/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai";
import * as fs from "fs/promises";
import * as yaml from "yaml";
import * as sinon from "sinon";
import * as nock from "nock";
import nock from "../../../../test/helpers/nock";

import * as api from "../../../../api";
import { FirebaseError } from "../../../../error";
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/remoteconfig/remoteconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import * as sinon from "sinon";

import { remoteConfigApiOrigin } from "../../api";
Expand Down
2 changes: 1 addition & 1 deletion src/downloadUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { readFileSync } from "fs-extra";
import * as nock from "nock";
import nock from "./test/helpers/nock";
import { gunzipSync, gzipSync } from "zlib";

import { downloadToTmp } from "./downloadUtils";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/cloudFunctions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";

import { AuthCloudFunction } from "./cloudFunctions";
import { EmulatorRegistry } from "../registry";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/emailLink.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import { FirebaseJwtPayload, parseBlockingFunctionJwt } from "./operations";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/idp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import { FirebaseJwtPayload } from "./operations";
import { PROVIDER_PASSWORD, SIGNIN_METHOD_EMAIL_LINK } from "./state";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/mfa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/password.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import { FirebaseJwtPayload } from "./operations";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/phone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import { FirebaseJwtPayload } from "./operations";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/auth/signUp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import * as nock from "nock";
import nock from "../../test/helpers/nock";
import { decode as decodeJwt, JwtHeader } from "jsonwebtoken";
import { FirebaseJwtPayload } from "./operations";
import { describeAuthEmulator, PROJECT_ID } from "./testing/setup";
Expand Down
Loading
Loading