Пример #1
0
        public void Send_ThrowException()
        {
            _smtpClient.Setup(client => client.SendMailAsync(It.IsAny <MailMessage>()))
            .Throws(new Exception("Exception01"));

            var provider = new SmtpProvider(_smtpClient.Object, _loggerFactory.Object);

            var message = new EmailMessage
            {
                FromAddress = "*****@*****.**",
                ToAddresses = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Test Email"
            };

            var exception = Record.ExceptionAsync(() => provider.Send(message.ToParameters()));

            Assert.Equal("Exception01", exception.Result.Message);

            var startMessage = LoggerHelper.FormatLogValues(SmtpLogMessages.Sending_Start, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, startMessage);

            var endMessage = LoggerHelper.FormatLogValues(SmtpLogMessages.Sending_End, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, endMessage, Times.Never());
        }
        public async void Send_Failed()
        {
            _sendGridClient.Setup(client => client.SendEmailAsync(It.IsAny <SGMail.SendGridMessage>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new Response(System.Net.HttpStatusCode.BadRequest, new StringContent("Error01"), null));

            var message = new SendGridMessage
            {
                FromAddress = "*****@*****.**",
                ToAddresses = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Test Email"
            };

            var provider = new SendGridProvider(_sendGridClient.Object, _loggerFactory.Object);

            var result = await provider.Send(message.ToParameters());

            Assert.False(result.IsSuccess);
            Assert.NotEmpty(result.Errors);
            Assert.Equal("Error01", result.Errors[0]);

            var startMessage = LoggerHelper.FormatLogValues(SendGridLogMessages.Sending_Start, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, startMessage);

            var endMessage = LoggerHelper.FormatLogValues(SendGridLogMessages.Sending_End, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, endMessage, Times.Never());
        }
Пример #3
0
        public async void Send_Success()
        {
            _smtpClient.Setup(client => client.SendMailAsync(It.IsAny <MailMessage>()))
            .Returns(Task.CompletedTask);

            var provider = new SmtpProvider(_smtpClient.Object, _loggerFactory.Object);

            var message = new EmailMessage
            {
                FromAddress = "*****@*****.**",
                ToAddresses = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Test Email"
            };

            var result = await provider.Send(message.ToParameters());

            Assert.True(result.IsSuccess);
            Assert.Empty(result.Errors);

            var startMessage = LoggerHelper.FormatLogValues(SmtpLogMessages.Sending_Start, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, startMessage);

            var endMessage = LoggerHelper.FormatLogValues(SmtpLogMessages.Sending_End, message.Subject, message.ToAddresses);

            _logger.VerifyLog(LogLevel.Debug, endMessage);
        }
        public async void Send_Failed()
        {
            _twilioHttpClient.Setup(client => client.MakeRequestAsync(It.IsAny <TwilioHttp.Request>()))
            .ReturnsAsync(new TwilioHttp.Response(System.Net.HttpStatusCode.BadRequest, "Error01"));

            var message = new SMSMessage
            {
                FromNumber = "0123",
                ToNumber   = "0321",
                Content    = "Test"
            };

            var provider = new TwilioProvider(new TwilioOptions(), _twilioHttpClient.Object, _loggerFactory.Object);
            var result   = await provider.Send(message.ToParameters());

            Assert.False(result.IsSuccess);
            Assert.NotEmpty(result.Errors);
            Assert.Contains("Error01", result.Errors[0]);

            var startMessage = LoggerHelper.FormatLogValues(TwilioLogMessages.Sending_Start, message.ToNumber);

            _logger.VerifyLog(LogLevel.Debug, startMessage);

            var warningMessage = LoggerHelper.FormatLogValues(TwilioLogMessages.Sending_Failed, message.ToNumber, result.Errors);

            _logger.VerifyLog(LogLevel.Warning, warningMessage);
        }
        public async void Send_Success()
        {
            _twilioHttpClient.Setup(client => client.MakeRequestAsync(It.IsAny <TwilioHttp.Request>()))
            .ReturnsAsync(new TwilioHttp.Response(System.Net.HttpStatusCode.OK, "{'sid': '111'}"));

            var message = new SMSMessage
            {
                FromNumber = "0123",
                ToNumber   = "0321",
                Content    = "Test"
            };

            var provider = new TwilioProvider(new TwilioOptions(), _twilioHttpClient.Object, _loggerFactory.Object);
            var result   = await provider.Send(message.ToParameters());

            var sid = result.ReturnedValues["Sid"].ToString();

            Assert.True(result.IsSuccess);
            Assert.Equal("111", sid);
            Assert.Empty(result.Errors);

            var startMessage = LoggerHelper.FormatLogValues(TwilioLogMessages.Sending_Start, message.ToNumber);

            _logger.VerifyLog(LogLevel.Debug, startMessage);

            var endMessage = LoggerHelper.FormatLogValues(TwilioLogMessages.Sending_End, message.ToNumber, sid);

            _logger.VerifyLog(LogLevel.Debug, endMessage);
        }
Пример #6
0
        public async void Send_Failed_NoProvider()
        {
            var notification = Notification.GetInstance(_logger.Object, true);

            var result = await notification.Send(DummyProvider.DefaultName, new MessageParameterCollection());

            Assert.False(result.IsSuccess);
            Assert.NotEmpty(result.Errors);
            Assert.Equal($"Provider {DummyProvider.DefaultName} was not found", result.Errors[0]);

            var failedMessage = LoggerHelper.FormatLogValues(LogMessages.Failed_NoProvider, DummyProvider.DefaultName);

            _logger.VerifyLog(LogLevel.Warning, failedMessage);
        }
Пример #7
0
        public async void Send_Failed_ExceptionErrors()
        {
            var provider = new DummyProvider(new Exception("Exception01"));

            var notification = Notification.GetInstance(_logger.Object, true);

            notification.AddProvider(provider);

            var result = await notification.Send(DummyProvider.DefaultName, new MessageParameterCollection());

            Assert.False(result.IsSuccess);
            Assert.NotEmpty(result.Errors);
            Assert.Equal("Exception01", result.Errors[0]);

            var failedMessage = LoggerHelper.FormatLogValues(LogMessages.Failed_Simple, DummyProvider.DefaultName);

            _logger.VerifyLog(LogLevel.Warning, failedMessage);
        }
Пример #8
0
        public async void Send_Success()
        {
            var provider = new DummyProvider(new NotificationResult(true));

            var notification = Notification.GetInstance(_logger.Object, true);

            notification.AddProvider(provider);

            var result = await notification.Send(DummyProvider.DefaultName, new MessageParameterCollection());

            Assert.True(result.IsSuccess);
            Assert.Empty(result.Errors);

            var introMessage = LoggerHelper.FormatLogValues(LogMessages.Intro_SendingMessage, DummyProvider.DefaultName);

            _logger.VerifyLog(LogLevel.Debug, introMessage);

            var successMessage = LoggerHelper.FormatLogValues(LogMessages.Success_Simple, DummyProvider.DefaultName);

            _logger.VerifyLog(LogLevel.Debug, successMessage);
            _logger.VerifyLog(LogLevel.Warning, Times.Never());
        }