async def _test_sas_signed_identifier(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        access_policy = AccessPolicy()
        access_policy.start = datetime.utcnow() - timedelta(hours=1)
        access_policy.expiry = datetime.utcnow() + timedelta(hours=1)
        access_policy.permission = QueuePermissions.READ

        identifiers = {'testid': access_policy}

        queue_client = await self._create_queue()
        resp = await queue_client.set_queue_access_policy(identifiers)

        await queue_client.enqueue_message(u'message1')

        token = queue_client.generate_shared_access_signature(
            policy_id='testid')

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.peek_messages()

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    def test_create_service_with_custom_account_endpoint_path(self, resource_group, location, storage_account, storage_account_key):
        custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token
        for service_type in SERVICES.items():
            conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};QueueEndpoint={};'.format(
                storage_account.name, storage_account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(conn_string, queue_name="foo")

            # Assert
            self.assertEqual(service.account_name, storage_account.name)
            self.assertEqual(service.credential.account_name, storage_account.name)
            self.assertEqual(service.credential.account_key, storage_account_key)
            self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')

        service = QueueServiceClient(account_url=custom_account_url)
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
        self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/?'))

        service = QueueClient(account_url=custom_account_url, queue_name="foo")
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.queue_name, "foo")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
        self.assertTrue(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)
        self.assertEqual(service.account_name, None)
        self.assertEqual(service.queue_name, "foo")
        self.assertEqual(service.credential, None)
        self.assertEqual(service.primary_hostname, 'local-machine:11002/custom/account/path')
        self.assertTrue(service.url.startswith('http://local-machine:11002/custom/account/path/foo?'))
    async def test_sas_read(self, resource_group, location, storage_account,
                            storage_account_key):
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        # SAS URL is calculated from storage key, so this test runs live only
        if self.is_playback():
            return

        # Arrange
        queue_client = await self._create_queue(qsc)
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.READ,
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=5))

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.peek_messages()

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    async def test_message_base64_decode_fails(self, resource_group, location,
                                               storage_account,
                                               storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self.account_url(storage_account.name,
                                                  "queue"),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        queue = QueueClient(
            account_url=self.account_url(storage_account.name, "queue"),
            queue_name=self.get_resource_name(TEST_QUEUE_PREFIX),
            credential=storage_account_key,
            message_encode_policy=None,
            message_decode_policy=BinaryBase64DecodePolicy(),
            transport=AiohttpTestTransport())
        try:
            await queue.create_queue()
        except ResourceExistsError:
            pass
        message = u'xyz'
        await queue.send_message(message)

        # Action.
        with self.assertRaises(DecodeError) as e:
            await queue.peek_messages()

        # Asserts
        self.assertNotEqual(
            -1,
            str(e.exception).find('Message content is not valid base 64'))
Пример #5
0
    async def _test_message_base64_decode_fails(self):
        # Arrange
        queue_url = self._get_queue_url()
        credentials = self._get_shared_key_credential()
        queue = QueueClient(queue_url=queue_url,
                            queue=self.get_resource_name(TEST_QUEUE_PREFIX),
                            credential=credentials,
                            message_encode_policy=TextXMLEncodePolicy(),
                            message_decode_policy=BinaryBase64DecodePolicy(),
                            transport=AiohttpTestTransport())
        try:
            await queue.create_queue()
        except ResourceExistsError:
            pass
        message = u'xyz'
        await queue.enqueue_message(message)

        # Action.
        with self.assertRaises(DecodeError) as e:
            await queue.peek_messages()

        # Asserts
        self.assertNotEqual(
            -1,
            str(e.exception).find('Message content is not valid base 64'))
    async def _test_sas_process(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        queue_client = await self._create_queue()
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.PROCESS,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        message = messages[0]

        # Assert
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    async def _test_sas_add(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        queue_client = await self._create_queue()
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.ADD,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.enqueue_message(u'addedmessage')

        # Assert
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]
        self.assertEqual(u'addedmessage', result.content)
    async def _test_sas_update(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        queue_client = await self._create_queue()
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.UPDATE,
            datetime.utcnow() + timedelta(hours=1),
        )
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        await service.update_message(
            result.id,
            pop_receipt=result.pop_receipt,
            visibility_timeout=0,
            content=u'updatedmessage1',
        )

        # Assert
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]
        self.assertEqual(u'updatedmessage1', result.content)
    async def _test_sas_read(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        queue_client = await self._create_queue()
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.READ,
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=5))

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.peek_messages()

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    async def test_sas_process(self, resource_group, location, storage_account,
                               storage_account_key):
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        # SAS URL is calculated from storage key, so this test runs live only
        if self.is_playback():
            return

        # Arrange
        queue_client = await self._create_queue(qsc)
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.PROCESS,
            datetime.utcnow() + timedelta(hours=1),
        )

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        message = messages[0]

        # Assert
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
Пример #11
0
    async def test_sas_add(self, resource_group, location, storage_account,
                           storage_account_key):
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        # SAS URL is calculated from storage key, so this test runs live only
        if self.is_playback():
            return

        # Arrange
        queue_client = await self._create_queue(qsc)
        token = queue_client.generate_shared_access_signature(
            QueueSasPermissions(add=True),
            datetime.utcnow() + timedelta(hours=1),
        )

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.enqueue_message(u'addedmessage')

        # Assert
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]
        self.assertEqual(u'addedmessage', result.content)
    def test_queue_client_api_version_property(self):
        queue_client = QueueClient(
            "https://foo.queue.core.windows.net/account",
            "queue_name",
            credential="fake_key",
            api_version=self.api_version_1)
        self.assertEqual(queue_client.api_version, self.api_version_1)
        self.assertEqual(queue_client._client._config.version,
                         self.api_version_1)

        queue_client = QueueClient(
            "https://foo.queue.core.windows.net/account",
            "queue_name",
            credential="fake_key")
        self.assertEqual(queue_client.api_version, self.api_version_2)
        self.assertEqual(queue_client._client._config.version,
                         self.api_version_2)
