public void CoordinatorCompleteWithEmailAndDefaultEmailSendsEmail()
        {
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);

            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));

            var emailService = new EmailService { MailActioner = mailActioner, RavenDocStore = ravenDocStore };
            var coordinatorComplete = new CoordinatorCompleteEmail
                                          {
                                              CoordinatorId = Guid.NewGuid(),
                                              EmailAddress = "*****@*****.**",
                                              FinishTimeUtc = DateTime.Now,
                                              StartTimeUtc = DateTime.Now.AddMinutes(-10),
                                              SendingData = new SendingData()
                                          };
            emailService.Handle(coordinatorComplete);

            mailActioner.VerifyAllExpectations();
            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.EmailAddress));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
        public void CoordinatorCreatedNoEmailWithDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var dateTimeMapper = MockRepository.GenerateMock<IDateTimeOlsenFromUtcMapping>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg<DateTime>.Is.Anything, Arg<string>.Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService { RavenDocStore = ravenDocStore, MailActioner = mailActioner, DateTimeOlsenFromUtcMapping = dateTimeMapper };
            var coordinatorComplete = new CoordinatorCreated { ScheduledMessages = new List<MessageSchedule> { new MessageSchedule { ScheduledTimeUtc = DateTime.Now }}, MetaData = new SmsMetaData()};
            emailService.Handle(new CoordinatorCreatedEmail(coordinatorComplete));

            Assert.That(message.From.Address, Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
        public void CoordinatorCompleteNoEmailInMessageOrDefaultNoAction()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification();
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);

            var emailService = new EmailService { RavenDocStore = ravenDocStore};
            var coordinatorComplete = new CoordinatorCompleteEmail();
            emailService.Handle(coordinatorComplete);
        }
        public void CoordinatorCompleteWithEmailDefaultFromNotSentThrowsException()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration { ApiKey = "key", DomainName = "domain", DefaultFrom = string.Empty };
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);

            var emailService = new EmailService { RavenDocStore = ravenDocStore };
            var coordinatorComplete = new CoordinatorCompleteEmail { EmailAddress = "*****@*****.**" };
            Assert.That(() => emailService.Handle(coordinatorComplete), Throws.Exception.With.Message.EqualTo("Could not find the default 'From' sender."));
        }
 public ActionResult Edit(DefaultEmailModel configuration)
 {
     var isValid = TryUpdateModel(configuration);
     if (!isValid)
         return View("Edit", configuration);
     using (var session = DocumentStore.GetStore().OpenSession("Configuration"))
     {
         var emailAddresses = configuration.DefaultEmails.Split(',');
         var cleanedEmailInList = emailAddresses.ToList().Select(e => e.Trim()).ToList();
         var emailDefaultNotification = session.Load<EmailDefaultNotification>("EmailDefaultConfig");
         if (emailDefaultNotification == null)
         {
             var defaultNotification = new EmailDefaultNotification {EmailAddresses = cleanedEmailInList};
             session.Store(defaultNotification, "EmailDefaultConfig");
         }
         else
         {
             emailDefaultNotification.EmailAddresses = cleanedEmailInList;
         }
         session.SaveChanges();
         return RedirectToAction("Details");
     }
 }
        public void CoordinatorCompleteWithEmailAndDefaultEmailSendsEmail()
        {
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var dateTimeMapper = MockRepository.GenerateMock<IDateTimeOlsenFromUtcMapping>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var mailgunConfig = new MailgunConfiguration { ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**" };
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);

            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg<DateTime>.Is.Anything, Arg<string>.Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService { MailActioner = mailActioner, RavenDocStore = ravenDocStore, DateTimeOlsenFromUtcMapping = dateTimeMapper };
            var coordinatorComplete = new CoordinatorCompleteEmailWithSummary
                                          {
                                              CoordinatorId = Guid.NewGuid(),
                                              EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" },
                                              FinishTimeUtc = DateTime.Now,
                                              StartTimeUtc = DateTime.Now.AddMinutes(-10),
                                              FailedCount = 4,
                                              SuccessCount = 3,
                                              Cost = 33.44m
                                          };
            emailService.Handle(coordinatorComplete);

            mailActioner.VerifyAllExpectations();
            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[1]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[3].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
        public void CoordinatorCreatedWithEmailWithDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));

            var emailService = new EmailService { RavenDocStore = ravenDocStore, MailActioner = mailActioner };
            var coordinatorComplete = new CoordinatorCreated { ConfirmationEmailAddress = "*****@*****.**", ScheduledMessages = new List<MessageSchedule> { new MessageSchedule { ScheduledTimeUtc = DateTime.Now } } };
            emailService.Handle(coordinatorComplete);

            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.ConfirmationEmailAddress));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }