postmark 1.14.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -106,6 +106,15 @@ describe(Postmark::ApiInputError) do
106
106
  end
107
107
  end
108
108
 
109
+ describe Postmark::InvalidTemplateError do
110
+ subject(:error) { Postmark::InvalidTemplateError.new(:foo => 'bar') }
111
+
112
+ it 'is created with a response' do
113
+ expect(error.message).to start_with('Failed to render the template.')
114
+ expect(error.postmark_response).to eq(:foo => 'bar')
115
+ end
116
+ end
117
+
109
118
  describe(Postmark::TimeoutError) do
110
119
  it { is_expected.to be_a(Postmark::Error) }
111
120
  specify { expect(subject.retry?).to be true }
@@ -129,6 +138,10 @@ describe(Postmark::UnexpectedHttpResponseError) do
129
138
  it { is_expected.to be_a(Postmark::Error) }
130
139
  end
131
140
 
141
+ describe(Postmark::MailAdapterError) do
142
+ it { is_expected.to be_a(Postmark::Error) }
143
+ end
144
+
132
145
  describe(Postmark::InactiveRecipientError) do
133
146
  describe '.parse_recipients' do
134
147
  let(:recipients) do
@@ -10,42 +10,74 @@ describe Mail::Postmark do
10
10
  end
11
11
  end
12
12
 
13
- it "can be set as delivery_method" do
13
+ before do
14
14
  message.delivery_method Mail::Postmark
15
15
  end
16
16
 
17
- it "wraps Postmark.send_through_postmark" do
18
- allow_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
19
- message.delivery_method Mail::Postmark
20
- message.deliver
21
- end
17
+ subject(:handler) { message.delivery_method }
22
18
 
23
- it "returns self by default" do
24
- allow_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
19
+ it "can be set as delivery_method" do
25
20
  message.delivery_method Mail::Postmark
26
- message.deliver.should eq message
27
- end
28
21
 
29
- it "returns the actual response if :return_response setting is present" do
30
- allow_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
31
- message.delivery_method Mail::Postmark, :return_response => true
32
- message.deliver.should eq message
22
+ is_expected.to be_a(Mail::Postmark)
33
23
  end
34
24
 
35
- it "allows setting the api token" do
36
- message.delivery_method Mail::Postmark, :api_token => 'api-token'
37
- message.delivery_method.settings[:api_token].should == 'api-token'
38
- end
25
+ describe '#deliver!' do
26
+ it "returns self by default" do
27
+ expect_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
28
+ message.deliver.should eq message
29
+ end
30
+
31
+ it "returns the actual response if :return_response setting is present" do
32
+ expect_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
33
+ message.delivery_method Mail::Postmark, :return_response => true
34
+ message.deliver.should eq message
35
+ end
36
+
37
+ it "allows setting the api token" do
38
+ message.delivery_method Mail::Postmark, :api_token => 'api-token'
39
+ message.delivery_method.settings[:api_token].should == 'api-token'
40
+ end
41
+
42
+ it 'uses provided API token' do
43
+ message.delivery_method Mail::Postmark, :api_token => 'api-token'
44
+ expect(Postmark::ApiClient).to receive(:new).with('api-token', {}).and_return(double(:deliver_message => true))
45
+ message.deliver
46
+ end
47
+
48
+ it 'uses API token provided as legacy api_key' do
49
+ message.delivery_method Mail::Postmark, :api_key => 'api-token'
50
+ expect(Postmark::ApiClient).to receive(:new).with('api-token', {}).and_return(double(:deliver_message => true))
51
+ message.deliver
52
+ end
39
53
 
