mandrill_mailer 0.6.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,25 +27,29 @@ module MandrillMailer
27
27
 
28
28
  class TemplateMailer
29
29
  def deliver
30
- MandrillMailer.deliveries << MandrillMailer::Mock.new({
30
+ MandrillMailer::Mock.new({
31
31
  :template_name => template_name,
32
32
  :template_content => template_content,
33
33
  :message => message,
34
34
  :async => async,
35
35
  :ip_pool => ip_pool,
36
36
  :send_at => send_at
37
- })
37
+ }).tap do |mock|
38
+ MandrillMailer.deliveries << mock
39
+ end
38
40
  end
39
41
  end
42
+
40
43
  class MessageMailer
41
44
  def deliver
42
-
43
- MandrillMailer.deliveries << MandrillMailer::Mock.new({
45
+ MandrillMailer::Mock.new({
44
46
  :message => message,
45
47
  :async => async,
46
48
  :ip_pool => ip_pool,
47
49
  :send_at => send_at
48
- })
50
+ }).tap do |mock|
51
+ MandrillMailer.deliveries << mock
52
+ end
49
53
  end
50
- end
54
+ end
51
55
  end
@@ -100,8 +100,8 @@
100
100
  # message HTML - only for HTML documents less than 256KB in size
101
101
 
102
102
  # :important - whether or not this message is important, and should be delivered ahead of non-important messages
103
- require 'base64'
104
103
  require 'mandrill_mailer/core_mailer'
104
+ require 'mandrill_mailer/arg_formatter'
105
105
 
106
106
  module MandrillMailer
107
107
  class TemplateMailer < MandrillMailer::CoreMailer
@@ -114,107 +114,19 @@ module MandrillMailer
114
114
 
115
115
  # Public: Triggers the stored Mandrill params to be sent to the Mandrill api
116
116
  def deliver
117
- mandrill = Mandrill::API.new(api_key)
118
- mandrill.messages.send_template(template_name, template_content, message, async, ip_pool, send_at)
117
+ mandrill_api.messages.send_template(template_name, template_content, message, async, ip_pool, send_at)
119
118
  end
120
119
 
121
- # Public: Build the hash needed to send to the mandrill api
122
- #
123
- # args - The Hash options used to refine the selection:
124
- # :template - Template name in Mandrill
125
- # :subject - Subject of the email
126
- # :to - Email to send the mandrill email to
127
- # :vars - Global merge vars used in the email for dynamic data
128
- # :recipient_vars - Merge vars used in the email for recipient-specific dynamic data
129
- # :bcc - bcc email for the mandrill email
130
- # :tags - Tags for the email
131
- # :google_analytics_domains - Google analytics domains
132
- # :google_analytics_campaign - Google analytics campaign
133
- # :inline_css - whether or not to automatically inline all CSS styles provided in the message HTML
134
- # :important - whether or not this message is important
135
- # :async - whether or not this message should be sent asynchronously
136
- # :ip_pool - name of the dedicated IP pool that should be used to send the message
137
- # :send_at - when this message should be sent
138
- #
139
- # Examples
140
- #
141
- # mandrill_mail template: 'Group Invite',
142
- # subject: I18n.t('invitation_mailer.invite.subject'),
143
- # to: invitation.email,
144
- # vars: {
145
- # 'OWNER_NAME' => invitation.owner_name,
146
- # 'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
147
- # }
148
- #
149
- # Returns the the mandrill mailer class (this is so you can chain #deliver like a normal mailer)
150
- def mandrill_mail(args)
151
-
152
- # Mandrill requires template content to be there
153
- args[:template_content] = {"blank" => ""} if args[:template_content].blank?
154
-
155
- # format the :to param to what Mandrill expects if a string or array is passed
156
- args[:to] = format_to_params(args[:to])
157
-
158
- # Set the template name
159
- self.template_name = args.delete(:template)
120
+ # Handle template mailer specifics before formating the given args
121
+ def mandrill_mail_handler(args)
122
+ # Mandrill requires template content to be there, set default value
123
+ args[:template_content] = {"blank" => ""} if args[:template_content].blank?
160
124
 
