1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import platform
import unittest
from datetime import datetime, timedelta
import pytest
from azure.storage.queue import AccountSasPermissions, generate_account_sas, ResourceTypes, VERSION
from azure.storage.queue.aio import QueueClient, QueueServiceClient
from devtools_testutils.aio import recorded_by_proxy_async
from devtools_testutils.storage.aio import AsyncStorageRecordedTestCase
from settings.testcase import QueuePreparer
# ------------------------------------------------------------------------------
SERVICES = {
QueueServiceClient: "queue",
QueueClient: "queue",
}
_CONNECTION_ENDPOINTS = {"queue": "QueueEndpoint"}
_CONNECTION_ENDPOINTS_SECONDARY = {"queue": "QueueSecondaryEndpoint"}
class TestAsyncStorageQueueClient(AsyncStorageRecordedTestCase):
def setUp(self):
self.sas_token = self.generate_fake_sas_token()
self.token_credential = self.get_credential(QueueServiceClient, is_async=True)
# --Helpers-----------------------------------------------------------------
def validate_standard_account_endpoints(self, service, url_type, storage_account_name, storage_account_key):
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert f"{storage_account_name}.{url_type}.core.windows.net" in service.url
assert f"{storage_account_name}-secondary.{url_type}.core.windows.net" in service.secondary_endpoint
def generate_fake_sas_token(self):
fake_key = "a" * 30 + "b" * 30
return "?" + generate_account_sas(
account_name="test", # name of the storage account
account_key=fake_key, # key for the storage account
resource_types=ResourceTypes(object=True),
permission=AccountSasPermissions(read=True, list=True),
start=datetime.now() - timedelta(hours=24),
expiry=datetime.now() + timedelta(days=8),
)
# --Direct Parameters Test Cases --------------------------------------------
@QueuePreparer()
def test_create_service_with_key(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account_name, "queue"), credential=storage_account_key, queue_name="foo"
)
# Assert
self.validate_standard_account_endpoints(service, url, storage_account_name, storage_account_key)
assert service.scheme == "https"
@QueuePreparer()
def test_create_service_with_connection_string(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(
self.connection_string(storage_account_name, storage_account_key), queue_name="test"
)
# Assert
self.validate_standard_account_endpoints(
service, service_type[1], storage_account_name, storage_account_key
)
assert service.scheme == "https"
@QueuePreparer()
def test_create_service_with_sas(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES:
# Act
service = service_type(
self.account_url(storage_account_name, "queue"), credential=self.sas_token, queue_name="foo"
)
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.url.startswith("https://" + storage_account_name + ".queue.core.windows.net")
assert service.url.endswith(self.sas_token)
assert service.credential is None
@QueuePreparer()
async def test_create_service_with_token(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
self.setUp()
for service_type in SERVICES:
# Act
service = service_type(
self.account_url(storage_account_name, "queue"), credential=self.token_credential, queue_name="foo"
)
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.url.startswith("https://" + storage_account_name + ".queue.core.windows.net")
assert service.credential == self.token_credential
assert not hasattr(service.credential, "account_key")
assert hasattr(service.credential, "get_token")
@QueuePreparer()
async def test_create_service_with_token_and_http(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
self.setUp()
for service_type in SERVICES:
# Act
with pytest.raises(ValueError):
url = self.account_url(storage_account_name, "queue").replace("https", "http")
service_type(url, credential=self.token_credential, queue_name="foo")
@QueuePreparer()
def test_create_service_china(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
# Act
url = self.account_url(storage_account_name, "queue").replace("core.windows.net", "core.chinacloudapi.cn")
service = service_type[0](url, credential=storage_account_key, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert (
service.primary_endpoint.startswith(
f"https://{storage_account_name}.{service_type[1]}.core.chinacloudapi.cn"
)
is True
)
assert (
service.secondary_endpoint.startswith(
f"https://{storage_account_name}-secondary.{service_type[1]}.core.chinacloudapi.cn"
)
is True
)
@QueuePreparer()
def test_create_service_protocol(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
# Act
url = self.account_url(storage_account_name, "queue").replace("https", "http")
service = service_type[0](url, credential=storage_account_key, queue_name="foo")
# Assert
self.validate_standard_account_endpoints(
service, service_type[1], storage_account_name, storage_account_key
)
assert service.scheme == "http"
@QueuePreparer()
def test_create_service_empty_key(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
QUEUE_SERVICES = [QueueServiceClient, QueueClient]
for service_type in QUEUE_SERVICES:
# Act
with pytest.raises(ValueError) as e:
test_service = service_type("testaccount", credential="", queue_name="foo")
assert str(e.value) == "You need to provide either a SAS token or an account shared key to authenticate."
@QueuePreparer()
def test_create_service_with_socket_timeout(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
# Act
default_service = service_type[0](
self.account_url(storage_account_name, "queue"), credential=storage_account_key, queue_name="foo"
)
service = service_type[0](
self.account_url(storage_account_name, "queue"),
credential=storage_account_key,
queue_name="foo",
connection_timeout=22,
)
# Assert
self.validate_standard_account_endpoints(
service, service_type[1], storage_account_name, storage_account_key
)
assert service._client._client._pipeline._transport.connection_config.timeout == 22
assert default_service._client._client._pipeline._transport.connection_config.timeout in [20, (20, 2000)]
# --Connection String Test Cases --------------------------------------------
@QueuePreparer()
def test_create_service_with_connection_string_key(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
conn_string = f"AccountName={storage_account_name};" f"AccountKey={storage_account_key};"
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
self.validate_standard_account_endpoints(
service, service_type[1], storage_account_name, storage_account_key
)
assert service.scheme == "https"
@QueuePreparer()
def test_create_service_with_connection_string_sas(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
conn_string = f"AccountName={storage_account_name};" f"SharedAccessSignature={self.sas_token};"
for service_type in SERVICES:
# Act
service = service_type.from_connection_string(conn_string, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.url.startswith("https://" + storage_account_name + ".queue.core.windows.net")
assert service.url.endswith(self.sas_token)
assert service.credential is None
@QueuePreparer()
def test_create_service_with_conn_str_endpoint_protocol(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
"DefaultEndpointsProtocol=http;EndpointSuffix=core.chinacloudapi.cn;"
)
for service_type in SERVICES.items():
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert (
service.primary_endpoint.startswith(
f"http://{storage_account_name}.{service_type[1]}.core.chinacloudapi.cn/"
)
is True
)
assert (
service.secondary_endpoint.startswith(
f"http://{storage_account_name}-secondary.{service_type[1]}.core.chinacloudapi.cn"
)
is True
)
assert service.scheme == "http"
@QueuePreparer()
def test_create_service_with_connection_string_emulated(self, *args):
# Arrange
for service_type in SERVICES.items():
conn_string = "UseDevelopmentStorage=true;"
# Act
with pytest.raises(ValueError):
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
@QueuePreparer()
def test_create_service_with_connection_string_custom_domain(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
"QueueEndpoint=www.mydomain.com;"
)
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert service.primary_endpoint.startswith("https://www.mydomain.com/")
assert service.secondary_endpoint.startswith(
f"https://{storage_account_name}-secondary.queue.core.windows.net"
)
@QueuePreparer()
def test_create_serv_with_cs_custom_dmn_trlng_slash(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
"QueueEndpoint=www.mydomain.com/;"
)
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert service.primary_endpoint.startswith("https://www.mydomain.com/")
assert service.secondary_endpoint.startswith(
f"https://{storage_account_name}-secondary.queue.core.windows.net"
)
@QueuePreparer()
def test_create_service_with_cs_custom_dmn_sec_override(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for service_type in SERVICES.items():
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
"QueueEndpoint=www.mydomain.com/;"
)
# Act
service = service_type[0].from_connection_string(
conn_string, secondary_hostname="www-sec.mydomain.com", queue_name="foo"
)
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert service.primary_endpoint.startswith("https://www.mydomain.com/")
assert service.secondary_endpoint.startswith("https://www-sec.mydomain.com/")
@QueuePreparer()
def test_create_service_with_cs_fails_if_sec_without_prim(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
for service_type in SERVICES.items():
# Arrange
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
f"{_CONNECTION_ENDPOINTS_SECONDARY.get(service_type[1])}=www.mydomain.com;"
)
# Act
# Fails if primary excluded
with pytest.raises(ValueError):
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
@QueuePreparer()
def test_create_service_with_cs_succeeds_if_sec_with_prim(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
for service_type in SERVICES.items():
# Arrange
conn_string = (
f"AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
f"{_CONNECTION_ENDPOINTS.get(service_type[1])}=www.mydomain.com;"
f"{_CONNECTION_ENDPOINTS_SECONDARY.get(service_type[1])}=www-sec.mydomain.com;"
)
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
assert service is not None
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert service.primary_endpoint.startswith("https://www.mydomain.com/")
assert service.secondary_endpoint.startswith("https://www-sec.mydomain.com/")
@QueuePreparer()
def test_create_service_with_custom_account_endpoint_path(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token
for service_type in SERVICES.items():
conn_string = (
f"DefaultEndpointsProtocol=http;AccountName={storage_account_name};"
f"AccountKey={storage_account_key};"
f"QueueEndpoint={custom_account_url};"
)
# Act
service = service_type[0].from_connection_string(conn_string, queue_name="foo")
# Assert
assert service.account_name == storage_account_name
assert service.credential.account_name == storage_account_name
assert service.credential.account_key == storage_account_key
assert service.primary_hostname == "local-machine:11002/custom/account/path"
service = QueueServiceClient(account_url=custom_account_url)
assert service.account_name == None
assert service.credential == None
assert service.primary_hostname == "local-machine:11002/custom/account/path"
assert service.url.startswith("http://local-machine:11002/custom/account/path/?")
service = QueueClient(account_url=custom_account_url, queue_name="foo")
assert service.account_name == None
assert service.queue_name == "foo"
assert service.credential == None
assert service.primary_hostname == "local-machine:11002/custom/account/path"
assert service.url.startswith("http://local-machine:11002/custom/account/path/foo?")
service = QueueClient.from_queue_url("http://local-machine:11002/custom/account/path/foo" + self.sas_token)
assert service.account_name == None
assert service.queue_name == "foo"
assert service.credential == None
assert service.primary_hostname == "local-machine:11002/custom/account/path"
assert service.url.startswith("http://local-machine:11002/custom/account/path/foo?")
@QueuePreparer()
@recorded_by_proxy_async
async def test_request_callback_signed_header(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
service = QueueServiceClient(self.account_url(storage_account_name, "queue"), credential=storage_account_key)
name = self.get_resource_name("cont")
# Act
try:
headers = {"x-ms-meta-hello": "world"}
queue = await service.create_queue(name, headers=headers)
# Assert
metadata_cr = await queue.get_queue_properties()
metadata = metadata_cr.metadata
assert metadata == {"hello": "world"}
finally:
await service.delete_queue(name)
@QueuePreparer()
@recorded_by_proxy_async
async def test_response_callback(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
service = QueueServiceClient(self.account_url(storage_account_name, "queue"), credential=storage_account_key)
name = self.get_resource_name("cont")
queue = service.get_queue_client(name)
# Act
def callback(response):
response.http_response.status_code = 200
# Assert
exists = await queue.get_queue_properties(raw_response_hook=callback)
assert exists
@QueuePreparer()
@recorded_by_proxy_async
async def test_user_agent_default(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
service = QueueServiceClient(self.account_url(storage_account_name, "queue"), credential=storage_account_key)
def callback(response):
assert "User-Agent" in response.http_request.headers
assert f"azsdk-python-storage-queue/{VERSION}" in response.http_request.headers["User-Agent"]
await service.get_service_properties(raw_response_hook=callback)
@QueuePreparer()
@recorded_by_proxy_async
async def test_user_agent_custom(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
custom_app = "TestApp/v1.0"
service = QueueServiceClient(
self.account_url(storage_account_name, "queue"), credential=storage_account_key, user_agent=custom_app
)
def callback(response):
assert "User-Agent" in response.http_request.headers
assert (
f"TestApp/v1.0 azsdk-python-storage-queue/{VERSION} "
f"Python/{platform.python_version()} "
f"({platform.platform()})"
) in response.http_request.headers["User-Agent"]
await service.get_service_properties(raw_response_hook=callback)
def callback(response):
assert "User-Agent" in response.http_request.headers
assert (
f"TestApp/v2.0 TestApp/v1.0 azsdk-python-storage-queue/{VERSION} "
f"Python/{platform.python_version()} ({platform.platform()})"
) in response.http_request.headers["User-Agent"]
await service.get_service_properties(raw_response_hook=callback, user_agent="TestApp/v2.0")
@QueuePreparer()
@recorded_by_proxy_async
async def test_user_agent_append(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
service = QueueServiceClient(self.account_url(storage_account_name, "queue"), credential=storage_account_key)
def callback(response):
assert "User-Agent" in response.http_request.headers
assert (
f"customer_user_agent azsdk-python-storage-queue/{VERSION} "
f"Python/{platform.python_version()} ({platform.platform()})"
) in response.http_request.headers["User-Agent"]
await service.get_service_properties(raw_response_hook=callback, user_agent="customer_user_agent")
@QueuePreparer()
async def test_closing_pipeline_client(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account_name, "queue"), credential=storage_account_key, queue_name="queue"
)
# Assert
async with service:
assert hasattr(service, "close")
await service.close()
@QueuePreparer()
async def test_closing_pipeline_client_simple(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")
# Arrange
for client, url in SERVICES.items():
# Act
service = client(
self.account_url(storage_account_name, "queue"), credential=storage_account_key, queue_name="queue"
)
await service.close()
@QueuePreparer()
@recorded_by_proxy_async
async def test_get_and_set_queue_access_policy_oauth(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
self.setUp()
# Arrange
service_client = QueueServiceClient(self.account_url(storage_account_name, "queue"), self.token_credential)
queue_client = service_client.get_queue_client(self.get_resource_name("pyqueueasync"))
await queue_client.create_queue()
# Act / Assert
await queue_client.set_queue_access_policy(signed_identifiers={})
acl = await queue_client.get_queue_access_policy()
assert acl is not None
# ------------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()
|