maily_herald 0.8.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +1 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +0 -2
  5. data/Gemfile.lock +5 -13
  6. data/README.md +186 -69
  7. data/app/mailers/maily_herald/mailer.rb +44 -26
  8. data/app/models/maily_herald/ad_hoc_mailing.rb +109 -0
  9. data/app/models/maily_herald/dispatch.rb +97 -4
  10. data/app/models/maily_herald/list.rb +36 -7
  11. data/app/models/maily_herald/log.rb +79 -0
  12. data/app/models/maily_herald/mailing.rb +149 -24
  13. data/app/models/maily_herald/one_time_mailing.rb +114 -9
  14. data/app/models/maily_herald/periodical_mailing.rb +76 -47
  15. data/app/models/maily_herald/sequence.rb +57 -18
  16. data/app/models/maily_herald/sequence_mailing.rb +29 -20
  17. data/app/models/maily_herald/subscription.rb +23 -2
  18. data/config/routes.rb +2 -2
  19. data/lib/maily_herald.rb +57 -18
  20. data/lib/maily_herald/autonaming.rb +8 -0
  21. data/lib/maily_herald/context.rb +6 -2
  22. data/lib/maily_herald/logging.rb +2 -0
  23. data/lib/maily_herald/manager.rb +15 -31
  24. data/lib/maily_herald/utils.rb +65 -24
  25. data/lib/maily_herald/version.rb +1 -1
  26. data/maily_herald.gemspec +1 -1
  27. data/spec/controllers/maily_herald/tokens_controller_spec.rb +13 -13
  28. data/spec/dummy/app/mailers/ad_hoc_mailer.rb +11 -0
  29. data/spec/dummy/app/mailers/custom_one_time_mailer.rb +11 -0
  30. data/spec/dummy/config/application.rb +1 -1
  31. data/spec/dummy/config/environments/test.rb +1 -1
  32. data/spec/dummy/config/initializers/maily_herald.rb +3 -69
  33. data/spec/dummy/config/maily_herald.yml +3 -0
  34. data/spec/dummy/db/seeds.rb +73 -0
  35. data/spec/lib/context_spec.rb +7 -7
  36. data/spec/lib/maily_herald_spec.rb +7 -8
  37. data/spec/lib/utils_spec.rb +65 -25
  38. data/spec/mailers/maily_herald/mailer_spec.rb +20 -13
  39. data/spec/models/maily_herald/ad_hoc_mailing_spec.rb +169 -0
  40. data/spec/models/maily_herald/list_spec.rb +2 -1
  41. data/spec/models/maily_herald/log_spec.rb +10 -10
  42. data/spec/models/maily_herald/mailing_spec.rb +9 -8
  43. data/spec/models/maily_herald/one_time_mailing_spec.rb +212 -39
  44. data/spec/models/maily_herald/periodical_mailing_spec.rb +158 -92
  45. data/spec/models/maily_herald/sequence_mailing_spec.rb +2 -2
  46. data/spec/models/maily_herald/sequence_spec.rb +152 -139
  47. data/spec/models/maily_herald/subscription_spec.rb +21 -4
  48. metadata +17 -8
  49. data/lib/maily_herald/condition_evaluator.rb +0 -82
  50. data/lib/maily_herald/config.rb +0 -5
  51. data/spec/dummy/app/mailers/test_mailer.rb +0 -11
@@ -3,36 +3,43 @@ require 'spec_helper'
3
3
  describe MailyHerald::Mailer do
4
4
  before(:each) do
5
5
  @entity = FactoryGirl.create :user
6
- @mailing = MailyHerald.dispatch(:sample_mail)
6
+ @mailing = MailyHerald.dispatch(:ad_hoc_mail)
7
7
  @list = @mailing.list
8
8
  end
9
9
 
10
- describe "without subscription" do
10
+ context "without subscription" do
11
11
  it "should not deliver" do
12
- MailyHerald::Log.delivered.count.should eq(0)
12
+ expect(MailyHerald::Log.delivered.count).to eq(0)
13
13
 
14
- TestMailer.sample_mail(@entity).deliver
14
+ AdHocMailer.ad_hoc_mail(@entity).deliver
15
15
 
16
- MailyHerald::Log.delivered.count.should eq(0)
16
+ expect(MailyHerald::Log.delivered.count).to eq(0)
17
17
  end