Пример #13
0
    async def test_set_access_policy(self, resource_group, location,
                                     storage_account, storage_account_key):
        connection_string = self.connection_string(storage_account,
                                                   storage_account_key)
        storage_url = self._account_url(storage_account.name)
        # [START async_create_queue_client_from_connection_string]
        from azure.storage.queue.aio import QueueClient
        queue_client = QueueClient.from_connection_string(
            self.connection_string(storage_account, storage_account_key),
            "asyncqueuetest")
        # [END async_create_queue_client_from_connection_string]

        # Create the queue
        try:
            await queue_client.create_queue()
        except ResourceExistsError:
            pass
        await queue_client.enqueue_message(u"hello world")

        try:
            # [START async_set_access_policy]
            # Create an access policy
            from azure.storage.queue.aio import AccessPolicy, QueueSasPermissions
            access_policy = AccessPolicy()
            access_policy.start = datetime.utcnow() - timedelta(hours=1)
            access_policy.expiry = datetime.utcnow() + timedelta(hours=1)
            access_policy.permission = QueueSasPermissions(read=True)
            identifiers = {'my-access-policy-id': access_policy}

            # Set the access policy
            await queue_client.set_queue_access_policy(identifiers)
            # [END async_set_access_policy]

            # Use the access policy to generate a SAS token
            sas_token = queue_client.generate_shared_access_signature(
                policy_id='my-access-policy-id')
            # [END async_set_access_policy]

            # Authenticate with the sas token
            # [START async_create_queue_client]
            from azure.storage.queue.aio import QueueClient
            q = QueueClient(queue_url=queue_client.url, credential=sas_token)
            # [END async_create_queue_client]

            # Use the newly authenticated client to receive messages
            my_messages = q.receive_messages()

        finally:
            # Delete the queue
            await queue_client.delete_queue()
Пример #14
0
    async def _test_message_bytes_base64(self):
        # Arrange.
        queue_url = self._get_queue_url()
        credentials = self._get_shared_key_credential()
        queue = QueueClient(queue_url=queue_url,
                            queue=self.get_resource_name(TEST_QUEUE_PREFIX),
                            credential=credentials,
                            message_encode_policy=BinaryBase64EncodePolicy(),
                            message_decode_policy=BinaryBase64DecodePolicy(),
                            transport=AiohttpTestTransport())

        message = b'xyz'

        # Asserts
        await self._validate_encoding(queue, message)
