Skip to content

Commit dd99cf7

Browse files
authored
Changed the delete_app() function to accept an App instance (#8)
1 parent 64e0c45 commit dd99cf7

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

firebase_admin/__init__.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,31 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME):
5858
'you call initialize_app().').format(name))
5959

6060

61-
def delete_app(name):
61+
def delete_app(app):
6262
"""Gracefully deletes an App instance.
6363
6464
Args:
65-
name: Name of the app instance to be deleted.
65+
app: The app instance to be deleted.
6666
6767
Raises:
68-
ValueError: If the name is not a string.
68+
ValueError: If the app is not initialized.
6969
"""
70-
if not isinstance(name, six.string_types):
71-
raise ValueError('Illegal app name argument type: "{}". App name '
72-
'must be a string.'.format(type(name)))
70+
if not isinstance(app, App):
71+
raise ValueError('Illegal app argument type: "{}". Argument must be of '
72+
'type App.'.format(type(app)))
7373
with _apps_lock:
74-
if name in _apps:
75-
del _apps[name]
74+
if _apps.get(app.name) is app:
75+
del _apps[app.name]
7676
return
77-
if name == _DEFAULT_APP_NAME:
77+
if app.name == _DEFAULT_APP_NAME:
7878
raise ValueError(
79-
'The default Firebase app does not exist. Make sure to initialize '
80-
'the SDK by calling initialize_app().')
79+
'The default Firebase app is not initialized. Make sure to initialize '
80+
'the default app by calling initialize_app().')
8181
else:
8282
raise ValueError(
83-
('Firebase app named "{0}" does not exist. Make sure to initialize '
84-
'the SDK by calling initialize_app() with your app name as the '
85-
'second argument.').format(name))
83+
('Firebase app named "{0}" is not initialized. Make sure to initialize '
84+
'the app by calling initialize_app() with your app name as the '
85+
'second argument.').format(app.name))
8686

8787

8888
def get_app(name=_DEFAULT_APP_NAME):

tests/test_app.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,24 @@ def app_credential(request):
6262
yield provider.get()
6363
provider.cleanup()
6464

65+
@pytest.fixture(params=[None, 'myApp'], ids=['DefaultApp', 'CustomApp'])
66+
def init_app(request):
67+
if request.param:
68+
return firebase_admin.initialize_app(CREDENTIAL, name=request.param)
69+
else:
70+
return firebase_admin.initialize_app(CREDENTIAL)
71+
6572

6673
class TestFirebaseApp(object):
6774
"""Test cases for App initialization and life cycle."""
6875

6976
invalid_credentials = ['', 'foo', 0, 1, dict(), list(), tuple(), True, False]
7077
invalid_options = ['', 0, 1, list(), tuple(), True, False]
7178
invalid_names = [None, '', 0, 1, dict(), list(), tuple(), True, False]
79+
invalid_apps = [
80+
None, '', 0, 1, dict(), list(), tuple(), True, False,
81+
firebase_admin.App('uninitialized', CREDENTIAL, {})
82+
]
7283

7384
def teardown_method(self):
7485
testutils.cleanup_apps()
@@ -108,13 +119,8 @@ def test_app_init_with_invalid_name(self, name):
108119
with pytest.raises(ValueError):
109120
firebase_admin.initialize_app(CREDENTIAL, name=name)
110121

111-
def test_default_app_get(self):
112-
app = firebase_admin.initialize_app(CREDENTIAL)
113-
assert app is firebase_admin.get_app()
114-
115-
def test_non_default_app_get(self):
116-
app = firebase_admin.initialize_app(CREDENTIAL, name='myApp')
117-
assert app is firebase_admin.get_app('myApp')
122+
def test_app_get(self, init_app):
123+
assert init_app is firebase_admin.get_app(init_app.name)
118124

119125
@pytest.mark.parametrize('args', [(), ('myApp',)],
120126
ids=['DefaultApp', 'CustomApp'])
@@ -126,3 +132,16 @@ def test_non_existing_app_get(self, args):
126132
def test_app_get_with_invalid_name(self, name):
127133
with pytest.raises(ValueError):
128134
firebase_admin.get_app(name)
135+
136+
@pytest.mark.parametrize('app', invalid_apps)
137+
def test_invalid_app_delete(self, app):
138+
with pytest.raises(ValueError):
139+
firebase_admin.delete_app(app)
140+
141+
def test_app_delete(self, init_app):
142+
assert firebase_admin.get_app(init_app.name) is init_app
143+
firebase_admin.delete_app(init_app)
144+
with pytest.raises(ValueError):
145+
firebase_admin.get_app(init_app.name)
146+
with pytest.raises(ValueError):
147+
firebase_admin.delete_app(init_app)

tests/test_auth.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def setup_module():
4949
firebase_admin.initialize_app(MOCK_CREDENTIAL, name='testApp')
5050

5151
def teardown_module():
52-
firebase_admin.delete_app('[DEFAULT]')
53-
firebase_admin.delete_app('testApp')
52+
firebase_admin.delete_app(firebase_admin.get_app())
53+
firebase_admin.delete_app(firebase_admin.get_app('testApp'))
5454

5555
@pytest.fixture(params=[None, 'testApp'], ids=['DefaultApp', 'CustomApp'])
5656
def authtest(request):
@@ -73,7 +73,7 @@ def non_cert_app():
7373
"""
7474
app = firebase_admin.initialize_app(credentials.Base(), name='non-cert-app')
7575
yield app
76-
firebase_admin.delete_app(app.name)
76+
firebase_admin.delete_app(app)
7777

7878
def verify_custom_token(custom_token, expected_claims):
7979
assert isinstance(custom_token, six.binary_type)

tests/testutils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def resource(filename):
1919

2020
def cleanup_apps():
2121
with firebase_admin._apps_lock:
22-
app_names = list(firebase_admin._apps.keys())
23-
for name in app_names:
24-
firebase_admin.delete_app(name)
22+
apps = list(firebase_admin._apps.values())
23+
for app in apps:
24+
firebase_admin.delete_app(app)
2525

2626

2727
class HttpMock(object):

0 commit comments

Comments
 (0)