mirror of
https://github.com/zhigang1992/firebase-tools.git
synced 2026-04-29 12:25:21 +08:00
mock auth globally for tests (#2805)
* mock auth globally for tests * remove unused method * remove unused dep * fix imports in integration scripts
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
require:
|
||||
- ts-node/register
|
||||
- source-map-support/register
|
||||
- src/test/helpers/mocha-bootstrap.js
|
||||
- src/test/helpers/mocha-bootstrap.ts
|
||||
file:
|
||||
- src/test/helpers/global-mock-auth.ts
|
||||
timeout: 1000
|
||||
recursive: true
|
||||
|
||||
@@ -5,5 +5,5 @@ source scripts/set-default-credentials.sh
|
||||
mocha \
|
||||
--require ts-node/register \
|
||||
--require source-map-support/register \
|
||||
--require src/test/helpers/mocha-bootstrap.js \
|
||||
--require src/test/helpers/mocha-bootstrap.ts \
|
||||
scripts/client-integration-tests/tests.ts
|
||||
@@ -11,5 +11,5 @@ cd - # Return to root so that we don't need a relative path for mocha
|
||||
mocha \
|
||||
--require ts-node/register \
|
||||
--require source-map-support/register \
|
||||
--require src/test/helpers/mocha-bootstrap.js \
|
||||
--require src/test/helpers/mocha-bootstrap.ts \
|
||||
scripts/extensions-emulator-tests/tests.ts
|
||||
|
||||
@@ -18,7 +18,7 @@ echo "NODE_VERSION=$NODE_VERSION"
|
||||
mocha \
|
||||
--require ts-node/register \
|
||||
--require source-map-support/register \
|
||||
--require src/test/helpers/mocha-bootstrap.js \
|
||||
--require src/test/helpers/mocha-bootstrap.ts \
|
||||
--exit \
|
||||
scripts/triggers-end-to-end-tests/tests.ts
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ var os = require("os");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var accountExporter = require("../accountExporter");
|
||||
var helpers = require("./helpers");
|
||||
|
||||
var expect = chai.expect;
|
||||
describe("accountExporter", function() {
|
||||
@@ -44,7 +43,6 @@ describe("accountExporter", function() {
|
||||
|
||||
beforeEach(function() {
|
||||
sandbox = sinon.createSandbox();
|
||||
helpers.mockAuth(sandbox);
|
||||
spyWrite = sandbox.spy(writeStream, "write");
|
||||
for (var i = 0; i < 7; i++) {
|
||||
userList.push({
|
||||
|
||||
@@ -4,7 +4,6 @@ var chai = require("chai");
|
||||
var sinon = require("sinon");
|
||||
var api = require("../api");
|
||||
var accountImporter = require("../accountImporter");
|
||||
var helpers = require("./helpers");
|
||||
|
||||
var expect = chai.expect;
|
||||
describe("accountImporter", function() {
|
||||
@@ -110,7 +109,6 @@ describe("accountImporter", function() {
|
||||
|
||||
beforeEach(function() {
|
||||
sandbox = sinon.createSandbox();
|
||||
helpers.mockAuth(sandbox);
|
||||
mockApi = sandbox.mock(api);
|
||||
for (var i = 0; i < 10; i++) {
|
||||
batches.push([
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { expect } from "chai";
|
||||
import * as nock from "nock";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { Client } from "../apiv2";
|
||||
import { FirebaseError } from "../error";
|
||||
import { streamToString } from "../utils";
|
||||
import * as helpers from "./helpers";
|
||||
|
||||
describe("apiv2", () => {
|
||||
beforeEach(() => {
|
||||
@@ -18,16 +16,6 @@ describe("apiv2", () => {
|
||||
});
|
||||
|
||||
describe("request", () => {
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
helpers.mockAuth(sandbox);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it("should throw on a basic 404 GET request", async () => {
|
||||
nock("https://example.com")
|
||||
.get("/path/to/foo")
|
||||
@@ -227,16 +215,6 @@ describe("apiv2", () => {
|
||||
});
|
||||
|
||||
describe("verbs", () => {
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
helpers.mockAuth(sandbox);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it("should make a GET request", async () => {
|
||||
nock("https://example.com")
|
||||
.get("/path/to/foo")
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
import { FirebaseError } from "../../error";
|
||||
import * as api from "../../api";
|
||||
import { Distribution } from "../../appdistribution/distribution";
|
||||
import { mockAuth } from "../helpers";
|
||||
|
||||
tmp.setGracefulCleanup();
|
||||
|
||||
@@ -27,7 +26,6 @@ describe("distribution", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
sandbox.useFakeTimers();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { expect } from "chai";
|
||||
import * as sinon from "sinon";
|
||||
import * as nock from "nock";
|
||||
import * as utils from "../../utils";
|
||||
|
||||
import * as helpers from "../helpers";
|
||||
import * as utils from "../../utils";
|
||||
import { RTDBRemoveRemote } from "../../database/removeRemote";
|
||||
|
||||
describe("RemoveRemote", () => {
|
||||
@@ -11,15 +9,8 @@ describe("RemoveRemote", () => {
|
||||
const host = "https://firebaseio.com";
|
||||
const remote = new RTDBRemoveRemote(instance, host);
|
||||
const serverUrl = utils.getDatabaseUrl(host, instance, "");
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
helpers.mockAuth(sandbox);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as sinon from "sinon";
|
||||
|
||||
import * as api from "../../../api";
|
||||
import * as rcDeploy from "../../../deploy/remoteconfig/functions";
|
||||
import { mockAuth } from "../../helpers";
|
||||
import * as remoteconfig from "../../../remoteconfig/get";
|
||||
import { RemoteConfigTemplate } from "../../../remoteconfig/interfaces";
|
||||
|
||||
@@ -67,7 +66,6 @@ describe("Remote Config Deploy", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
templateStub = sandbox.stub(remoteconfig, "getTemplate");
|
||||
etagStub = sandbox.stub(rcDeploy, "getEtag");
|
||||
|
||||
@@ -22,7 +22,6 @@ import {
|
||||
} from "../../emulator/auth/state";
|
||||
import { useFakeTimers } from "sinon";
|
||||
|
||||
/* eslint-disable camelcase, @typescript-eslint/camelcase, @typescript-eslint/no-non-null-assertion */
|
||||
const PROJECT_ID = "example";
|
||||
|
||||
const TEST_PHONE_NUMBER = "+15555550100";
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import * as _ from "lodash";
|
||||
import { expect } from "chai";
|
||||
import * as nock from "nock";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import * as helpers from "../helpers";
|
||||
import * as api from "../../api";
|
||||
import { FirebaseError } from "../../error";
|
||||
|
||||
@@ -75,14 +73,6 @@ const TEST_SOURCE = {
|
||||
};
|
||||
|
||||
describe("extensions", () => {
|
||||
beforeEach(() => {
|
||||
helpers.mockAuth(sinon);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("listInstances", () => {
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { expect } from "chai";
|
||||
import * as _ from "lodash";
|
||||
import * as nock from "nock";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { cloudscheduler } from "../../gcp";
|
||||
import { FirebaseError } from "../../error";
|
||||
import { mockAuth } from "../helpers";
|
||||
import * as api from "../../api";
|
||||
|
||||
const VERSION = "v1beta1";
|
||||
@@ -23,14 +21,7 @@ const TEST_JOB = {
|
||||
|
||||
describe("cloudscheduler", () => {
|
||||
describe("createOrUpdateJob", () => {
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
|
||||
12
src/test/helpers/global-mock-auth.ts
Normal file
12
src/test/helpers/global-mock-auth.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { mockAuth } from "./";
|
||||
|
||||
const authSandbox = sinon.createSandbox();
|
||||
before(() => {
|
||||
mockAuth(authSandbox);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
authSandbox.restore();
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
var auth = require("../../auth");
|
||||
|
||||
exports.mockAuth = function(sandbox) {
|
||||
var authMock = sandbox.mock(auth);
|
||||
authMock
|
||||
.expects("getAccessToken")
|
||||
.atLeast(1)
|
||||
.resolves({ access_token: "an_access_token" });
|
||||
};
|
||||
15
src/test/helpers/index.ts
Normal file
15
src/test/helpers/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import * as sinon from "sinon";
|
||||
import * as auth from "../../auth";
|
||||
|
||||
/**
|
||||
* Mocks getAccessToken so that tests don't take forever.
|
||||
* @param sandbox a sinon sandbox.
|
||||
*/
|
||||
export function mockAuth(sandbox: sinon.SinonSandbox): void {
|
||||
const authMock = sandbox.mock(auth);
|
||||
authMock
|
||||
.expects("getAccessToken")
|
||||
.atLeast(1)
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
.resolves({ access_token: "an_access_token" });
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
const chai = require("chai");
|
||||
const chaiAsPromised = require("chai-as-promised");
|
||||
const sinonChai = require("sinon-chai");
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
chai.use(sinonChai);
|
||||
|
||||
process.on("unhandledRejection", (error) => {
|
||||
throw error;
|
||||
});
|
||||
10
src/test/helpers/mocha-bootstrap.ts
Normal file
10
src/test/helpers/mocha-bootstrap.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import * as chai from "chai";
|
||||
import * as chaiAsPromised from "chai-as-promised";
|
||||
import * as sinonChai from "sinon-chai";
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
chai.use(sinonChai);
|
||||
|
||||
process.on("unhandledRejection", (error) => {
|
||||
throw error;
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
import * as nock from "nock";
|
||||
import * as sinon from "sinon";
|
||||
import * as helpers from "../helpers";
|
||||
|
||||
import * as api from "../../api";
|
||||
import * as hostingApi from "../../hosting/api";
|
||||
|
||||
@@ -36,14 +35,6 @@ const PROJECT_ID = "test-project";
|
||||
const SITE = "my-site";
|
||||
|
||||
describe("hosting", () => {
|
||||
beforeEach(() => {
|
||||
helpers.mockAuth(sinon);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("getCleanDomains", () => {
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
|
||||
@@ -9,7 +9,6 @@ import cloudRunProxy, {
|
||||
CloudRunProxyOptions,
|
||||
CloudRunProxyRewrite,
|
||||
} from "../../hosting/cloudRunProxy";
|
||||
import { mockAuth } from "../helpers";
|
||||
|
||||
describe("cloudRunProxy", () => {
|
||||
const fakeOptions: CloudRunProxyOptions = {
|
||||
@@ -18,15 +17,8 @@ describe("cloudRunProxy", () => {
|
||||
const fakeRewrite: CloudRunProxyRewrite = { run: { serviceId: "helloworld" } };
|
||||
const cloudRunServiceOrigin = "https://helloworld-hash-uc.a.run.app";
|
||||
|
||||
let sandbox: sinon.SinonSandbox;
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
nock.cleanAll();
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
it("should error when not provided a valid Cloud Run service ID", async () => {
|
||||
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
WebAppMetadata,
|
||||
} from "../../management/apps";
|
||||
import * as pollUtils from "../../operation-poller";
|
||||
import { mockAuth } from "../helpers";
|
||||
import { FirebaseError } from "../../error";
|
||||
|
||||
const PROJECT_ID = "the-best-firebase-project";
|
||||
@@ -70,7 +69,6 @@ describe("App management", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
pollOperationStub = sandbox.stub(pollUtils, "pollOperation").throws("Unexpected poll call");
|
||||
readFileSyncStub = sandbox.stub(fs, "readFileSync").throws("Unxpected readFileSync call");
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as sinon from "sinon";
|
||||
|
||||
import * as api from "../../api";
|
||||
|
||||
import { mockAuth } from "../helpers";
|
||||
import {
|
||||
DatabaseLocation,
|
||||
DatabaseInstance,
|
||||
@@ -92,7 +91,6 @@ describe("Database management", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import * as sinon from "sinon";
|
||||
import * as api from "../../api";
|
||||
import * as projectManager from "../../management/projects";
|
||||
import * as pollUtils from "../../operation-poller";
|
||||
import { mockAuth } from "../helpers";
|
||||
import * as prompt from "../../prompt";
|
||||
|
||||
const PROJECT_ID = "the-best-firebase-project";
|
||||
@@ -86,7 +85,6 @@ describe("Project management", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
pollOperationStub = sandbox.stub(pollUtils, "pollOperation").throws("Unexpected poll call");
|
||||
});
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as nock from "nock";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { FirebaseError } from "../error";
|
||||
import { mockAuth } from "./helpers";
|
||||
import { OperationPollerOptions, pollOperation } from "../operation-poller";
|
||||
import TimeoutError from "../throttler/errors/timeout-error";
|
||||
|
||||
@@ -19,7 +18,6 @@ describe("OperationPoller", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
pollerOptions = {
|
||||
apiOrigin: TEST_ORIGIN,
|
||||
apiVersion: VERSION,
|
||||
|
||||
@@ -2,7 +2,6 @@ import { expect } from "chai";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import * as api from "../../api";
|
||||
import { mockAuth } from "../helpers";
|
||||
import * as remoteconfig from "../../remoteconfig/get";
|
||||
import { RemoteConfigTemplate } from "../../remoteconfig/interfaces";
|
||||
|
||||
@@ -97,7 +96,6 @@ describe("Remote Config GET", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import { expect } from "chai";
|
||||
import api = require("../../api");
|
||||
import sinon = require("sinon");
|
||||
|
||||
import { mockAuth } from "../helpers";
|
||||
import { RemoteConfigTemplate } from "../../remoteconfig/interfaces";
|
||||
import * as remoteconfig from "../../remoteconfig/rollback";
|
||||
|
||||
@@ -40,7 +39,6 @@ describe("RemoteConfig Rollback", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import * as sinon from "sinon";
|
||||
|
||||
import * as api from "../../api";
|
||||
import * as remoteconfig from "../../remoteconfig/versionslist";
|
||||
import { mockAuth } from "../helpers";
|
||||
import { ListVersionsResult, Version } from "../../remoteconfig/interfaces";
|
||||
|
||||
const PROJECT_ID = "the-remoteconfig-test-project";
|
||||
@@ -53,7 +52,6 @@ describe("RemoteConfig ListVersions", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox();
|
||||
mockAuth(sandbox);
|
||||
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user