18
18
  end
19
19
 
20
- describe "with subscription" do
20
+ context "with subscription" do
21
21
  before(:each) do
22
22
  @list.subscribe! @entity
23
23
  end
24
24
 
25
25
  it "should deliver" do
26
- MailyHerald::Log.delivered.count.should eq(0)
26
+ expect(MailyHerald::Log.delivered.count).to eq(0)
27
27
 
28
- TestMailer.sample_mail(@entity).deliver
28
+ AdHocMailer.ad_hoc_mail(@entity).deliver
29
29
 
30
- MailyHerald::Log.delivered.count.should eq(1)
30
+ expect(MailyHerald::Log.delivered.count).to eq(1)
31
31
  end
32
32
  end
33
33
 
34
- # missing mailers are how handled silently (bypassing Maily)
35
- #it "should handle missing mailer" do
36
- #expect { TestMailer.sample_mail_error(@entity).deliver }.to raise_error
37
- #end
34
+ context "without defined mailing" do
35
+ it "should not deliver" do
36
+ expect do
37
+ expect(MailyHerald::Log.delivered.count).to eq(0)
38
+
39
+ AdHocMailer.missing_mailing_mail(@entity).deliver
40
+
41
+ expect(MailyHerald::Log.delivered.count).to eq(0)
42
+ end.not_to change { ActionMailer::Base.deliveries.count }
43
+ end
44
+ end
38
45
  end
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+
3
+ describe MailyHerald::AdHocMailing do
4
+ before(:each) do
5
+ @entity = FactoryGirl.create :user
6
+
7
+ @list = MailyHerald.list(:generic_list)
8
+ expect(@list.context).to be_a(MailyHerald::Context)
9
+ end
10
+
11
+ describe "with subscription" do
12
+ before(:each) do
13
+ @list.subscribe!(@entity)
14
+ end
15
+
16
+ describe "run all delivery" do
17
+ before(:each) do
18
+ @mailing = MailyHerald.ad_hoc_mailing(:ad_hoc_mail)
19
+ expect(@mailing).to be_kind_of(MailyHerald::AdHocMailing)
20
+ expect(@mailing).not_to be_a_new_record
21
+ end
22
+
23
+ it "should not be delivered without explicit scheduling" do
24
+ expect(MailyHerald::Subscription.count).to eq(1)
25
+ expect(@mailing.conditions_met?(@entity)).to be_truthy
26
+ expect(@mailing.processable?(@entity)).to be_truthy
27
+
28
+ expect(@mailing.logs.scheduled.count).to eq(0)
29
+ expect(@mailing.logs.processed.count).to eq(0)
30
+
31
+ @mailing.run
32
+
33
+ expect(@mailing.logs.scheduled.count).to eq(0)
34
+ expect(@mailing.logs.processed.count).to eq(0)
35
+ end
36
+
37
+ it "should be delivered" do
38
+ subscription = @mailing.subscription_for(@entity)
39
+
40
+ expect(MailyHerald::Subscription.count).to eq(1)
41
+ expect(MailyHerald::Log.delivered.count).to eq(0)
42
+
43
+ expect(subscription).to be_kind_of(MailyHerald::Subscription)
44
+
45
+ expect(@mailing.conditions_met?(@entity)).to be_truthy
46
+ expect(@mailing.processable?(@entity)).to be_truthy
47
+
48
+ @mailing.schedule_delivery_to_all Time.now - 5
49
+
50
+ ret = @mailing.run
51
+ expect(ret).to be_kind_of(Array)
52
+ expect(ret.first).to be_kind_of(MailyHerald::Log)
53
+ expect(ret.first).to be_delivered
54
+ expect(ret.first.mail).to be_kind_of(Mail::Message)
55
+
56
+ expect(MailyHerald::Subscription.count).to eq(1)
57
+ expect(MailyHerald::Log.delivered.count).to eq(1)
58
+
59
+ log = MailyHerald::Log.delivered.first
60
+ expect(log.entity).to eq(@entity)
61
+ expect(log.mailing).to eq(@mailing)
62
+ expect(log.entity_email).to eq(@entity.email)
63
+ end
64
+ end
65
+
66
+ describe "single entity delivery" do
67
+ before(:each) do
68
+ @mailing = MailyHerald.ad_hoc_mailing(:ad_hoc_mail)
69
+ expect(@mailing).to be_kind_of(MailyHerald::AdHocMailing)
70
+ expect(@mailing).not_to be_a_new_record
71
+ end
72
+
73
+ context "without explicit scheduling" do
74
+ it "should be delivered using Mailer deliver method" do
75
+ MailyHerald::Log.delivered.count.should eq(0)
76
+ msg = AdHocMailer.ad_hoc_mail(@entity).deliver
77
+ msg.should be_a(Mail::Message)
78
+ MailyHerald::Log.delivered.count.should eq(1)
79
+ expect(MailyHerald::Log.delivered.first.entity).to eq(@entity)
80
+ end
81
+ end
82
+
83
+ context "with explicit scheduling" do
84
+ it "should be delivered" do
85
+ MailyHerald::Log.delivered.count.should eq(0)
86
+
87
+ @mailing.schedule_delivery_to @entity, Time.now - 5
88
+
89
+ msg = AdHocMailer.ad_hoc_mail(@entity).deliver
90
+
91
+ expect(msg).to be_kind_of(Mail::Message)
92
+ MailyHerald::Log.delivered.count.should eq(1)
93
+ end
94
+
95
+ it "should not be delivered if subscription inactive" do
96
+ @mailing.schedule_delivery_to @entity, Time.now - 5
97
+
98
+ @list.unsubscribe!(@entity)
99
+
100
+ expect(MailyHerald::Log.delivered.count).to eq(0)
101
+
102
+ AdHocMailer.ad_hoc_mail(@entity).deliver
103
+
104
+ expect(MailyHerald::Log.delivered.count).to eq(0)
105
+ end
106
+ end
107
+ end
108
+
109
+ describe "with entity outside the scope" do
110
+ before(:each) do
111
+ @mailing = MailyHerald.ad_hoc_mailing(:ad_hoc_mail)
112
+ end
113
+
114
+ it "should not process mailings" do
115
+ expect(@list.context.scope).to include(@entity)
116
+ expect(@mailing).to be_processable(@entity)
117
+ expect(@mailing).to be_enabled
118
+
119
+ @entity.update_attribute(:active, false)
120
+
121
+ expect(@list.context.scope).not_to include(@entity)
122
+ expect(@list).to be_subscribed(@entity)
123
+
124
+ expect(@mailing).not_to be_processable(@entity)
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "with subscription override" do
130
+ before(:each) do
131
+ @mailing = MailyHerald.ad_hoc_mailing(:ad_hoc_mail)
132
+ @mailing.update_attribute(:override_subscription, true)
133
+ end
134
+
135
+ after(:each) do
136
+ @mailing.update_attribute(:override_subscription, false)
137
+ end
138
+
139
+ it "single mail should be delivered" do
140
+ MailyHerald::Log.delivered.count.should eq(0)
141
+ @mailing.processable?(@entity).should be_truthy
142
+ @mailing.override_subscription?.should be_truthy
143
+ @mailing.enabled?.should be_truthy
144
+
145
+ @mailing.schedule_delivery_to @entity, Time.now - 5
146
+
147
+ msg = AdHocMailer.ad_hoc_mail(@entity).deliver
148
+ msg.should be_a(Mail::Message)
149
+
150
+ MailyHerald::Log.delivered.count.should eq(1)
151
+ end
152
+ end
153
+
154
+ describe "preview" do
155
+ before(:each) do
156
+ @mailing = MailyHerald.ad_hoc_mailing(:ad_hoc_mail)
157
+ @list.subscribe!(@entity)
158
+ end
159
+
160
+ it "should not deliver" do
161
+ expect(@mailing.logs).to be_empty
162
+
163
+ mail = @mailing.build_mail @entity
164
+ @mailing.reload
165
+
166
+ expect(@mailing.logs).to be_empty
167
+ end
168
+ end
169
+ end
@@ -54,8 +54,9 @@ describe MailyHerald::List do
54
54
  end