40
- it 'uses provided API token' do
41
- message.delivery_method Mail::Postmark, :api_token => 'api-token'
42
- expect(Postmark::ApiClient).to receive(:new).with('api-token', {}).and_return(double(:deliver_message => true))
43
- message.deliver
54
+ context 'when sending a pre-rendered message' do
55
+ it "uses ApiClient#deliver_message to send the message" do
56
+ expect_any_instance_of(Postmark::ApiClient).to receive(:deliver_message).with(message)
57
+ message.deliver
58
+ end
59
+ end
60
+
61
+ context 'when sending a Postmark template' do
62
+ let(:message) do
63
+ Mail.new do
64
+ from "sheldon@bigbangtheory.com"
65
+ to "lenard@bigbangtheory.com"
66
+ template_alias "hello"
67
+ template_model :name => "Sheldon"
68
+ end
69
+ end
70
+
71
+ it 'uses ApiClient#deliver_message_with_template to send the message' do
72
+ expect_any_instance_of(Postmark::ApiClient).to receive(:deliver_message_with_template).with(message)
73
+ message.deliver
74
+ end
75
+ end
44
76
  end
45
77
 
46
- it 'uses API token provided as legacy api_key' do
47
- message.delivery_method Mail::Postmark, :api_key => 'api-token'
48
- expect(Postmark::ApiClient).to receive(:new).with('api-token', {}).and_return(double(:deliver_message => true))
49
- message.deliver
78
+ describe '#api_client' do
79
+ subject { handler.api_client }
80
+
81
+ it { is_expected.to be_a(Postmark::ApiClient) }
50
82
  end
51
- end
83
+ end
@@ -164,6 +164,15 @@ describe Postmark::MailMessageConverter do
164
164
  end
165
165
  end
166
166
 
167
+ let(:templated_message) do
168
+ Mail.new do
169
+ from "sheldon@bigbangtheory.com"
170
+ to "lenard@bigbangtheory.com"
171
+ template_alias "hello"
172
+ template_model :name => "Sheldon"
173
+ end
174
+ end
175
+
167
176
  it 'converts plain text messages correctly' do
168
177
  subject.new(mail_message).run.should == {
169
178
  "From" => "sheldon@bigbangtheory.com",
@@ -225,6 +234,14 @@ describe Postmark::MailMessageConverter do
225
234
  "ReplyTo" => 'Penny The Neighbor <penny@bigbangtheory.com>'}
226
235
  end
227
236
 
237
+ it 'convertes templated messages correctly' do
238
+ expect(subject.new(templated_message).run).
239
+ to eq("From" => "sheldon@bigbangtheory.com",
240
+ "TemplateAlias" => "hello",
241
+ "TemplateModel" => { :name => "Sheldon" },
242
+ "To" => "lenard@bigbangtheory.com")
243
+ end
244
+
228
245
  context 'open tracking' do
229
246
  context 'setup inside of mail' do
230
247
  it 'converts open tracking enabled messages correctly' do
@@ -24,6 +24,15 @@ describe Mail::Message do
24
24
  end
25
25
  end
26
26
 
27
+ let(:templated_message) do
28
+ Mail.new do
29
+ from "sheldon@bigbangtheory.com"
30
+ to "lenard@bigbangtheory.com"
31
+ template_alias "Hello!"
32
+ template_model :name => "Sheldon"
33
+ end
34
+ end
35
+
27
36
  let(:mail_message_with_bogus_headers) do
28
37
  mail_message.header['Return-Path'] = 'bounce@wildbit.com'
29
38
  mail_message.header['From'] = 'info@wildbit.com'
@@ -239,4 +248,109 @@ describe Mail::Message do
239
248
  # See mail_message_converter_spec.rb
240
249
  end
241
250
 
