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

[Key Vault] Target multiple API versions with tests (keys) #18149

Merged
merged 9 commits into from
Apr 23, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
50 changes: 47 additions & 3 deletions sdk/keyvault/azure-keyvault-keys/tests/_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,60 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools
import os

from azure.keyvault.keys._shared import HttpChallengeCache
from azure.keyvault.keys._shared.client_base import ApiVersion, DEFAULT_VERSION
from devtools_testutils import AzureTestCase
from parameterized import parameterized
from parameterized import parameterized, param
import pytest
from six.moves.urllib_parse import urlparse


def client_setup(testcase_func):
"""decorator that creates a client to be passed in to a test method"""
@functools.wraps(testcase_func)
def wrapper(test_class_instance, api_version, is_hsm=False, **kwargs):
test_class_instance._skip_if_not_configured(api_version, is_hsm)
endpoint_url = test_class_instance.managed_hsm_url if is_hsm else test_class_instance.vault_url
client = test_class_instance.create_key_client(endpoint_url, api_version=api_version, **kwargs)

if kwargs.get("is_async"):
import asyncio

coroutine = testcase_func(test_class_instance, client, is_hsm=is_hsm)
loop = asyncio.get_event_loop()
loop.run_until_complete(coroutine)
else:
testcase_func(test_class_instance, client, is_hsm=is_hsm)
return wrapper


def get_decorator(hsm_only=False, vault_only=False, **kwargs):
"""returns a test decorator for test parameterization"""
params = [param(api_version=p[0], is_hsm=p[1], **kwargs) for p in get_test_parameters(hsm_only, vault_only)]
return functools.partial(parameterized.expand, params, name_func=suffixed_test_name)


def get_test_parameters(hsm_only=False, vault_only=False):
"""generates a list of parameter pairs for test case parameterization, where [x, y] = [api_version, is_hsm]"""
combinations = []
hsm_supported_versions = {ApiVersion.V7_2_preview}
for api_version in ApiVersion:
if not vault_only and api_version in hsm_supported_versions:
combinations.append([api_version, True])
if not hsm_only:
combinations.append([api_version, False])
return combinations


def suffixed_test_name(testcase_func, param_num, param):
api_version = param.kwargs.get("api_version")
suffix = "mhsm" if param.kwargs.get("is_hsm") else "vault"
return "{}_{}".format(testcase_func.__name__, parameterized.to_safe_name(suffix))
return "{}_{}_{}".format(
testcase_func.__name__, parameterized.to_safe_name(api_version), parameterized.to_safe_name(suffix)
)


class KeysTestCase(AzureTestCase):
Expand Down Expand Up @@ -69,6 +111,8 @@ def _set_mgmt_settings_real_values(self):
os.environ["AZURE_CLIENT_ID"] = os.environ["KEYVAULT_CLIENT_ID"]
os.environ["AZURE_CLIENT_SECRET"] = os.environ["KEYVAULT_CLIENT_SECRET"]

