-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.js
63 lines (54 loc) · 1.28 KB
/
helpers.js
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
const firebase = require('@firebase/testing');
const fs = require('fs');
module.exports.setup = async (auth, data) => {
const projectId = `rules-spec-${Date.now()}`;
const app = await firebase.initializeTestApp({
projectId,
auth
});
const db = app.firestore();
const dbAdmin = firebase.initializeAdminApp({projectId}).firestore();
// Write mock documents before rules
if (data) {
for (const key in data) {
const ref = dbAdmin.doc(key);
await ref.set(data[key]);
}
}
// Apply rules
await firebase.loadFirestoreRules({
projectId,
rules: fs.readFileSync('firestore.rules', 'utf8')
});
return db;
};
module.exports.teardown = async () => {
Promise.all(firebase.apps().map(app => app.delete()));
};
expect.extend({
async toAllow(x) {
let pass = false;
try {
await firebase.assertSucceeds(x);
pass = true;
} catch (err) {}
return {
pass,
message: () => 'Expected Firebase operation to be allowed, but it failed'
};
}
});
expect.extend({
async toDeny(x) {
let pass = false;
try {
await firebase.assertFails(x);
pass = true;
} catch (err) {}
return {
pass,
message: () =>
'Expected Firebase operation to be denied, but it was allowed'
};
}
});