55
55
 
56
56
  it "should be lockable" do
57
- @list = MailyHerald.list :locked_list
57
+ @list = MailyHerald.list :generic_list
58
58
  expect(@list).to be_locked
59
+ @list.title = "foo"
59
60
  expect(@list.save).to be_falsy
60
61
  expect(@list.errors.messages).to include(:base)
61
62
  @list.destroy
@@ -9,14 +9,14 @@ describe MailyHerald::Log do
9
9
  describe "Associations" do
10
10
  it "should have proper scopes" do
11
11
  log = MailyHerald::Log.create_for @mailing, @entity, {status: :delivered}
12
- log.should be_valid
13
- log.entity.should eq(@entity)
14
- log.mailing.should eq(@mailing)
12
+ expect(log).to be_valid
13
+ expect(log.entity).to eq(@entity)
14
+ expect(log.mailing).to eq(@mailing)
15
15
 
16
- MailyHerald::Log.for_entity(@entity).should include(log)
17
- MailyHerald::Log.for_mailing(@mailing).should include(log)
16
+ expect(MailyHerald::Log.for_entity(@entity)).to include(log)
17
+ expect(MailyHerald::Log.for_mailing(@mailing)).to include(log)
18
18
 
19
- MailyHerald::Log.for_entity(@entity).for_mailing(@mailing).last.should eq(log)
19
+ expect(MailyHerald::Log.for_entity(@entity).for_mailing(@mailing).last).to eq(log)
20
20
  end
