From 9aecf748417ef8a844f36bc50b58adae18caf0e5 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Thu, 4 Feb 2021 10:13:20 -0800 Subject: [PATCH 1/6] Add emulator-based integration tests. --- .github/workflows/ci.yml | 8 +++++++- CONTRIBUTING.md | 22 +++++++++++++++++++++- package.json | 2 ++ test/integration/database.spec.ts | 22 ++++++++++++++++++---- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b5810ed895..72e66a2a12 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,10 +17,16 @@ jobs: uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - - name: Install, build and test + - name: Install and build run: | npm ci npm run build npm run build:tests + - name: Lint and run unit tests + run: | npm test npm run api-extractor + - name: Run emulator-based integration tests + run: | + npm install -g firebase-tools + npm run integration:emulator diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dbb374fa14..326e632093 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -123,6 +123,8 @@ There are two test suites: unit and integration. The unit test suite is intended development, and the integration test suite is intended to be run before packaging up release candidates. +#### Unit Tests + To run the unit test suite: ```bash @@ -135,7 +137,25 @@ If you wish to skip the linter, and only run the unit tests: $ npm run test:unit ``` -The integration tests run against an actual Firebase project. Create a new +#### Integration Tests with Emulator Suite + +Some of the integration tests work with the Emulator Suite and you can run them +without an actual Firebase project. + +First, make sure to [install Firebase CLI](https://firebase.google.com/docs/cli#install_the_firebase_cli). +And then: + +```bash +npm integration:emulator +``` + +Currently, only the Auth, Database, and Firestore test suites work. Some test +cases will be automatically skipped due to lack of emulator support. The section +below covers how to run the full test suite against an actual Firebase project. + +#### Integration Tests with an actual Firebase project + +Other integration tests require an actual Firebase project. Create a new project in the [Firebase Console](https://console.firebase.google.com), if you do not already have one suitable for running the tests against. Then obtain the following credentials from the project: diff --git a/package.json b/package.json index ee75e5e196..a15b40ef0b 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,10 @@ "lint": "run-p lint:src lint:test", "test": "run-s lint test:unit", "integration": "run-s build test:integration", + "integration:emulator": "run-s build test:integration:emulator", "test:unit": "mocha test/unit/*.spec.ts --require ts-node/register", "test:integration": "mocha test/integration/*.ts --slow 5000 --timeout 20000 --require ts-node/register", + "test:integration:emulator": "firebase emulators:exec --project fake-project-id --only auth,database,firestore 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register'", "test:coverage": "nyc npm run test:unit", "lint:src": "eslint src/ --ext .ts", "lint:test": "eslint test/ --ext .ts", diff --git a/test/integration/database.spec.ts b/test/integration/database.spec.ts index 708c3a153a..77204a1839 100644 --- a/test/integration/database.spec.ts +++ b/test/integration/database.spec.ts @@ -17,7 +17,7 @@ import * as admin from '../../lib/index'; import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; -import { defaultApp, nullApp, nonNullApp, cmdArgs, databaseUrl } from './setup'; +import { defaultApp, nullApp, nonNullApp, cmdArgs, databaseUrl, isEmulator } from './setup'; // eslint-disable-next-line @typescript-eslint/no-var-requires const chalk = require('chalk'); @@ -64,7 +64,13 @@ describe('admin.database', () => { .should.eventually.be.fulfilled; }); - it('App with null auth overrides is blocked by security rules', () => { + it('App with null auth overrides is blocked by security rules', function () { + if (isEmulator) { + // RTDB emulator has open security rules by default and won't block this. + // TODO(https://github.com/firebase/firebase-admin-node/issues/1149): + // remove this once updating security rules through admin is in place. + return this.skip(); + } return nullApp.database().ref('blocked').set(admin.database.ServerValue.TIMESTAMP) .should.eventually.be.rejectedWith('PERMISSION_DENIED: Permission denied'); }); @@ -157,13 +163,21 @@ describe('admin.database', () => { }); }); - it('admin.database().getRules() returns currently defined rules as a string', () => { + it('admin.database().getRules() returns currently defined rules as a string', function () { + if (isEmulator) { + // https://github.com/firebase/firebase-admin-node/issues/1149 + return this.skip(); + } return admin.database().getRules().then((result) => { return expect(result).to.be.not.empty; }); }); - it('admin.database().getRulesJSON() returns currently defined rules as an object', () => { + it('admin.database().getRulesJSON() returns currently defined rules as an object', function () { + if (isEmulator) { + // https://github.com/firebase/firebase-admin-node/issues/1149 + return this.skip(); + } return admin.database().getRulesJSON().then((result) => { return expect(result).to.be.not.undefined; }); From ab876b8cb2cbc89ce51fa7db3b1b95e1a9445b20 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Wed, 10 Feb 2021 09:22:59 -0800 Subject: [PATCH 2/6] Move emulator stuff out of package.json. --- .github/workflows/ci.yml | 5 ++++- package.json | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72e66a2a12..470ddc58fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,8 +25,11 @@ jobs: - name: Lint and run unit tests run: | npm test + - name: Run api-extractor + run: | npm run api-extractor - name: Run emulator-based integration tests run: | npm install -g firebase-tools - npm run integration:emulator + firebase emulators:exec --project fake-project-id --only auth,database,firestore \ + 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' diff --git a/package.json b/package.json index a15b40ef0b..ee75e5e196 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,8 @@ "lint": "run-p lint:src lint:test", "test": "run-s lint test:unit", "integration": "run-s build test:integration", - "integration:emulator": "run-s build test:integration:emulator", "test:unit": "mocha test/unit/*.spec.ts --require ts-node/register", "test:integration": "mocha test/integration/*.ts --slow 5000 --timeout 20000 --require ts-node/register", - "test:integration:emulator": "firebase emulators:exec --project fake-project-id --only auth,database,firestore 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register'", "test:coverage": "nyc npm run test:unit", "lint:src": "eslint src/ --ext .ts", "lint:test": "eslint test/ --ext .ts", From ca429c0066114cce55253fd2444fc3e14b7b4068 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Wed, 10 Feb 2021 09:25:28 -0800 Subject: [PATCH 3/6] Update CONTRIBUTING.md too. --- CONTRIBUTING.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 326e632093..7d82caf6a1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -146,7 +146,8 @@ First, make sure to [install Firebase CLI](https://firebase.google.com/docs/cli# And then: ```bash -npm integration:emulator + firebase emulators:exec --project fake-project-id --only auth,database,firestore \ + 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' ``` Currently, only the Auth, Database, and Firestore test suites work. Some test From a3f8072dcc28d940327813efaf7a1183d523b82c Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Wed, 10 Feb 2021 09:32:16 -0800 Subject: [PATCH 4/6] Add npx. --- .github/workflows/ci.yml | 2 +- CONTRIBUTING.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 470ddc58fd..67ca361f01 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,4 +32,4 @@ jobs: run: | npm install -g firebase-tools firebase emulators:exec --project fake-project-id --only auth,database,firestore \ - 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' + 'npx mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7d82caf6a1..7ac6a71cb1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -147,7 +147,7 @@ And then: ```bash firebase emulators:exec --project fake-project-id --only auth,database,firestore \ - 'mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' + 'npx mocha \"test/integration/{auth,database,firestore}.spec.ts\" --slow 5000 --timeout 20000 --require ts-node/register' ``` Currently, only the Auth, Database, and Firestore test suites work. Some test From d053f3e50b44ddface7e2bec416e9d0491eda995 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Wed, 10 Feb 2021 09:44:40 -0800 Subject: [PATCH 5/6] Skip new unsupported tests. --- test/integration/auth.spec.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/integration/auth.spec.ts b/test/integration/auth.spec.ts index bf9f7f134d..29060b4cdd 100644 --- a/test/integration/auth.spec.ts +++ b/test/integration/auth.spec.ts @@ -683,7 +683,10 @@ describe('admin.auth', () => { }); }); - it('can link/unlink with a federated provider', async () => { + it('can link/unlink with a federated provider', async function () { + if (authEmulatorHost) { + return this.skip(); // Not yet supported in Auth Emulator. + } const googleFederatedUid = 'google_uid_' + generateRandomString(10); let userRecord = await admin.auth().updateUser(updateUser.uid, { providerToLink: { @@ -707,7 +710,10 @@ describe('admin.auth', () => { expect(providerIds).to.not.deep.include('google.com'); }); - it('can unlink multiple providers at once, incl a non-federated provider', async () => { + it('can unlink multiple providers at once, incl a non-federated provider', async function () { + if (authEmulatorHost) { + return this.skip(); // Not yet supported in Auth Emulator. + } await deletePhoneNumberUser('+15555550001'); const googleFederatedUid = 'google_uid_' + generateRandomString(10); From 1bec96d760776a26745699789de3a97e6fe6b6c8 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Thu, 11 Feb 2021 09:09:47 -0800 Subject: [PATCH 6/6] Inline commands in ci.yml. --- .github/workflows/ci.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67ca361f01..244d123dd6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,11 +23,9 @@ jobs: npm run build npm run build:tests - name: Lint and run unit tests - run: | - npm test + run: npm test - name: Run api-extractor - run: | - npm run api-extractor + run: npm run api-extractor - name: Run emulator-based integration tests run: | npm install -g firebase-tools