161
125
  # Set the template content
162
- self.template_content = mandrill_args(args.delete(:template_content))
163
- self.async = args.delete(:async)
164
- self.ip_pool = args.delete(:ip_pool)
165
-
166
- if args.has_key?(:send_at)
167
- self.send_at = args.delete(:send_at).getutc.strftime('%Y-%m-%d %H:%M:%S')
168
- end
169
-
170
- # Construct message hash
171
- self.message = {
172
- "subject" => args[:subject],
173
- "from_email" => args[:from] || self.class.defaults[:from],
174
- "from_name" => args[:from_name] || self.class.defaults[:from_name] || self.class.defaults[:from],
175
- "to" => args[:to],
176
- "headers" => args[:headers],
177
- "important" => args[:important],
178
- "track_opens" => args.fetch(:track_opens, true),
179
- "track_clicks" => args.fetch(:track_clicks, true),
180
- "auto_text" => true,
181
- "inline_css" => args[:inline_css],
182
- "url_strip_qs" => args.fetch(:url_strip_qs, true),
183
- "preserve_recipients" => args[:preserve_recipients],
184
- "bcc_address" => args[:bcc],
185
- "merge_language" => args[:merge_language],
186
- "global_merge_vars" => mandrill_args(args[:vars]),
187
- "merge_vars" => mandrill_rcpt_args(args[:recipient_vars]),
188
- "tags" => args[:tags],
189
- "subaccount" => args[:subaccount],
190
- "google_analytics_domains" => args[:google_analytics_domains],
191
- "google_analytics_campaign" => args[:google_analytics_campaign],
192
- "metadata" => args[:metadata],
193
- "attachments" => mandrill_attachment_args(args[:attachments]),
194
- "images" => mandrill_images_args(args[:images])
195
- }
196
- unless MandrillMailer.config.interceptor_params.nil?
197
- unless MandrillMailer.config.interceptor_params.is_a?(Hash)
198
- raise InvalidInterceptorParams.new "The interceptor_params config must be a Hash"
199
- end
200
- self.message.merge!(MandrillMailer.config.interceptor_params.stringify_keys)
201
- end
202
-
203
- # return self so we can chain deliver after the method call, like a normal mailer.
204
- return self
205
- end
126
+ self.template_content = MandrillMailer::ArgFormatter.mandrill_args(args.delete(:template_content))
206
127
 
207
- # Public: Data hash (deprecated)
208
- def data
209
- {
210
- "key" => api_key,
211
- "template_name" => template_name,
212
- "template_content" => template_content,
213
- "message" => message,
214
- "async" => async,
215
- "ip_pool" => ip_pool,
216
- "send_at" => send_at
217
- }
128
+ # Set the template name
129
+ self.template_name = args.delete(:template)
218
130
  end
219
131
  end
220
132
  end
@@ -1,3 +1,3 @@
1
1
  module MandrillMailer