21
21
  end
22
22
 
@@ -26,11 +26,11 @@ describe MailyHerald::Log do
26
26
  expect(MailyHerald::Log.count).to eq(2)
27
27
 
28
28
  log1.update_attribute(:status, :skipped)
29
- MailyHerald::Log.count.should eq(2)
30
- MailyHerald::Log.skipped.count.should eq(1)
29
+ expect(MailyHerald::Log.count).to eq(2)
30
+ expect(MailyHerald::Log.skipped.count).to eq(1)
31
31
 
32
32
  log1.update_attribute(:status, :error)
33
- MailyHerald::Log.count.should eq(2)
34
- MailyHerald::Log.error.count.should eq(1)
33
+ expect(MailyHerald::Log.count).to eq(2)
34
+ expect(MailyHerald::Log.error.count).to eq(1)
35
35
  end
36
36
  end
@@ -4,20 +4,20 @@ describe MailyHerald::Mailing do
4
4
  describe "Validations" do
5
5
  it "should validate template syntax" do
6
6
  @mailing = MailyHerald.one_time_mailing :test_mailing
7
- @mailing.should be_valid
7
+ expect(@mailing).to be_valid
8
8
  @mailing.template = "foo {{ bar"
9
- @mailing.should_not be_valid
10
- @mailing.errors.messages.keys.should include(:template)
11
- @mailing.errors.messages[:template].should_not be_empty
9
+ expect(@mailing).not_to be_valid
10
+ expect(@mailing.errors.messages.keys).to include(:template)
11
+ expect(@mailing.errors.messages[:template]).not_to be_empty
12
12
  end
13
13
 
14
14
  it "should validate conditions syntax" do
15
15
  @mailing = MailyHerald.one_time_mailing :test_mailing
16
- @mailing.should be_valid
16
+ expect(@mailing).to be_valid
17
17
  @mailing.conditions = "foo {{ bar"
18
- @mailing.should_not be_valid
19
- @mailing.errors.messages.keys.should include(:conditions)
20
- @mailing.errors.messages[:conditions].should_not be_empty
18
+ expect(@mailing).not_to be_valid
19
+ expect(@mailing.errors.messages.keys).to include(:conditions)
20
+ expect(@mailing.errors.messages[:conditions]).not_to be_empty
21
21
  end
22
22
  end
23
23
 
@@ -25,6 +25,7 @@ describe MailyHerald::Mailing do
25
25
  it "should produce valiadtion errors" do
26
26
  @mailing = MailyHerald.dispatch :locked_mailing
27
27
  expect(@mailing).to be_locked
28
+ @mailing.title = "foo"
28
29
  expect(@mailing.save).to be_falsy
29
30
  expect(@mailing.errors.messages).to include(:base)
30
31
  @mailing.destroy
@@ -8,6 +8,10 @@ describe MailyHerald::OneTimeMailing do
8
8
  expect(@list.context).to be_a(MailyHerald::Context)
9
9
  end
10
10
 
11
+ after(:all) do
12
+ Timecop.return
13
+ end
14
+
11
15
  describe "with subscription" do
12
16
  before(:each) do
13
17
  @list.subscribe!(@entity)
