-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(secrets): helper function gets secret by name
- Loading branch information
1 parent
29a0d41
commit 6ec2455
Showing
2 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
|
||
from benefits.secrets import KEY_VAULT_URL, get_secret_by_name | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_DefaultAzureCredential(mocker): | ||
# patching the class to ensure new instances always return the same mock | ||
credential_cls = mocker.patch("benefits.secrets.DefaultAzureCredential") | ||
credential_cls.return_value = mocker.Mock() | ||
return credential_cls | ||
|
||
|
||
def test_get_secret_by_name__mock_client__returns_value(mocker): | ||
secret_name = "the secret name" | ||
secret_value = "the secret value" | ||
client = mocker.patch("benefits.secrets.SecretClient") | ||
client.get_secret.return_value = mocker.Mock(value=secret_value) | ||
|
||
actual_value = get_secret_by_name(secret_name, client) | ||
|
||
client.get_secret.assert_called_once_with(secret_name) | ||
assert actual_value == secret_value | ||
|
||
|
||
def test_get_secret_by_name__None_client__returns_value(mocker, settings, mock_DefaultAzureCredential): | ||
secret_name = "the secret name" | ||
secret_value = "the secret value" | ||
|
||
# override runtime to dev | ||
settings.RUNTIME_ENVIRONMENT = lambda: "dev" | ||
expected_keyvault_url = KEY_VAULT_URL.format(env="d") | ||
|
||
# set up the mock client class and expected return values | ||
# this test does not pass in a known client, instead checking that a client is constructed as expected | ||
mock_credential = mock_DefaultAzureCredential.return_value | ||
client_cls = mocker.patch("benefits.secrets.SecretClient") | ||
client = client_cls.return_value | ||
client.get_secret.return_value = mocker.Mock(value=secret_value) | ||
|
||
actual_value = get_secret_by_name(secret_name) | ||
|
||
client_cls.assert_called_once_with(vault_url=expected_keyvault_url, credential=mock_credential) | ||
client.get_secret.assert_called_once_with(secret_name) | ||
assert actual_value == secret_value |