2
- VERSION = "0.6.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,236 @@
1
+ require "spec_helper"
2
+ require 'base64'
3
+
4
+ describe MandrillMailer::ArgFormatter do
5
+ subject(:formatter) { described_class }
6
+ let(:api_args) do
7
+ {
8
+ type: "some/type",
9
+ name: 'test',
10
+ content: "testing some test test file"
11
+ }
12
+ end
13
+
14
+ let(:file_args) do
15
+ {
16
+ mimetype: "some/type",
17
+ filename: 'test',
18
+ file: "testing some test test file"
19
+ }
20
+ end
21
+
22
+
23
+ describe 'attachment_args' do
24
+ context "args are blank" do
25
+ it "returns nil" do
26
+ expect(formatter.attachment_args(nil)).to be_nil
27
+ end
28
+ end
29
+
30
+ context "with file syntax" do
31
+ it "formats the correct attachment data" do
32
+ expect(formatter.attachment_args([file_args])).to eq([{
33
+ 'type' => "some/type",
34
+ 'name' => 'test',
35
+ 'content' => Base64.encode64("testing some test test file")
36
+ }])
37
+ end
38
+ end
39
+
40
+ context "with api syntax" do
41
+ it "formats the correct attachment data" do
42
+ expect(formatter.attachment_args([api_args])).to eq([{
43
+ 'type' => "some/type",
44
+ 'name' => 'test',
45
+ 'content' => Base64.encode64("testing some test test file")
46
+ }])
47
+ end
48
+ end
49
+ end
50
+
51
+ describe ".images_args" do
52
+ context "args are blank" do
53
+ it "returns nil" do
54
+ expect(formatter.images_args(nil)).to be_nil
55
+ end
56
+ end
57
+
58
+ context "with file syntax" do
59
+ it "formats the correct attachment data" do
60
+ expect(formatter.images_args([file_args])).to eq([{
61
+ 'type' => "some/type",
62
+ 'name' => 'test',
63
+ 'content' => Base64.encode64("testing some test test file")
64
+ }])
65
+ end
66
+ end
67
+
68
+ context "with api syntax" do
69
+ it "formats the correct attachment data" do
70
+ expect(formatter.images_args([api_args])).to eq([{
71
+ 'type' => "some/type",
72
+ 'name' => 'test',
73
+ 'content' => Base64.encode64("testing some test test file")
74
+ }])
75
+ end
76
+ end
77
+ end
78
+
79
+ describe ".mandrill_args" do
80
+ context "args are blank" do
81
+ it "returns an empty array" do
82
+ expect(formatter.mandrill_args(nil)).to eq []
83
+ end
84
+ end
85
+
86
+ context "args are not blank" do
87
+ let(:arg_name) { 'USER_NAME' }
88
+ let(:arg_value) { 'bob' }
89
+
90
+ it 'should convert the args to the correct format' do
91
+ results = formatter.mandrill_args({arg_name => arg_value})
92
+ expect(results).to eq [{'name' => arg_name, 'content' => arg_value}]
93
+ end
94
+ end
95
+ end
96
+
97
+ describe ".merge_vars" do
98
+ context "args are blank" do
99
+ it "returns an empty array" do
100
+ expect(formatter.merge_vars(nil)).to eq []
101
+ end
102
+ end
103
+ end
104
+
105
+ describe ".rcpt_metadata" do
106
+ context "args are blank" do
107
+ it "returns an empty array" do
108
+ expect(formatter.rcpt_metadata(nil)).to eq []
109
+ end
110
+ end
111
+
112
+ context "args are not blank" do
113
+ let(:rcpt) { 'email@email.com' }
114
+ let(:arg_name) { 'USER_NAME' }
115
+ let(:arg_value) { 'bob' }
116
+
117
+ it 'should convert the args to the merge_vars format' do
118
+ results = formatter.merge_vars([{rcpt => {arg_name => arg_value}}])
119
+ expect(results).to eq [{'rcpt' => rcpt, 'vars' => [{'name' => arg_name, 'content' => arg_value}]}]
120
+ end
121
+ end
122
+ end
123
+
124
+ describe ".boolean" do
125
+ it "typecasts values to a boolean" do
126
+ expect(formatter.boolean(1)).to eq true
127
+ expect(formatter.boolean('1')).to eq true
128
+ expect(formatter.boolean(nil)).to eq false
129
+ expect(formatter.boolean(false)).to eq false
130
+ expect(formatter.boolean(true)).to eq true
131
+ end
132
+ end
133
+
134
+ describe ".params" do
135
+ let(:email) { 'bob@email.com' }
136
+ let(:name) { 'bob' }
137
+
138
+ context "item is not an array" do
139
+ it "returns an array" do
140
+ results = formatter.params("yay test")
141
+ expect(results).to be_kind_of Array
142
+ end
143
+
144
+ context 'with a single email string' do
145
+ it 'should format args to a format mandrill likes' do
146
+ results = formatter.params(email)
147
+ expect(results).to eq [{"email" => email, "name" => email}]
148
+ end
149
+ end
150
+
151
+ context 'with a single email/name Hash' do
152
+ it 'should format args to a format mandrill likes' do
153
+ results = formatter.params({"email" => email, "name" => name})
154
+ expect(results).to eq [{"email" => email, "name" => name}]
155
+ end
156
+ end
157
+ end
158
+
159
+
160
+ context "item is an array" do
161
+ it "returns an array" do
162
+ results = formatter.params(["yay test", "item2"])
163
+ expect(results).to be_kind_of Array
164
+ end
165
+
166
+ context 'with a single email string array' do
167
+ it 'should format args to a format mandrill likes' do
168
+ results = formatter.params([email])
169
+ expect(results).to eq [{"email" => email, "name" => email}]
170
+ end
171
+ end
172
+
173
+ context 'with a single email/name hash Array' do
174
+ it 'should format args to a format mandrill likes' do
175
+ results = formatter.params([{"email" => email, "name" => name}])
176
+ expect(results).to eq [{"email" => email, "name" => name}]
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ describe ".params_item" do
183
+ context "when item is a hash" do
184
+ it "returns the item" do
185
+ sample_hash = {"email" => "test@email.com", "name" => "test"}
186
+ expect(formatter.params_item(sample_hash)).to eq sample_hash
187
+ end
188
+ end
189
+
190
+ context "when item is not a hash" do
191
+ it "returns the correctly formatted hash" do
192
+ sample_email = "test@email.com"
193
+ expect(formatter.params_item(sample_email)).to eq({"email" => sample_email, "name" => sample_email})
194
+ end
195
+ end
196
+ end
197
+
198
+ describe ".format_messages_api_message_data" do
199
+ it "includes all api values" do
200
+ result = formatter.format_messages_api_message_data({})
201
+ api_values = ["html", "text", "subject", "from_email", "from_name", "to",
202
+ "headers", "important", "track_opens", "track_clicks", "auto_text",
203
+ "auto_html", "inline_css", "url_strip_qs", "preserve_recipients",
204
+ "view_content_link", "bcc_address", "tracking_domain", "signing_domain",
205
+ "return_path_domain", "merge", "merge_language", "global_merge_vars",
206
+ "merge_vars", "tags", "subaccount", "google_analytics_domains",
207
+ "google_analytics_campaign", "metadata", "recipient_metadata",
208
+ "attachments", "images"]
209
+
210
+
211
+ api_values.each do |val|
212
+ expect(result.keys.include?(val)).to eq true
213
+ end
214
+ end
215
+
216
+ context "merge_language exists" do
217
+ context "merge_language is an accepted merge language" do
218
+ it "does not raise an error" do
219
+ expect { formatter.format_messages_api_message_data({merge_language: "handlebars"}) }.not_to raise_error
220
+ end
221
+ end
222
+
223
+ context "merge_language is not an accepted merge language" do
224
+ it "raises an error" do
225
+ expect { formatter.format_messages_api_message_data({merge_language: "not_valid"}) }.to raise_error(MandrillMailer::CoreMailer::InvalidMergeLanguageError)
226
+ end
227
+ end
228
+ end
229
+
230
+ context "merge_language does not exist" do
231
+ it "does not raise an error" do
232
+ expect { formatter.format_messages_api_message_data({}) }.not_to raise_error
233
+ end
234
+ end
235
+ end
236
+ end
@@ -4,63 +4,319 @@ require 'base64'
4
4
  describe MandrillMailer::CoreMailer do