def _skip_if_not_configured(self, is_hsm):
def _skip_if_not_configured(self, api_version, is_hsm):
if self.is_live and api_version != DEFAULT_VERSION:
pytest.skip("This test only uses the default API version for live tests")
if self.is_live and is_hsm and self.managed_hsm_url is None:
pytest.skip("No HSM endpoint for live testing")
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '0'
Content-Type:
- application/json
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: POST
uri: https://vaultname.vault.azure.net/keys/livekvtesteckeyf85d12bb/create?api-version=2016-10-01
response:
body:
string: '{"error":{"code":"Unauthorized","message":"Request is missing a Bearer
or PoP token."}}'
headers:
cache-control:
- no-cache
content-length:
- '87'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:33 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
www-authenticate:
- Bearer authorization="https://login.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47",
resource="https://vault.azure.net"
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 401
message: Unauthorized
- request:
body: '{"kty": "EC"}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '13'
Content-Type:
- application/json
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: POST
uri: https://vaultname.vault.azure.net/keys/livekvtesteckeyf85d12bb/create?api-version=2016-10-01
response:
body:
string: '{"key":{"kid":"https://vaultname.vault.azure.net/keys/livekvtesteckeyf85d12bb/bea3d5415a7c4e3591410cebfb0dfaaf","kty":"EC","key_ops":["sign","verify"],"crv":"P-256","x":"b0O3jO6GL6l38y_K2_QCFqeRJ4SDkyva0COo42w_Ayo","y":"7BYDFEutE1q9IkShnut7vqBoPtQQ6WTneKsIDmkQM-4"},"attributes":{"enabled":true,"created":1618865374,"updated":1618865374,"recoveryLevel":"Recoverable+Purgeable"}}'
headers:
cache-control:
- no-cache
content-length:
- '381'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:34 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://vaultname.vault.azure.net/keys/livekvtesteckeyf85d12bb/bea3d5415a7c4e3591410cebfb0dfaaf?api-version=7.2-preview
response:
body:
string: '{"key":{"kid":"https://vaultname.vault.azure.net/keys/livekvtesteckeyf85d12bb/bea3d5415a7c4e3591410cebfb0dfaaf","kty":"EC","key_ops":["sign","verify"],"crv":"P-256","x":"b0O3jO6GL6l38y_K2_QCFqeRJ4SDkyva0COo42w_Ayo","y":"7BYDFEutE1q9IkShnut7vqBoPtQQ6WTneKsIDmkQM-4"},"attributes":{"enabled":true,"created":1618865374,"updated":1618865374,"recoveryLevel":"Recoverable+Purgeable","recoverableDays":90}}'
headers:
cache-control:
- no-cache
content-length:
- '402'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:35 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '0'
Content-Type:
- application/json
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: POST
uri: https://vaultname.vault.azure.net/keys/livekvtesteckey834a1138/create?api-version=7.0
response:
body:
string: '{"error":{"code":"Unauthorized","message":"Request is missing a Bearer
or PoP token."}}'
headers:
cache-control:
- no-cache
content-length:
- '87'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:33 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
www-authenticate:
- Bearer authorization="https://login.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47",
resource="https://vault.azure.net"
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 401
message: Unauthorized
- request:
body: '{"kty": "EC"}'
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '13'
Content-Type:
- application/json
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: POST
uri: https://vaultname.vault.azure.net/keys/livekvtesteckey834a1138/create?api-version=7.0
response:
body:
string: '{"key":{"kid":"https://vaultname.vault.azure.net/keys/livekvtesteckey834a1138/061999b7542745bd81eaea5a4e5baf7c","kty":"EC","key_ops":["sign","verify"],"crv":"P-256","x":"iGlAng69Ar98HwKA-lYEMxFuFlCg7VnK0e7fj1eVEO0","y":"gKStaSqV9l5xm_4pyzisXrdfHHhdTG7HX1LrMjOhY0k"},"attributes":{"enabled":true,"created":1618865374,"updated":1618865374,"recoveryLevel":"Recoverable+Purgeable"}}'
headers:
cache-control:
- no-cache
content-length:
- '381'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:34 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-keyvault-keys/4.4.0b5 Python/3.5.3 (Windows-10-10.0.19041-SP0)
method: GET
uri: https://vaultname.vault.azure.net/keys/livekvtesteckey834a1138/061999b7542745bd81eaea5a4e5baf7c?api-version=7.2-preview
response:
body:
string: '{"key":{"kid":"https://vaultname.vault.azure.net/keys/livekvtesteckey834a1138/061999b7542745bd81eaea5a4e5baf7c","kty":"EC","key_ops":["sign","verify"],"crv":"P-256","x":"iGlAng69Ar98HwKA-lYEMxFuFlCg7VnK0e7fj1eVEO0","y":"gKStaSqV9l5xm_4pyzisXrdfHHhdTG7HX1LrMjOhY0k"},"attributes":{"enabled":true,"created":1618865374,"updated":1618865374,"recoveryLevel":"Recoverable+Purgeable","recoverableDays":90}}'
headers:
cache-control:
- no-cache
content-length:
- '402'
content-type:
- application/json; charset=utf-8
date:
- Mon, 19 Apr 2021 20:49:36 GMT
expires:
- '-1'
pragma:
- no-cache
strict-transport-security:
- max-age=31536000;includeSubDomains
x-content-type-options:
- nosniff
x-ms-keyvault-network-info:
- conn_type=Ipv4;addr=172.92.159.124;act_addr_fam=InterNetwork;
x-ms-keyvault-region:
- eastus2
x-ms-keyvault-service-version:
- 1.2.236.0
x-powered-by:
- ASP.NET
status:
code: 200
message: OK
version: 1
Loading