|
33 | 33 |
|
34 | 34 | FIREBASE_AUDIENCE = ('https://identitytoolkit.googleapis.com/'
|
35 | 35 | 'google.identity.identitytoolkit.v1.IdentityToolkit')
|
| 36 | +GCLOUD_PROJECT_ENV_VAR = 'GCLOUD_PROJECT' |
36 | 37 |
|
37 | 38 | MOCK_UID = 'user1'
|
38 | 39 | MOCK_CREDENTIAL = credentials.Certificate(
|
@@ -98,6 +99,21 @@ def non_cert_app():
|
98 | 99 | yield app
|
99 | 100 | firebase_admin.delete_app(app)
|
100 | 101 |
|
| 102 | +@pytest.fixture |
| 103 | +def env_var_app(request): |
| 104 | + """Returns an App instance initialized with the given set of environment variables. |
| 105 | +
|
| 106 | + The lines of code following the yield statement are guaranteed to run after each test case |
| 107 | + that depends on this fixture. This ensures that the environment is left intact after the |
| 108 | + tests. |
| 109 | + """ |
| 110 | + environ = os.environ |
| 111 | + os.environ = request.param |
| 112 | + app = firebase_admin.initialize_app(testutils.MockCredential(), name='env-var-app') |
| 113 | + yield app |
| 114 | + os.environ = environ |
| 115 | + firebase_admin.delete_app(app) |
| 116 | + |
101 | 117 | def verify_custom_token(custom_token, expected_claims):
|
102 | 118 | assert isinstance(custom_token, six.binary_type)
|
103 | 119 | token = google.oauth2.id_token.verify_token(
|
@@ -232,28 +248,25 @@ def test_invalid_token(self, authtest, id_token):
|
232 | 248 | with pytest.raises(ValueError):
|
233 | 249 | authtest.verify_id_token(id_token)
|
234 | 250 |
|
235 |
| - def test_project_id_env_var(self, non_cert_app): |
236 |
| - gcloud_project = os.environ.get(auth.GCLOUD_PROJECT_ENV_VAR) |
| 251 | + def test_project_id_option(self): |
| 252 | + app = firebase_admin.initialize_app( |
| 253 | + testutils.MockCredential(), options={'projectId': 'mock-project-id'}, name='myApp') |
237 | 254 | try:
|
238 |
| - os.environ[auth.GCLOUD_PROJECT_ENV_VAR] = MOCK_CREDENTIAL.project_id |
239 |
| - claims = auth.verify_id_token(TEST_ID_TOKEN, non_cert_app) |
| 255 | + claims = auth.verify_id_token(TEST_ID_TOKEN, app) |
240 | 256 | assert claims['admin'] is True
|
| 257 | + assert claims['uid'] == claims['sub'] |
241 | 258 | finally:
|
242 |
| - if gcloud_project: |
243 |
| - os.environ[auth.GCLOUD_PROJECT_ENV_VAR] = gcloud_project |
244 |
| - else: |
245 |
| - del os.environ[auth.GCLOUD_PROJECT_ENV_VAR] |
246 |
| - |
247 |
| - def test_no_project_id(self, non_cert_app): |
248 |
| - gcloud_project = os.environ.get(auth.GCLOUD_PROJECT_ENV_VAR) |
249 |
| - if gcloud_project: |
250 |
| - del os.environ[auth.GCLOUD_PROJECT_ENV_VAR] |
251 |
| - try: |
252 |
| - with pytest.raises(ValueError): |
253 |
| - auth.verify_id_token(TEST_ID_TOKEN, non_cert_app) |
254 |
| - finally: |
255 |
| - if gcloud_project: |
256 |
| - os.environ[auth.GCLOUD_PROJECT_ENV_VAR] = gcloud_project |
| 259 | + firebase_admin.delete_app(app) |
| 260 | + |
| 261 | + @pytest.mark.parametrize('env_var_app', [{'GCLOUD_PROJECT': 'mock-project-id'}], indirect=True) |
| 262 | + def test_project_id_env_var(self, env_var_app): |
| 263 | + claims = auth.verify_id_token(TEST_ID_TOKEN, env_var_app) |
| 264 | + assert claims['admin'] is True |
| 265 | + |
| 266 | + @pytest.mark.parametrize('env_var_app', [{}], indirect=True) |
| 267 | + def test_no_project_id(self, env_var_app): |
| 268 | + with pytest.raises(ValueError): |
| 269 | + auth.verify_id_token(TEST_ID_TOKEN, env_var_app) |
257 | 270 |
|
258 | 271 | def test_custom_token(self, authtest):
|
259 | 272 | id_token = authtest.create_custom_token(MOCK_UID)
|
|
0 commit comments