radiant-mailer-extension 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +95 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/app/controllers/mail_controller.rb +37 -0
- data/app/models/mail.rb +121 -0
- data/app/models/mailer.rb +19 -0
- data/lib/mailer_process.rb +36 -0
- data/lib/mailer_tags.rb +331 -0
- data/lib/radiant-mailer-extension.rb +0 -0
- data/lib/tasks/mailer_extension_tasks.rake +16 -0
- data/mailer_extension.rb +16 -0
- data/radiant-mailer-extension.gemspec +71 -0
- data/spec/controllers/mail_controller_spec.rb +48 -0
- data/spec/dataset/mailer_dataset.rb +17 -0
- data/spec/lib/mailer_process_spec.rb +80 -0
- data/spec/lib/mailer_tags_spec.rb +326 -0
- data/spec/models/mail_spec.rb +212 -0
- data/spec/models/mailer_spec.rb +76 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +36 -0
- metadata +103 -0
@@ -0,0 +1,326 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe "MailerTags" do
|
4
|
+
dataset :mailer
|
5
|
+
describe "<r:mailer>" do
|
6
|
+
it "should render an error if the configuration is invalid" do
|
7
|
+
pages(:home).should render("<r:mailer>true</r:mailer>").as('Mailer config is not valid (see Mailer.valid_config?)')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should render its contents if the configuration is valid" do
|
11
|
+
pages(:mail_form).should render("<r:mailer>true</r:mailer>").as('true')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "<r:mailer:if_error>" do
|
16
|
+
before :each do
|
17
|
+
@page = pages(:mail_form)
|
18
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'body' => 'Hello, world!')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should render its contents if there was an error in the last posted mail" do
|
22
|
+
@mail.should_receive(:valid?).and_return(false)
|
23
|
+
@page.should render('<r:mailer:if_error>Oops.</r:mailer:if_error>').as('Oops.')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not render its contents if the last posted mail was valid" do
|
27
|
+
@mail.should_receive(:valid?).and_return(true)
|
28
|
+
@page.should render('<r:mailer:if_error>Oops.</r:mailer:if_error>').as('')
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "when a field is specified" do
|
32
|
+
it "should render its contents if there was an error on the specified field in the last posted mail" do
|
33
|
+
@mail.errors['email'] = "is not a valid email"
|
34
|
+
@page.should render('<r:mailer:if_error on="email">true</r:mailer:if_error>').as('true')
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not render its contents if there wasn't an error on the specified field in the last posted mail" do
|
38
|
+
@mail.errors['email'] = "is not a valid email"
|
39
|
+
@page.should render('<r:mailer:if_error on="name">true</r:mailer:if_error>').as('')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "<r:mailer:if_error:message>" do
|
45
|
+
before :each do
|
46
|
+
@page = pages(:mail_form)
|
47
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'body' => 'Hello, world!')
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should render the error message on the specified attribute" do
|
51
|
+
@mail.errors['email'] = 'is not a valid email'
|
52
|
+
@page.should render('<r:mailer:if_error on="email"><r:message /></r:mailer:if_error>').as("is not a valid email")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "<r:mailer:unless_error>" do
|
57
|
+
before :each do
|
58
|
+
@page = pages(:mail_form)
|
59
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'body' => 'Hello, world!')
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should render its contents if there was not error in the last posted mail" do
|
63
|
+
@mail.should_receive(:valid?).and_return(true)
|
64
|
+
@page.should render('<r:mailer:unless_error>Oops.</r:mailer:unless_error>').as('Oops.')
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should render its contents if the last posted mail was valid" do
|
68
|
+
@mail.should_receive(:valid?).and_return(false)
|
69
|
+
@page.should render('<r:mailer:unless_error>Oops.</r:mailer:unless_error>').as('')
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "when a field is specified" do
|
73
|
+
it "should not render its contents if there was an error on the specified field in the last posted mail" do
|
74
|
+
@mail.errors['email'] = "is not a valid email"
|
75
|
+
@page.should render('<r:mailer:unless_error on="email">true</r:mailer:unless_error>').as('')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should render its contents if there wasn't an error on the specified field in the last posted mail" do
|
79
|
+
@mail.errors['email'] = "is not a valid email"
|
80
|
+
@page.should render('<r:mailer:unless_error on="name">true</r:mailer:unless_error>').as('true')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "<r:mailer:form>" do
|
86
|
+
it "should render a form that posts back to the page when mailer.post_to_page? is true" do
|
87
|
+
Radiant::Config['mailer.post_to_page?'] = true
|
88
|
+
pages(:mail_form).should render('<r:mailer:form />').as('<form action="/mail-form/" method="post" id="mailer"></form>')
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should render a form that posts back to the controller when mailer.post_to_page? is false" do
|
92
|
+
Radiant::Config['mailer.post_to_page?'] = false
|
93
|
+
pages(:mail_form).should render('<r:mailer:form />').as(%Q{<form action="/pages/#{page_id(:mail_form)}/mail#mailer" method="post" id="mailer"></form>})
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should render permitted passed attributes as attributes of the form tag" do
|
97
|
+
pages(:mail_form).should render('<r:mailer:form class="foo" />').matching(/class="foo"/)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "<r:mailer:if_success>" do
|
102
|
+
before :each do
|
103
|
+
@page = pages(:mail_form)
|
104
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'body' => 'Hello, world!')
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should render its contents if the last posted mail was sent successfully" do
|
108
|
+
@mail.should_receive(:sent?).and_return(true)
|
109
|
+
@page.should render('<r:mailer:if_success>true</r:mailer:if_success>').as('true')
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should not render its contents if the last posted mail was not sent successfully" do
|
113
|
+
@mail.should_receive(:sent?).and_return(false)
|
114
|
+
@page.should render('<r:mailer:if_success>true</r:mailer:if_success>').as('')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
%w(text checkbox radio hidden).each do |type|
|
119
|
+
describe "<r:mailer:#{type}>" do
|
120
|
+
it "should render an input tag with the type #{type}" do
|
121
|
+
pages(:mail_form).should render("<r:mailer:#{type} name='foo' />").as(%Q{<input type="#{type}" value="" id="foo" name="mailer[foo]" />})
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should render permitted passed attributes as attributes of the input tag" do
|
125
|
+
pages(:mail_form).should render("<r:mailer:#{type} name='foo' class='bar'/>").as(%Q{<input type="#{type}" value="" class="bar" id="foo" name="mailer[foo]" />})
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should render the specified value as the value attribute" do
|
129
|
+
pages(:mail_form).should render("<r:mailer:#{type} name='foo' value='bar'/>").as(%Q{<input type="#{type}" value="bar" id="foo" name="mailer[foo]" />})
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should render the previously posted value when present as the value attribute, overriding a passed value attribute" do
|
133
|
+
@page = pages(:mail_form)
|
134
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo' => 'Hello, world!')
|
135
|
+
@page.should render("<r:mailer:#{type} name='foo' value='bar'/>").as(%Q{<input type="#{type}" value="Hello, world!" id="foo" name="mailer[foo]" />})
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should add a 'required' hidden field when the required attribute is specified" do
|
139
|
+
pages(:mail_form).should render("<r:mailer:#{type} name='foo' required='true'/>").as(%Q{<input type="#{type}" value="" id="foo" name="mailer[foo]" /><input type="hidden" name="mailer[required][foo]" value="true" />})
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should raise an error if the name attribute is not specified" do
|
143
|
+
pages(:mail_form).should render("<r:mailer:#{type} />").with_error("`mailer:#{type}' tag requires a `name' attribute")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "<r:mailer:select>" do
|
149
|
+
it "should render a select tag" do
|
150
|
+
pages(:mail_form).should render('<r:mailer:select name="foo" />').as('<select size="1" id="foo" name="mailer[foo]"></select>')
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should raise an error if the name attribute is not specified" do
|
154
|
+
pages(:mail_form).should render("<r:mailer:select />").with_error("`mailer:select' tag requires a `name' attribute")
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should render its contents within the select tag" do
|
158
|
+
pages(:mail_form).should render('<r:mailer:select name="foo">bar</r:mailer:select>').as('<select size="1" id="foo" name="mailer[foo]">bar</select>')
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should render nested <r:mailer:option> tags as option tags" do
|
162
|
+
pages(:mail_form).should render('<r:mailer:select name="foo"><r:option value="bar">bar</r:option></r:mailer:select>').as('<select size="1" id="foo" name="mailer[foo]"><option value="bar" >bar</option></select>')
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should select the specified option tag on a new form" do
|
166
|
+
pages(:mail_form).should render('<r:mailer:select name="foo"><r:option value="bar" selected="selected">bar</r:option><r:option value="baz">baz</r:option></r:mailer:select>').as('<select size="1" id="foo" name="mailer[foo]"><option value="bar" selected="selected" >bar</option><option value="baz" >baz</option></select>')
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should select the option tag with previously posted value" do
|
170
|
+
@page = pages(:mail_form)
|
171
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo' => 'baz')
|
172
|
+
@page.should render('<r:mailer:select name="foo"><r:option value="bar" selected="selected">bar</r:option><r:option value="baz">baz</r:option></r:mailer:select>').as('<select size="1" id="foo" name="mailer[foo]"><option value="bar" >bar</option><option value="baz" selected="selected" >baz</option></select>')
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should add a 'required' hidden field when the required attribute is specified" do
|
176
|
+
pages(:mail_form).should render("<r:mailer:select name='foo' required='true'/>").as(%Q{<select size="1" id="foo" name="mailer[foo]"></select><input type="hidden" name="mailer[required][foo]" value="true" />})
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "<r:mailer:date_select>" do
|
181
|
+
# HACK: Quick way to get a tag rendered to string in the context of a page.
|
182
|
+
# How can this be made better?
|
183
|
+
def render_tag_in_mailer(content)
|
184
|
+
tag = Spec::Rails::Matchers::RenderTags.new
|
185
|
+
tag.send(:render_content_with_page, content, pages(:mail_form))
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should render select tags for each date component" do
|
189
|
+
date_select = render_tag_in_mailer('<r:mailer:date_select name="foo" />')
|
190
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(1i)]")
|
191
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(2i)]")
|
192
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(3i)]")
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should include blank options" do
|
196
|
+
date_select = render_tag_in_mailer('<r:mailer:date_select name="foo" include_blank="true" />')
|
197
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(1i)]") do
|
198
|
+
with_tag('option[value=?]', '')
|
199
|
+
end
|
200
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(2i)]") do
|
201
|
+
with_tag('option[value=?]', '')
|
202
|
+
end
|
203
|
+
date_select.should have_tag('select[name=?]', "mailer[foo(3i)]") do
|
204
|
+
with_tag('option[value=?]', '')
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should order select tags" do
|
209
|
+
date_select = render_tag_in_mailer('<r:mailer:date_select name="foo" order="day,year,month" />')
|
210
|
+
date_select.should have_tag('select[name=?]+select[name=?]+select[name=?]','mailer[foo(3i)]', 'mailer[foo(1i)]', 'mailer[foo(2i)]')
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
describe "<r:mailer:textarea>" do
|
215
|
+
it "should render a textarea tag" do
|
216
|
+
pages(:mail_form).should render('<r:mailer:textarea name="body" />').as('<textarea id="body" rows="5" cols="35" name="mailer[body]"></textarea>')
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should raise an error if the name attribute is not specified" do
|
220
|
+
pages(:mail_form).should render("<r:mailer:textarea />").with_error("`mailer:textarea' tag requires a `name' attribute")
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should render its contents as the contents of the textarea tag" do
|
224
|
+
pages(:mail_form).should render('<r:mailer:textarea name="body">Hello, world!</r:mailer:textarea>').as('<textarea id="body" rows="5" cols="35" name="mailer[body]">Hello, world!</textarea>')
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should add a 'required' hidden field when the required attribute is specified" do
|
228
|
+
pages(:mail_form).should render("<r:mailer:textarea name='body' required='true'/>").as(%Q{<textarea id="body" rows="5" cols="35" name="mailer[body]"></textarea><input type="hidden" name="mailer[required][body]" value="true" />})
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe "<r:mailer:radiogroup>" do
|
233
|
+
it "should render its contents" do
|
234
|
+
pages(:mail_form).should render('<r:mailer:radiogroup name="foo">bar</r:mailer:radiogroup>').as('bar')
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should raise an error if the name attribute is not specified" do
|
238
|
+
pages(:mail_form).should render("<r:mailer:radiogroup />").with_error("`mailer:radiogroup' tag requires a `name' attribute")
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should render nested <r:mailer:option> tags as radio buttons" do
|
242
|
+
pages(:mail_form).should render('<r:mailer:radiogroup name="foo"><r:option value="bar" /></r:mailer:radiogroup>').as('<input type="radio" value="bar" id="foo" name="mailer[foo]" />')
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should select the specified radio button on a new form" do
|
246
|
+
pages(:mail_form).should render('<r:mailer:radiogroup name="foo"><r:option value="bar" selected="selected"/><r:option value="baz" /></r:mailer:radiogroup>').as('<input type="radio" value="bar" checked="checked" id="foo" name="mailer[foo]" /><input type="radio" value="baz" id="foo" name="mailer[foo]" />')
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should select the radio button with previously posted value" do
|
250
|
+
@page = pages(:mail_form)
|
251
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo' => 'baz')
|
252
|
+
@page.should render('<r:mailer:radiogroup name="foo"><r:option value="bar" selected="selected"/><r:option value="baz" /></r:mailer:radiogroup>').as('<input type="radio" value="bar" id="foo" name="mailer[foo]" /><input type="radio" value="baz" checked="checked" id="foo" name="mailer[foo]" />')
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
describe "<r:mailer:get>" do
|
257
|
+
before :each do
|
258
|
+
@page = pages(:mail_form)
|
259
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo' => 'baz')
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should render the data as YAML when no name is given" do
|
263
|
+
@page.should render('<r:mailer:get />').as("--- \nfoo: baz\n")
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should render the specified datum" do
|
267
|
+
@page.should render('<r:mailer:get name="foo" />').as('baz')
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should render nothing when the value is not present" do
|
271
|
+
@page.should render('<r:mailer:get name="body" />').as('')
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should render date when date params are detected" do
|
275
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo(1i)' => '2008', 'foo(2i)' => '10', 'foo(3i)' => '29')
|
276
|
+
@page.should render('<r:mailer:get name="foo" />').as('2008-10-29')
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe "<r:mailer:get_each>" do
|
281
|
+
before :each do
|
282
|
+
# This simulates variables like :
|
283
|
+
# products[0][qty]=10
|
284
|
+
# products[0][name]=foo
|
285
|
+
# products[1][qty]=5
|
286
|
+
# products[1][name]=bar
|
287
|
+
test_array=[ { 'qty' => 10, 'name' => 'foo' },
|
288
|
+
{ 'qty' => 5, 'name' => 'bar' } ]
|
289
|
+
@page = pages(:mail_form)
|
290
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'qty' => 'wrong', 'products' => test_array)
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should not alter the content on its own" do
|
294
|
+
@page.should render('<r:mailer:get_each />').as('')
|
295
|
+
end
|
296
|
+
|
297
|
+
it "should make mailer:get use the local variables" do
|
298
|
+
# If this fails, it will show "wrongwrong" or something like that
|
299
|
+
@page.should render('<r:mailer:get_each name="products"><r:mailer:get name="qty" /></r:mailer:get_each>').as('105')
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should iterate and provide mailer:get the local variables" do
|
303
|
+
@page.should render('<r:mailer:get_each name="products"><r:mailer:get name="qty" />x<r:mailer:get name="name" />,</r:mailer:get_each>').as('10xfoo,5xbar,')
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should provide mailer:index" do
|
307
|
+
@page.should render('<r:mailer:get_each name="products"><r:mailer:index /><r:mailer:get name="name" /></r:mailer:get_each>').as('0foo1bar')
|
308
|
+
end
|
309
|
+
|
310
|
+
end
|
311
|
+
|
312
|
+
describe "<r:mailer:if_value>" do
|
313
|
+
before :each do
|
314
|
+
@page = pages(:mail_form)
|
315
|
+
@page.last_mail = @mail = Mail.new(@page, @page.config, 'foo' => 'baz')
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should render its contained block if the specified value was submitted" do
|
319
|
+
@page.should render('<r:mailer:if_value name="foo">true</r:mailer:if_value>').as('true')
|
320
|
+
end
|
321
|
+
|
322
|
+
it "should render not its contained block if the specified value was not submitted" do
|
323
|
+
@page.should render('<r:mailer:if_value name="bar">true</r:mailer:if_value>').as('')
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe Mail do
|
4
|
+
dataset :mailer
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@page = pages(:mail_form)
|
8
|
+
@page.request = ActionController::TestRequest.new
|
9
|
+
@page.last_mail = @mail = Mail.new(@page, {:recipients => ['foo@bar.com'], :from => 'foo@baz.com'}, {'body' => 'Hello, world!'})
|
10
|
+
ActionMailer::Base.delivery_method = :test
|
11
|
+
ActionMailer::Base.deliveries = []
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should have an invalid config when recipients or from keys are absent" do
|
15
|
+
Mail.valid_config?('from' => 'foo@baz.com').should be_false
|
16
|
+
Mail.valid_config?('recipients' => 'foo@bar.com').should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have a valid config when recipients and from keys are present" do
|
20
|
+
Mail.valid_config?('recipients' => 'foo@bar.com', 'from' => 'foo@baz.com').should be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should have a valid config when recipients_field stands in for recipients" do
|
24
|
+
Mail.valid_config?('recipients_field' => 'to', 'from' => 'foo@baz.com').should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should have a valid config when from_field stands in for from" do
|
28
|
+
Mail.valid_config?('recipients' => 'foo@bar.com', 'from_field' => 'from').should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should derive the from field from the configuration" do
|
32
|
+
@mail.from.should == 'foo@baz.com'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should derive the from field from the data when not in the configuration" do
|
36
|
+
@mail.config[:from] = nil
|
37
|
+
@mail.config[:from_field] = 'from'
|
38
|
+
@mail.data['from'] = 'radiant@foo.com'
|
39
|
+
@mail.from.should == 'radiant@foo.com'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should derive the recipients field from the configuration" do
|
43
|
+
@mail.recipients.should == ['foo@bar.com']
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should derive the recipients field from the data when not in the configuration" do
|
47
|
+
@mail.config[:recipients] = nil
|
48
|
+
@mail.config[:recipients_field] = 'to'
|
49
|
+
@mail.data['to'] = 'radiant@foo.com'
|
50
|
+
@mail.recipients.should == %w(radiant@foo.com)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should derive the reply_to field from the configuration" do
|
54
|
+
@mail.config[:reply_to] = "sean@radiant.com"
|
55
|
+
@mail.reply_to.should == "sean@radiant.com"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should derive the reply_to field from the data when not in the configuration" do
|
59
|
+
@mail.config[:reply_to_field] = 'reply_to'
|
60
|
+
@mail.data['reply_to'] = 'sean@radiant.com'
|
61
|
+
@mail.reply_to.should == 'sean@radiant.com'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should derive the sender field from the configuration" do
|
65
|
+
@mail.config[:sender] = "sean@radiant.com"
|
66
|
+
@mail.sender.should == "sean@radiant.com"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should derive the subject field from the data" do
|
70
|
+
@mail.data[:subject] = "My subject"
|
71
|
+
@mail.subject.should == 'My subject'
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should derive the subject field from the configuration when not present in the data" do
|
75
|
+
@mail.config[:subject] = "My subject"
|
76
|
+
@mail.subject.should == 'My subject'
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should generate the subject field when not present in data or configuration" do
|
80
|
+
@mail.subject.should == 'Form Mail from test.host'
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should derive the cc field from the data when configured" do
|
84
|
+
@mail.data['cc'] = "sean@radiant.com"
|
85
|
+
@mail.config[:cc_field] = 'cc'
|
86
|
+
@mail.cc.should == "sean@radiant.com"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should derive the cc field from the configuration when not in the data" do
|
90
|
+
@mail.config[:cc] = "sean@radiant.com"
|
91
|
+
@mail.cc.should == "sean@radiant.com"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should return a blank cc when not in the data or configuration" do
|
95
|
+
@mail.cc.should be_blank
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should initially have no errors" do
|
99
|
+
@mail.errors.should == {}
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should be valid when the configuration and fields are correct" do
|
103
|
+
@mail.should be_valid
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should be invalid when the recipients are empty" do
|
107
|
+
@mail.config[:recipients] = []
|
108
|
+
@mail.should_not be_valid
|
109
|
+
@mail.errors['form'].should_not be_blank
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should be invalid when the recipients contains invalid email adresses" do
|
113
|
+
@mail.config[:recipients] = ['sean AT radiant DOT com']
|
114
|
+
@mail.should_not be_valid
|
115
|
+
@mail.errors['form'].should_not be_blank
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should be invalid when the from field is empty" do
|
119
|
+
@mail.config[:from] = nil
|
120
|
+
@mail.should_not be_valid
|
121
|
+
@mail.errors['form'].should_not be_blank
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be invalid when the from field contains an invalid email address" do
|
125
|
+
@mail.config[:from] = 'sean AT radiant DOT com'
|
126
|
+
@mail.should_not be_valid
|
127
|
+
@mail.errors['form'].should_not be_blank
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should be invalid when a required field is missing" do
|
131
|
+
@mail = Mail.new(@page, {:recipients => ['foo@bar.com'], :from => 'foo@baz.com'}, {:required => {'first_name' => 'true'}})
|
132
|
+
@mail.should_not be_valid
|
133
|
+
@mail.errors['first_name'].should_not be_blank
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should not send the mail if invalid" do
|
137
|
+
@mail.should_receive(:valid?).and_return(false)
|
138
|
+
@mail.send.should be_false
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should send an email" do
|
142
|
+
@mail.send.should be_true
|
143
|
+
ActionMailer::Base.deliveries.should_not be_empty
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should not send and should register an error if the Mailer raised an exception" do
|
147
|
+
Mailer.should_receive(:deliver_generic_mail).and_raise("Boom!")
|
148
|
+
@mail.should be_valid
|
149
|
+
@mail.send.should be_false
|
150
|
+
@mail.errors['base'].should == "Boom!"
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should set the Reply-To header on the sent mail to the reply_to value given in the configuration" do
|
154
|
+
@mail.config[:reply_to] = "reply_to@example.com"
|
155
|
+
@mail.send
|
156
|
+
ActionMailer::Base.deliveries.last.reply_to.should == [@mail.config[:reply_to]] # reply_to on TMail is an Array
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should set the Reply-To header on the sent mail to the reply_to_field data value given in the configuration" do
|
160
|
+
@mail.config[:reply_to_field] = 'email'
|
161
|
+
@mail.data['email'] = 'reply_to_field@example.com'
|
162
|
+
@mail.send
|
163
|
+
ActionMailer::Base.deliveries.last.reply_to.should == [@mail.data['email']] # reply_to on TMail is an Array
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should set the Reply-To header on the sent mail to the from value when the configuration does not specify a reply_to" do
|
167
|
+
@mail.send
|
168
|
+
ActionMailer::Base.deliveries.last.reply_to.should == ['foo@baz.com'] # reply_to on TMail is an Array
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "when the page has no email body specified" do
|
172
|
+
it "should render the submitted data as YAML to the plain body" do
|
173
|
+
Mailer.should_receive(:deliver_generic_mail) do |params|
|
174
|
+
params[:plain_body].should == "The following information was posted:\n--- \nbody: Hello, world!\n\n"
|
175
|
+
params[:html_body].should be_blank
|
176
|
+
end
|
177
|
+
@mail.send.should be_true
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "when the page has specified a plain email body" do
|
182
|
+
before :each do
|
183
|
+
@page = pages(:plain_mail)
|
184
|
+
@page.request = ActionController::TestRequest.new
|
185
|
+
@page.last_mail = @mail = Mail.new(@page, {:recipients => ['foo@bar.com'], :from => 'foo@baz.com'}, {'body' => 'Hello, world!'})
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should send an email with the rendered plain body" do
|
189
|
+
Mailer.should_receive(:deliver_generic_mail) do |params|
|
190
|
+
params[:plain_body].should == 'The body: Hello, world!'
|
191
|
+
params[:html_body].should be_blank
|
192
|
+
end
|
193
|
+
@mail.send.should be_true
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "when the page has specified an HTML email body" do
|
198
|
+
before :each do
|
199
|
+
@page = pages(:html_mail)
|
200
|
+
@page.request = ActionController::TestRequest.new
|
201
|
+
@page.last_mail = @mail = Mail.new(@page, {:recipients => ['foo@bar.com'], :from => 'foo@baz.com'}, {'body' => 'Hello, world!'})
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should send an email with the rendered plain body" do
|
205
|
+
Mailer.should_receive(:deliver_generic_mail) do |params|
|
206
|
+
params[:plain_body].should be_blank
|
207
|
+
params[:html_body].should == '<html><body>Hello, world!</body></html>'
|
208
|
+
end
|
209
|
+
@mail.send.should be_true
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "Mailer" do
|
4
|
+
before :each do
|
5
|
+
ActionMailer::Base.delivery_method = :test
|
6
|
+
@deliveries = ActionMailer::Base.deliveries = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def do_deliver(options={})
|
10
|
+
Mailer.deliver_generic_mail({:recipients => ['foo@bar.com'], :plain_body => ''}.merge(options).reject {|k,v| v.nil? })
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set the recipients" do
|
14
|
+
do_deliver
|
15
|
+
@deliveries.first.to.should == ['foo@bar.com']
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be the multipart/alternative content type" do
|
19
|
+
do_deliver
|
20
|
+
@deliveries.first.content_type.should == 'multipart/alternative'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should render a plain body" do
|
24
|
+
do_deliver :plain_body => "Hello, world!"
|
25
|
+
@deliveries.first.body.should match(/Hello, world!/)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should render an HTML body" do
|
29
|
+
do_deliver :plain_body => nil, :html_body => "<html><body>Hello, world!</body></html>"
|
30
|
+
@deliveries.first.body.should match(%r{<html><body>Hello, world!</body></html>})
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should render both bodies when present" do
|
34
|
+
do_deliver :plain_body => "Hiya!", :html_body => "<html><body>Hello, world!</body></html>"
|
35
|
+
@deliveries.first.body.should match(/Hiya!/)
|
36
|
+
@deliveries.first.body.should match(%r{<html><body>Hello, world!</body></html>})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set the subject" do
|
40
|
+
do_deliver :subject => "Testing 123"
|
41
|
+
@deliveries.first.subject.should == "Testing 123"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should set the from field" do
|
45
|
+
do_deliver :from => "sean@radiant.com"
|
46
|
+
@deliveries.first.from.should == %w'sean@radiant.com'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should set the cc field" do
|
50
|
+
do_deliver :cc => "sean@radiant.com"
|
51
|
+
@deliveries.first.cc.should == %w"sean@radiant.com"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set the bcc field" do
|
55
|
+
do_deliver :bcc => "sean@radiant.com"
|
56
|
+
@deliveries.first.bcc.should == %w"sean@radiant.com"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should set the headers" do
|
60
|
+
do_deliver :headers => {'Reply-To' => 'sean@cribbs.com'}
|
61
|
+
@deliveries.first['Reply-To'].inspect.should match(/sean@cribbs.com/)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Not sure that charset works, can see no effect in tests
|
65
|
+
it "should set the default character set to utf8" do
|
66
|
+
pending
|
67
|
+
do_deliver
|
68
|
+
@deliveries.first.charset.should == 'utf8'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should set the character set" do
|
72
|
+
pending
|
73
|
+
do_deliver :charset => 'iso8859-1'
|
74
|
+
@deliveries.first.charset.should == 'iso8859-1'
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
unless defined? RADIANT_ROOT
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
case
|
4
|
+
when ENV["RADIANT_ENV_FILE"]
|
5
|
+
require ENV["RADIANT_ENV_FILE"]
|
6
|
+
when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
|
7
|
+
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
|
8
|
+
else
|
9
|
+
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
require "#{RADIANT_ROOT}/spec/spec_helper"
|
13
|
+
|
14
|
+
Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")
|
15
|
+
|
16
|
+
if File.directory?(File.dirname(__FILE__) + "/matchers")
|
17
|
+
Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
|
18
|
+
end
|
19
|
+
|
20
|
+
Spec::Runner.configure do |config|
|
21
|
+
# config.use_transactional_fixtures = true
|
22
|
+
# config.use_instantiated_fixtures = false
|
23
|
+
# config.fixture_path = RAILS_ROOT + '/spec/fixtures'
|
24
|
+
|
25
|
+
# You can declare fixtures for each behaviour like this:
|
26
|
+
# describe "...." do
|
27
|
+
# fixtures :table_a, :table_b
|
28
|
+
#
|
29
|
+
# Alternatively, if you prefer to declare them only once, you can
|
30
|
+
# do so here, like so ...
|
31
|
+
#
|
32
|
+
# config.global_fixtures = :table_a, :table_b
|
33
|
+
#
|
34
|
+
# If you declare global fixtures, be aware that they will be declared
|
35
|
+
# for all of your examples, even those that don't use them.
|
36
|
+
end
|