Skip to content

Commit 297f935

Browse files
authored
Making it possible to run integration tests against any Firebase project (#22)
* Making it possible to run integration tests againsty any Firebase project * Added method comment; Better check for API key argument
1 parent d1db69f commit 297f935

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,17 @@ $ gulp # Lint, build, and run unit test suite
134134
To run the integration test suite:
135135

136136
```
137-
$ node test/integration # Run integration test suite
137+
$ node test/integration <apiKey> # Run integration test suite
138138
```
139139

140-
The integration test suite requires you to generate a service account key JSON file for the
141-
**admin-sdks-test** Firebase project and save it to `test/resources/key.json`. You can generate this
142-
from the
143-
[**Service Accounts**](https://console.firebase.google.com/project/admin-sdks-test/settings/serviceaccounts/adminsdk)
144-
tab of that project's settings page.
140+
The integration test suite requires you to generate a service account key JSON file for a
141+
Firebase project and save it to `test/resources/key.json`. Create a new project in the
142+
[Firebase console](https://console.firebase.google.com) if you do not already have one.
143+
Use a separate, dedicated project for integration tests since the test suite makes a large
144+
number of writes to the Firebase realtime database. Download the service account key file
145+
from the "Settings > Service Accounts" page of the project, and copy it to
146+
`test/resources/key.json`. Also obtain the API key for the same project from "Settings > General".
147+
This API key should be passed into the test suite as a command-line argument.
145148

146149
### Repo Organization
147150

test/integration/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function test(utils) {
4040
);
4141

4242
utils.assert(
43-
defaultApp.options.databaseURL === 'https://admin-sdks-test.firebaseio.com',
43+
defaultApp.options.databaseURL === 'https://' + utils.getProjectId() + '.firebaseio.com',
4444
'app.options.databaseURL',
4545
'databaseURL is incorrect.'
4646
);

test/integration/auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ var _ = require('lodash');
1818
var firebase = require('firebase');
1919

2020
var admin = require('../../lib/index');
21+
var testutils = require('./utils');
2122

2223
firebase.initializeApp({
23-
apiKey: "AIzaSyB9merDS-vSCzloW_WpTibO_-NzgoFR2JA",
24-
authDomain: "admin-sdks-test.firebaseapp.com",
24+
apiKey: testutils.getApiKey(),
25+
authDomain: testutils.getProjectId() + ".firebaseapp.com",
2526
});
2627

2728
function test(utils) {

test/integration/index.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,23 @@ var auth = require('./auth');
2626
var database = require('./database');
2727
var messaging = require('./messaging');
2828

29-
var serviceAccount;
30-
try {
31-
serviceAccount = require('../resources/key.json');
32-
} catch(error) {
33-
console.log(chalk.red(
34-
'The integration test suite requires a service account key JSON file for the ' +
35-
'`admin-sdks-test` project to be saved to `test/resources/key.json`.',
36-
error
37-
));
38-
process.exit(1);
39-
}
29+
var serviceAccount = utils.getCredential();
30+
var databaseURL = 'https://' + utils.getProjectId() + '.firebaseio.com';
4031

4132
var defaultApp = admin.initializeApp({
4233
credential: admin.credential.cert(serviceAccount),
43-
databaseURL: 'https://admin-sdks-test.firebaseio.com',
34+
databaseURL: databaseURL,
4435
});
4536

4637
var nullApp = admin.initializeApp({
4738
credential: admin.credential.cert(serviceAccount),
48-
databaseURL: 'https://admin-sdks-test.firebaseio.com',
39+
databaseURL: databaseURL,
4940
databaseAuthVariableOverride: null,
5041
}, 'null');
5142

5243
var nonNullApp = admin.initializeApp({
5344
credential: admin.credential.cert(serviceAccount),
54-
databaseURL: 'https://admin-sdks-test.firebaseio.com',
45+
databaseURL: databaseURL,
5546
databaseAuthVariableOverride: {
5647
uid: utils.generateRandomString(20),
5748
},

test/integration/utils.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ var successCount = 0;
2323
var failure = chalk.red;
2424
var success = chalk.green;
2525

26+
var serviceAccount;
27+
28+
try {
29+
serviceAccount = require('../resources/key.json');
30+
} catch(error) {
31+
console.log(chalk.red(
32+
'The integration test suite requires a service account key JSON file for a ' +
33+
'Firebase project to be saved to `test/resources/key.json`.',
34+
error
35+
));
36+
process.exit(1);
37+
}
38+
39+
var apiKey = process.argv[2];
40+
if (typeof apiKey === 'undefined') {
41+
console.log(chalk.red(
42+
'The integration test suite requires an API key for a ' +
43+
'Firebase project to be specified as a command-line argument.'));
44+
process.exit(1);
45+
}
46+
47+
/**
48+
* Returns the service account credential used for runnnig integration tests.
49+
*
50+
* @return {Object} A service account credential.
51+
*/
52+
function getCredential() {
53+
return serviceAccount;
54+
}
55+
56+
/**
57+
* Returns the ID of the project the integration tests are executed against.
58+
*
59+
* @return {string} A project ID.
60+
*/
61+
function getProjectId() {
62+
return serviceAccount.project_id;
63+
}
64+
65+
/**
66+
* Returns the API key of the project the integration tests are executed against.
67+
*
68+
* @return {string} A Firebase API key.
69+
*/
70+
function getApiKey() {
71+
return apiKey;
72+
}
2673

2774
/**
2875
* Logs a message to the console in green text.
@@ -125,5 +172,8 @@ module.exports = {
125172
logFailure: logFailure,
126173
logSuccess: logSuccess,
127174
logResults: logResults,
128-
generateRandomString: generateRandomString
175+
generateRandomString: generateRandomString,
176+
getCredential: getCredential,
177+
getProjectId: getProjectId,
178+
getApiKey: getApiKey
129179
}

0 commit comments

Comments
 (0)