Skip to content

Commit 9fe378d

Browse files
Provides contributors with a prompt to choose whether to overwrite, s… (#61)
* Provides contributors with a prompt to choose whether to overwrite, skip or abort the rules overwrite necessary for running integration tests. * Adds support for passing an optional command line flag to integration test to specify whether Database rules should be overwritten or not.
1 parent 1666011 commit 9fe378d

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

test/integration/index.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* Runs Firebase Admin SDK integration tests.
19+
* Usage:
20+
* node index.js
21+
* Where the following configuration files need to be provided:
22+
* test/resources/key.json: The service account key file.
23+
* test/resources/apikey.txt: The API key for the same project.
24+
*
25+
* Accepts an optional flag to specify whether to overwrite
26+
* the specified project's Database rules or skip that step.
27+
* node index.js --overwrite yes|skip
28+
*/
29+
1730
var _ = require('lodash');
1831
var chalk = require('chalk');
32+
var readline = require('readline');
1933

2034
var admin = require('../../lib/index');
2135

@@ -78,6 +92,66 @@ utils.assert(
7892
'App instances do not match.'
7993
);
8094

95+
/**
96+
* Prompts the developer whether the Database rules should be
97+
* overwritten with the relevant rules for the integration tests.
98+
* The developer has 3 options:
99+
* yes/y to agree to the rules overwrite.
100+
* skip to skip the overwrite (rules already manually configured) and continue
101+
* with the tests.
102+
* no/n or other to abort.
103+
* @param {*|undefined} overwrite An optional answer that can be provided to
104+
* bypass the need to prompt the user whether to proceed with, skip or abort
105+
* the Database rules overwrite.
106+
* @return {Promise} A promise that resolves when the rules change is processed.
107+
*/
108+
function promptForUpdateRules(overwrite) {
109+
return new Promise(function(resolve, reject) {
110+
// Overwrite answer already provided.
111+
if (typeof overwrite === 'string') {
112+
resolve(overwrite);
113+
return;
114+
}
115+
// Defines prompt interface.
116+
var rl = readline.createInterface({
117+
input: process.stdin,
118+
output: process.stdout
119+
});
120+
// Options to display to end user.
121+
var question = 'Warning: This test will overwrite your ';
122+
question += 'project\'s existing Database rules.\n';
123+
question += 'Overwrite Database rules for tests?\n';
124+
// Yes to overwrite the rules.
125+
question += '* \'yes\' to agree\n'
126+
// Skip to continue without overwriting the rules.
127+
question += '* \'skip\' to continue without the overwrite\n';
128+
// No to cancel.
129+
question += '* \'no\' to cancel\n';
130+
// Prompt user with the 3 options.
131+
rl.question(question, function(answer) {
132+
rl.close();
133+
// Resolve the promise with the answer.
134+
resolve(answer);
135+
});
136+
})
137+
.then(function(answer) {
138+
switch (answer.toLowerCase()) {
139+
case 'y':
140+
case 'yes':
141+
// Proceed and update the rules.
142+
return updateRules();
143+
case 'skip':
144+
// Continue without updating the rules.
145+
return Promise.resolve();
146+
case 'no':
147+
case 'n':
148+
default:
149+
// Abort and exit.
150+
throw new Error('Integration test aborted!');
151+
}
152+
});
153+
}
154+
81155
function updateRules() {
82156
// Update database rules to the defaults. Rest of the test suite
83157
// expects it.
@@ -96,8 +170,38 @@ function updateRules() {
96170
'PUT', defaultRules, headers, 10000);
97171
}
98172

99-
return Promise.resolve()
100-
.then(updateRules)
173+
/**
174+
* Parses the script arguments and returns all the provided flags.
175+
* @param {Array<string>} argv The process.argv list of script arguments.
176+
* @return {Object} A key/value object with the provided script flags and
177+
* their values.
178+
*/
179+
function getScriptArguments(argv) {
180+
// Dictionary of flags.
181+
var flags = {};
182+
var lastFlag = null;
183+
// Skip first 2 arguments: node index.js
184+
// Expected format: --flagA valueA --flagB --flagC valueC
185+
for (var i = 2; i < argv.length; i++) {
186+
if (argv[i].indexOf('--') === 0) {
187+
// Get the last flag name.
188+
lastFlag = argv[i].substr(2);
189+
// If flag passed with no argument, set to true.
190+
flags[lastFlag] = true;
191+
} else if (lastFlag) {
192+
// Argument provided for last flag, overwrite its value.
193+
flags[lastFlag] = argv[i];
194+
lastFlag = null;
195+
} else {
196+
// Value provided without a flag name.
197+
throw new Error('Invalid script argument: "' + argv[i] + '"!');
198+
}
199+
}
200+
return flags;
201+
}
202+
203+
var flags = getScriptArguments(process.argv);
204+
return promptForUpdateRules(flags['overwrite'])
101205
.then(_.partial(app.test, utils))
102206
.then(_.partial(auth.test, utils))
103207
.then(_.partial(database.test, utils))

0 commit comments

Comments
 (0)