public void Execute_WhenBodyContainsEmailTemplatingElement_ShouldReplaceWithTheRecipientEmail()
        {
            DroneActions.EditSettings<DroneSettings>(x => { x.StoreHostname = DefaultHostUrl; x.BaseUrl = "htto://drone.com"; });
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);

            var recipients = new List<Recipient> { AddRecipient("contacts/1", "*****@*****.**") };

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                                  {
                                                                                                      x.Id = "fragment/1";
                                                                                                      x.CreativeId = "creative/1";
                                                                                                      x.TextBody = CreateTextBodyWithLinkAndEmailTemplating("http://www.dealexpress.com/deal");
                                                                                                      x.HtmlBody = CreateHtmlBodyWithLinkAndEmailTemplating("http://www.dealexpress.com/deal");
                                                                                                      x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                      x.Subject = "hello world subject";
                                                                                                      x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                      x.Recipients = recipients;
                                                                                                      x.FromName = "david";
                                                                                                      x.FromAddressDomainPrefix = "sales";
                                                                                                  });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            AssertHtmlBodyContains("also we have the email here [email protected]");
            AssertTextBodyContains("also we have the email here [email protected]");
        }
        public void Execute_WhenWeObtainAFragment_ShouldStoreThePackagesCurrentTime()
        {
            DroneActions.EditSettings<DroneSettings>(x => { x.StoreHostname = DefaultHostUrl; x.BaseUrl = "htto://drone.com"; });
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<DroneSettings>(x => x.Identifier = "192.1.1.1");

            var recipients = new List<Recipient> { AddRecipient("contacts/1", "*****@*****.**") };

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                              {
                                                                                                  x.Id = "fragment/1";
                                                                                                  x.CreativeId = "creative/1";
                                                                                                  x.HtmlBody = CreateHtmlBodyWithLink("http://www.dealexpress.com/deal");
                                                                                                  x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                  x.Subject = "hello world subject";
                                                                                                  x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                  x.Recipients = recipients;
                                                                                                  x.FromName = "david";
                                                                                                  x.FromAddressDomainPrefix = "sales";
                                                                                              });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);
            DroneActions.WaitForDocumentToExist<CreativePackage>();

            var result = DroneActions.FindSingle<CreativePackage>();

            result.TouchTime.Should().BeAfter(DateTime.UtcNow + TimeSpan.FromSeconds(-10));
        }
        public void Execute_WhenWeObtainAFragment_ShouldSetTheFromAddresCorrectly()
        {
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<DroneSettings>(x =>
                                                         {
                                                             x.Identifier = "192.1.1.1";
                                                             x.StoreHostname = DefaultHostUrl;
                                                         });

            var recipients = new List<Recipient> { AddRecipient("contacts/1", "*****@*****.**") };

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                              {
                                                                                                  x.Id = "fragment/1";
                                                                                                  x.CreativeId = "creative/1";
                                                                                                  x.HtmlBody = CreateHtmlBodyWithLink("http://www.dealexpress.com/deal");
                                                                                                  x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                  x.Subject = "hello world subject";
                                                                                                  x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                  x.Recipients = recipients;
                                                                                                  x.FromName = "david";
                                                                                                  x.FromAddressDomainPrefix = "sales";
                                                                                              });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailSent(x => x.From.Address == "*****@*****.**" && x.From.DisplayName == "david");
        }
        public void Execute_WhenNotAllCreativePackagesAreSentAndNoSendingJobsAreRunning_ShouldResumeSending()
        {
            DroneActions.EditSettings<DroneSettings>(x => { x.StoreHostname = DefaultHostUrl; x.BaseUrl = "htto://drone.com"; });
            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            Api.ListenToApiCall<ServiceEndpoints.Creative.FetchFragment>();

            var creativePackage = new CreativePackage
                                      {
                                          Group = "$default$",
                                          HtmlBody = "body",
                                          Subject = "subject",
                                          To = "*****@*****.**",
                                          FromAddressDomainPrefix = "david",
                                          FromName = "sales",
                                          Interval = 10,
                                          CreativeId = "creative/1"
                                      };

            DroneActions.Store(creativePackage);

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailsSentTo(new[] { "*****@*****.**" });
        }
        public void Execute_WhenThereAreNoFragments_ShouldDoNothing()
        {
            DroneActions.EditSettings<DroneSettings>(x => { x.StoreHostname = DefaultHostUrl; x.BaseUrl = "htto://drone.com"; });
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(default(CreativeFragment));

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailNotSentTo();
        }
        public void Execute_WhenThereAreCreativePackagesInTheStoreThatWereAlreadyProcessed_ShouldFetchFragment()
        {
            DroneActions.EditSettings<DroneSettings>(x => { x.StoreHostname = DefaultHostUrl; x.BaseUrl = "htto://drone.com"; });
            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<EmailingSettings>(x =>
                                                            {
                                                                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                                                                x.MailingDomain = "example.com";
                                                            });

            Api.ListenToApiCall<ServiceEndpoints.Creative.FetchFragment>();

            var creativePackage = new CreativePackage
                                      {
                                          Group = "$default$",
                                          HtmlBody = "body",
                                          Subject = "subject",
                                          To = "*****@*****.**",
                                          FromAddressDomainPrefix = "david",
                                          FromName = "sales",
                                          Interval = 10,
                                          Processed = true,
                                          CreativeId = "creative/1"
                                      };

            DroneActions.Store(creativePackage);

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Api.AssertApiCalled<ServiceEndpoints.Creative.FetchFragment>();
        }
        public void Execute_WhenTheOnlyRecipientsThatAreLeftAreOfAPausedGroup_ShouldFetchAFragmentAlthoughThereAreRecipientsInTheStore()
        {
            DroneActions.EditSettings<EmailingSettings>(x =>
            {
                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                x.MailingDomain = "example.com";
            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<DroneSettings>(x =>
                                                         {
                                                             x.Identifier = "192.1.1.1";
                                                             x.StoreHostname = DefaultHostUrl;
                                                         });

            DroneActions.Store(new GroupsAndIndividualDomainsSendingPolicies
                                   {
                                       GroupSendingPolicies = new Dictionary<string, ResumeSendingPolicy>
                                                                  {
                                                                      { "yahoo",new ResumeSendingPolicy { ResumeAt = DateTime.UtcNow + TimeSpan.FromHours(4)} }
                                                                  }
                                   });

            DroneActions.StoreCollection(new List<CreativePackage>
                                             {
                                                 new CreativePackage
                                                     {
                                                         Group = "yahoo",
                                                         HtmlBody = "body",
                                                         FromAddressDomainPrefix = "sales",
                                                         FromName = "david",
                                                         Interval = 3,
                                                         Subject = "subject",
                                                         To = "*****@*****.**",
                                                         CreativeId = "creative/1"
                                                     }
                                             });

            var recipients = new List<Recipient>
                                 {
                                     AddRecipient("contacts/4", "*****@*****.**"),
                                     AddRecipient("contacts/5", "*****@*****.**"),
                                     AddRecipient("contacts/6", "*****@*****.**"),
                                 };

            recipients.ForEach(x => x.Interval = 3);

            recipients.ToList().ForEach(x => x.Group = "gmail");

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                                  {
                                                                                                      x.Id = "fragment/1";
                                                                                                      x.CreativeId = "creative/1";
                                                                                                      x.HtmlBody = CreateHtmlBodyWithLink("http://www.dealexpress.com/deal");
                                                                                                      x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                      x.Subject = "hello world subject";
                                                                                                      x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                      x.Recipients = recipients;
                                                                                                      x.FromName = "david";
                                                                                                      x.FromAddressDomainPrefix = "sales";
                                                                                                  });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailsSentWithInterval(recipients, 3, 30);
            Email.AssertEmailNotSentTo(new[] { new Recipient { Email = "david@david" } }, 3);
        }
        public void Execute_WhenStarted_ShouldFetchACreativeFragmentFromServer()
        {
            DroneActions.EditSettings<DroneSettings>(x =>
                {
                    x.StoreHostname = DefaultHostUrl;
                    x.BaseUrl = "htto://drone.com";
                    x.Identifier = "drone.com";
                });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            Api.ListenToApiCall<ServiceEndpoints.Creative.FetchFragment>();

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Api.AssertApiCalled<ServiceEndpoints.Creative.FetchFragment>(x => x.DroneId == "drone.com");
        }
        public void Execute_WhenRecipientsHaveIntervalWithinDifferentGroups_ShouldHaveTwoTypesOfIntervals()
        {
            DroneActions.EditSettings<EmailingSettings>(x =>
            {
                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                x.MailingDomain = "example.com";
            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<DroneSettings>(x =>
                                                         {
                                                             x.Identifier = "192.1.1.1";
                                                             x.StoreHostname = DefaultHostUrl;
                                                         });

            var recipients = new List<Recipient>
                                 {
                                     AddRecipient("contacts/1", "*****@*****.**"),
                                     AddRecipient("contacts/2", "*****@*****.**"),
                                     AddRecipient("contacts/3", "*****@*****.**"),
                                     AddRecipient("contacts/4", "*****@*****.**"),
                                     AddRecipient("contacts/5", "*****@*****.**"),
                                     AddRecipient("contacts/6", "*****@*****.**"),
                                 };

            recipients.ForEach(x => x.Interval = 3);

            recipients.Take(3).ToList().ForEach(x => x.Group = "gmail");
            recipients.Skip(3).ToList().ForEach(x => x.Group = "yahoo");

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                                  {
                                                                                                      x.Id = "fragment/1";
                                                                                                      x.CreativeId = "creative/1";
                                                                                                      x.HtmlBody = CreateHtmlBodyWithLink("http://www.dealexpress.com/deal");
                                                                                                      x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                      x.Subject = "hello world subject";
                                                                                                      x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                      x.Recipients = recipients;
                                                                                                      x.FromName = "david";
                                                                                                      x.FromAddressDomainPrefix = "sales";
                                                                                                  });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailsSentWithInterval(recipients.Take(3).ToList(), 3, 30);
            Email.AssertEmailsSentWithInterval(recipients.Skip(3).ToList(), 3, 30);

            Email.AssertEmailsWereSendAtTheSameTime(new[] { recipients[0], recipients[3] }, 30);
        }
        public void Execute_WhenRecipientsHaveIntervalWithinDifferentGroupsAndOneOfTheGroupsIsPaused_ShouldOnlyStartTheFirstGroup()
        {
            DroneActions.EditSettings<EmailingSettings>(x =>
            {
                x.WritingEmailsToDiskPath = IntergrationHelpers.AssemblyDirectory;
                x.MailingDomain = "example.com";
            });

            DroneActions.EditSettings<ApiCallsSettings>(x => x.ApiBaseUri = DefaultBaseUrl);
            DroneActions.EditSettings<DroneSettings>(x =>
                                                         {
                                                             x.Identifier = "192.1.1.1";
                                                             x.StoreHostname = DefaultHostUrl;
                                                         });

            DroneActions.Store(new GroupsAndIndividualDomainsSendingPolicies
                                   {
                                       GroupSendingPolicies = new Dictionary<string, ResumeSendingPolicy>
                                                                  {
                                                                      { "yahoo",new ResumeSendingPolicy { ResumeAt = DateTime.UtcNow + TimeSpan.FromHours(4)} }
                                                                  }
                                   });

            var recipients = new List<Recipient>
                                 {
                                     AddRecipient("contacts/1", "*****@*****.**"),
                                     AddRecipient("contacts/2", "*****@*****.**"),
                                     AddRecipient("contacts/3", "*****@*****.**"),
                                     AddRecipient("contacts/4", "*****@*****.**"),
                                     AddRecipient("contacts/5", "*****@*****.**"),
                                     AddRecipient("contacts/6", "*****@*****.**"),
                                 };

            recipients.ForEach(x => x.Interval = 3);

            recipients.Take(3).ToList().ForEach(x => x.Group = "gmail");
            recipients.Skip(3).ToList().ForEach(x => x.Group = "yahoo");

            Api.PrepareApiResponse<ServiceEndpoints.Creative.FetchFragment, CreativeFragment>(x =>
                                                                                                  {
                                                                                                      x.Id = "fragment/1";
                                                                                                      x.CreativeId = "creative/1";
                                                                                                      x.HtmlBody = CreateHtmlBodyWithLink("http://www.dealexpress.com/deal");
                                                                                                      x.DealUrl = "http://www.dealexpress.com/deal";
                                                                                                      x.Subject = "hello world subject";
                                                                                                      x.UnsubscribeTemplate = "here  is a template ^url^";
                                                                                                      x.Recipients = recipients;
                                                                                                      x.FromName = "david";
                                                                                                      x.FromAddressDomainPrefix = "sales";
                                                                                                  });

            var task = new FetchCreativeFragmentsTask();

            DroneActions.StartScheduledTask(task);

            Email.AssertEmailsSentWithInterval(recipients.Take(3).ToList(), 3, 30);
            Email.AssertEmailNotSentTo(recipients.Skip(3).ToList(), 3);
        }