Skip to content

Commit 14d1c01

Browse files
committed
Skip tests that fail with auth emulator
1 parent 16f9f3d commit 14d1c01

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def _overwrite_iam_request(app, request):
141141
client = auth._get_client(app)
142142
client._token_generator.request = request
143143

144+
145+
def _is_emulated():
146+
emulator_host = os.getenv(EMULATOR_HOST_ENV_VAR, '')
147+
return emulator_host and '//' not in emulator_host
148+
149+
144150
@pytest.fixture(scope='module', params=[{'emulated': False}, {'emulated': True}])
145151
def auth_app(request):
146152
"""Returns an App initialized with a mock service account credential.
@@ -219,6 +225,8 @@ class TestCreateCustomToken:
219225

220226
@pytest.mark.parametrize('values', valid_args.values(), ids=list(valid_args))
221227
def test_valid_params(self, auth_app, values):
228+
if _is_emulated():
229+
pytest.skip("Not supported with auth emulator")
222230
user, claims = values
223231
custom_token = auth.create_custom_token(user, claims, app=auth_app)
224232
verify_custom_token(custom_token, claims)
@@ -230,10 +238,14 @@ def test_invalid_params(self, auth_app, values):
230238
auth.create_custom_token(user, claims, app=auth_app)
231239

232240
def test_noncert_credential(self, user_mgt_app):
241+
if _is_emulated():
242+
pytest.skip("Not supported with auth emulator")
233243
with pytest.raises(ValueError):
234244
auth.create_custom_token(MOCK_UID, app=user_mgt_app)
235245

236246
def test_sign_with_iam(self):
247+
if _is_emulated():
248+
pytest.skip("Not supported with auth emulator")
237249
options = {'serviceAccountId': 'test-service-account', 'projectId': 'mock-project-id'}
238250
app = firebase_admin.initialize_app(
239251
testutils.MockCredential(), name='iam-signer-app', options=options)
@@ -248,6 +260,8 @@ def test_sign_with_iam(self):
248260
firebase_admin.delete_app(app)
249261

250262
def test_sign_with_iam_error(self):
263+
if _is_emulated():
264+
pytest.skip("Not supported with auth emulator")
251265
options = {'serviceAccountId': 'test-service-account', 'projectId': 'mock-project-id'}
252266
app = firebase_admin.initialize_app(
253267
testutils.MockCredential(), name='iam-signer-app', options=options)
@@ -264,6 +278,8 @@ def test_sign_with_iam_error(self):
264278
firebase_admin.delete_app(app)
265279

266280
def test_sign_with_discovered_service_account(self):
281+
if _is_emulated():
282+
pytest.skip("Not supported with auth emulator")
267283
request = testutils.MockRequest(200, 'discovered-service-account')
268284
options = {'projectId': 'mock-project-id'}
269285
app = firebase_admin.initialize_app(testutils.MockCredential(), name='iam-signer-app',
@@ -287,6 +303,8 @@ def test_sign_with_discovered_service_account(self):
287303
firebase_admin.delete_app(app)
288304

289305
def test_sign_with_discovery_failure(self):
306+
if _is_emulated():
307+
pytest.skip("Not supported with auth emulator")
290308
request = testutils.MockFailedRequest(Exception('test error'))
291309
options = {'projectId': 'mock-project-id'}
292310
app = firebase_admin.initialize_app(testutils.MockCredential(), name='iam-signer-app',
@@ -431,6 +449,8 @@ def test_valid_token_check_revoked(self, user_mgt_app, id_token):
431449

432450
@pytest.mark.parametrize('id_token', valid_tokens.values(), ids=list(valid_tokens))
433451
def test_revoked_token_check_revoked(self, user_mgt_app, revoked_tokens, id_token):
452+
if _is_emulated():
453+
pytest.skip("Not supported with auth emulator")
434454
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
435455
_instrument_user_manager(user_mgt_app, 200, revoked_tokens)
436456
with pytest.raises(auth.RevokedIdTokenError) as excinfo:
@@ -460,13 +480,18 @@ def test_invalid_arg(self, user_mgt_app, id_token):
460480

461481
@pytest.mark.parametrize('id_token', invalid_tokens.values(), ids=list(invalid_tokens))
462482
def test_invalid_token(self, user_mgt_app, id_token):
483+
if _is_emulated():
484+
pytest.skip("Not supported with auth emulator")
463485
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
464486
with pytest.raises(auth.InvalidIdTokenError) as excinfo:
465487
auth.verify_id_token(id_token, app=user_mgt_app)
466488
assert isinstance(excinfo.value, exceptions.InvalidArgumentError)
467489
assert excinfo.value.http_response is None
468490

469491
def test_expired_token(self, user_mgt_app):
492+
if _is_emulated():
493+
pytest.skip("Not supported with auth emulator")
494+
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
470495
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
471496
id_token = self.invalid_tokens['ExpiredToken']
472497
with pytest.raises(auth.ExpiredIdTokenError) as excinfo:
@@ -505,6 +530,8 @@ def test_custom_token(self, auth_app):
505530
assert str(excinfo.value) == message
506531

507532
def test_certificate_request_failure(self, user_mgt_app):
533+
if _is_emulated():
534+
pytest.skip("Not supported with auth emulator")
508535
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
509536
with pytest.raises(auth.CertificateFetchError) as excinfo:
510537
auth.verify_id_token(TEST_ID_TOKEN, app=user_mgt_app)
@@ -580,13 +607,17 @@ def test_invalid_args(self, user_mgt_app, cookie):
580607

581608
@pytest.mark.parametrize('cookie', invalid_cookies.values(), ids=list(invalid_cookies))
582609
def test_invalid_cookie(self, user_mgt_app, cookie):
610+
if _is_emulated():
611+
pytest.skip("Not supported with auth emulator")
583612
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
584613
with pytest.raises(auth.InvalidSessionCookieError) as excinfo:
585614
auth.verify_session_cookie(cookie, app=user_mgt_app)
586615
assert isinstance(excinfo.value, exceptions.InvalidArgumentError)
587616
assert excinfo.value.http_response is None
588617

589618
def test_expired_cookie(self, user_mgt_app):
619+
if _is_emulated():
620+
pytest.skip("Not supported with auth emulator")
590621
_overwrite_cert_request(user_mgt_app, MOCK_REQUEST)
591622
cookie = self.invalid_cookies['ExpiredCookie']
592623
with pytest.raises(auth.ExpiredSessionCookieError) as excinfo:
@@ -620,6 +651,8 @@ def test_custom_token(self, auth_app):
620651
auth.verify_session_cookie(custom_token, app=auth_app)
621652

622653
def test_certificate_request_failure(self, user_mgt_app):
654+
if _is_emulated():
655+
pytest.skip("Not supported with auth emulator")
623656
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
624657
with pytest.raises(auth.CertificateFetchError) as excinfo:
625658
auth.verify_session_cookie(TEST_SESSION_COOKIE, app=user_mgt_app)
@@ -632,6 +665,8 @@ def test_certificate_request_failure(self, user_mgt_app):
632665
class TestCertificateCaching:
633666

634667
def test_certificate_caching(self, user_mgt_app, httpserver):
668+
if _is_emulated():
669+
pytest.skip("Not supported with auth emulator")
635670
httpserver.serve_content(MOCK_PUBLIC_CERTS, 200, headers={'Cache-Control': 'max-age=3600'})
636671
verifier = _token_gen.TokenVerifier(user_mgt_app)
637672
verifier.cookie_verifier.cert_url = httpserver.url

0 commit comments

Comments
 (0)