Skip to content

Commit 8f7d63d

Browse files
Add POC for RUTv2 with JS v9 modular SDK. (#209)
* Add POC for RUTv2 with JS v9 modular SDK. * Add more Firestore unit tests * Add database tests * Define tests contexts in individual tests * Fix and cleanup. Co-authored-by: Rachel Myers <[email protected]>
1 parent 192288e commit 8f7d63d

File tree

11 files changed

+4086
-0
lines changed

11 files changed

+4086
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "demo-example-testing"
4+
}
5+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
9+
# Firebase cache
10+
.firebase/
11+
12+
# Firebase config
13+
14+
# Uncomment this if you'd like others to create their own Firebase project.
15+
# For a team working on the same Firebase project(s), it is recommended to leave
16+
# it commented so all members can deploy to the same project(s) in .firebaserc.
17+
# .firebaserc
18+
19+
# Runtime data
20+
pids
21+
*.pid
22+
*.seed
23+
*.pid.lock
24+
25+
# Directory for instrumented libs generated by jscoverage/JSCover
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (http://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
49+
# Optional npm cache directory
50+
.npm
51+
52+
# Optional eslint cache
53+
.eslintcache
54+
55+
# Optional REPL history
56+
.node_repl_history
57+
58+
# Output of 'npm pack'
59+
*.tgz
60+
61+
# Yarn Integrity file
62+
.yarn-integrity
63+
64+
# dotenv environment variables file
65+
.env

unit-test-security-rules-v9/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Unit Test Security Rules with JS SDK v9
2+
3+
This sample demonstrates how to write **unit tests** for security rules
4+
using the Firebase Emulator Suite, with latest modular JS SDK v9 and
5+
`@firebase/rules-unit-testing` v2.
6+
7+
## Setup
8+
9+
To install the dependencies for this sample run `npm install` inside this directory.
10+
You will also need the [Firebase CLI](https://firebase.google.com/docs/cli).
11+
12+
## Run
13+
14+
To run the Realtime Database tests:
15+
16+
```
17+
firebase emulators:exec --only database "npm run test-database"
18+
```
19+
20+
To run the Cloud Firestore tests:
21+
22+
```
23+
firebase emulators:exec --only firestore "npm run test-firestore"
24+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"rules": {
3+
"users": {
4+
"$userId": {
5+
".read": true,
6+
".write": "auth.uid == $userId"
7+
}
8+
},
9+
"rooms": {
10+
"$roomId": {
11+
".write": "data.child('owner').val() == auth.uid || !data.exists()",
12+
".validate": "newData.hasChild('owner')",
13+
"owner": {
14+
".validate": "newData.isString() && newData.val() == auth.uid"
15+
},
16+
"members": {
17+
"$memberId": {
18+
".write": "!newData.exists() && auth.uid == $memberId"
19+
}
20+
},
21+
"messages": {
22+
".read": "auth != null && data.parent().child('members').child(auth.uid).exists()",
23+
"$messageId": {
24+
".write": "auth != null && data.parent().child('members').child(auth.uid).exists()"
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"database": {
3+
"rules": "database.rules.json"
4+
},
5+
"storage": {
6+
"rules": "storage.rules"
7+
},
8+
"firestore": {
9+
"rules": "firestore.rules",
10+
"indexes": "firestore.indexes.json"
11+
},
12+
"emulators": {
13+
"firestore": {
14+
"port": 8080
15+
},
16+
"database": {
17+
"port": 9000
18+
},
19+
"ui": {
20+
"enabled": true
21+
},
22+
"storage": {
23+
"port": 9199
24+
}
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
rules_version = '2';
2+
service cloud.firestore {
3+
match /databases/{database}/documents {
4+
match /users/{userId} {
5+
allow read;
6+
allow create: if request.auth.uid == userId && request.resource.data.createdAt == request.time;
7+
}
8+
match /rooms/{roomId} {
9+
allow read;
10+
// If you create a room, you must set yourself as the owner.
11+
allow create: if request.resource.data.owner == request.auth.uid;
12+
// Only the room owner is allowed to modify it, and owner mustn't be able to assign his room to other user.
13+
allow update: if resource.data.owner == request.auth.uid && request.resource.data.owner == request.auth.uid;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)