forked from firebase/firebase-admin-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.py
More file actions
161 lines (125 loc) · 5.33 KB
/
test_app.py
File metadata and controls
161 lines (125 loc) · 5.33 KB
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
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for firebase_admin.App."""
import os
import pytest
import firebase_admin
from firebase_admin import credentials
from tests import testutils
CREDENTIAL = credentials.Certificate(
testutils.resource_filename('service_account.json'))
class CredentialProvider(object):
def init(self):
pass
def get(self):
pass
def cleanup(self):
pass
class Cert(CredentialProvider):
def get(self):
return CREDENTIAL
class RefreshToken(CredentialProvider):
def get(self):
return credentials.RefreshToken(testutils.resource_filename('refresh_token.json'))
class ExplicitAppDefault(CredentialProvider):
VAR_NAME = 'GOOGLE_APPLICATION_CREDENTIALS'
def init(self):
self.file_path = os.environ.get(self.VAR_NAME)
os.environ[self.VAR_NAME] = testutils.resource_filename('service_account.json')
def get(self):
return credentials.ApplicationDefault()
def cleanup(self):
if self.file_path:
os.environ[self.VAR_NAME] = self.file_path
else:
del os.environ[self.VAR_NAME]
class ImplicitAppDefault(ExplicitAppDefault):
def get(self):
return None
@pytest.fixture(params=[Cert(), RefreshToken(), ExplicitAppDefault(), ImplicitAppDefault()],
ids=['cert', 'refreshtoken', 'explicit-appdefault', 'implicit-appdefault'])
def app_credential(request):
provider = request.param
provider.init()
yield provider.get()
provider.cleanup()
@pytest.fixture(params=[None, 'myApp'], ids=['DefaultApp', 'CustomApp'])
def init_app(request):
if request.param:
return firebase_admin.initialize_app(CREDENTIAL, name=request.param)
else:
return firebase_admin.initialize_app(CREDENTIAL)
class TestFirebaseApp(object):
"""Test cases for App initialization and life cycle."""
invalid_credentials = ['', 'foo', 0, 1, dict(), list(), tuple(), True, False]
invalid_options = ['', 0, 1, list(), tuple(), True, False]
invalid_names = [None, '', 0, 1, dict(), list(), tuple(), True, False]
invalid_apps = [
None, '', 0, 1, dict(), list(), tuple(), True, False,
firebase_admin.App('uninitialized', CREDENTIAL, {})
]
def teardown_method(self):
testutils.cleanup_apps()
def test_default_app_init(self, app_credential):
app = firebase_admin.initialize_app(app_credential)
assert firebase_admin._DEFAULT_APP_NAME == app.name
if app_credential:
assert app_credential is app.credential
else:
assert isinstance(app.credential, credentials.ApplicationDefault)
with pytest.raises(ValueError):
firebase_admin.initialize_app(app_credential)
def test_non_default_app_init(self, app_credential):
app = firebase_admin.initialize_app(app_credential, name='myApp')
assert app.name == 'myApp'
if app_credential:
assert app_credential is app.credential
else:
assert isinstance(app.credential, credentials.ApplicationDefault)
with pytest.raises(ValueError):
firebase_admin.initialize_app(app_credential, name='myApp')
@pytest.mark.parametrize('cred', invalid_credentials)
def test_app_init_with_invalid_credential(self, cred):
with pytest.raises(ValueError):
firebase_admin.initialize_app(cred)
@pytest.mark.parametrize('options', invalid_options)
def test_app_init_with_invalid_options(self, options):
with pytest.raises(ValueError):
firebase_admin.initialize_app(CREDENTIAL, options=options)
@pytest.mark.parametrize('name', invalid_names)
def test_app_init_with_invalid_name(self, name):
with pytest.raises(ValueError):
firebase_admin.initialize_app(CREDENTIAL, name=name)
def test_app_get(self, init_app):
assert init_app is firebase_admin.get_app(init_app.name)
@pytest.mark.parametrize('args', [(), ('myApp',)],
ids=['DefaultApp', 'CustomApp'])
def test_non_existing_app_get(self, args):
with pytest.raises(ValueError):
firebase_admin.get_app(*args)
@pytest.mark.parametrize('name', invalid_names)
def test_app_get_with_invalid_name(self, name):
with pytest.raises(ValueError):
firebase_admin.get_app(name)
@pytest.mark.parametrize('app', invalid_apps)
def test_invalid_app_delete(self, app):
with pytest.raises(ValueError):
firebase_admin.delete_app(app)
def test_app_delete(self, init_app):
assert firebase_admin.get_app(init_app.name) is init_app
firebase_admin.delete_app(init_app)
with pytest.raises(ValueError):
firebase_admin.get_app(init_app.name)
with pytest.raises(ValueError):
firebase_admin.delete_app(init_app)