14
14
* limitations under the License.
15
15
*/
16
16
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
+
17
30
var _ = require ( 'lodash' ) ;
18
31
var chalk = require ( 'chalk' ) ;
32
+ var readline = require ( 'readline' ) ;
19
33
20
34
var admin = require ( '../../lib/index' ) ;
21
35
@@ -78,6 +92,66 @@ utils.assert(
78
92
'App instances do not match.'
79
93
) ;
80
94
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
+
81
155
function updateRules ( ) {
82
156
// Update database rules to the defaults. Rest of the test suite
83
157
// expects it.
@@ -96,8 +170,38 @@ function updateRules() {
96
170
'PUT' , defaultRules , headers , 10000 ) ;
97
171
}
98
172
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' ] )
101
205
. then ( _ . partial ( app . test , utils ) )
102
206
. then ( _ . partial ( auth . test , utils ) )
103
207
. then ( _ . partial ( database . test , utils ) )
0 commit comments