5
5
  subject(:core_mailer) { described_class }
6
6
 
7
- let(:file_args) do
7
+ let(:image_path) { '/assets/image.jpg' }
8
+ let(:default_host) { 'localhost:3000' }
9
+ let(:mailer) { described_class.new }
10
+ let(:api_key) { '1237861278' }
11
+
12
+ let(:from_email) { 'from@email.com' }
13
+ let(:to_email) { 'bob@email.com' }
14
+ let(:to_name) { 'bob' }
15
+ let(:bcc) { "bcc@email.com" }
16
+ let(:var_name) { 'USER_NAME' }
17
+ let(:var_content) { 'bobert' }
18
+ let(:var_rcpt_name) { 'USER_INFO' }
19
+ let(:var_rcpt_content) { 'boboblacksheep' }
20
+
21
+ let(:attachment_file) { File.read(File.expand_path('spec/support/test_image.png')) }
22
+ let(:attachment_filename) { 'test_image.png' }
23
+ let(:attachment_mimetype) { 'image/png' }
24
+ let(:image_file) { File.read(File.expand_path('spec/support/test_image.png')) }
25
+ let(:image_filename) { 'test_image.png' }
26
+ let(:image_mimetype) { 'image/png' }
27
+ let(:send_at) { Time.utc(2020, 1, 1, 8, 0) }
28
+ let(:bcc) { "bcc@email.com" }
29
+
30
+ let(:async) { double(:async) }
31
+ let(:ip_pool) { double(:ip_pool) }
32
+
33
+ let(:args) do
8
34
  {
9
- mimetype: "some/type",
10
- filename: 'test',
11
- file: "testing some test test file"
35
+ from: from_email,
36
+ subject: "super secret",
37
+ to: {'email' => to_email, 'name' => to_name},
38
+ preserve_recipients: false,
39
+ vars: {
40
+ var_name => var_content
41
+ },
42
+ recipient_vars: [
43
+ { to_email => { var_rcpt_name => var_rcpt_content } }
44
+ ],
45
+ headers: {"Reply-To" => "support@email.com"},
46
+ bcc: bcc,
47
+ tags: ['tag1'],
48
+ subaccount: "subaccount1",
49
+ google_analytics_domains: ["http://site.com"],
50
+ google_analytics_campaign: '1237423474',
51
+ attachments: [{file: attachment_file, filename: attachment_filename, mimetype: attachment_mimetype}],
52
+ images: [{file: image_file, filename: image_filename, mimetype: image_mimetype}],
53
+ inline_css: true,
54
+ important: true,
55
+ send_at: send_at,
56
+ async: async,
57
+ ip_pool: ip_pool,
58
+ track_opens: false,
59
+ track_clicks: false,
60
+ url_strip_qs: false
12
61
  }
