forked from janvogt/firebase-rules-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
182 lines (181 loc) · 5.67 KB
/
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
const Rules = require('./index.js')
try {
new Rules({
'rules': {
'a': {
'.read': 'true'
},
'c': {
'd': {
'.read': 'true'
}
},
'variable': {
'$var': {
'.read': 'auth.uid === $var',
'a': {
'.read': 'true'
}
}
},
'd': {
'str': {
'.read': 'data.isString()'
},
'num': {
'.read': 'data.isNumber()'
},
'notExists': {
'.read': 'data.exists()'
},
'bool': {
'.read': 'data.isBoolean()'
},
'obj': {
'.read': 'data.hasChild(\'a/b\')',
'a': {
'.read': 'data.parent().hasChildren([\'a\', \'b\'])'
},
'b': {
'.read': 'root.child(\'d\').child(\'obj/b\').val() === false'
}
}
},
'e': {
'.write': 'false',
'f': {
'.write': 'auth.uid != null',
'.validate': 'newData.hasChildren([\'g\', \'j\']) && newData.child(\'g\').hasChildren([\'h\', \'i\'])',
'j': {
'.validate': '!data.exists() && newData.exists() && newData.isString()'
},
'g': {
'h': {
'.validate': 'newData.parent().child(\'h\').val() === newData.val()'
}
}
}
},
'string': {
'.write': 'newData.isString()',
'.validate': 'newData.val().length === 7 && newData.val().contains(\'ab\') &&' +
'newData.val().beginsWith(\'cab\') && newData.val().endsWith(\'Art\') &&' +
'newData.val().replace(\'5\', \'6\') === \'cab6Art\' &&' +
'newData.val().toLowerCase() === \'cab5art\' &&' +
'newData.val().toUpperCase().toLowerCase() == \'cab5art\''
},
'serverValue': {
'.write': 'newData.isNumber()',
'.validate': 'newData.val() === now'
},
'deeper': {
'.write': 'newData.hasChildren([\'serverValue\'])',
'serverValue': {
'.write': 'newData.isNumber()',
'.validate': 'newData.val() === now'
}
},
'shouldFail': {
'.write': 'newData.child().val() === null'
}
}
})
.read('a')
.allow('everyone should be able to read a')
.read('b')
.deny('noone should be able to read b')
.read('a/b')
.allow('children of readable nodes should be readable')
.read('/c')
.deny('parents of readable nodes should not be readable')
.read('c/d')
.allow('deeper paths should be readable if allowed')
.read('variable/value/a')
.allow('paths with variables should be handled')
.authenticate('useruid')
.read('/variable/useruid/')
.allow('should be able to read as correct user')
.read('/variable/otheruid')
.deny('other node should not be readable')
.fixture({
'd': {
'str': 'string',
'num': 2,
'bool': true,
'obj': {
'a': true,
'b': false
}
}
})
.read('d/str')
.allow('should be able to read string')
.read('d/num')
.allow('should be able to read number')
.read('/d/notExists')
.deny('should not be able to read not existing data')
.read('/d/bool/')
.allow('should be able to read bool')
.read('d/obj')
.deny('should not be able to read data with missing child')
.read('d/obj/a')
.allow('should be able to read data with existing siblings')
.read('d/obj/b')
.allow('should be able to read data with correct value')
.set('e', {
'f': {
'g': {
'h': true,
'i': false
},
'j': 'hallo'
}
})
.deny('should not allow writing to /e')
.set('e/f', {
'g': {
'h': true,
'i': false
},
'j': 'hallo'
})
.allow('should be allowed to write to /e/f')
.set('e/f', {
'g': {
'h': true,
'i': false
},
'j': 3
})
.deny('should not be able to write a wrong value.')
.update('e,f', {
'g/h': 1,
'g/i': 2
})
.deny('should not be able to set wrong data')
.update('e,f', {
'g/h': 2,
'g/i': 2
})
.deny('should be able to set correct data')
.set('string/', 'cab5Art')
.allow('All string methods should be availiable.')
.set('/serverValue', { ['.sv']: 'timestamp' })
.allow('server value should be recognized')
.set('/deeper', { 'serverValue': { ['.sv']: 'timestamp' }})
.allow('server value should be recognized deeper nested')
.update('/', {
'serverValue': { ['.sv']: 'timestamp' },
'deeper': {
'serverValue': { ['.sv']: 'timestamp' }
}
})
.allow('server value should be recognized in multipath update')
.update('shouldFail', {
'shouldFail/test': 'abc'
})
.allow('exceptions should fail nicely')
.stats()
} catch (e) {
console.log(`ERROR: Test throws error: '${e}'`)
}