diff --git a/firebase_admin/__init__.py b/firebase_admin/__init__.py index 0ca82ec5e..e542dcaad 100644 --- a/firebase_admin/__init__.py +++ b/firebase_admin/__init__.py @@ -65,7 +65,7 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME): credential = credentials.ApplicationDefault() app = App(name, credential, options) with _apps_lock: - if app.name not in _apps: + if not is_app_initialized(app.name): _apps[app.name] = app return app @@ -85,6 +85,17 @@ def initialize_app(credential=None, options=None, name=_DEFAULT_APP_NAME): 'you call initialize_app().').format(name)) +def is_app_initialized(name=_DEFAULT_APP_NAME): + """Returns True if the App instance for the specified name is initialized. + Args: + name: Name of the App instance to retrieve (optional). + Returns: + bool: True if the App instance is initialized, False otherwise. + """ + with _apps_lock: + return name in _apps + + def delete_app(app): """Gracefully deletes an App instance. @@ -129,9 +140,8 @@ def get_app(name=_DEFAULT_APP_NAME): if not isinstance(name, str): raise ValueError('Illegal app name argument type: "{}". App name ' 'must be a string.'.format(type(name))) - with _apps_lock: - if name in _apps: - return _apps[name] + if is_app_initialized(name): + return _apps[name] if name == _DEFAULT_APP_NAME: raise ValueError( diff --git a/tests/test_app.py b/tests/test_app.py index 4233d5849..8bae96a30 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -285,6 +285,24 @@ def test_app_init_with_default_config(self, env_test_case): app = firebase_admin.initialize_app(CREDENTIAL, options=env_test_case.init_options) assert app.options._options == env_test_case.want_options + def test_default_app_is_initialized(self, app_credential): + firebase_admin.initialize_app(app_credential) + assert firebase_admin.is_app_initialized() + + def test_no_app_is_initialized(self): + assert firebase_admin.is_app_initialized() is False + + def test_correct_app_is_initialized(self): + app_name = 'myApp' + firebase_admin.initialize_app(CREDENTIAL, name=app_name) + assert firebase_admin.is_app_initialized(app_name) + + def test_correct_app_is_not_initialized(self): + app_name = 'myApp' + other_app_name = 'otherApp' + firebase_admin.initialize_app(CREDENTIAL, name=app_name) + assert firebase_admin.is_app_initialized(other_app_name) is False + def test_project_id_from_options(self, app_credential): app = firebase_admin.initialize_app( app_credential, options={'projectId': 'test-project'}, name='myApp')