Skip to content

Commit

Permalink
mock auth globally for tests (#2805)
Browse files Browse the repository at this point in the history
* mock auth globally for tests

* remove unused method

* remove unused dep

* fix imports in integration scripts
  • Loading branch information
bkendall committed Nov 11, 2020
1 parent efd3f45 commit 03050ea
Show file tree
Hide file tree
Showing 27 changed files with 45 additions and 117 deletions.
4 changes: 3 additions & 1 deletion .mocharc.yml
@@ -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
2 changes: 1 addition & 1 deletion scripts/client-integration-tests/run.sh
Expand Up @@ -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
2 changes: 1 addition & 1 deletion scripts/extensions-emulator-tests/run.sh
Expand Up @@ -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
2 changes: 1 addition & 1 deletion scripts/triggers-end-to-end-tests/run.sh
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/accountExporter.spec.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 0 additions & 2 deletions src/test/accountImporter.spec.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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([
Expand Down
22 changes: 0 additions & 22 deletions src/test/apiv2.spec.ts
@@ -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(() => {
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions src/test/appdistro/client.spec.ts
Expand Up @@ -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();

Expand All @@ -27,7 +26,6 @@ describe("distribution", () => {

beforeEach(() => {
sandbox = sinon.createSandbox();
mockAuth(sandbox);
sandbox.useFakeTimers();
});

Expand Down
11 changes: 1 addition & 10 deletions src/test/database/removeRemote.spec.ts
@@ -1,25 +1,16 @@
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", () => {
const instance = "fake-db";
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();
});

Expand Down
2 changes: 0 additions & 2 deletions src/test/deploy/remoteconfig/remoteconfig.spec.ts
Expand Up @@ -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";

Expand Down Expand Up @@ -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");
Expand Down
1 change: 0 additions & 1 deletion src/test/emulators/auth.spec.ts
Expand Up @@ -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";
Expand Down
10 changes: 0 additions & 10 deletions src/test/extensions/extensionsApi.spec.ts
@@ -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";

Expand Down Expand Up @@ -75,14 +73,6 @@ const TEST_SOURCE = {
};

describe("extensions", () => {
beforeEach(() => {
helpers.mockAuth(sinon);
});

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

describe("listInstances", () => {
afterEach(() => {
nock.cleanAll();
Expand Down
9 changes: 0 additions & 9 deletions src/test/gcp/cloudscheduler.spec.ts
@@ -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";
Expand All @@ -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();
});

Expand Down
12 changes: 12 additions & 0 deletions src/test/helpers/global-mock-auth.ts
@@ -0,0 +1,12 @@
import * as sinon from "sinon";

import { mockAuth } from "./";

const authSandbox = sinon.createSandbox();
before(() => {
mockAuth(authSandbox);
});

after(() => {
authSandbox.restore();
});
11 changes: 0 additions & 11 deletions src/test/helpers/index.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/test/helpers/index.ts
@@ -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" });
}
10 changes: 0 additions & 10 deletions src/test/helpers/mocha-bootstrap.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/test/helpers/mocha-bootstrap.ts
@@ -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;
});
11 changes: 1 addition & 10 deletions src/test/hosting/api.spec.ts
@@ -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";

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 0 additions & 8 deletions src/test/hosting/cloudRunProxy.spec.ts
Expand Up @@ -9,7 +9,6 @@ import cloudRunProxy, {
CloudRunProxyOptions,
CloudRunProxyRewrite,
} from "../../hosting/cloudRunProxy";
import { mockAuth } from "../helpers";

describe("cloudRunProxy", () => {
const fakeOptions: CloudRunProxyOptions = {
Expand All @@ -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 () => {
Expand Down
2 changes: 0 additions & 2 deletions src/test/management/apps.spec.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 0 additions & 2 deletions src/test/management/database.spec.ts
Expand Up @@ -3,7 +3,6 @@ import * as sinon from "sinon";

import * as api from "../../api";

import { mockAuth } from "../helpers";
import {
DatabaseLocation,
DatabaseInstance,
Expand Down Expand Up @@ -92,7 +91,6 @@ describe("Database management", () => {

beforeEach(() => {
sandbox = sinon.createSandbox();
mockAuth(sandbox);
apiRequestStub = sandbox.stub(api, "request").throws("Unexpected API request call");
});

Expand Down
2 changes: 0 additions & 2 deletions src/test/management/projects.spec.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
});
Expand Down

0 comments on commit 03050ea

Please sign in to comment.