From 8c8c4fa1cec8ad5f0dcec9cb2882cd839058b8a5 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Tue, 9 May 2017 14:43:06 -0700 Subject: [PATCH 1/2] Making it possible to run integration tests againsty any Firebase project --- CONTRIBUTING.md | 15 ++++++++----- test/integration/app.js | 2 +- test/integration/auth.js | 5 +++-- test/integration/index.js | 19 +++++----------- test/integration/utils.js | 47 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5d55a01fcd..90b9af67b5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -134,14 +134,17 @@ $ gulp # Lint, build, and run unit test suite To run the integration test suite: ``` -$ node test/integration # Run integration test suite +$ node test/integration # Run integration test suite ``` -The integration test suite requires you to generate a service account key JSON file for the -**admin-sdks-test** Firebase project and save it to `test/resources/key.json`. You can generate this -from the -[**Service Accounts**](https://console.firebase.google.com/project/admin-sdks-test/settings/serviceaccounts/adminsdk) -tab of that project's settings page. +The integration test suite requires you to generate a service account key JSON file for a +Firebase project and save it to `test/resources/key.json`. Create a new project in the +[Firebase console](https://console.firebase.google.com) if you do not already have one. +Use a separate, dedicated project for integration tests since the test suite makes a large +number of writes to the Firebase realtime database. Download the service account key file +from the "Settings > Service Accounts" page of the project, and copy it to +`test/resources/key.json`. Also obtain the API key for the same project from "Settings > General". +This API key should be passed into the test suite as a command-line argument. ### Repo Organization diff --git a/test/integration/app.js b/test/integration/app.js index 74c0d69769..8285496125 100644 --- a/test/integration/app.js +++ b/test/integration/app.js @@ -40,7 +40,7 @@ function test(utils) { ); utils.assert( - defaultApp.options.databaseURL === 'https://admin-sdks-test.firebaseio.com', + defaultApp.options.databaseURL === 'https://' + utils.getProjectId() + '.firebaseio.com', 'app.options.databaseURL', 'databaseURL is incorrect.' ); diff --git a/test/integration/auth.js b/test/integration/auth.js index fac94ce182..e43f5adc96 100644 --- a/test/integration/auth.js +++ b/test/integration/auth.js @@ -18,10 +18,11 @@ var _ = require('lodash'); var firebase = require('firebase'); var admin = require('../../lib/index'); +var testutils = require('./utils'); firebase.initializeApp({ - apiKey: "AIzaSyB9merDS-vSCzloW_WpTibO_-NzgoFR2JA", - authDomain: "admin-sdks-test.firebaseapp.com", + apiKey: testutils.getApiKey(), + authDomain: testutils.getProjectId() + ".firebaseapp.com", }); function test(utils) { diff --git a/test/integration/index.js b/test/integration/index.js index b59efdaede..97a3093545 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -26,32 +26,23 @@ var auth = require('./auth'); var database = require('./database'); var messaging = require('./messaging'); -var serviceAccount; -try { - serviceAccount = require('../resources/key.json'); -} catch(error) { - console.log(chalk.red( - 'The integration test suite requires a service account key JSON file for the ' + - '`admin-sdks-test` project to be saved to `test/resources/key.json`.', - error - )); - process.exit(1); -} +var serviceAccount = utils.getCredential(); +var databaseURL = 'https://' + utils.getProjectId() + '.firebaseio.com'; var defaultApp = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), - databaseURL: 'https://admin-sdks-test.firebaseio.com', + databaseURL: databaseURL, }); var nullApp = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), - databaseURL: 'https://admin-sdks-test.firebaseio.com', + databaseURL: databaseURL, databaseAuthVariableOverride: null, }, 'null'); var nonNullApp = admin.initializeApp({ credential: admin.credential.cert(serviceAccount), - databaseURL: 'https://admin-sdks-test.firebaseio.com', + databaseURL: databaseURL, databaseAuthVariableOverride: { uid: utils.generateRandomString(20), }, diff --git a/test/integration/utils.js b/test/integration/utils.js index 3d17605ce0..110e758088 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -23,6 +23,48 @@ var successCount = 0; var failure = chalk.red; var success = chalk.green; +var serviceAccount; + +try { + serviceAccount = require('../resources/key.json'); +} catch(error) { + console.log(chalk.red( + 'The integration test suite requires a service account key JSON file for a ' + + 'Firebase project to be saved to `test/resources/key.json`.', + error + )); + process.exit(1); +} + +var apiKey = process.argv[2]; +if (apiKey == undefined) { + console.log(chalk.red( + 'The integration test suite requires a API key for a ' + + 'Firebase project to be specified as a command-line argument.')); + process.exit(1); +} + +/** + * Returns the service account credential used for runnnig integration tests. + * + * @return {Object} A service account credential. + */ +function getCredential() { + return serviceAccount; +} + +/** + * Returns the ID of the project the integration tests are executed against. + * + * @return {string} A project ID. + */ +function getProjectId() { + return serviceAccount.project_id; +} + +function getApiKey() { + return apiKey; +} /** * Logs a message to the console in green text. @@ -125,5 +167,8 @@ module.exports = { logFailure: logFailure, logSuccess: logSuccess, logResults: logResults, - generateRandomString: generateRandomString + generateRandomString: generateRandomString, + getCredential: getCredential, + getProjectId: getProjectId, + getApiKey: getApiKey } From 97390342f704e4f8b1648f078d93d811fa39c170 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Thu, 11 May 2017 10:05:54 -0700 Subject: [PATCH 2/2] Added method comment; Better check for API key argument --- test/integration/utils.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/integration/utils.js b/test/integration/utils.js index 110e758088..8a0a9350df 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -37,9 +37,9 @@ try { } var apiKey = process.argv[2]; -if (apiKey == undefined) { +if (typeof apiKey === 'undefined') { console.log(chalk.red( - 'The integration test suite requires a API key for a ' + + 'The integration test suite requires an API key for a ' + 'Firebase project to be specified as a command-line argument.')); process.exit(1); } @@ -62,6 +62,11 @@ function getProjectId() { return serviceAccount.project_id; } +/** + * Returns the API key of the project the integration tests are executed against. + * + * @return {string} A Firebase API key. + */ function getApiKey() { return apiKey; }