-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsimulator.ts
368 lines (314 loc) · 9.57 KB
/
simulator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Firebase Rules test simulator.
*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {assert} from 'chai';
import * as rest from './firebase-rest';
import * as util from './util';
import * as fileIO from './file-io';
import * as bolt from './bolt';
let generate = util.lift(bolt.generate);
let readFile = util.liftArgs(fileIO.readFile);
var MAX_TEST_MS = 60000;
export type SuiteFunction = (fn: TestFunction) => void;
// Interface for 'test' function passed back to rulesSuite callback.
// test is a function as well as a namespace for some static methods
// and constants.
export interface TestFunction {
(name: string, fnTest: (rules: RulesTest) => void): void;
rules: (path: string) => void;
database: (appName: string, secret: string) => void;
uid: (username: string) => string;
TIMESTAMP: Object;
};
export function rulesSuite(suiteName: string, fnSuite: SuiteFunction) {
new RulesSuite(suiteName, fnSuite).run();
}
export class RulesSuite {
public debug = false;
private users = <{[name: string]: rest.Client}>{};
private tests = <RulesTest[]>[];
private rulesPath: string;
private rulesPathResolve: (path: string) => void;
private databaseReady: () => void;
private ready: Promise<any>;
private adminClient: rest.Client;
private appName: string;
private appSecret: string;
constructor(public suiteName: string,
private fnSuite: SuiteFunction) {}
setDebug(debug = true) {
this.debug = debug;
return this;
}
run() {
var self = this;
// Run Mocha Test Suite - serialize with any other mocha test suites.
suite("Firebase Rules Simulator: " + self.suiteName, function() {
this.timeout(MAX_TEST_MS);
suiteSetup(function() {
var rulesPath = new Promise(function(resolve) {
self.rulesPathResolve = resolve;
});
var database = new Promise(function(resolve) {
self.databaseReady = resolve;
});
var rulesJSON = generate(util.getProp(readFile(rulesPath), 'content'));
self.ready = Promise.all([rulesJSON, database])
.then(self.onRulesReady.bind(self));
// Execute initialization and get test definitions (in client code).
self.fnSuite(self.getInterface());
return self.ready;
});
test("Initialization.", function() {
// pass
});
test("Rules test.", function() {
return self.runTests();
});
});
}
getInterface() {
var test = this.test.bind(this);
test.rules = this.rules.bind(this);
test.database = this.database.bind(this);
test.uid = this.uid.bind(this);
test.TIMESTAMP = rest.TIMESTAMP;
return test;
}
// Called when rules are generated and test database is known.
onRulesReady(prereq: [Object, any]) {
let rulesJSON = prereq[0];
return this.adminClient.put(rest.RULES_LOCATION, rulesJSON);
}
runTests() {
var p = Promise.resolve();
function next(prev: Promise<any>, test: RulesTest): Promise<any> {
return prev.then(function() {
return test.run();
});
}
for (var i = 0; i < this.tests.length; i++) {
p = next(p, this.tests[i]);
}
return p;
}
test(testName: string, fnTest: (rules: RulesTest) => void): void {
this.tests.push(new RulesTest(testName, this, fnTest));
}
rules(rulesPath: string): void {
if (this.rulesPath) {
throw new Error("Only expect a single call to the test.rules function.");
}
this.rulesPath = rulesPath;
this.rulesPathResolve(util.ensureExtension(rulesPath, bolt.FILE_EXTENSION));
}
database(appName: string, appSecret: string) {
if (this.adminClient) {
throw new Error("Only expect a single call to the test.database function.");
}
this.appName = appName;
this.appSecret = appSecret;
this.adminClient = this.ensureUser('admin');
this.databaseReady();
}
uid(username: string): string | undefined {
return this.ensureUser(username).uid;
}
ensureUser(username: string): rest.Client {
if (!(username in this.users)) {
if (username === 'anon') {
this.users[username] = new rest.Client(this.appName);
} else {
let tokenInfo = rest.generateUidAuthToken(
this.appSecret,
{ debug: true,
admin: username === 'admin' });
this.users[username] = new rest.Client(this.appName, tokenInfo.token, tokenInfo.uid);
}
}
return this.users[username];
}
}
interface Step {
label: string;
fn: () => Promise<any>;
}
export class RulesTest {
private lastError: string;
private steps: Step[] = [];
private failed = false;
private path: string | undefined;
private client: rest.Client;
private status: boolean | undefined;
constructor(private testName: string,
private suite: RulesSuite,
private fnTest: (rules: RulesTest) => void) {}
run() {
this.debug(false);
this.as('admin');
this.at('/');
this.write(null);
this.succeeds("initialization");
this.at(undefined);
this.as('anon');
this.fnTest(this);
this.debug(false);
return this.executeQueue()
.then(() => {
this.log("Finished");
})
.catch((error) => {
this.log("Failed: " + error);
throw error;
});
}
// Queue a function to be called in sequence after previous step
// in test is (successfully) completed.
queue(op: string, args: ArrayLike<any>, fn: () => Promise<any>) {
if (this.failed) {
return;
}
let argsT = util.copyArray(args).map(function(x) {
return util.prettyJSON(x);
});
var label = op + '(' + argsT.join(', ') + ')';
this.steps.push({label: label, fn: fn});
}
executeQueue() {
var self = this;
this.log("Executing (" + this.steps.length + " steps)");
var p = Promise.resolve(true);
function next(prev: Promise<any>, step: Step): Promise<any> {
return prev.then(function() {
self.log(step.label);
return step.fn();
});
}
for (var i = 0; i < this.steps.length; i++) {
p = next(p, this.steps[i]);
}
return p;
}
debug(debug?: boolean): RulesTest {
this.suite.setDebug(debug);
this.queue('debug', arguments, () => {
this.suite.setDebug(debug);
return Promise.resolve();
});
return this;
}
as(username: string): RulesTest {
var client = this.suite.ensureUser(username);
this.queue('as', arguments, () => {
client.setDebug(this.suite.debug);
this.client = client;
return Promise.resolve();
});
return this;
}
at(opPath: string | undefined): RulesTest {
this.queue('at', arguments, () => {
this.path = opPath;
return Promise.resolve();
});
return this;
}
write(obj: any): RulesTest {
this.queue('write', arguments, () => {
if (this.path === undefined) {
return Promise.reject(new Error("Use at() function to set path to write."));
}
return this.client.put(this.path, obj)
.then(() => {
this.status = true;
})
.catch((error) => {
this.status = false;
this.lastError = error;
});
});
return this;
}
push(obj: any): RulesTest {
this.queue('write', arguments, () => {
if (this.path === undefined) {
return Promise.reject(new Error("Use at() function to set path to push."));
}
let path = this.path;
if (path.slice(-1)[0] !== '/') {
path += '/';
}
path += rest.generatePushID();
return this.client.put(path, obj)
.then(() => {
this.status = true;
})
.catch((error) => {
this.status = false;
this.lastError = error;
});
});
return this;
}
read(): RulesTest {
this.queue('read', arguments, () => {
if (this.path === undefined) {
return Promise.reject(new Error("Use at() function to set path to read."));
}
return this.client.get(this.path)
.then(() => {
this.status = true;
})
.catch((error) => {
this.status = false;
this.lastError = error;
});
});
return this;
}
succeeds(message: string): RulesTest {
this.queue('succeeds', arguments, () => {
assert(this.status === true,
this.messageFormat(message + " (should have succeed)\n" + this.lastError));
this.good(message);
this.status = undefined;
return Promise.resolve();
});
return this;
}
fails(message: string): RulesTest {
this.queue('fails', arguments, () => {
assert(this.status === false,
this.messageFormat(message + " (should have failed)"));
this.good(message);
this.status = undefined;
return Promise.resolve();
});
return this;
}
private good(message: string): void {
this.log(message + " (Correct)");
}
private log(message: string): void {
if (this.suite.debug) {
console.log(this.messageFormat(message));
}
}
messageFormat(message: string): string {
return this.suite.suiteName + "." + this.testName + " " + message;
}
}