Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split update_key into 2 methods #16405

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,36 @@ def api_key(self):
"""
return self._api_key

def update(self, subscription_key=None, api_key=None):
# type: (str, str) -> None
"""Update the subscription and/or api key.
def update_subscription_key(self, subscription_key):
# type: (str) -> None
"""Update the subscription key.

This can be used when you've regenerated your service keys and want
to update long-lived clients.

:param str subscription_key: The subscription key
:param str api_key: The api key
:raises: ValueError or TypeError
"""
if not subscription_key and not api_key:
raise ValueError("Pass at least one non-empty key for updating.")
if subscription_key:
if not isinstance(subscription_key, six.string_types):
raise TypeError("The subscription_key used for updating must be a string.")
self._subscription_key = subscription_key
else:
raise ValueError("Subscription key cannot be None.")

def update_api_key(self, api_key):
# type: (str) -> None
"""Update the api key.

This can be used when you've regenerated your service keys and want
to update long-lived clients.

:param str api_key: The api key
:raises: ValueError or TypeError
"""
if api_key:
if not isinstance(api_key, six.string_types):
raise TypeError("The api_key used for updating must be a string.")
self._api_key = api_key
else:
raise ValueError("API key cannot be None.")
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ def test_credential_rotate_both_keys(self):
assert result

# rotate both keys
credential.update(subscription_key="xxx", api_key="xxx")
credential.update_subscription_key("xxx")
assert credential.subscription_key == "xxx"
credential.update_api_key("xxx")
assert credential.api_key == "xxx"

# call fails
with pytest.raises(ClientAuthenticationError):
result = client.get_feedback(feedback_id=self.feedback_id)

# rotate back to valid credentials
credential.update(subscription_key=self.subscription_key, api_key=self.api_key)
credential.update_subscription_key(self.subscription_key)
assert credential.subscription_key == self.subscription_key
credential.update_api_key(self.api_key)
assert credential.api_key == self.api_key

# make successful call
Expand All @@ -58,7 +60,7 @@ def test_credential_rotate_sub_key_only(self):
assert result

# rotate one key
credential.update(subscription_key="xxx")
credential.update_subscription_key("xxx")
assert credential.subscription_key == "xxx"
assert credential.api_key == self.api_key

Expand All @@ -67,7 +69,7 @@ def test_credential_rotate_sub_key_only(self):
result = client.get_feedback(feedback_id=self.feedback_id)

# rotate back to valid credentials
credential.update(subscription_key=self.subscription_key)
credential.update_subscription_key(self.subscription_key)
assert credential.subscription_key == self.subscription_key
assert credential.api_key == self.api_key

Expand All @@ -84,7 +86,7 @@ def test_credential_rotate_api_key_only(self):
assert result

# rotate one key
credential.update(api_key="xxx")
credential.update_api_key("xxx")
assert credential.subscription_key == self.subscription_key
assert credential.api_key == "xxx"

Expand All @@ -93,7 +95,7 @@ def test_credential_rotate_api_key_only(self):
result = client.get_feedback(feedback_id=self.feedback_id)

# rotate back to valid credentials
credential.update(api_key=self.api_key)
credential.update_api_key(self.api_key)
assert credential.subscription_key == self.subscription_key
assert credential.api_key == self.api_key

Expand All @@ -105,8 +107,10 @@ def test_credential_bad_input(self):
credential = MetricsAdvisorKeyCredential(self.subscription_key, self.api_key)

with pytest.raises(ValueError):
credential.update()
credential.update_subscription_key(None)
with pytest.raises(ValueError):
credential.update_api_key(None)
with pytest.raises(TypeError):
credential.update(subscription_key=34)
credential.update_subscription_key(subscription_key=34)
with pytest.raises(TypeError):
credential.update(api_key=34)
credential.update_api_key(api_key=34)