|
1 | 1 | const firebase = require("@firebase/testing"); |
2 | 2 | const fs = require("fs"); |
| 3 | +const projectId = "rules-codelab"; |
3 | 4 |
|
4 | | -/* |
5 | | - * ============ |
6 | | - * Setup |
7 | | - * ============ |
8 | | - */ |
9 | | -const projectId = "firestore-emulator-example"; |
10 | | -const coverageUrl = `http://localhost:8080/emulator/v1/projects/${projectId}:ruleCoverage.html`; |
11 | | - |
12 | | -const rules = fs.readFileSync("firestore.rules", "utf8"); |
13 | | - |
14 | | -/** |
15 | | - * Creates a new app with authentication data matching the input. |
16 | | - * |
17 | | - * @param {object} auth the object to use for authentication (typically {uid: some-uid}) |
18 | | - * @return {object} the app. |
19 | | - */ |
20 | | -function authedApp(auth) { |
21 | | - return firebase |
22 | | - .initializeTestApp({ projectId, auth }) |
23 | | - .firestore(); |
24 | | -} |
25 | | - |
26 | | -/* |
27 | | - * ============ |
28 | | - * Test Cases |
29 | | - * ============ |
30 | | - */ |
31 | | -beforeEach(async () => { |
32 | | - // Clear the database between tests |
33 | | - await firebase.clearFirestoreData({ projectId }); |
34 | | -}); |
35 | | - |
36 | | -before(async () => { |
37 | | - await firebase.loadFirestoreRules({ projectId, rules }); |
38 | | -}); |
39 | | - |
40 | | -after(async () => { |
41 | | - await Promise.all(firebase.apps().map(app => app.delete())); |
42 | | - console.log(`View rule coverage information at ${coverageUrl}\n`); |
43 | | -}); |
44 | | - |
45 | | -describe("My app", () => { |
46 | | - it("require users to log in before creating a profile", async () => { |
47 | | - const db = authedApp(null); |
48 | | - const profile = db.collection("users").doc("alice"); |
49 | | - await firebase.assertFails(profile.set({ birthday: "January 1" })); |
50 | | - }); |
51 | | - |
52 | | - it("should enforce the createdAt date in user profiles", async () => { |
53 | | - const db = authedApp({ uid: "alice" }); |
54 | | - const profile = db.collection("users").doc("alice"); |
55 | | - await firebase.assertFails(profile.set({ birthday: "January 1" })); |
56 | | - await firebase.assertSucceeds( |
57 | | - profile.set({ |
58 | | - birthday: "January 1", |
59 | | - createdAt: firebase.firestore.FieldValue.serverTimestamp() |
60 | | - }) |
61 | | - ); |
62 | | - }); |
63 | | - |
64 | | - it("should only let users create their own profile", async () => { |
65 | | - const db = authedApp({ uid: "alice" }); |
66 | | - await firebase.assertSucceeds( |
67 | | - db |
68 | | - .collection("users") |
69 | | - .doc("alice") |
70 | | - .set({ |
71 | | - birthday: "January 1", |
72 | | - createdAt: firebase.firestore.FieldValue.serverTimestamp() |
73 | | - }) |
74 | | - ); |
75 | | - await firebase.assertFails( |
76 | | - db |
77 | | - .collection("users") |
78 | | - .doc("bob") |
79 | | - .set({ |
80 | | - birthday: "January 1", |
81 | | - createdAt: firebase.firestore.FieldValue.serverTimestamp() |
82 | | - }) |
83 | | - ); |
84 | | - }); |
85 | | - |
86 | | - it("should let anyone read any profile", async () => { |
87 | | - const db = authedApp(null); |
88 | | - const profile = db.collection("users").doc("alice"); |
89 | | - await firebase.assertSucceeds(profile.get()); |
90 | | - }); |
91 | | - |
92 | | - it("should let anyone create a room", async () => { |
93 | | - const db = authedApp({ uid: "alice" }); |
94 | | - const room = db.collection("rooms").doc("firebase"); |
95 | | - await firebase.assertSucceeds( |
96 | | - room.set({ |
97 | | - owner: "alice", |
98 | | - topic: "All Things Firebase" |
99 | | - }) |
100 | | - ); |
101 | | - }); |
102 | | - |
103 | | - it("should force people to name themselves as room owner when creating a room", async () => { |
104 | | - const db = authedApp({ uid: "alice" }); |
105 | | - const room = db.collection("rooms").doc("firebase"); |
106 | | - await firebase.assertFails( |
107 | | - room.set({ |
108 | | - owner: "scott", |
109 | | - topic: "Firebase Rocks!" |
110 | | - }) |
111 | | - ); |
112 | | - }); |
113 | | - |
114 | | - it("should not let one user steal a room from another user", async () => { |
115 | | - const alice = authedApp({ uid: "alice" }); |
116 | | - const bob = authedApp({ uid: "bob" }); |
117 | | - |
118 | | - await firebase.assertSucceeds( |
119 | | - bob |
120 | | - .collection("rooms") |
121 | | - .doc("snow") |
122 | | - .set({ |
123 | | - owner: "bob", |
124 | | - topic: "All Things Snowboarding" |
125 | | - }) |
126 | | - ); |
127 | | - |
128 | | - await firebase.assertFails( |
129 | | - alice |
130 | | - .collection("rooms") |
131 | | - .doc("snow") |
132 | | - .set({ |
133 | | - owner: "alice", |
134 | | - topic: "skiing > snowboarding" |
135 | | - }) |
136 | | - ); |
137 | | - }); |
| 5 | +it("a random user can write a random document", async () => { |
| 6 | + const db = firebase.initializeTestApp({ projectId, auth: null }).firestore(); |
| 7 | + await firebase.assertSucceeds( |
| 8 | + db.collection("random-collection").doc("random-document").set({ hello: "world" }) |
| 9 | + ); |
138 | 10 | }); |
0 commit comments