@@ -16,8 +20,30 @@ describe MailyHerald::OneTimeMailing do
16
20
  describe "run all delivery" do
17
21
  before(:each) do
18
22
  @mailing = MailyHerald.one_time_mailing(:test_mailing)
19
- @mailing.should be_a MailyHerald::OneTimeMailing
20
- @mailing.should_not be_a_new_record
23
+ expect(@mailing).to be_kind_of(MailyHerald::OneTimeMailing)
24
+ expect(@mailing).not_to be_a_new_record
25
+ expect(@mailing).to be_valid
26
+ end
27
+
28
+ it "should be delivered only once per user" do
29
+ subscription = @mailing.subscription_for(@entity)
30
+
31
+ expect(@mailing.logs.scheduled.count).to eq(1)
32
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(1)
33
+
34
+ ret = @mailing.run
35
+
36
+ expect(@mailing.logs.processed.for_entity(@entity).count).to eq(1)
37
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(0)
38
+
39
+ @mailing.set_schedules
40
+
41
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(0)
42
+
43
+ ret = @mailing.run
44
+
45
+ expect(@mailing.logs.processed.for_entity(@entity).count).to eq(1)
46
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(0)
21
47
  end
22
48
 
23
49
  it "should be delivered" do
@@ -25,46 +51,54 @@ describe MailyHerald::OneTimeMailing do
25
51
 
26
52
  expect(MailyHerald::Subscription.count).to eq(1)
27
53
  expect(MailyHerald::Log.delivered.count).to eq(0)
54
+ expect(@mailing.logs.scheduled.count).to eq(1)
28
55
 
29
- subscription.should be_kind_of(MailyHerald::Subscription)
56
+ expect(subscription).to be_kind_of(MailyHerald::Subscription)
30
57
 
31
- @mailing.conditions_met?(@entity).should be_truthy
32
- @mailing.processable?(@entity).should be_truthy
33
- @mailing.mailer_name.should eq(:generic)
58
+ expect(@mailing.conditions_met?(@entity)).to be_truthy
59
+ expect(@mailing.processable?(@entity)).to be_truthy
60
+ expect(@mailing.mailer_name).to eq(:generic)
34
61
 
35
62
  ret = @mailing.run
36
- ret.should be_a(Array)
37
- ret.first.should be_a(Mail::Message)
63
+ expect(ret).to be_kind_of(Array)
64
+ expect(ret.first).to be_kind_of(MailyHerald::Log)
65
+ expect(ret.first).to be_delivered
66
+ expect(ret.first.mail).to be_kind_of(Mail::Message)
38
67
 
39
- MailyHerald::Subscription.count.should eq(1)
40
- MailyHerald::Log.delivered.count.should eq(1)
68
+ expect(MailyHerald::Subscription.count).to eq(1)
69
+ expect(MailyHerald::Log.delivered.count).to eq(1)
41
70
 
42
71
  log = MailyHerald::Log.delivered.first
43
- log.entity.should eq(@entity)
44
- log.mailing.should eq(@mailing)
45
- log.entity_email.should eq(@entity.email)
72
+ expect(log.entity).to eq(@entity)
73
+ expect(log.mailing).to eq(@mailing)
74
+ expect(log.entity_email).to eq(@entity.email)
75
+ end
76
+
77
+ it "should handle template errors" do
78
+ MailyHerald::Log.delivered.count.should eq(0)
79
+
80
+ @mailing = MailyHerald.dispatch(:mail_with_error)
81
+ schedule = @mailing.schedule_for(@entity)
82
+ expect(schedule).to be_a(MailyHerald::Log)
83
+ expect(schedule.processing_at).to be <= Time.now
84
+
85
+ @mailing.run
86
+
87
+ schedule.reload
88
+ expect(schedule).to be_error
46
89
  end
47
90
  end
48
91
 
49
92
  describe "single entity delivery" do
50
- before(:each) do
51
- @mailing = MailyHerald.one_time_mailing(:test_mailing)
52
- @mailing.should be_a MailyHerald::OneTimeMailing
53
- @mailing.should_not be_a_new_record
54
- end
93
+ it "should not be possible via Mailer" do
94
+ expect(MailyHerald::Log.delivered.count).to eq(0)
55
95
 
