private static void AddEmailParticipantsToMessage( MailMessage message, EmailParticipants emailParticipants) { message.From = emailParticipants.From.ToMailAddress(); AddEmailAddresses(message.To, emailParticipants.To); AddEmailAddresses(message.CC, emailParticipants.Cc); AddEmailAddresses(message.Bcc, emailParticipants.Bcc); AddEmailAddresses(message.ReplyToList, emailParticipants.ReplyTo); }
public static async Task ExecuteAsync___Should_return_SendEmailResponse_with_SendEmailResult_FailedToAddParticipantsToEmail___When_SendEmailRequest_EmailParticipants_contains_malformed_email_address() { // Arrange var smtpServerConnectionDefinition = A.Dummy <SmtpServerConnectionDefinition>(); var emailParticipants = new EmailParticipants( new EmailMailbox("*****@*****.**"), new[] { new EmailMailbox("bad-email-address") }); var emailContent = A.Dummy <EmailContent>(); var sendEmailRequest = new SendEmailRequest(emailParticipants, emailContent); var operation = new SendEmailOp(sendEmailRequest); var protocol = new SendEmailProtocol(smtpServerConnectionDefinition); // Act var actual = await protocol.ExecuteAsync(operation); // Assert actual.SendEmailResult.AsTest().Must().BeEqualTo(SendEmailResult.FailedToAddParticipantsToEmail); }
static EmailParticipantsTest() { ConstructorArgumentValidationTestScenarios .RemoveAllScenarios() .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentNullException when parameter 'from' is null scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( null, referenceObject.To, referenceObject.Cc, referenceObject.Bcc, referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentNullException), ExpectedExceptionMessageContains = new[] { "from", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentNullException when parameter 'to' is null scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, null, referenceObject.Cc, referenceObject.Bcc, referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentNullException), ExpectedExceptionMessageContains = new[] { "to", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentException when parameter 'to' is an empty enumerable scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, new List <EmailMailbox>(), referenceObject.Cc, referenceObject.Bcc, referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "to", "is an empty enumerable", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentException when parameter 'to' contains a null element scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, new EmailMailbox[0].Concat(referenceObject.To).Concat(new EmailMailbox[] { null }).Concat(referenceObject.To).ToList(), referenceObject.Cc, referenceObject.Bcc, referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "to", "contains at least one null element", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentException when parameter 'cc' contains a null element scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, referenceObject.To, new EmailMailbox[0].Concat(referenceObject.Cc).Concat(new EmailMailbox[] { null }).Concat(referenceObject.Cc).ToList(), referenceObject.Bcc, referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "cc", "contains at least one null element", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentException when parameter 'bcc' contains a null element scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, referenceObject.To, referenceObject.Cc, new EmailMailbox[0].Concat(referenceObject.Bcc).Concat(new EmailMailbox[] { null }).Concat(referenceObject.Bcc).ToList(), referenceObject.ReplyTo); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "bcc", "contains at least one null element", }, }) .AddScenario(() => new ConstructorArgumentValidationTestScenario <EmailParticipants> { Name = "constructor should throw ArgumentException when parameter 'replyTo' contains a null element scenario", ConstructionFunc = () => { var referenceObject = A.Dummy <EmailParticipants>(); var result = new EmailParticipants( referenceObject.From, referenceObject.To, referenceObject.Cc, referenceObject.Bcc, new EmailMailbox[0].Concat(referenceObject.ReplyTo).Concat(new EmailMailbox[] { null }).Concat(referenceObject.ReplyTo).ToList()); return(result); }, ExpectedExceptionType = typeof(ArgumentException), ExpectedExceptionMessageContains = new[] { "replyTo", "contains at least one null element", }, }); }
public async Task <PrepareToSendOnChannelResult> ExecuteAsync( PrepareToSendOnChannelOp operation) { new { operation }.AsArg().Must().NotBeNull(); new { operation.Notification }.AsArg().Must().NotBeNull().And().BeOfType <IntegrationTestNotification>(); new { operation.Audience }.AsArg().Must().NotBeNull().And().BeOfType <IntegrationTestAudience>(); new { operation.DeliveryChannel }.AsArg().Must().NotBeNull(); new { operation.InheritableTags }.AsArg().Must().NotBeNullNorEmptyDictionaryNorContainAnyNullValues(); IReadOnlyList <ChannelOperationInstruction> channelOperationInstructions; var audience = (IntegrationTestAudience)operation.Audience; var notification = (IntegrationTestNotification)operation.Notification; if (operation.DeliveryChannel is EmailDeliveryChannel) { var trackingCodeId = await this.streams.EmailOperationStream.GetNextUniqueLongAsync(); var emailParticipants = new EmailParticipants( new EmailMailbox(audience.SenderEmailAddress), new[] { new EmailMailbox(audience.RecipientEmailAddress) }); var emailContent = new EmailContent(notification.ScenarioBeingTested, "Here is the body for scenario: " + notification.ScenarioBeingTested); var tags = (operation.InheritableTags.DeepClone() ?? new List <NamedValue <string> >()).ToList(); tags.Add(new NamedValue <string>("recipient-email-address", emailParticipants.To.First().Address)); tags.Add(new NamedValue <string>("sender-email-address", emailParticipants.From.Address)); channelOperationInstructions = new[] { new ChannelOperationInstruction( new SendEmailOp(new SendEmailRequest(emailParticipants, emailContent)), new ChannelOperationMonitoringInfo( trackingCodeId, typeof(SucceededInSendingEmailEvent <long>).ToRepresentation(), typeof(FailedToSendEmailEvent <long>).ToRepresentation()), tags), }; } else if (operation.DeliveryChannel is SlackDeliveryChannel) { var sendMessageTrackingCodeId = await this.streams.SlackOperationStream.GetNextUniqueLongAsync(); var uploadFileTrackingCodeId = await this.streams.SlackOperationStream.GetNextUniqueLongAsync(); var tags = (operation.InheritableTags.DeepClone() ?? new List <NamedValue <string> >()).ToList(); tags.Add(new NamedValue <string>("slack-channel-id", audience.SlackChannelId)); var fileBytes = AssemblyHelper.ReadEmbeddedResourceAsBytes("test-file-png"); channelOperationInstructions = new[] { new ChannelOperationInstruction( new SendSlackMessageOp(new SendSlackTextMessageRequest(audience.SlackChannelId, notification.ScenarioBeingTested)), new ChannelOperationMonitoringInfo( sendMessageTrackingCodeId, typeof(SucceededInSendingSlackMessageEvent <long>).ToRepresentation(), typeof(FailedToSendSlackMessageEvent <long>).ToRepresentation()), tags), new ChannelOperationInstruction( new UploadFileToSlackOp(new UploadFileToSlackRequest(fileBytes, new[] { audience.SlackChannelId }, FileType.Png)), new ChannelOperationMonitoringInfo( uploadFileTrackingCodeId, typeof(SucceededInUploadingFileToSlackEvent <long>).ToRepresentation(), typeof(FailedToUploadFileToSlackEvent <long>).ToRepresentation()), tags), }; } else { throw new NotSupportedException("This delivery channel is not supported: " + operation.DeliveryChannel.GetType().ToStringReadable()); } var prepareToSendOnChannelResult = new PrepareToSendOnChannelResult( channelOperationInstructions, new[] { new ExceptionThrownFailure("exception-thrown-prepare-to-send-on-channel-" + operation.DeliveryChannel.GetType().ToStringReadable()), }, PrepareToSendOnChannelFailureAction.IgnoreAndProceedIfPossibleOtherwiseDoNotSendOnChannel); var result = await Task.FromResult(prepareToSendOnChannelResult); return(result); }
public static async Task ExecuteAsync___Should_send_email_and_return_SendEmailResponse_with_SendEmailResult_Success___When_server_connection_is_well_formed() { // Arrange var smtpServerConnectionDefinition = new SmtpServerConnectionDefinition( "HOST_NAME_HERE", 587, SecureConnectionMethod.StartTls, "USERNAME_HERE", "PASSWORD_HERE"); var emailParticipants = new EmailParticipants( new EmailMailbox("FROM_ADDRESS_HERE"), new[] { new EmailMailbox("TO_ADDRESS_HERE") }, new EmailMailbox[] { }, new EmailMailbox[] { }, new EmailMailbox[] { }); const string heartFileName = "heart.png"; const string documentFileName = "document.pdf"; var heartFileBytes = AssemblyHelper.ReadEmbeddedResourceAsBytes(heartFileName); var heartAttachment = new EmailAttachment(heartFileBytes, heartFileName, MediaType.ImagePng); var documentFileBytes = AssemblyHelper.ReadEmbeddedResourceAsBytes(documentFileName); var documentAttachment = new EmailAttachment(documentFileBytes, documentFileName, MediaType.ApplicationPdf); const string heartContentId = "heart-content-id"; var emailContent = new EmailContent( "SUBJECT_LINE_HERE", "this is some plain text", $@"<table style=""border: solid 1px #e9e9e9;""> <tr> <td>A1</td> <td>B1</td> <td>C1</td> </tr> <tr> <td>A2</td> <td><img src=""cid:{heartContentId}""></td> <td>C2</td> </tr> <tr> <td>A3</td> <td>B3</td> <td>C3</td> </tr> </table>", new EmailAttachment[] { documentAttachment, }, new Dictionary <string, EmailAttachment> { { heartContentId, heartAttachment }, }, null, null); var sendEmailRequest = new SendEmailRequest(emailParticipants, emailContent); var operation = new SendEmailOp(sendEmailRequest); var protocol = new SendEmailProtocol(smtpServerConnectionDefinition); // Act var actual = await protocol.ExecuteAsync(operation); // Assert actual.SendEmailResult.AsTest().Must().BeEqualTo(SendEmailResult.Success); }