Skip to content

Commit

Permalink
fix(auth): Make auth client respect app options httpTimeout (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellehanks authored Mar 15, 2021
1 parent c10c747 commit 32e45f1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion firebase_admin/_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def __init__(self, app, tenant_id=None):

credential = app.credential.get_credential()
version_header = 'Python/Admin/{0}'.format(firebase_admin.__version__)
timeout = app.options.get('httpTimeout', _http_client.DEFAULT_TIMEOUT_SECONDS)
http_client = _http_client.JsonHttpClient(
credential=credential, headers={'X-Client-Version': version_header})
credential=credential, headers={'X-Client-Version': version_header}, timeout=timeout)

self._tenant_id = tenant_id
self._token_generator = _token_gen.TokenGenerator(app, http_client)
Expand Down
27 changes: 26 additions & 1 deletion tests/test_user_mgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

USER_MGT_URL_PREFIX = 'https://identitytoolkit.googleapis.com/v1/projects/mock-project-id'

TEST_TIMEOUT = 42


@pytest.fixture(scope='module')
def user_mgt_app():
Expand All @@ -60,6 +62,16 @@ def user_mgt_app():
yield app
firebase_admin.delete_app(app)

@pytest.fixture(scope='module')
def user_mgt_app_with_timeout():
app = firebase_admin.initialize_app(
testutils.MockCredential(),
name='userMgtTimeout',
options={'projectId': 'mock-project-id', 'httpTimeout': TEST_TIMEOUT}
)
yield app
firebase_admin.delete_app(app)

def _instrument_user_manager(app, status, payload):
client = auth._get_client(app)
user_manager = client._user_manager
Expand Down Expand Up @@ -105,14 +117,16 @@ def _check_user_record(user, expected_uid='testuser'):
assert provider.provider_id == 'phone'


def _check_request(recorder, want_url, want_body=None):
def _check_request(recorder, want_url, want_body=None, want_timeout=None):
assert len(recorder) == 1
req = recorder[0]
assert req.method == 'POST'
assert req.url == '{0}{1}'.format(USER_MGT_URL_PREFIX, want_url)
if want_body:
body = json.loads(req.body.decode())
assert body == want_body
if want_timeout:
assert recorder[0]._extra_kwargs['timeout'] == pytest.approx(want_timeout, 0.001)


class TestAuthServiceInitialization:
Expand All @@ -122,6 +136,11 @@ def test_default_timeout(self, user_mgt_app):
user_manager = client._user_manager
assert user_manager.http_client.timeout == _http_client.DEFAULT_TIMEOUT_SECONDS

def test_app_options_timeout(self, user_mgt_app_with_timeout):
client = auth._get_client(user_mgt_app_with_timeout)
user_manager = client._user_manager
assert user_manager.http_client.timeout == TEST_TIMEOUT

def test_fail_on_no_project_id(self):
app = firebase_admin.initialize_app(testutils.MockCredential(), name='userMgt2')
with pytest.raises(ValueError):
Expand Down Expand Up @@ -225,6 +244,12 @@ def test_get_user(self, user_mgt_app):
_check_user_record(auth.get_user('testuser', user_mgt_app))
_check_request(recorder, '/accounts:lookup', {'localId': ['testuser']})

def test_get_user_with_timeout(self, user_mgt_app_with_timeout):
_, recorder = _instrument_user_manager(
user_mgt_app_with_timeout, 200, MOCK_GET_USER_RESPONSE)
_check_user_record(auth.get_user('testuser', user_mgt_app_with_timeout))
_check_request(recorder, '/accounts:lookup', {'localId': ['testuser']}, TEST_TIMEOUT)

@pytest.mark.parametrize('arg', INVALID_STRINGS + ['not-an-email'])
def test_invalid_get_user_by_email(self, arg, user_mgt_app):
with pytest.raises(ValueError):
Expand Down

0 comments on commit 32e45f1

Please sign in to comment.