56
- it "should be delivered" do
57
- MailyHerald::Log.delivered.count.should eq(0)
58
- msg = TestMailer.sample_mail(@entity).deliver
59
- msg.should be_a(Mail::Message)
60
- MailyHerald::Log.delivered.count.should eq(1)
61
- end
96
+ schedule = MailyHerald.dispatch(:one_time_mail).schedule_for(@entity)
97
+ schedule.update_attribute(:processing_at, Time.now + 1.day)
62
98
 
63
- it "should not be delivered if subscription inactive" do
64
- @list.unsubscribe!(@entity)
65
- MailyHerald::Log.delivered.count.should eq(0)
66
- TestMailer.sample_mail(@entity).deliver
67
- MailyHerald::Log.delivered.count.should eq(0)
99
+ expect{ CustomOneTimeMailer.one_time_mail(@entity).deliver }.not_to change{ActionMailer::Base.deliveries.count}
100
+
101
+ expect(MailyHerald::Log.delivered.count).to eq(0)
68
102
  end
69
103
  end
70
104
 
@@ -73,7 +107,7 @@ describe MailyHerald::OneTimeMailing do
73
107
  @mailing = MailyHerald.one_time_mailing(:test_mailing)
74
108
  end
75
109
 
76
- it "should not process mailings" do
110
+ it "should not process mailings, postpone them and finally skip them" do
77
111
  expect(@list.context.scope).to include(@entity)
78
112
  expect(@mailing).to be_processable(@entity)
79
113
  expect(@mailing).to be_enabled
@@ -84,13 +118,51 @@ describe MailyHerald::OneTimeMailing do
84
118
  expect(@list).to be_subscribed(@entity)
85
119
 
86
120
  expect(@mailing).not_to be_processable(@entity)
121
+
122
+ schedule = @mailing.schedule_for(@entity)
123
+ processing_at = schedule.processing_at
124
+ expect(schedule).not_to be_nil
125
+ expect(schedule.processing_at).to be <= Time.now
126
+
127
+ @mailing.run
128
+
129
+ schedule.reload
130
+ expect(schedule).to be_scheduled
131
+ expect(schedule.processing_at.to_i).to eq((Time.now + 1.day).to_i)
132
+ expect(schedule.data[:original_processing_at]).to eq(processing_at)
133
+ expect(schedule.data[:delivery_attempts].length).to eq(1)
134
+
135
+ Timecop.freeze schedule.processing_at + 1
136
+
137
+ @mailing.run
138
+
139
+ schedule.reload
140
+ expect(schedule).to be_scheduled
141
+ expect(schedule.data[:delivery_attempts].length).to eq(2)
142
+
143
+ Timecop.freeze schedule.processing_at + 1
144
+
145
+ @mailing.run
146
+
147
+ schedule.reload
148
+ expect(schedule).to be_scheduled
149
+ expect(schedule.data[:delivery_attempts].length).to eq(3)
150
+
151
+ Timecop.freeze schedule.processing_at + 1
152
+
153
+ @mailing.run
154
+
155
+ schedule.reload
156
+ expect(schedule).to be_skipped
157
+ expect(schedule.data[:delivery_attempts].length).to eq(3)
158
+ expect(schedule.data[:skip_reason]).to eq(:not_in_scope)
87
159
  end
88
160
  end
89
161
  end
90
162
 
91
163
  describe "with subscription override" do
92
164
  before(:each) do
93
- @mailing = MailyHerald.one_time_mailing(:sample_mail)
165
+ @mailing = MailyHerald.one_time_mailing(:one_time_mail)
94
166
  @mailing.update_attribute(:override_subscription, true)
95
167
  end
96
168
 
@@ -98,15 +170,116 @@ describe MailyHerald::OneTimeMailing do
98
170
  @mailing.update_attribute(:override_subscription, false)
99
171
  end
100
172
 
101
- it "single mail should be delivered" do
102
- MailyHerald::Log.delivered.count.should eq(0)
103
- @mailing.processable?(@entity).should be_truthy
104
- @mailing.override_subscription?.should be_truthy
105
- @mailing.enabled?.should be_truthy
106
- msg = TestMailer.sample_mail(@entity).deliver
107
- msg.should be_a(Mail::Message)
108
- MailyHerald::Log.delivered.count.should eq(1)
173
+ it "should deliver single mail" do
174
+ expect(MailyHerald::Log.delivered.count).to eq(0)
175
+ expect(@mailing.processable?(@entity)).to be_truthy
176
+ expect(@mailing.override_subscription?).to be_truthy
177
+ expect(@mailing.enabled?).to be_truthy
178
+ @mailing.run
179
+ expect(MailyHerald::Log.delivered.count).to eq(1)
109
180
  end
