Skip to content

Commit 17e28e6

Browse files
committed
Skip tests that fail with auth emulator
1 parent 6837b74 commit 17e28e6

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

integration/test_auth.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Integration tests for firebase_admin.auth module."""
1616
import base64
1717
import datetime
18+
import os
1819
import random
1920
import string
2021
import time
@@ -32,11 +33,18 @@
3233
from firebase_admin import credentials
3334

3435

35-
_verify_token_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken'
36-
_verify_password_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword'
37-
_password_reset_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword'
38-
_verify_email_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo'
39-
_email_sign_in_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/emailLinkSignin'
36+
_EMULATOR_HOST_ENV_VAR = 'FIREBASE_AUTH_EMULATOR_HOST'
37+
URL_PREFIX = 'https://www.googleapis.com/identitytoolkit'
38+
39+
emulator_host = os.getenv(_EMULATOR_HOST_ENV_VAR)
40+
if emulator_host:
41+
URL_PREFIX = 'http://{0}/www.googleapis.com/identitytoolkit'.format(emulator_host)
42+
43+
_verify_token_url = '{0}/v3/relyingparty/verifyCustomToken'.format(URL_PREFIX)
44+
_verify_password_url = '{0}/v3/relyingparty/verifyPassword'.format(URL_PREFIX)
45+
_password_reset_url = '{0}/v3/relyingparty/resetPassword'.format(URL_PREFIX)
46+
_verify_email_url = '{0}/v3/relyingparty/setAccountInfo'.format(URL_PREFIX)
47+
_email_sign_in_url = '{0}/v3/relyingparty/emailLinkSignin'.format(URL_PREFIX)
4048

4149
ACTION_LINK_CONTINUE_URL = 'http://localhost?a=1&b=5#f=1'
4250

@@ -123,6 +131,8 @@ def test_custom_token(api_key):
123131
assert claims['uid'] == 'user1'
124132

125133
def test_custom_token_without_service_account(api_key):
134+
if emulator_host:
135+
pytest.skip("Not supported with auth emulator")
126136
google_cred = firebase_admin.get_app().credential.get_credential()
127137
cred = CredentialWrapper.from_existing_credential(google_cred)
128138
custom_app = firebase_admin.initialize_app(cred, {
@@ -427,7 +437,8 @@ def test_create_user(new_user):
427437
assert user.disabled is False
428438
assert user.custom_claims is None
429439
assert user.user_metadata.creation_timestamp > 0
430-
assert user.user_metadata.last_sign_in_timestamp is None
440+
if not emulator_host:
441+
assert user.user_metadata.last_sign_in_timestamp is None
431442
assert len(user.provider_data) == 0
432443
with pytest.raises(auth.UidAlreadyExistsError):
433444
auth.create_user(uid=new_user.uid)
@@ -560,6 +571,9 @@ def test_verify_id_token_revoked(new_user, api_key):
560571
# verify_id_token succeeded because it didn't check revoked.
561572
assert claims['iat'] * 1000 < user.tokens_valid_after_timestamp
562573

574+
if emulator_host:
575+
pytest.skip("Not supported with auth emulator")
576+
563577
with pytest.raises(auth.RevokedIdTokenError) as excinfo:
564578
claims = auth.verify_id_token(id_token, check_revoked=True)
565579
assert str(excinfo.value) == 'The Firebase ID token has been revoked.'
@@ -604,6 +618,8 @@ def test_import_users():
604618
auth.delete_user(uid)
605619

606620
def test_import_users_with_password(api_key):
621+
if emulator_host:
622+
pytest.skip("Not supported with auth emulator")
607623
uid, email = _random_id()
608624
password_hash = base64.b64decode(
609625
'V358E8LdWJXAO7muq0CufVpEOXaj8aFiC7T/rcaGieN04q/ZPJ08WhJEHGjj9lz/2TT+/86N5VjVoc5DdBhBiw==')
@@ -687,6 +703,8 @@ def oidc_provider():
687703

688704

689705
def test_create_oidc_provider_config(oidc_provider):
706+
if emulator_host:
707+
pytest.skip("Not supported with auth emulator")
690708
assert isinstance(oidc_provider, auth.OIDCProviderConfig)
691709
assert oidc_provider.client_id == 'OIDC_CLIENT_ID'
692710
assert oidc_provider.issuer == 'https://oidc.com/issuer'
@@ -695,6 +713,8 @@ def test_create_oidc_provider_config(oidc_provider):
695713

696714

697715
def test_get_oidc_provider_config(oidc_provider):
716+
if emulator_host:
717+
pytest.skip("Not supported with auth emulator")
698718
provider_config = auth.get_oidc_provider_config(oidc_provider.provider_id)
699719
assert isinstance(provider_config, auth.OIDCProviderConfig)
700720
assert provider_config.provider_id == oidc_provider.provider_id
@@ -705,6 +725,8 @@ def test_get_oidc_provider_config(oidc_provider):
705725

706726

707727
def test_list_oidc_provider_configs(oidc_provider):
728+
if emulator_host:
729+
pytest.skip("Not supported with auth emulator")
708730
page = auth.list_oidc_provider_configs()
709731
result = None
710732
for provider_config in page.iterate_all():
@@ -716,6 +738,8 @@ def test_list_oidc_provider_configs(oidc_provider):
716738

717739

718740
def test_update_oidc_provider_config():
741+
if emulator_host:
742+
pytest.skip("Not supported with auth emulator")
719743
provider_config = _create_oidc_provider_config()
720744
try:
721745
provider_config = auth.update_oidc_provider_config(
@@ -733,6 +757,8 @@ def test_update_oidc_provider_config():
733757

734758

735759
def test_delete_oidc_provider_config():
760+
if emulator_host:
761+
pytest.skip("Not supported with auth emulator")
736762
provider_config = _create_oidc_provider_config()
737763
auth.delete_oidc_provider_config(provider_config.provider_id)
738764
with pytest.raises(auth.ConfigurationNotFoundError):
@@ -747,6 +773,8 @@ def saml_provider():
747773

748774

749775
def test_create_saml_provider_config(saml_provider):
776+
if emulator_host:
777+
pytest.skip("Not supported with auth emulator")
750778
assert isinstance(saml_provider, auth.SAMLProviderConfig)
751779
assert saml_provider.idp_entity_id == 'IDP_ENTITY_ID'
752780
assert saml_provider.sso_url == 'https://example.com/login'
@@ -758,6 +786,8 @@ def test_create_saml_provider_config(saml_provider):
758786

759787

760788
def test_get_saml_provider_config(saml_provider):
789+
if emulator_host:
790+
pytest.skip("Not supported with auth emulator")
761791
provider_config = auth.get_saml_provider_config(saml_provider.provider_id)
762792
assert isinstance(provider_config, auth.SAMLProviderConfig)
763793
assert provider_config.provider_id == saml_provider.provider_id
@@ -771,6 +801,8 @@ def test_get_saml_provider_config(saml_provider):
771801

772802

773803
def test_list_saml_provider_configs(saml_provider):
804+
if emulator_host:
805+
pytest.skip("Not supported with auth emulator")
774806
page = auth.list_saml_provider_configs()
775807
result = None
776808
for provider_config in page.iterate_all():
@@ -782,6 +814,8 @@ def test_list_saml_provider_configs(saml_provider):
782814

783815

784816
def test_update_saml_provider_config():
817+
if emulator_host:
818+
pytest.skip("Not supported with auth emulator")
785819
provider_config = _create_saml_provider_config()
786820
try:
787821
provider_config = auth.update_saml_provider_config(
@@ -806,13 +840,17 @@ def test_update_saml_provider_config():
806840

807841

808842
def test_delete_saml_provider_config():
843+
if emulator_host:
844+
pytest.skip("Not supported with auth emulator")
809845
provider_config = _create_saml_provider_config()
810846
auth.delete_saml_provider_config(provider_config.provider_id)
811847
with pytest.raises(auth.ConfigurationNotFoundError):
812848
auth.get_saml_provider_config(provider_config.provider_id)
813849

814850

815851
def _create_oidc_provider_config():
852+
if emulator_host:
853+
pytest.skip("Not supported with auth emulator")
816854
provider_id = 'oidc.{0}'.format(_random_string())
817855
return auth.create_oidc_provider_config(
818856
provider_id=provider_id,
@@ -823,6 +861,8 @@ def _create_oidc_provider_config():
823861

824862

825863
def _create_saml_provider_config():
864+
if emulator_host:
865+
pytest.skip("Not supported with auth emulator")
826866
provider_id = 'saml.{0}'.format(_random_string())
827867
return auth.create_saml_provider_config(
828868
provider_id=provider_id,

tests/test_token_gen.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import firebase_admin
3131
from firebase_admin import auth
32+
from firebase_admin import _auth_utils
3233
from firebase_admin import credentials
3334
from firebase_admin import exceptions
3435
from firebase_admin import _token_gen
@@ -219,6 +220,8 @@ class TestCreateCustomToken:
219220

220221
@pytest.mark.parametrize('values', valid_args.values(), ids=list(valid_args))
221222
def test_valid_params(self, auth_app, values):
223+
if _auth_utils.is_emulated():
224+
pytest.skip("Not supported with auth emulator")
222225
user, claims = values
223226
custom_token = auth.create_custom_token(user, claims, app=auth_app)
224227
verify_custom_token(custom_token, claims)
@@ -230,10 +233,14 @@ def test_invalid_params(self, auth_app, values):
230233
auth.create_custom_token(user, claims, app=auth_app)
231234

232235
def test_noncert_credential(self, user_mgt_app):
236+
if _auth_utils.is_emulated():
237+
pytest.skip("Not supported with auth emulator")
233238
with pytest.raises(ValueError):
234239
auth.create_custom_token(MOCK_UID, app=user_mgt_app)
235240

236241
def test_sign_with_iam(self):
242+
if _auth_utils.is_emulated():
243+
pytest.skip("Not supported with auth emulator")
237244
options = {'serviceAccountId': 'test-service-account', 'projectId': 'mock-project-id'}
238245
app = firebase_admin.initialize_app(
239246
testutils.MockCredential(), name='iam-signer-app', options=options)
@@ -248,6 +255,8 @@ def test_sign_with_iam(self):
248255
firebase_admin.delete_app(app)
249256

250257
def test_sign_with_iam_error(self):
258+
if _auth_utils.is_emulated():
259+
pytest.skip("Not supported with auth emulator")
251260
options = {'serviceAccountId': 'test-service-account', 'projectId': 'mock-project-id'}
252261
app = firebase_admin.initialize_app(
253262
testutils.MockCredential(), name='iam-signer-app', options=options)
@@ -264,6 +273,8 @@ def test_sign_with_iam_error(self):
264273
firebase_admin.delete_app(app)
265274

266275
def test_sign_with_discovered_service_account(self):
276+
if _auth_utils.is_emulated():
277+
pytest.skip("Not supported with auth emulator")
267278
request = testutils.MockRequest(200, 'discovered-service-account')
268279
options = {'projectId': 'mock-project-id'}
269280
app = firebase_admin.initialize_app(testutils.MockCredential(), name='iam-signer-app',
@@ -287,6 +298,8 @@ def test_sign_with_discovered_service_account(self):
287298
firebase_admin.delete_app(app)
288299

289300
def test_sign_with_discovery_failure(self):
301+
if _auth_utils.is_emulated():
302+
pytest.skip("Not supported with auth emulator")
290303
request = testutils.MockFailedRequest(Exception('test error'))
291304
options = {'projectId': 'mock-project-id'}
292305
app = firebase_admin.initialize_app(testutils.MockCredential(), name='iam-signer-app',
@@ -431,6 +444,8 @@ def test_valid_token_check_revoked(self, user_mgt_app, id_token):
431444

432445
@pytest.mark.parametrize('id_token', valid_tokens.values(), ids=list(valid_tokens))
433446
def test_revoked_token_check_revoked(self, user_mgt_app, revoked_tokens, id_token):
447+
if _auth_utils.is_emulated():
448+
pytest.skip("Not supported with auth emulator")
434449
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
435450
_instrument_user_manager(user_mgt_app, 200, revoked_tokens)
436451
with pytest.raises(auth.RevokedIdTokenError) as excinfo:
@@ -460,13 +475,18 @@ def test_invalid_arg(self, user_mgt_app, id_token):
460475

461476
@pytest.mark.parametrize('id_token', invalid_tokens.values(), ids=list(invalid_tokens))
462477
def test_invalid_token(self, user_mgt_app, id_token):
478+
if _auth_utils.is_emulated():
479+
pytest.skip("Not supported with auth emulator")
463480
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
464481
with pytest.raises(auth.InvalidIdTokenError) as excinfo:
465482
auth.verify_id_token(id_token, app=user_mgt_app)
466483
assert isinstance(excinfo.value, exceptions.InvalidArgumentError)
467484
assert excinfo.value.http_response is None
468485

469486
def test_expired_token(self, user_mgt_app):
487+
if _auth_utils.is_emulated():
488+
pytest.skip("Not supported with auth emulator")
489+
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
470490
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
471491
id_token = self.invalid_tokens['ExpiredToken']
472492
with pytest.raises(auth.ExpiredIdTokenError) as excinfo:
@@ -505,6 +525,8 @@ def test_custom_token(self, auth_app):
505525
assert str(excinfo.value) == message
506526

507527
def test_certificate_request_failure(self, user_mgt_app):
528+
if _auth_utils.is_emulated():
529+
pytest.skip("Not supported with auth emulator")
508530
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
509531
with pytest.raises(auth.CertificateFetchError) as excinfo:
510532
auth.verify_id_token(TEST_ID_TOKEN, app=user_mgt_app)
@@ -580,13 +602,17 @@ def test_invalid_args(self, user_mgt_app, cookie):
580602

581603
@pytest.mark.parametrize('cookie', invalid_cookies.values(), ids=list(invalid_cookies))
582604
def test_invalid_cookie(self, user_mgt_app, cookie):
605+
if _auth_utils.is_emulated():
606+
pytest.skip("Not supported with auth emulator")
583607
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
584608
with pytest.raises(auth.InvalidSessionCookieError) as excinfo:
585609
auth.verify_session_cookie(cookie, app=user_mgt_app)
586610
assert isinstance(excinfo.value, exceptions.InvalidArgumentError)
587611
assert excinfo.value.http_response is None
588612

589613
def test_expired_cookie(self, user_mgt_app):
614+
if _auth_utils.is_emulated():
615+
pytest.skip("Not supported with auth emulator")
590616
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
591617
cookie = self.invalid_cookies['ExpiredCookie']
592618
with pytest.raises(auth.ExpiredSessionCookieError) as excinfo:
@@ -620,6 +646,8 @@ def test_custom_token(self, auth_app):
620646
auth.verify_session_cookie(custom_token, app=auth_app)
621647

622648
def test_certificate_request_failure(self, user_mgt_app):
649+
if _auth_utils.is_emulated():
650+
pytest.skip("Not supported with auth emulator")
623651
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
624652
with pytest.raises(auth.CertificateFetchError) as excinfo:
625653
auth.verify_session_cookie(TEST_SESSION_COOKIE, app=user_mgt_app)
@@ -632,6 +660,8 @@ def test_certificate_request_failure(self, user_mgt_app):
632660
class TestCertificateCaching:
633661

634662
def test_certificate_caching(self, user_mgt_app, httpserver):
663+
if _auth_utils.is_emulated():
664+
pytest.skip("Not supported with auth emulator")
635665
httpserver.serve_content(MOCK_PUBLIC_CERTS, 200, headers={'Cache-Control': 'max-age=3600'})
636666
verifier = _token_gen.TokenVerifier(user_mgt_app)
637667
verifier.cookie_verifier.cert_url = httpserver.url

0 commit comments

Comments
 (0)