13
62
  end
14
63
 
15
- let(:api_args) do
64
+ let(:sample_message) do
16
65
  {
17
- type: "some/type",
18
- name: 'test',
19
- content: "testing some test test file"
66
+ "from_email" => from_email,
67
+ "to" => [{'email' => to_email, 'name' => to_name}],
68
+ "bcc_address" => bcc,
20
69
  }
21
70
  end
22
71
 
72
+ before do
73
+ MandrillMailer.config.api_key = api_key
74
+ MandrillMailer.config.default_url_options = { host: default_host }
75
+ allow(MandrillMailer.config).to receive(:image_path).and_return(image_path)
76
+ end
77
+
78
+ describe "#mandrill_mail" do
79
+ it "applies interceptors to the message" do
80
+ expect(mailer).to receive(:apply_interceptors!)
81
+ mailer.mandrill_mail(args)
82
+ end
83
+
84
+ it "calls the mandrill_mail_handler" do
85
+ expect(mailer).to receive(:mandrill_mail_handler)
86
+ mailer.mandrill_mail(args)
87
+ end
88
+
89
+ it "sets the mailer message attribute" do
90
+ expect { mailer.mandrill_mail(args) }.to change { mailer.message }.from(nil)
91
+ end
92
+
93
+ it "extracts api_options" do
94
+ expect(mailer).to receive(:extract_api_options!).with(args)
95
+ mailer.mandrill_mail(args)
96
+ end
97
+
98
+ describe "vars attribute" do
99
+ it "returns the vars" do
100
+ mailer.mandrill_mail(args)
101
+ expect(mailer.message["global_merge_vars"].first.values).to include(var_name)
102
+ expect(mailer.message["global_merge_vars"].first.values).to include(var_content)
103
+ end
104
+
105
+ context "when no vars are set" do
106
+ before do
107
+ args.delete(:vars)
108
+ end
109
+
110
+ it "doesn't explode" do
111
+ expect { mailer.mandrill_mail(args) }.not_to raise_error
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ describe 'url helpers in mailer' do
118
+ subject { mailer.send(:course_url) }
119
+
120
+ context 'Rails is defined (Rails app)' do
121
+ let(:url) { '/courses/1' }
122
+ let(:host) { 'codeschool.com' }
123
+ let(:router) { Rails.application.routes.url_helpers }
124
+
125
+ before do
126
+ # use load since we are loading multiple times
127
+ mailer.send(:load, 'fake_rails/fake_rails.rb')
128
+ MandrillMailer.config.default_url_options[:host] = host
129
+ Rails.application.routes.draw do |builder|
130
+ builder.course_url "#{url}"
131
+ end
132
+ end
133
+
134
+ # Unrequire the fake rails class so it doesn't pollute the rest of the tests
135
+ after do
136
+ Rails.unload!
137
+ end
138
+
139
+ it 'should return the correct route' do
140
+ expect(subject).to eq router.course_url(host: host)
141
+ end
142
+
143
+ context 'route helper with an argument' do
144
+ it 'should return the correct route' do
145
+ expect(subject).to eq router.course_url({id: 1, title: 'zombies'}, host: host)
146
+ end
147
+ end
148
+ end
149
+
150
+ context 'Rails is not defined' do
151
+ it 'should raise an exception' do
152
+ expect{subject}.to raise_error
153
+ end
154
+ end
155
+ end
156
+
157
+ describe '#image_path' do
158
+ subject { mailer.image_path('logo.png') }
159
+
160
+ context 'Rails exists' do
161
+ let(:image) { 'image.png' }
162
+ let(:host) { 'codeschool.com' }
163
+ let(:router) { Rails.application.routes.url_helpers }
164
+
165
+ before do
166
+ # use load instead of require since we have to reload for every test
167
+ mailer.send(:load, 'fake_rails/fake_rails.rb')
168
+ MandrillMailer.config.default_url_options[:host] = host
169
+ end
170
+
171
+ # Essentially un-requiring the fake rails class so it doesn't pollute
172
+ # the rest of the tests
173
+ after do
174
+ Rails.unload!
175
+ end
176
+
177
+ it 'should return the image url' do
178
+ #mailer.send(:image_path, image).should eq ActionController::Base.asset_path(image)
179
+ expect(mailer.send(:image_path, image)).to eq(ActionController::Base.asset_path(image))
180
+ end
181
+ end
182
+
183
+ context 'Rails does not exist' do
184
+ it 'should raise exception' do
185
+ expect{ subject }.to raise_error
186
+ end
187
+ end
188
+ end
189
+
190
+ describe 'defaults' do
191
+ it 'should not share between different subclasses' do
192
+ klassA = Class.new(core_mailer) do
193
+ default from_name: 'ClassA'
194
+ end
195
+ klassB = Class.new(core_mailer) do
196
+ default from_name: 'ClassB'
197
+ end
198
+
199
+ expect(klassA.defaults[:from_name]).to eq 'ClassA'
200
+ expect(klassB.defaults[:from_name]).to eq 'ClassB'
201
+ end
23
202
 