110
181
  end
111
182
 
183
+ describe "with block start_at" do
184
+ before(:each) do
185
+ @mailing = MailyHerald::OneTimeMailing.new
186
+ @mailing.title = "Foobar"
187
+ @mailing.subject = "Foo"
188
+ @mailing.template = "Sample template"
189
+ @mailing.list = @list
190
+ @mailing.start_at = Proc.new{|user| user.created_at + 1.hour}
191
+ @mailing.enable
192
+ @mailing.save!
193
+ end
194
+
195
+ after(:each) do
196
+ @mailing.destroy
197
+ end
198
+
199
+ it "should be delivered" do
200
+ expect(@mailing.has_start_at_proc?).to be_truthy
201
+
202
+ expect(@mailing.processed_logs(@entity).count).to eq(0)
203
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(0)
204
+
205
+ @list.subscribe!(@entity)
206
+
207
+ expect(@list.subscription_for(@entity)).to be_a(MailyHerald::Subscription)
208
+ expect(@list.subscription_for(@entity)).to be_active
209
+ expect(@list.subscribed?(@entity)).to be_truthy
210
+
211
+ # automatic schedule updater should be triggered
212
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(1)
213
+ expect(@mailing.schedules.for_entity(@entity).last.processing_at.to_i).to eq((@entity.created_at + 1.hour).to_i)
214
+
215
+ # manually setting schedules should not change anything now
216
+ @mailing.set_schedules
217
+
218
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(1)
219
+ expect(@mailing.schedules.for_entity(@entity).last.processing_at.to_i).to eq((@entity.created_at + 1.hour).to_i)
220
+ end
221
+ end
222
+
223
+ describe "with block conditions" do
224
+ before(:each) do
225
+ @mailing = MailyHerald::OneTimeMailing.new
226
+ @mailing.title = "Foobar"
227
+ @mailing.subject = "Foo"
228
+ @mailing.template = "Sample template"
229
+ @mailing.list = @list
230
+ @mailing.start_at = "user.created_at"
231
+ @mailing.conditions = Proc.new {|user| user.weekly_notifications}
232
+ @mailing.enable
233
+ @mailing.save!
234
+ end
235
+
236
+ after(:each) do
237
+ @mailing.destroy
238
+ @entity.reload
239
+ @entity.update_attribute(:weekly_notifications, true)
240
+ end
241
+
242
+ it "should deliver when positive" do
243
+ expect(@mailing.has_conditions_proc?).to be_truthy
244
+
245
+ @list.subscribe!(@entity)
246
+ @mailing.set_schedules
247
+
248
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(1)
249
+
250
+ schedule = @mailing.schedules.for_entity(@entity).last
251
+
252
+ expect(schedule.processing_at.to_i).to eq(@entity.created_at.to_i)
253
+ expect(@entity.weekly_notifications).to be_truthy
254
+ expect(@mailing.conditions_met?(@entity)).to be_truthy
255
+
256
+ @mailing.run
257
+
258
+ schedule.reload
259
+ expect(schedule.status).to eq(:delivered)
260
+ end
261
+
262
+ it "should skip when negative" do
263
+ expect(@mailing.has_conditions_proc?).to be_truthy
264
+
265
+ @list.subscribe!(@entity)
266
+ @mailing.set_schedules
267
+
268
+ expect(@mailing.schedules.for_entity(@entity).count).to eq(1)
269
+
270
+ schedule = @mailing.schedules.for_entity(@entity).last
271
+
272
+ expect(schedule.processing_at.to_i).to eq(@entity.created_at.to_i)
273
+
274
+ @entity.update_attribute(:weekly_notifications, false)
275
+
276
+ expect(@entity.weekly_notifications).to be_falsey
277
+ expect(@mailing.conditions_met?(@entity)).to be_falsey
278
+
279
+ @mailing.run
280
+
281
+ schedule.reload
282
+ expect(schedule.status).to eq(:skipped)
283
+ end
284
+ end
112
285
  end