public async Task Should_set_valid_until_datetime_on_blob_same_as_message_TTL()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId  = Guid.NewGuid().ToString(),
                TimeToLive = TimeSpan.FromHours(1)
            };
            var dateTimeNowUtc = new DateTime(2017, 1, 2);
            var configuration  = new AzureStorageAttachmentConfiguration(connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments",
                                                                         messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            AzureStorageAttachment.DateTimeFunc = () => dateTimeNowUtc;
            var plugin = new AzureStorageAttachment(configuration);
            await plugin.BeforeMessageSend(message);

            var account   = CloudStorageAccount.Parse(await configuration.ConnectionStringProvider.GetConnectionString());
            var client    = account.CreateCloudBlobClient();
            var container = client.GetContainerReference(configuration.ContainerName);
            var blobName  = (string)message.UserProperties[configuration.MessagePropertyToIdentifyAttachmentBlob];
            var blob      = container.GetBlockBlobReference(blobName);
            await blob.FetchAttributesAsync();

            var validUntil = blob.Metadata[AzureStorageAttachment.ValidUntilUtc];

            Assert.Equal(dateTimeNowUtc.Add(message.TimeToLive).ToString(AzureStorageAttachment.DateFormat), validUntil);
        }
        public async Task Should_download_attachment_using_provided_blob_sas_uri()
        {
            await fixture.CreateContainer("attachments-sendonly");

            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };
            var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                        connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider,
                                                        containerName: "attachments-sendonly",
                                                        messagePropertyToIdentifyAttachmentBlob:
                                                        "attachment-id")
                                                    .WithBlobSasUri(
                                                        sasTokenValidationTime: TimeSpan.FromHours(4),
                                                        messagePropertyToIdentifySasUri: "mySasUriProperty"));
            await plugin.BeforeMessageSend(message);

            var messageReceiver = new MessageReceiver(new ServiceBusConnectionStringBuilder(
                                                          endpoint: "sb://test.servicebus.windows.net/",
                                                          entityPath: "entity",
                                                          sharedAccessKey: "---",
                                                          sharedAccessKeyName: "RootManageSharedAccessKey"));

            messageReceiver.RegisterAzureStorageAttachmentPluginForReceivingOnly("mySasUriProperty");
            var receiveOnlyPlugin = messageReceiver.RegisteredPlugins[0];
            var result            = await receiveOnlyPlugin.AfterMessageReceive(message);

            Assert.True(message.UserProperties.ContainsKey("mySasUriProperty"));
            Assert.Equal(payload, Encoding.UTF8.GetString(result.Body));
        }
        /// <summary>Instantiate plugin with the required configuration.</summary>
        /// <param name="client"><see cref="IClientEntity"/>, <see cref="SubscriptionClient"/>, <see cref="QueueClient"/>, <see cref="MessageSender"/>, <see cref="MessageReceiver"/>, or <see cref="SessionClient"/> to register plugin with.</param>
        /// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param>
        /// <returns>Registered plugin as <see cref="ServiceBusPlugin"/>.</returns>
        public static ServiceBusPlugin RegisterAzureStorageAttachmentPlugin(this IClientEntity client, AzureStorageAttachmentConfiguration configuration)
        {
            var plugin = new AzureStorageAttachment(configuration);

            client.RegisterPlugin(plugin);

            return(plugin);
        }
        public async Task Should_set_body_to_null_if_body_replacer_override_is_not_provided()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };
            var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                        connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"));
            var result = await plugin.BeforeMessageSend(message);

            Assert.Null(result.Body);
        }
        public async Task Should_nullify_body_when_body_should_be_sent_as_attachment()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };
            var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                        connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"));
            var result = await plugin.BeforeMessageSend(message);

            Assert.Null(result.Body);
            Assert.True(message.UserProperties.ContainsKey("attachment-id"));
        }
Example #6
0
        public async Task Should_nullify_body_when_body_should_be_sent_as_attachment()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };
            var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments"));
            var plugin      = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"));
            var result      = await plugin.BeforeMessageSend(message);

            Assert.Null(result.Body);
            Assert.True(message.UserProperties.ContainsKey("attachment-id"));
        }
        public async Task Should_receive_it()
        {
            var payload       = "payload";
            var bytes         = Encoding.UTF8.GetBytes(payload);
            var message       = new Message(bytes);
            var configuration = new AzureStorageAttachmentConfiguration(
                connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            var plugin = new AzureStorageAttachment(configuration);
            await plugin.BeforeMessageSend(message);

            Assert.Null(message.Body);

            var receivedMessage = await plugin.AfterMessageReceive(message);

            Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
        }
        public async Task Should_set_sas_uri_when_specified()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };
            var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                        connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")
                                                    .WithSasUri(sasTokenValidationTime: TimeSpan.FromHours(4), messagePropertyToIdentifySasUri: "mySasUriProperty"));
            var result = await plugin.BeforeMessageSend(message);

            Assert.Null(result.Body);
            Assert.True(message.UserProperties.ContainsKey("attachment-id"));
            Assert.True(message.UserProperties.ContainsKey("mySasUriProperty"));
        }