24
- describe 'protected#mandrill_attachment_args' do
25
- context "with file syntax" do
26
- it "formats the correct attachment data" do
27
- expect(core_mailer.new.send(:mandrill_attachment_args, [file_args])).to eq([{
28
- 'type' => "some/type",
29
- 'name' => 'test',
30
- 'content' => Base64.encode64("testing some test test file")
31
- }])
203
+ it 'should use defaults from the parent class' do
204
+ klassA = Class.new(core_mailer) do
205
+ default from_name: 'ClassA'
32
206
  end
207
+ klassB = Class.new(klassA) do
208
+ end
209
+
210
+ expect(klassB.defaults[:from_name]).to eq 'ClassA'
33
211
  end
34
212
 
35
- context "with api syntax" do
36
- it "formats the correct attachment data" do
37
- expect(core_mailer.new.send(:mandrill_attachment_args, [api_args])).to eq([{
38
- 'type' => "some/type",
39
- 'name' => 'test',
40
- 'content' => Base64.encode64("testing some test test file")
41
- }])
213
+ it 'should allow overriding defaults from the parent' do
214
+ klassA = Class.new(core_mailer) do
215
+ default from_name: 'ClassA'
216
+ end
217
+ klassB = Class.new(klassA) do
218
+ default from_name: 'ClassB'
42
219
  end