Пример #15
0
    async def test_message_bytes_base64(self, resource_group, location,
                                        storage_account, storage_account_key):
        # Arrange.
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        queue = QueueClient(queue_url=self._account_url(storage_account.name),
                            queue=self.get_resource_name(TEST_QUEUE_PREFIX),
                            credential=storage_account_key,
                            message_encode_policy=BinaryBase64EncodePolicy(),
                            message_decode_policy=BinaryBase64DecodePolicy(),
                            transport=AiohttpTestTransport())

        message = b'xyz'

        # Asserts
        await self._validate_encoding(queue, message)
Пример #16
0
    async def test_message_text_base64(self, storage_account_name,
                                       storage_account_key):
        # Arrange.
        qsc = QueueServiceClient(self.account_url(storage_account_name,
                                                  "queue"),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        queue = QueueClient(
            account_url=self.account_url(storage_account_name, "queue"),
            queue_name=self.get_resource_name(TEST_QUEUE_PREFIX),
            credential=storage_account_key,
            message_encode_policy=TextBase64EncodePolicy(),
            message_decode_policy=TextBase64DecodePolicy(),
            transport=AiohttpTestTransport())

        message = '\u0001'

        # Asserts
        await self._validate_encoding(queue, message)
Пример #17
0
    async def _test_message_text_fails(self):
        # Arrange
        queue_url = self._get_queue_url()
        credentials = self._get_shared_key_credential()
        queue = QueueClient(queue_url=queue_url,
                            queue=self.get_resource_name(TEST_QUEUE_PREFIX),
                            credential=credentials,
                            message_encode_policy=BinaryBase64EncodePolicy(),
                            message_decode_policy=BinaryBase64DecodePolicy(),
                            transport=AiohttpTestTransport())

        # Action.
        with self.assertRaises(TypeError) as e:
            message = u'xyz'
            await queue.enqueue_message(message)

        # Asserts
        self.assertTrue(
            str(e.exception).startswith('Message content must be bytes'))
Пример #18
0
    async def test_message_text_fails(self, resource_group, location,
                                      storage_account, storage_account_key):
        # Arrange
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        queue = QueueClient(queue_url=self._account_url(storage_account.name),
                            queue=self.get_resource_name(TEST_QUEUE_PREFIX),
                            credential=storage_account_key,
                            message_encode_policy=BinaryBase64EncodePolicy(),
                            message_decode_policy=BinaryBase64DecodePolicy(),
                            transport=AiohttpTestTransport())

        # Action.
        with self.assertRaises(TypeError) as e:
            message = u'xyz'
            await queue.enqueue_message(message)

        # Asserts
        self.assertTrue(
            str(e.exception).startswith('Message content must be bytes'))
Пример #19
0
    async def test_sas_signed_identifier(self, resource_group, location,
                                         storage_account, storage_account_key):
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        # SAS URL is calculated from storage key, so this test runs live only
        if self.is_playback():
            return

        # Arrange
        access_policy = AccessPolicy()
        access_policy.start = datetime.utcnow() - timedelta(hours=1)
        access_policy.expiry = datetime.utcnow() + timedelta(hours=1)
        access_policy.permission = QueueSasPermissions(read=True)

        identifiers = {'testid': access_policy}

        queue_client = await self._create_queue(qsc)
        resp = await queue_client.set_queue_access_policy(identifiers)

        await queue_client.enqueue_message(u'message1')

        token = queue_client.generate_shared_access_signature(
            policy_id='testid')

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        result = await service.peek_messages()

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.id)
        self.assertEqual(u'message1', message.content)
    async def test_sas_update(self, resource_group, location, storage_account,
                              storage_account_key):
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())
        # SAS URL is calculated from storage key, so this test runs live only
        if self.is_playback():
            return

        # Arrange
        queue_client = await self._create_queue(qsc)
        await queue_client.enqueue_message(u'message1')
        token = queue_client.generate_shared_access_signature(
            QueuePermissions.UPDATE,
            datetime.utcnow() + timedelta(hours=1),
        )
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]

        # Act
        service = QueueClient(
            queue_url=queue_client.url,
            credential=token,
        )
        await service.update_message(
            result.id,
            pop_receipt=result.pop_receipt,
            visibility_timeout=0,
            content=u'updatedmessage1',
        )

        # Assert
        messages = []
        async for m in queue_client.receive_messages():
            messages.append(m)
        result = messages[0]
        self.assertEqual(u'updatedmessage1', result.content)