-
Notifications
You must be signed in to change notification settings - Fork 7
/
sender_spec.rb
181 lines (152 loc) · 5.32 KB
/
sender_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe MultiMail::Sender::Mandrill do
let :message do
Mail.new do
from 'foo@example.com'
to 'bit-bucket@test.smtp.org'
subject 'test'
body 'hello'
end
end
let :empty_message do
Mail.new
end
describe '#initialize' do
it 'should raise an error if :api_key is missing' do
expect{
message.delivery_method MultiMail::Sender::Mandrill
}.to raise_error(ArgumentError, "Missing required arguments: api_key")
end
it 'should raise an error if :api_key is nil' do
expect{
message.delivery_method MultiMail::Sender::Mandrill, :api_key => nil
}.to raise_error(ArgumentError, "Missing required arguments: api_key")
end
it 'should raise an error if :api_key is invalid' do
expect{
message.delivery_method MultiMail::Sender::Mandrill, :api_key => 'xxx'
message.deliver
}.to raise_error(MultiMail::InvalidAPIKey, 'Invalid API key')
end
it 'should raise an error if :template_name is invalid' do
expect{
message.delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :template_name => 'nonexistent'
message.deliver
}.to raise_error(MultiMail::InvalidTemplate, 'No such template "nonexistent"')
end
it 'should transform send_at to a string if it is not a string' do
sender = MultiMail::Sender::Mandrill.new(:api_user => '', :api_key => '', 'send_at' => Time.at(981203696))
sender.send_at.should == '2001-02-03 12:34:56'
end
it 'should not transform send_at to a string if it is a string' do
sender = MultiMail::Sender::Mandrill.new(:api_user => '', :api_key => '', 'send_at' => '2001-02-03 12:34:56')
sender.send_at.should == '2001-02-03 12:34:56'
end
it 'should have default settings' do
sender = MultiMail::Sender::Mandrill.new(:api_key => '')
sender.api_key.should == ''
sender.async.should == false
sender.ip_pool.should == nil
sender.send_at.should == nil
sender.template_name.should == nil
sender.template_content.should == nil
end
it 'should assign custom settings' do
sender = MultiMail::Sender::Mandrill.new({
:api_key => 'xxx',
:async => true,
:ip_pool => 'Main Pool',
:send_at => 'example send_at',
:template_name => 'foo',
:template_content => [{'name' => 'bar', 'content' => 'baz'}],
})
sender.api_key.should == 'xxx'
sender.async.should == true
sender.ip_pool.should == 'Main Pool'
sender.send_at.should == 'example send_at'
sender.template_name.should == 'foo'
sender.template_content.should == [{'name' => 'bar', 'content' => 'baz'}]
end
end
describe '#parameters' do
it 'should allow true, false and nil values' do
[true, false, nil].each do |value|
sender = MultiMail::Sender::Mandrill.new({
:api_key => 'xxx',
:track => {
:clicks => value,
}
})
sender.parameters.should == {:track_clicks => value}
end
end
it 'should transform "yes" and "no" values' do
sender = MultiMail::Sender::Mandrill.new({
:api_key => 'xxx',
:track => {
:opens => 'no',
:clicks => 'yes',
}
})
sender.parameters.should == {:track_opens => false, :track_clicks => true}
end
it 'should ignore "htmlonly" values' do
sender = MultiMail::Sender::Mandrill.new({
:api_key => 'xxx',
:track => {
:clicks => 'htmlonly',
}
})
sender.parameters.should == {}
end
end
describe '#deliver' do
before :all do
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY']
end
end
it 'should send a message' do
message.deliver.should == message
end
end
describe '#deliver!' do
context 'when :template_name is set' do
before do
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :return_response => true, :template_name => 'default'
end
end
it 'should send a mandrill template' do
results = message.deliver!
results.size.should == 1
result = results.first
result.size.should == 4
result['reject_reason'].should == nil
result['status'].should == 'sent'
result['email'].should == 'bit-bucket@test.smtp.org'
result['_id'].should match(/\A[0-9a-f]{32}\z/)
end
end
context 'when :template_name is not set' do
before do
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :return_response => true
end
end
it 'should send a message' do
results = message.deliver!
results.size.should == 1
result = results.first
result.size.should == 4
result['reject_reason'].should == nil
result['status'].should == 'sent'
result['email'].should == 'bit-bucket@test.smtp.org'
result['_id'].should match(/\A[0-9a-f]{32}\z/)
end
it 'should not send an empty message' do
empty_message.deliver!.should == [] # response not saved
end
end
end
end