This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
firestore.rules.test.js
340 lines (324 loc) · 9.23 KB
/
firestore.rules.test.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
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
import "babel-polyfill";
import path from "path";
import * as firestore from "expect-firestore";
let describeButSkipIfNoKey = describe;
let serviceAccountKey;
try {
serviceAccountKey = require(".service-account-key.json");
} catch (e) {
describeButSkipIfNoKey = describe.skip;
}
const TEST_DATA = {
organizations: [
{
key: "1",
fields: { name: "Org 1" },
collections: {}
},
{
key: "2",
fields: { name: "Org 2" },
collections: {}
}
],
profiles: [
{
key: "org1",
fields: { organization: "organizations/1" },
collections: {}
},
{
key: "org2",
fields: { organization: "organizations/2" },
collections: {}
}
]
};
describeButSkipIfNoKey("firestore.rules", () => {
let database;
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
database = new firestore.Database({
credential: serviceAccountKey
});
await database.authorize();
database.setRulesFromFile(path.join(__dirname, "../../firestore.rules"));
});
afterAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
});
describe("/organizations", () => {
beforeEach(() => {
database.setData({
...TEST_DATA
});
});
test.skip("organizations can be read by anyone in that organization", async () => {
firestore.assert(
await database.canGet({ uid: "org1" }, "organizations/1")
);
firestore.assert(
await database.cannotGet({ uid: "org1" }, "organizations/2")
);
});
// Not supported
test.skip("organizations cannot be listed", () => {});
test.skip("creating organizations", () => {});
test("organizations cannot be updated", async () => {
firestore.assert(
await database.cannotUpdate({ uid: "org1" }, "organizations/1", {
name: "Blah"
})
);
});
test("organizations cannot be deleted", async () => {
firestore.assert(
await database.cannotCommit({ uid: "org1" }, [
firestore.Batch.delete("organisations/1")
])
);
});
});
describe("/boxes", () => {
beforeEach(() => {
database.setData({
...TEST_DATA,
products: [
{
key: "1",
fields: {
name: "Socks",
organization: "organizations/1",
isDeleted: false
},
collections: {}
},
{
key: "2",
fields: {
name: "T-Shirts",
organization: "organizations/2",
isDeleted: false
},
collections: {}
}
],
boxes: [
{
key: "1",
fields: {
organization: "organizations/1",
product: "products/1",
quantity: 5,
humanID: 123456
},
collections: {}
},
{
key: "2",
fields: {
organization: "organizations/2",
product: "products/2",
quantity: 10,
humanID: 654321
},
collections: {}
}
]
});
});
test("boxes can only be read by a user in that organization", async () => {
firestore.assert(await database.canGet({ uid: "org1" }, "boxes/1"));
firestore.assert(await database.canGet({ uid: "org2" }, "boxes/2"));
firestore.assert(await database.cannotGet({ uid: "org1" }, "boxes/2"));
firestore.assert(await database.cannotGet({ uid: "org2" }, "boxes/1"));
});
test("boxes can only be created for an organization by a user in that organization", async () => {
firestore.assert(
await database.canSet({ uid: "org1" }, "boxes/5437890", {
product: "products/1",
organization: "organizations/1"
})
);
firestore.assert(
await database.cannotSet({ uid: "org1" }, "boxes/03248", {
product: "products/1",
organization: "organizations/2"
})
);
});
test.skip("boxes can only be created that pointed to products owned by the organization", async () => {
firestore.assert(
await database.cannotSet({ uid: "org1" }, "boxes/03248fdshjk", {
product: "products/2",
organization: "organizations/1"
})
);
});
test("boxes cannot be updated", async () => {
firestore.assert(
await database.cannotUpdate({ uid: "org1" }, "boxes/1", {
product: "products/2",
organization: "organizations/1"
})
);
});
test("boxes cannot be deleted", async () => {
firestore.assert(
await database.cannotCommit({ uid: "org1" }, [
firestore.Batch.delete("boxes/1")
])
);
});
});
describe("/products", () => {
beforeEach(() => {
database.setData({
...TEST_DATA,
products: [
{
key: "1",
fields: {
name: "Socks",
organization: "organizations/1"
},
collections: {}
}
]
});
});
test("products can only be created for an organization by a user in that organization", async () => {
firestore.assert(
await database.canSet({ uid: "org1" }, "products/5437890", {
name: "T-Shirts",
organization: "organizations/1"
})
);
firestore.assert(
await database.cannotSet({ uid: "org1" }, "products/03248", {
name: "Underwear",
organization: "organizations/2"
})
);
});
test("products can only be updated by correct organization", async () => {
firestore.assert(
await database.canUpdate({ uid: "org1" }, "products/1", {
name: "Long Socks",
organization: "organizations/1"
})
);
firestore.assert(
await database.cannotUpdate({ uid: "org2" }, "products/1", {
name: "Long Socks",
organization: "organizations/1"
})
);
});
test("organization cannot be changed", async () => {
firestore.assert(
await database.cannotUpdate({ uid: "org1" }, "products/1", {
name: "Long Socks",
organization: "organization/2"
})
);
});
test("products can only be deleted by correct organization", async () => {
firestore.assert(
await database.cannotCommit({ uid: "org2" }, [
firestore.Batch.delete("products/1")
])
);
firestore.assert(
await database.canCommit({ uid: "org1" }, [
firestore.Batch.delete("products/1")
])
);
});
});
describe("/invites", () => {
beforeEach(() => {
database.setData({
...TEST_DATA,
invites: [
{
key: "1",
fields: {
organization: "organizations/1"
},
collections: {}
}
]
});
});
test("invites can only be created for an organization by a user in that organization", async () => {
firestore.assert(
await database.canSet({ uid: "org1" }, "invites/5437890", {
organization: "organizations/1"
})
);
firestore.assert(
await database.cannotSet({ uid: "org1" }, "invites/03248", {
organization: "organizations/2"
})
);
});
test("unauthenticated users can get invites", async () => {
firestore.assert(await database.canGet({}, "invites/1"));
});
// Unsupported: https://github.com/GitbookIO/expect-firestore/issues/11
test.skip("invites can only be listed by organization", async () => {});
test("invites cannot be updated", async () => {
firestore.assert(
await database.cannotUpdate({ uid: "org1" }, "invites/1", {
organization: "organizations/org1"
})
);
});
test("invites cannot be deleted", async () => {
firestore.assert(
await database.cannotCommit({ uid: "org1" }, [
firestore.Batch.delete("invites/1")
])
);
});
});
describe("/profiles", () => {
beforeEach(() => {
database.setData({});
});
test("profiles can only be read by its user", async () => {
database.setData(TEST_DATA);
firestore.assert(await database.canGet({ uid: "org1" }, "profiles/org1"));
firestore.assert(
await database.cannotGet({ uid: "org1" }, "profiles/org2")
);
});
test("profiles for a user can only be created by that user", async () => {
firestore.assert(
await database.cannotSet({ uid: "org1" }, "profiles/03248", {
organization: "organizations/1"
})
);
firestore.assert(
await database.canSet({ uid: "org1" }, "profiles/org1", {
organization: "organizations/1"
})
);
});
test("profiles cannot be updated", async () => {
firestore.assert(
await database.cannotUpdate({ uid: "org1" }, "profiles/org1", {
organization: "organizations/org1"
})
);
});
test("invites cannot be deleted", async () => {
firestore.assert(
await database.cannotCommit({ uid: "org1" }, [
firestore.Batch.delete("profiles/org1")
])
);
});
});
});