1818
1919import pytest
2020
21+ import firebase_admin
2122from firebase_admin import db
23+ from integration import conftest
2224from tests import testutils
2325
24- def _update_rules ():
25- with open (testutils .resource_filename ('dinosaurs_index.json' )) as index_file :
26- index = json .load (index_file )
26+ @pytest .fixture (scope = 'module' )
27+ def update_rules ():
28+ with open (testutils .resource_filename ('dinosaurs_index.json' )) as rules_file :
29+ new_rules = json .load (rules_file )
2730 client = db .reference ()._client
2831 rules = client .request ('get' , '/.settings/rules.json' )
2932 existing = rules .get ('rules' , dict ()).get ('_adminsdk' )
30- if existing != index :
31- rules ['rules' ]['_adminsdk' ] = index
33+ if existing != new_rules :
34+ rules ['rules' ]['_adminsdk' ] = new_rules
3235 client .request ('put' , '/.settings/rules.json' , json = rules )
3336
3437@pytest .fixture (scope = 'module' )
@@ -37,7 +40,7 @@ def testdata():
3740 return json .load (dino_file )
3841
3942@pytest .fixture (scope = 'module' )
40- def testref ():
43+ def testref (update_rules ):
4144 """Adds the necessary DB indices, and sets the initial values.
4245
4346 This fixture is attached to the module scope, and therefore is guaranteed to run only once
@@ -46,7 +49,6 @@ def testref():
4649 Returns:
4750 Reference: A reference to the test dinosaur database.
4851 """
49- _update_rules ()
5052 ref = db .reference ('_adminsdk/python/dinodb' )
5153 ref .set (testdata ())
5254 return ref
@@ -134,6 +136,13 @@ def test_update_children_with_existing_values(self, testref):
134136 ref .update ({'since' : 1905 })
135137 assert ref .get () == {'name' : 'Edwin Colbert' , 'since' : 1905 }
136138
139+ def test_update_nested_children (self , testref ):
140+ python = testref .parent
141+ ref = python .child ('users' ).push ({'name' : 'Edward Cope' , 'since' : 1800 })
142+ nested_key = '{0}/since' .format (ref .key )
143+ python .child ('users' ).update ({nested_key : 1840 })
144+ assert ref .get () == {'name' : 'Edward Cope' , 'since' : 1840 }
145+
137146 def test_delete (self , testref ):
138147 python = testref .parent
139148 ref = python .child ('users' ).push ('foo' )
@@ -220,3 +229,93 @@ def test_filter_by_value(self, testref):
220229 assert len (value ) == 2
221230 assert 'pterodactyl' in value
222231 assert 'linhenykus' in value
232+
233+
234+ @pytest .fixture (scope = 'module' )
235+ def override_app (request , update_rules ):
236+ cred , project_id = conftest .integration_conf (request )
237+ ops = {
238+ 'databaseURL' : 'https://{0}.firebaseio.com' .format (project_id ),
239+ 'databaseAuthVariableOverride' : {'uid' : 'user1' }
240+ }
241+ app = firebase_admin .initialize_app (cred , ops , 'db-override' )
242+ yield app
243+ firebase_admin .delete_app (app )
244+
245+ @pytest .fixture (scope = 'module' )
246+ def none_override_app (request , update_rules ):
247+ cred , project_id = conftest .integration_conf (request )
248+ ops = {
249+ 'databaseURL' : 'https://{0}.firebaseio.com' .format (project_id ),
250+ 'databaseAuthVariableOverride' : None
251+ }
252+ app = firebase_admin .initialize_app (cred , ops , 'db-none-override' )
253+ yield app
254+ firebase_admin .delete_app (app )
255+
256+
257+ class TestAuthVariableOverride (object ):
258+ """Test cases for database auth variable overrides."""
259+
260+ def init_ref (self , path ):
261+ admin_ref = db .reference (path )
262+ admin_ref .set ('test' )
263+ assert admin_ref .get () == 'test'
264+
265+ def check_permission_error (self , excinfo ):
266+ assert isinstance (excinfo .value , db .ApiCallError )
267+ assert 'Reason: Permission denied' in str (excinfo .value )
268+
269+ def test_no_access (self , override_app ):
270+ path = '_adminsdk/python/admin'
271+ self .init_ref (path )
272+ user_ref = db .reference (path , override_app )
273+ with pytest .raises (db .ApiCallError ) as excinfo :
274+ assert user_ref .get ()
275+ self .check_permission_error (excinfo )
276+
277+ with pytest .raises (db .ApiCallError ) as excinfo :
278+ user_ref .set ('test2' )
279+ self .check_permission_error (excinfo )
280+
281+ def test_read (self , override_app ):
282+ path = '_adminsdk/python/protected/user2'
283+ self .init_ref (path )
284+ user_ref = db .reference (path , override_app )
285+ assert user_ref .get () == 'test'
286+ with pytest .raises (db .ApiCallError ) as excinfo :
287+ user_ref .set ('test2' )
288+ self .check_permission_error (excinfo )
289+
290+ def test_read_write (self , override_app ):
291+ path = '_adminsdk/python/protected/user1'
292+ self .init_ref (path )
293+ user_ref = db .reference (path , override_app )
294+ assert user_ref .get () == 'test'
295+ user_ref .set ('test2' )
296+ assert user_ref .get () == 'test2'
297+
298+ def test_query (self , override_app ):
299+ user_ref = db .reference ('_adminsdk/python/protected' , override_app )
300+ with pytest .raises (db .ApiCallError ) as excinfo :
301+ user_ref .order_by_key ().limit_to_first (2 ).get ()
302+ self .check_permission_error (excinfo )
303+
304+ def test_none_auth_override (self , none_override_app ):
305+ path = '_adminsdk/python/public'
306+ self .init_ref (path )
307+ public_ref = db .reference (path , none_override_app )
308+ assert public_ref .get () == 'test'
309+
310+ ref = db .reference ('_adminsdk/python' , none_override_app )
311+ with pytest .raises (db .ApiCallError ) as excinfo :
312+ assert ref .child ('protected/user1' ).get ()
313+ self .check_permission_error (excinfo )
314+
315+ with pytest .raises (db .ApiCallError ) as excinfo :
316+ assert ref .child ('protected/user2' ).get ()
317+ self .check_permission_error (excinfo )
318+
319+ with pytest .raises (db .ApiCallError ) as excinfo :
320+ assert ref .child ('admin' ).get ()
321+ self .check_permission_error (excinfo )
0 commit comments