Example #9
0
        public async Task Should_receive_it_using_container_sas()
        {
            var payload       = "payload";
            var bytes         = Encoding.UTF8.GetBytes(payload);
            var message       = new Message(bytes);
            var credentials   = new StorageCredentials(await fixture.GetContainerSas("attachments"));
            var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            var plugin = new AzureStorageAttachment(configuration);
            await plugin.BeforeMessageSend(message);

            Assert.Null(message.Body);

            var receivedMessage = await plugin.AfterMessageReceive(message);

            Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
        }
Example #10
0
        public async Task Should_leave_body_as_is_for_message_not_exceeding_max_size()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId  = Guid.NewGuid().ToString(),
                TimeToLive = TimeSpan.FromHours(1)
            };
            var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments"));
            var plugin      = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, "http://127.0.0.1:10000/devstoreaccount1", messagePropertyToIdentifyAttachmentBlob: "attachment -id",
                                                                                                 messageMaxSizeReachedCriteria: msg => msg.Body.Length > 100));
            var result = await plugin.BeforeMessageSend(message);

            Assert.NotNull(result.Body);
            Assert.False(message.UserProperties.ContainsKey("attachment-id"));
        }
        public async Task Should_leave_body_as_is_for_message_not_exceeding_max_size()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId  = Guid.NewGuid().ToString(),
                TimeToLive = TimeSpan.FromHours(1)
            };
            var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                        connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id",
                                                        messageMaxSizeReachedCriteria: msg => msg.Body.Length > 100));
            var result = await plugin.BeforeMessageSend(message);

            Assert.NotNull(result.Body);
            Assert.False(message.UserProperties.ContainsKey("attachment-id"));
        }
        public async Task Should_not_reupload_blob_if_one_is_already_assigned()
        {
            var payload       = "payload";
            var bytes         = Encoding.UTF8.GetBytes(payload);
            var message       = new Message(bytes);
            var configuration = new AzureStorageAttachmentConfiguration(
                connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            var plugin = new AzureStorageAttachment(configuration);

            var processedMessage = await plugin.BeforeMessageSend(message);

            var blobId = processedMessage.UserProperties["attachment-id"];

            var reprocessedMessage = await plugin.BeforeMessageSend(message);

            Assert.Equal(blobId, reprocessedMessage.UserProperties["attachment-id"]);
        }
Example #13
0
        public async Task Should_not_reupload_blob_if_one_is_already_assigned()
        {
            var payload       = "payload";
            var bytes         = Encoding.UTF8.GetBytes(payload);
            var message       = new Message(bytes);
            var credentials   = new StorageCredentials(await fixture.GetContainerSas("attachments"));
            var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            var plugin = new AzureStorageAttachment(configuration);

            var processedMessage = await plugin.BeforeMessageSend(message);

            var blobId = processedMessage.UserProperties["attachment-id"];

            var reprocessedMessage = await plugin.BeforeMessageSend(message);

            Assert.Equal(blobId, reprocessedMessage.UserProperties["attachment-id"]);
        }
        public async Task Should_be_able_to_send_if_container_was_not_found()
        {
            await fixture.DeleteContainer("attachments-that-didnt-exist");

            var payload       = "payload";
            var bytes         = Encoding.UTF8.GetBytes(payload);
            var message       = new Message(bytes);
            var configuration = new AzureStorageAttachmentConfiguration(
                connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments-that-didnt-exist", messagePropertyToIdentifyAttachmentBlob: "attachment-id");

            var plugin = new AzureStorageAttachment(configuration);
            await plugin.BeforeMessageSend(message);

            Assert.Null(message.Body);

            var receivedMessage = await plugin.AfterMessageReceive(message);

            Assert.Equal(payload, Encoding.UTF8.GetString(receivedMessage.Body));
        }
        public async Task Should_throw_exception_with_blob_path_for_blob_that_cant_be_found()
        {
            var payload = "payload";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = new Message(bytes)
            {
                MessageId = Guid.NewGuid().ToString(),
            };

            var sendingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                               connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments"));
            await sendingPlugin.BeforeMessageSend(message);

            var receivingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(
                                                                 connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments-wrong-containers"));

            var exception = await Assert.ThrowsAsync <Exception>(() => receivingPlugin.AfterMessageReceive(message));

            Assert.Contains("attachments-wrong-containers", actualString: exception.Message);
            Assert.Contains(message.UserProperties["$attachment.blob"].ToString(), actualString: exception.Message);
        }
Example #16
0
        /// <summary>Upload attachment to Azure Storage blob without registering plugin.</summary>
        /// <param name="message"><see cref="Message"/></param>
        /// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param>
        /// <returns><see cref="Message"/> with body uploaded to Azure Storage blob.</returns>
        public static async Task <Message> UploadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration)
        {
            var plugin = new AzureStorageAttachment(configuration);

            return(await plugin.BeforeMessageSend(message).ConfigureAwait(false));
        }
Example #17
0
        /// <summary>Download attachment from Azure Storage blob without registering plugin, using configuration object.</summary>
        /// <param name="message"><see cref="Message"/></param>
        /// <param name="configuration"><see cref="AzureStorageAttachmentConfiguration"/> object.</param>
        /// <returns><see cref="Message"/> with body downloaded from Azure Storage blob.</returns>
        public static async Task <Message> DownloadAzureStorageAttachment(this Message message, AzureStorageAttachmentConfiguration configuration)
        {
            var plugin = new AzureStorageAttachment(configuration);

            return(await plugin.AfterMessageReceive(message).ConfigureAwait(false));
        }