220
+
221
+ expect(klassB.defaults[:from_name]).to eq 'ClassB'
43
222
  end
44
223
  end
45
224
 
46
- describe '#mandrill_images_args' do
47
- context "with file syntax" do
48
- it "formats the correct attachment data" do
49
- expect(core_mailer.new.send(:mandrill_attachment_args, [file_args])).to eq([{
50
- 'type' => "some/type",
51
- 'name' => 'test',
52
- 'content' => Base64.encode64("testing some test test file")
53
- }])
225
+ describe "accessor helpers" do
226
+ before do
227
+ allow(mailer).to receive(:message).and_return(sample_message)
228
+ end
229
+
230
+ describe "#from" do
231
+ it "returns the from email" do
232
+ expect(mailer.from).to eq from_email
54
233
  end
55
234
  end
56
235
 
57
- context "with api syntax" do
58
- it "formats the correct attachment data" do
59
- expect(core_mailer.new.send(:mandrill_attachment_args, [api_args])).to eq([{
60
- 'type' => "some/type",
61
- 'name' => 'test',
62
- 'content' => Base64.encode64("testing some test test file")
63
- }])
236
+ describe "#to" do
237
+ it "returns the to email data" do
238
+ expect(mailer.to).to eq sample_message['to']
239
+ end
240
+ end
241
+
242
+ describe '#to=' do
243
+ it "updates the to email data" do
244
+ sample_email = "something@email.com"
245
+ mailer.to = sample_email
246
+ expect(mailer.to).to eq [{"email" => sample_email, "name" => sample_email}]
247
+ end
248
+ end
249
+
250
+ describe "#bcc" do
251
+ it "returns the bcc email/s" do
252
+ expect(mailer.bcc).to eq bcc
253
+ end
254
+ end
255
+ end
256
+
257
+ describe '#respond_to' do
258
+ it 'can respond to a symbol' do
259
+ klassA = Class.new(core_mailer) do
260
+ def test_method
261
+
262
+ end
263
+ end
264
+
265
+ expect(klassA).to respond_to('test_method')
266
+ end
267
+ it 'can respond to a string' do
268
+ klassA = Class.new(core_mailer) do
269
+ def test_method
270
+
271
+ end
272
+ end
273
+
274
+ expect(klassA).to respond_to('test_method')
275
+ end
276
+ end
277
+
278
+ describe "protected#extract_api_options!" do
279
+ it "sets the required api options associated with the messages api" do
280
+ results = mailer.send(:extract_api_options!, args)
281
+ expect(mailer.send_at).to eq('2020-01-01 08:00:00')
282
+ expect(mailer.ip_pool).to eq ip_pool
283
+ expect(mailer.async).to eq async
284
+ end
285
+ end
286
+
287
+ describe "protected#apply_interceptors!" do
288
+ # Clear the interceptor after these tests so we don't get errors elsewhere
289
+ after do
290
+ MandrillMailer.config.interceptor = nil
291
+ end
292
+
293
+ context "when interceptor config is a proc" do
294
+ let(:original_email) { "blah@email.com" }
295
+ let(:interceptor_email) { "intercept@email.com" }
296
+
297
+ before do
298
+ MandrillMailer.config.interceptor = Proc.new {|obj|
299
+ obj[:to] = interceptor_email
300
+ }
301
+ end
302
+
303
+ it "Applies interceptors to object" do
304
+ obj = {to: original_email}
305
+ expect { mailer.send(:apply_interceptors!, obj)}.to change {obj[:to]}.from(original_email).to(interceptor_email)
306
+ end
307
+
308
+ it "does not raise error" do
309
+ expect { mailer.send(:apply_interceptors!, {}) }.not_to raise_error
310
+ end
311
+ end
312
+
313
+ context "when interceptor config is not a proc" do
314
+ before do
315
+ MandrillMailer.config.interceptor = "not a proc"
316
+ end
317
+
318
+ it "raises an error" do
319
+ expect { mailer.send(:apply_interceptors!, {}) }.to raise_error MandrillMailer::CoreMailer::InvalidInterceptorParams
64
320
  end
65
321
  end
66
322
  end