Skip to content

Commit

Permalink
updated track opens flag to support true, false, nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Nov 22, 2016
1 parent e353ec8 commit a9abb23
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/postmark/mail_message_converter.rb
Expand Up @@ -30,7 +30,7 @@ def headers_part
'Subject' => @message.subject,
'Headers' => @message.export_headers,
'Tag' => @message.tag.to_s,
'TrackOpens' => @message.track_opens
'TrackOpens' => (cast_to_bool(@message.track_opens) unless @message.track_opens.empty?)
}
end

Expand Down
5 changes: 3 additions & 2 deletions lib/postmark/message_extensions/mail.rb
Expand Up @@ -16,11 +16,12 @@ def tag=(val)
end

def track_opens(val = nil)
default 'TRACK-OPENS', !!val
default 'TRACK-OPENS', (!!val).to_s unless val.nil?
header['TRACK-OPENS'].to_s
end

def track_opens=(val)
header['TRACK-OPENS'] = !!val
header['TRACK-OPENS'] = (!!val).to_s
end

def postmark_attachments=(value)
Expand Down
103 changes: 78 additions & 25 deletions spec/unit/postmark/mail_message_converter_spec.rb
Expand Up @@ -15,7 +15,7 @@
end

let(:mail_html_message) do
mail = Mail.new do
Mail.new do
from "sheldon@bigbangtheory.com"
to "lenard@bigbangtheory.com"
subject "Hello!"
Expand All @@ -25,7 +25,7 @@
end

let(:mail_message_with_tracking) do
mail = Mail.new do
Mail.new do
from "sheldon@bigbangtheory.com"
to "lenard@bigbangtheory.com"
subject "Hello!"
Expand All @@ -35,6 +35,28 @@
end
end

let(:mail_message_with_tracking_set_variable) do
mail = mail_html_message
mail.track_opens=true
mail
end

let(:mail_message_with_tracking_disabled_set_variable) do
mail = mail_html_message
mail.track_opens=true
mail
end

let(:mail_message_with_tracking_disabled) do
Mail.new do
from "sheldon@bigbangtheory.com"
to "lenard@bigbangtheory.com"
subject "Hello!"
content_type 'text/html; charset=UTF-8'
body "<b>Hello Sheldon!</b>"
track_opens false
end
end

let(:tagged_mail_message) do
Mail.new do
Expand All @@ -55,7 +77,7 @@
end

let(:mail_multipart_message) do
mail = Mail.new do
Mail.new do
from "sheldon@bigbangtheory.com"
to "lenard@bigbangtheory.com"
subject "Hello!"
Expand Down Expand Up @@ -123,8 +145,7 @@
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"TextBody" => "Hello Sheldon!",
"To" => "lenard@bigbangtheory.com",
'TrackOpens' => false}
"To" => "lenard@bigbangtheory.com"}
end

it 'converts tagged text messages correctly' do
Expand All @@ -133,25 +154,22 @@
"Subject" => "Hello!",
"TextBody" => "Hello Sheldon!",
"Tag" => "sheldon",
"To"=>"lenard@bigbangtheory.com",
'TrackOpens' => false}
"To"=>"lenard@bigbangtheory.com"}
end

it 'converts plain text messages without body correctly' do
subject.new(mail_message_without_body).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"To" => "lenard@bigbangtheory.com",
'TrackOpens' => false}
"To" => "lenard@bigbangtheory.com"}
end

it 'converts html messages correctly' do
subject.new(mail_html_message).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
'TrackOpens' => false}
"To" => "lenard@bigbangtheory.com"}
end

it 'converts multipart messages correctly' do
Expand All @@ -160,8 +178,7 @@
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"TextBody" => "Hello Sheldon!",
"To" => "lenard@bigbangtheory.com",
'TrackOpens' => false}
"To" => "lenard@bigbangtheory.com"}
end

it 'converts messages with attachments correctly' do
Expand All @@ -172,8 +189,7 @@
"Content"=>encoded_empty_gif_data,
"ContentType"=>"image/gif"}],
"TextBody"=>"Hello Sheldon!",
"To"=>"lenard@bigbangtheory.com",
'TrackOpens' => false}
"To"=>"lenard@bigbangtheory.com"}
end

it 'converts messages with named addresses correctly' do
Expand All @@ -182,18 +198,55 @@
"Subject" => "Hello!",
"TextBody" => "Hello Sheldon!",
"To" => "Leonard Hofstadter <leonard@bigbangtheory.com>",
"ReplyTo" => 'Penny The Neighbor <penny@bigbangtheory.com>',
'TrackOpens' => false
}
"ReplyTo" => 'Penny The Neighbor <penny@bigbangtheory.com>'}
end

it 'recognizes when open tracking is enabled' do
subject.new(mail_message_with_tracking).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
"TrackOpens" => true}
context 'recognizes open tracking' do

context 'setup inside of mail' do

it 'is enabled' do
subject.new(mail_message_with_tracking).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
"TrackOpens" => true}
end

it 'is disabled' do
subject.new(mail_message_with_tracking_disabled).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
"TrackOpens" => false}
end

end

context 'setup with tracking variable' do

it 'is enabled' do
subject.new(mail_message_with_tracking_set_variable).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
"TrackOpens" => true}
end

it 'is disabled' do
subject.new(mail_message_with_tracking_disabled_set_variable).run.should == {
"From" => "sheldon@bigbangtheory.com",
"Subject" => "Hello!",
"HtmlBody" => "<b>Hello Sheldon!</b>",
"To" => "lenard@bigbangtheory.com",
"TrackOpens" => true}
end

end

end

it 'correctly decodes unicode in messages transfered as quoted-printable' do
Expand Down

0 comments on commit a9abb23

Please sign in to comment.