Skip to content

Commit

Permalink
Refactor crypto example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Feb 3, 2021
1 parent de9ba74 commit f3918c9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
37 changes: 26 additions & 11 deletions sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools

from azure.keyvault.keys import KeyClient
from azure.keyvault.keys.crypto import CryptographyClient
from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer
from devtools_testutils import PowerShellPreparer

from _shared.test_case import KeyVaultTestCase
from crypto_client_preparer import CryptoClientPreparer

KeyVaultPreparer = functools.partial(
PowerShellPreparer,
"keyvault",
azure_keyvault_url="https://vaultname.vault.azure.net"
)


class TestCryptoExamples(KeyVaultTestCase):
def __init__(self, *args, **kwargs):
kwargs["match_body"] = False
super(TestCryptoExamples, self).__init__(*args, **kwargs)

def create_client(self, vault_uri, **kwargs):
credential = self.get_credential(KeyClient)
return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs)

def get_crypto_client_credential(self):
return self.get_credential(CryptographyClient)

# pylint:disable=unused-variable

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
def test_encrypt_decrypt(self, key_client, credential, **kwargs):
def test_encrypt_decrypt(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-encrypt-key")
key_client.create_rsa_key(key_name)

Expand Down Expand Up @@ -52,10 +67,10 @@ def test_encrypt_decrypt(self, key_client, credential, **kwargs):
print(result.plaintext)
# [END decrypt]

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
def test_wrap_unwrap(self, key_client, credential, **kwargs):
def test_wrap_unwrap(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand All @@ -79,10 +94,10 @@ def test_wrap_unwrap(self, key_client, credential, **kwargs):
key = result.key
# [END unwrap_key]

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
def test_sign_verify(self, key_client, credential, **kwargs):
def test_sign_verify(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools

from azure.keyvault.keys.aio import KeyClient
from azure.keyvault.keys.crypto.aio import CryptographyClient
from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer
from devtools_testutils import PowerShellPreparer
from _shared.test_case_async import KeyVaultTestCase
from crypto_client_preparer_async import CryptoClientPreparer

KeyVaultPreparer = functools.partial(
PowerShellPreparer,
"keyvault",
azure_keyvault_url="https://vaultname.vault.azure.net"
)


class TestCryptoExamples(KeyVaultTestCase):
def __init__(self, *args, **kwargs):
kwargs["match_body"] = False
super(TestCryptoExamples, self).__init__(*args, **kwargs)

def create_client(self, vault_uri, **kwargs):
credential = self.get_credential(KeyClient, is_async=True)
return self.create_client_from_credential(KeyClient, credential=credential, vault_url=vault_uri, **kwargs)

def get_crypto_client_credential(self):
return self.get_credential(CryptographyClient, is_async=True)

# pylint:disable=unused-variable

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
async def test_encrypt_decrypt_async(self, key_client, credential, **kwargs):
async def test_encrypt_decrypt_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-encrypt-key")
await key_client.create_rsa_key(key_name)

Expand Down Expand Up @@ -51,10 +66,10 @@ async def test_encrypt_decrypt_async(self, key_client, credential, **kwargs):
print(result.plaintext)
# [END decrypt]

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
async def test_wrap_unwrap_async(self, key_client, credential, **kwargs):
async def test_wrap_unwrap_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = await key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand All @@ -77,10 +92,10 @@ async def test_wrap_unwrap_async(self, key_client, credential, **kwargs):
result = await client.unwrap_key(KeyWrapAlgorithm.rsa_oaep, encrypted_key)
# [END unwrap_key]

@ResourceGroupPreparer(random_name_enabled=True)
@KeyVaultPreparer()
@CryptoClientPreparer()
async def test_sign_verify_async(self, key_client, credential, **kwargs):
async def test_sign_verify_async(self, azure_keyvault_url, **kwargs):
key_client = self.create_client(azure_keyvault_url)
credential = self.get_crypto_client_credential()
key_name = self.get_resource_name("crypto-test-wrapping-key")
key = await key_client.create_rsa_key(key_name)
client = CryptographyClient(key, credential)
Expand Down

0 comments on commit f3918c9

Please sign in to comment.