Skip to content

Making it possible to run integration tests against any Firebase project #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2017
Merged
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
15 changes: 9 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <apiKey> # 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

Expand Down
2 changes: 1 addition & 1 deletion test/integration/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand Down
5 changes: 3 additions & 2 deletions test/integration/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 5 additions & 14 deletions test/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
52 changes: 51 additions & 1 deletion test/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,53 @@ 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 (typeof apiKey === 'undefined') {
console.log(chalk.red(
'The integration test suite requires an 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;
}

/**
* Returns the API key of the project the integration tests are executed against.
*
* @return {string} A Firebase API key.
*/
function getApiKey() {
return apiKey;
}

/**
* Logs a message to the console in green text.
Expand Down Expand Up @@ -125,5 +172,8 @@ module.exports = {
logFailure: logFailure,
logSuccess: logSuccess,
logResults: logResults,
generateRandomString: generateRandomString
generateRandomString: generateRandomString,
getCredential: getCredential,
getProjectId: getProjectId,
getApiKey: getApiKey
}