-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirestore.rules.spec.js
177 lines (130 loc) · 5.64 KB
/
firestore.rules.spec.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
const { setup, teardown } = require('./spec/helpers')
describe(' Database rules', () => {
afterAll(async () => await teardown())
describe('non-existent collection', () => {
test('reading/writing denied from all', async () => {
const db = await setup()
const ref = db.collection('nonexistent-collection')
await expect(ref.get()).toDeny()
await expect(ref.doc('test').set({ foo: 'bar' })).toDeny()
})
})
describe('content collection', () => {
const data = {
'adminUsers/admin123': { uid: 'admin123' },
}
const collectionRef = db => db.collection('content')
test('read-only from all', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.get()).toAllow()
await expect(ref.doc('test').set({ foo: 'bar' })).toDeny()
})
test('write allowed from admin', async () => {
const ref = collectionRef(await setup({ uid: 'admin123' }, data))
await expect(ref.doc('test').set({ foo: 'bar' })).toAllow()
})
test('write denied from non admin', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('test').set({ foo: 'bar' })).toDeny()
})
})
describe('states collection', () => {
test('read-only from all', async () => {
const db = await setup()
const ref = db.collection('states')
await expect(ref.get()).toAllow()
await expect(ref.doc('test').set({ foo: 'bar' })).toDeny()
})
test('read-only in leaders collectionGroup', async () => {
const db = await setup()
const ref = db.collection('leaders')
await expect(ref.get()).toAllow()
await expect(ref.doc('test').set({ foo: 'bar' })).toDeny()
})
})
describe('adminUsers collection', () => {
const data = {
'adminUsers/admin123': { uid: 'admin123' },
}
const collectionRef = db => db.collection('adminUsers')
test('get denied from non-authenticated user', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.doc('joe123').get()).toDeny()
})
test('get allowed from authenticated user', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('joe123').get()).toAllow()
})
test('list denied to all', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.get()).toDeny()
})
test('write denied to all', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('joe123').set({ foo: 'bar' })).toDeny()
})
})
describe('userProfiles collection', () => {
const data = {
'userProfiles/joe123': { email: '[email protected]' },
'adminUsers/admin123': { uid: 'admin123' },
}
const collectionRef = db => db.collection('userProfiles')
test('get denied from non-authenticated user', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.doc('joe123').get()).toDeny()
})
test('get allowed from authenticated user', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('joe123').get()).toAllow()
})
test('list denied to non-authenticated user', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.get()).toDeny()
})
test('list denied to non-admins', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.get()).toDeny()
})
test('list allowed to admins', async () => {
const ref = collectionRef(await setup({ uid: 'admin123' }, data))
await expect(ref.get()).toAllow()
})
test('write denied from non-authenticated user', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.doc('joe123').set({ foo: 'bar' })).toDeny()
})
test('write allowed from authenticated user with same email', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('joe123').update({ foo: 'bar' })).toAllow()
await expect(ref.doc('joe123').update({ foo: 'bar', email: '[email protected]' })).toAllow()
await expect(ref.doc('joe123').set({ foo: 'bar', email: '[email protected]' })).toAllow()
})
test('write denied from authenticated user changing email', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('joe123').set({ foo: 'bar' })).toDeny()
await expect(ref.doc('joe123').set({ foo: 'bar', email: '[email protected]' })).toDeny()
})
})
describe('twitterAccounts collection', () => {
const data = {
'adminUsers/admin123': { uid: 'admin123' },
}
const collectionRef = db => db.collection('twitterAccounts')
test('access denied from non-authenticated user', async () => {
const ref = collectionRef(await setup(null, data))
await expect(ref.doc('Praying4_IN').get()).toDeny()
await expect(ref.doc('Praying4_IN').set({ foo: 'bar' })).toDeny()
})
test('access denied from authenticated user', async () => {
const ref = collectionRef(await setup({ uid: 'joe123' }, data))
await expect(ref.doc('Praying4_IN').get()).toDeny()
await expect(ref.doc('Praying4_IN').set({ foo: 'bar' })).toDeny()
})
test('access denied from authenticated admin user', async () => {
const ref = collectionRef(await setup({ uid: 'admin123' }, data))
await expect(ref.doc('Praying4_IN').get()).toAllow()
await expect(ref.doc('Praying4_IN').set({ foo: 'bar' })).toAllow()
})
})
})