251
+ describe '#templated?' do
252
+ specify { expect(mail_message).to_not be_templated }
253
+ specify { expect(templated_message).to be_templated }
254
+ end
255
+
256
+ describe '#prerender' do
257
+ let(:model) { templated_message.template_model }
258
+ let(:model_text) { model[:name] }
259
+
260
+ let(:template_response) do
261
+ {
262
+ :html_body => '<html><body>{{ name }}</body></html>',
263
+ :text_body => '{{ name }}'
264
+ }
265
+ end
266
+
267
+ let(:successful_render_response) do
268
+ {
269
+ :all_content_is_valid => true,
270
+ :subject => {
271
+ :rendered_content => 'Subject'
272
+ },
273
+ :text_body => {
274
+ :rendered_content => model_text
275
+ },
276
+ :html_body => {
277
+ :rendered_content => "<html><body>#{model_text}</body></html>"
278
+ }
279
+ }
280
+ end
281
+
282
+ let(:failed_render_response) do
283
+ {
284
+ :all_content_is_valid => false,
285
+ :subject => {
286
+ :rendered_content => 'Subject'
287
+ },
288
+ :text_body => {
289
+ :rendered_content => model_text
290
+ },
291
+ :html_body => {
292
+ :rendered_content => nil,
293
+ :validation_errors => [
294
+ { :message => 'The syntax for this template is invalid.', :line => 1, :character_position => 1 }
295
+ ]
296
+ }
297
+ }
298
+ end
299
+
300
+ subject(:rendering) { message.prerender }
301
+
302
+ context 'when called on a non-templated message' do
303
+ let(:message) { mail_message }
304
+
305
+ it 'raises a Postmark::Error' do
306
+ expect { rendering }.to raise_error(Postmark::Error, /Cannot prerender/)
307
+ end
308
+ end
309
+
310
+ context 'when called on a templated message' do
311
+ let(:message) { templated_message }
312
+
313
+ before do
314
+ message.delivery_method delivery_method
315
+ end
316
+
317
+ context 'and using a non-Postmark delivery method' do
318
+ let(:delivery_method) { Mail::SMTP }
319
+
320
+ specify { expect { rendering }.to raise_error(Postmark::MailAdapterError) }
321
+ end
322
+
323
+ context 'and using a Postmark delivery method' do
324
+ let(:delivery_method) { Mail::Postmark }
325
+
326
+ before do
327
+ expect_any_instance_of(Postmark::ApiClient).
328
+ to receive(:get_template).with(message.template_alias).
329
+ and_return(template_response)
330
+ expect_any_instance_of(Postmark::ApiClient).
331
+ to receive(:validate_template).with(template_response.merge(:test_render_model => model)).
332
+ and_return(render_response)
333
+ end
334
+
335
+ context 'and rendering succeeds' do
336
+ let(:render_response) { successful_render_response }
337
+
338
+ it 'sets HTML and Text parts to rendered values' do
339
+ expect { rendering }.
340
+ to change { message.subject }.to(render_response[:subject][:rendered_content]).
341
+ and change { message.body_text }.to(render_response[:text_body][:rendered_content]).
342
+ and change { message.body_html }.to(render_response[:html_body][:rendered_content])
343
+ end
344
+ end
345
+
346
+ context 'and rendering fails' do
347
+ let(:render_response) { failed_render_response }
348
+
349
+ it 'raises Postmark::InvalidTemplateError' do
350
+ expect { rendering }.to raise_error(Postmark::InvalidTemplateError)
351
+ end
352
+ end
353
+ end
354
+ end
355
+ end
242
356
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.0
4
+ version: 1.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petyo Ivanov
@@ -10,10 +10,10 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-08-22 00:00:00.000000000 Z
13
+ date: 2019-01-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rake
16
+ name: json
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ">="
@@ -27,13 +27,13 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
29
  - !ruby/object:Gem::Dependency
30
- name: json
30
+ name: mail
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0'
36
- type: :runtime
36
+ type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
@@ -41,7 +41,7 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  - !ruby/object:Gem::Dependency
44
- name: mail
44
+ name: rake
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
@@ -97,6 +97,7 @@ files:
97
97
  - lib/postmark/response_parsers/yajl.rb
98
98
  - lib/postmark/version.rb
99
99
  - postmark.gemspec
100
+ - postmark.png
100
101
  - spec/data/empty.gif
101
102
  - spec/integration/account_api_client_spec.rb
102
103
  - spec/integration/api_client_hashes_spec.rb
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: 1.3.7
147
148
  requirements: []
148
149
  rubyforge_project:
149
- rubygems_version: 2.6.14
150
+ rubygems_version: 2.7.7
150
151
  signing_key:
151
152
  specification_version: 4
152
153
  summary: Official Postmark API wrapper.