spreewald 2.8.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,22 +5,38 @@ class MailFinder
5
5
 
6
6
  attr_accessor :user_identity
7
7
 
8
- def find(conditions)
8
+ def find(raw_data, type = '')
9
+ header, body = raw_data.split(/\n\n/, 2) # 2: maximum number of fields
10
+ conditions = {}
11
+ header.split("\n").each do |row|
12
+ if row.match(/^[A-Za-z\-]+: /i)
13
+ key, value = row.split(": ", 2)
14
+ conditions[key.strip.underscore.to_sym] = value.strip
15
+ end
16
+ end
17
+
9
18
  filename_method = Rails::VERSION::MAJOR < 3 ? 'original_filename' : 'filename'
10
- ActionMailer::Base.deliveries.detect do |mail|
11
- mail_body = email_text_body(mail)
19
+ matching_header = ActionMailer::Base.deliveries.select do |mail|
12
20
  [ conditions[:to].nil? || mail.to.include?(resolve_email conditions[:to]),
13
21
  conditions[:cc].nil? || mail.cc && mail.cc.include?(resolve_email conditions[:cc]),
14
22
  conditions[:bcc].nil? || mail.bcc && mail.bcc.include?(resolve_email conditions[:bcc]),
15
23
  conditions[:from].nil? || mail.from.include?(resolve_email conditions[:from]),
16
24
  conditions[:reply_to].nil? || mail.reply_to.include?(resolve_email conditions[:reply_to]),
17
25
  conditions[:subject].nil? || mail.subject.include?(conditions[:subject]),
18
- conditions[:body].nil? || mail_body.include?(conditions[:body]),
19
26
  conditions[:attachments].nil? || conditions[:attachments].split(/\s*,\s*/).sort == Array(mail.attachments).collect(&:"#{filename_method}").sort
20
27
  ].all?
21
- end.tap do |mail|
22
- log(mail)
23
28
  end
29
+
30
+ matching_mails = if body
31
+ body_regex = expected_body_regex(body)
32
+ matching_header.select do |mail|
33
+ body_regex =~ email_text_body(mail, type).strip
34
+ end
35
+ else
36
+ matching_header
37
+ end
38
+
39
+ Results.new(matching_mails, matching_header, body_regex)
24
40
  end
25
41
 
26
42
  def resolve_email(identity)
@@ -31,46 +47,59 @@ class MailFinder
31
47
  end
32
48
  end
33
49
 
34
- def log(mail)
35
- if mail.present?
36
- File.open("log/test_mails.log", "a") do |file|
37
- file << "From: #{mail.from}\n"
38
- file << "To: #{mail.to.join(', ')}\n" if mail.to
39
- file << "Subject: #{mail.subject}\n\n"
40
- file << email_text_body(mail)
41
- file << "\n-------------------------\n\n"
42
- end
43
- end
50
+ def header_representation(mail)
51
+ header = ""
52
+ header << "From: #{mail.from}\n"
53
+ header << "Reply-To: #{mail.reply_to.join(', ')}\n" if mail.reply_to
54
+ header << "To: #{mail.to.join(', ')}\n" if mail.to
55
+ header << "CC: #{mail.cc.join(', ')}\n" if mail.cc
56
+ header << "BCC: #{mail.bcc.join(', ')}\n" if mail.bcc
57
+ header << "Subject: #{mail.subject}\n"
44
58
  end
45
59
 
46
- def email_text_body(mail)
47
- body = if mail.parts.any?
48
- mail_bodies = mail.parts.map { |part|
49
- if part.header.to_s.include?('Quoted-printable')
50
- if Rails.version.starts_with?('2.3')
51
- part.body.to_s
52
- else
53
- part.decoded
54
- end
55
- elsif part.content_type.include?('multipart/alternative')
56
- part.parts.map(&:decoded)
57
- else
58
- part.decoded
59
- end
60
- }
61
- utf8_parts = mail_bodies.flatten.select{ |part|
62
- encoding = part.try(:encoding)
63
- encoding.nil? or # Ruby < 1.9.1
64
- encoding.name == 'UTF-8' # Ruby > 1.9.1
65
- }
66
- utf8_parts.join('\n')
67
- elsif mail.body.respond_to? :raw_source
68
- mail.body.raw_source
60
+ def email_text_body(mail, type = '')
61
+ body = if mail.html_part && type != 'plain-text'
62
+ dom = Nokogiri::HTML(mail.html_part.body.to_s)
63
+ dom.at_css('body').text.gsub(/[\r\n]+/, "\n")
64
+ elsif mail.text_part && type != 'HTML'
65
+ mail.text_part.body.to_s
69
66
  else
70
- mail.body
67
+ mail.body.to_s
71
68
  end
72
69
  body.gsub("\r\n", "\n") # The mail gem (>= 2.7.1) switched from \n to \r\n line breaks (LF to CRLF) in plain text mails.
73
70
  end
74
71
 
72
+ def show_mails(mails, only_header = false)
73
+ message = ""
74
+ mails.each_with_index do |mail, i|
75
+ message << "E-Mail ##{i}\n"
76
+ message << "-" * 80 + "\n"
77
+ message << header_representation(mail)
78
+ message << "\n" + email_text_body(mail).strip + "\n" unless only_header
79
+ message << "-" * 80 + "\n\n"
80
+ end
81
+ message
82
+ end
83
+
84
+ def expected_body_regex(expected_body)
85
+ expected = '\A\n' + Regexp.quote(expected_body.strip) + '\n\Z'
86
+ expected.gsub! '\n\*\n', '\n[\s\S]*\n'
87
+ expected.gsub! '\*\n', '.*\n'
88
+
89
+ expected.gsub! '\A\n', '\A'
90
+ expected.gsub! '\n\Z', '\Z'
91
+
92
+ Regexp.new(expected)
93
+ end
94
+
95
+ end
96
+ end
97
+
98
+ class MailFinder::Results < Struct.new(:mails, :matching_header, :body_regex)
99
+ extend Forwardable
100
+ delegate [:size, :one?, :empty?] => :mails
101
+
102
+ def many?
103
+ size > 1
75
104
  end
76
105
  end
@@ -1,3 +1,3 @@
1
1
  module Spreewald
2
- VERSION = '2.8.0'
2
+ VERSION = '3.0.1'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- spreewald (2.8.0)
4
+ spreewald (3.0.1)
5
5
  cucumber
6
6
  cucumber_priority (>= 0.3.0)
7
7
  rspec (>= 2.13.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- spreewald (2.8.0)
4
+ spreewald (3.0.1)
5
5
  cucumber
6
6
  cucumber_priority (>= 0.3.0)
7
7
  rspec (>= 2.13.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- spreewald (2.8.0)
4
+ spreewald (3.0.1)
5
5
  cucumber
6
6
  cucumber_priority (>= 0.3.0)
7
7
  rspec (>= 2.13.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- spreewald (2.8.0)
4
+ spreewald (3.0.1)
5
5
  cucumber
6
6
  cucumber_priority (>= 0.3.0)
7
7
  rspec (>= 2.13.0)
@@ -17,7 +17,15 @@ class SpreewaldMailer < ApplicationMailer
17
17
  :subject => SUBJECT
18
18
  )
19
19
  end
20
-
20
+
21
+ def email_crlf
22
+ email
23
+ end
24
+
25
+ def email_with_umlauts
26
+ email
27
+ end
28
+
21
29
  def html_email_with_links
22
30
  email
23
31
  end
@@ -26,5 +34,9 @@ class SpreewaldMailer < ApplicationMailer
26
34
  email
27
35
  end
28
36
 
37
+ def html_email_with_linebreaks
38
+ email
39
+ end
40
+
29
41
  end
30
42
 
@@ -9,6 +9,16 @@ class EmailsController < ApplicationController
9
9
  render_nothing
10
10
  end
11
11
 
12
+ def send_crlf_email
13
+ deliver :email_crlf
14
+ render_nothing
15
+ end
16
+
17
+ def send_email_with_umlauts
18
+ deliver :email_with_umlauts
19
+ render_nothing
20
+ end
21
+
12
22
  def send_html_email_with_links
13
23
  deliver :html_email_with_links
14
24
  render_nothing
@@ -19,6 +29,11 @@ class EmailsController < ApplicationController
19
29
  render_nothing
20
30
  end
21
31
 
32
+ def send_html_email_with_linebreaks
33
+ deliver :html_email_with_linebreaks
34
+ render_nothing
35
+ end
36
+
22
37
  private
23
38
 
24
39
  def deliver(method_name)
@@ -21,6 +21,14 @@ class Mailer < ActionMailer::Base
21
21
  )
22
22
  end
23
23
 
24
+ def email_crlf
25
+ email
26
+ end
27
+
28
+ def email_with_umlauts
29
+ email
30
+ end
31
+
24
32
  def html_email_with_links
25
33
  email
26
34
  end
@@ -29,6 +37,10 @@ class Mailer < ActionMailer::Base
29
37
  email
30
38
  end
31
39
 
40
+ def html_email_with_linebreaks
41
+ email
42
+ end
43
+
32
44
  else
33
45
 
34
46
  def email
@@ -42,6 +54,10 @@ class Mailer < ActionMailer::Base
42
54
  body BODY
43
55
  end
44
56
 
57
+ def email_crlf
58
+ email
59
+ end
60
+
45
61
  def html_email_with_links
46
62
  email
47
63
  end
@@ -4,6 +4,9 @@
4
4
  = label_tag 'checked_name', 'Checked'
5
5
  = check_box_tag 'checked_name', 'value', checked
6
6
 
7
+ = label_tag 'checked_disabled_name', 'Checked disabled'
8
+ = check_box_tag 'checked_disabled_name', 'value', checked, disabled: true
9
+
7
10
  - checked = false
8
11
  = label_tag 'unchecked_name', 'Unchecked'
9
12
  = check_box_tag 'unchecked_name', 'value', checked
@@ -69,3 +69,4 @@
69
69
 
70
70
  = label_tag 'enabled_radio_2', 'Enabled radio button #2'
71
71
  = radio_button_tag 'enabled_radio', '2', selected
72
+
@@ -3,6 +3,9 @@
3
3
  = label_tag 'text_control', 'Text control'
4
4
  = text_field_tag 'text_control', 'Text control value'
5
5
 
6
+ = label_tag 'disabled_text_control', 'Disabled text control'
7
+ = text_field_tag 'disabled_text_control', 'Disabled text control value', disabled: true
8
+
6
9
  = label_tag 'select_control', 'Select control'
7
10
  = select_tag 'select_control', options_for_select([['Label 1', 'value1'], ['Label 2', 'value2'], ['Label 3', 'value3']], 'value2')
8
11
 
@@ -26,4 +29,4 @@
26
29
  = label_tag 'radio_button_control_radio1', 'Radio 1'
27
30
  = radio_button_tag 'radio_button_control', 'radio1'
28
31
  = label_tag 'radio_button_control_radio2', 'Radio 2'
29
- = radio_button_tag 'radio_button_control', 'radio2'
32
+ = radio_button_tag 'radio_button_control', 'radio2'
@@ -5,6 +5,9 @@
5
5
  .form-group.field_with_errors
6
6
  = label_tag 'textarea_control_1', 'B is invalid'
7
7
  = text_area_tag 'textarea_control_1', "Textarea control line 1\nTextarea control line 2\n"
8
+ .form-group.field_with_errors
9
+ = label_tag 'disabled_control', 'Disabled is invalid'
10
+ = text_field_tag 'disabled_control', "Disabled control value", disabled: true
8
11
  .form-group
9
12
  = label_tag 'textarea_control_2', 'C'
10
13
  = text_area_tag 'textarea_control_2', "Textarea control line 1\nTextarea control line 2\n"
@@ -0,0 +1,5 @@
1
+ Body
2
+ with
3
+ CRLF
4
+ line
5
+ breaks
@@ -0,0 +1,6 @@
1
+ <p>
2
+ Hello!
3
+ </p>
4
+ <p>
5
+ Bye!
6
+ </p>
@@ -6,8 +6,11 @@ Rails.application.routes.draw do
6
6
 
7
7
  get '/emails/do_nothing', to: 'emails#do_nothing'
8
8
  get '/emails/send_email', to: 'emails#send_email'
9
+ get '/emails/send_crlf_email', to: 'emails#send_crlf_email'
10
+ get '/emails/send_email_with_umlauts', to: 'emails#send_email_with_umlauts'
9
11
  get '/emails/send_html_email_with_links', to: 'emails#send_html_email_with_links'
10
12
  get '/emails/send_text_email_with_links', to: 'emails#send_text_email_with_links'
13
+ get '/emails/send_html_email_with_linebreaks', to: 'emails#send_html_email_with_linebreaks'
11
14
 
12
15
  get '/forms/checkbox_form', to: 'forms#checkbox_form'
13
16
  get '/forms/disabled_elements', to: 'forms#disabled_elements'
@@ -1,10 +1,10 @@
1
1
  Feature: Test Spreewald's email steps
2
-
2
+
3
3
  Scenario: /^no e?mail should have been sent$/
4
4
  When I go to "/emails/do_nothing"
5
5
  Then the following step should succeed:
6
6
  | no email should have been sent |
7
-
7
+
8
8
  When I go to "/emails/send_email"
9
9
  Then the following step should fail:
10
10
  | no email should have been sent |
@@ -50,6 +50,53 @@ Feature: Test Spreewald's email steps
50
50
  '''
51
51
  """
52
52
 
53
+ # Test with end-of-line wildcards
54
+ Then the following multiline step should succeed:
55
+ """
56
+ Then an email should have been sent with:
57
+ '''
58
+ From: from@example.com
59
+ Reply-To: reply-to@example.com
60
+ To: to@example.com
61
+ Subject: SUBJECT
62
+
63
+ Bo*
64
+ wi*
65
+ li*
66
+ br*
67
+ '''
68
+ """
69
+
70
+ # Test with multiple-line wildcards
71
+ Then the following multiline step should succeed:
72
+ """
73
+ Then an email should have been sent with:
74
+ '''
75
+ From: from@example.com
76
+ Reply-To: reply-to@example.com
77
+ To: to@example.com
78
+ Subject: SUBJECT
79
+
80
+ Body
81
+ *
82
+ breaks
83
+ '''
84
+ """
85
+
86
+ # Test with the whole body being a multiple-line wildcard
87
+ Then the following multiline step should succeed:
88
+ """
89
+ Then an email should have been sent with:
90
+ '''
91
+ From: from@example.com
92
+ Reply-To: reply-to@example.com
93
+ To: to@example.com
94
+ Subject: SUBJECT
95
+
96
+ *
97
+ '''
98
+ """
99
+
53
100
  # Test with incorrect From header
54
101
  Then the following multiline step should fail:
55
102
  """
@@ -148,73 +195,96 @@ Feature: Test Spreewald's email steps
148
195
  with
149
196
  line
150
197
  breaks
151
-
198
+
152
199
  MORE-BODY
153
200
  '''
154
201
  """
155
202
 
156
- Scenario: /^(an|no) e?mail should have been sent((?: |and|with|from "[^"]+"|bcc "[^"]+"|cc "[^"]+"|to "[^"]+"|the subject "[^"]+"|the body "[^"]+"|the attachments "[^"]+")+)$/
157
- When I go to "/emails/send_email"
158
-
159
- # Test with correct conditions
160
- Then the following multiline step should succeed:
161
- """
162
- Then an email should have been sent from "from@example.com" to "to@example.com" cc "cc@example.com" bcc "bcc@example.com" and the subject "SUBJECT" and the attachments "attached_file.pdf"
163
- """
164
-
165
- # Test with wrong conditions
203
+ # Test body with to few paragraphs
166
204
  Then the following multiline step should fail:
167
205
  """
168
- Then an email should have been sent from "from@example.com" to "to@example.com" cc "cc@example.com" bcc "wrong_bcc@example.com" and the subject "SUBJECT" and the attachments "attached_file.pdf"
169
- """
170
-
171
- Scenario: /^that e?mail should have the following lines in the body:$/
172
- When I go to "/emails/send_email"
173
-
174
- # Test with correct body lines
175
- Then the following multiline step should succeed:
176
- """
177
- Then that email should have the following lines in the body:
206
+ Then an email should have been sent with:
178
207
  '''
208
+ From: from@example.com
209
+ Reply-To: reply-to@example.com
210
+ To: to@example.com
211
+ Subject: SUBJECT
212
+
213
+ Body
179
214
  with
180
215
  line
181
216
  '''
182
217
  """
183
218
 
184
- # Test with wrong body lines
219
+ # Test body with wildcard not at the end of a line
185
220
  Then the following multiline step should fail:
186
221
  """
187
- Then that email should have the following lines in the body:
222
+ Then an email should have been sent with:
188
223
  '''
189
- wrong
224
+ From: from@example.com
225
+ Reply-To: reply-to@example.com
226
+ To: to@example.com
227
+ Subject: SUBJECT
228
+
229
+ Body
230
+ *th
190
231
  line
232
+ break
191
233
  '''
192
234
  """
193
235
 
236
+ When I clear my emails
237
+ And I go to "/emails/send_crlf_email"
194
238
 
195
- Scenario: /^that e?mail should have the following( content in the)? body$/
196
- When I go to "/emails/send_email"
197
-
198
- # Test with correct body lines
199
239
  Then the following multiline step should succeed:
200
240
  """
201
- Then that email should have the following content in the body:
241
+ Then an email should have been sent with:
202
242
  '''
243
+ From: from@example.com
244
+ Reply-To: reply-to@example.com
245
+ To: to@example.com
246
+ Subject: SUBJECT
247
+
248
+ Body
203
249
  with
250
+ CRLF
204
251
  line
252
+ breaks
205
253
  '''
206
254
  """
207
255
 
208
- # Test with wrong body lines
209
- Then the following multiline step should fail:
210
- """
211
- Then that email should have the following body:
256
+ When I clear my emails
257
+ And I go to "/emails/send_email_with_umlauts"
258
+
259
+ Then the following multiline step should succeed:
260
+ """
261
+ Then an email should have been sent with:
212
262
  '''
213
- wrong
214
- line
263
+ From: from@example.com
264
+ Reply-To: reply-to@example.com
265
+ To: to@example.com
266
+ Subject: SUBJECT
267
+
268
+ Viele Grüße
215
269
  '''
216
270
  """
217
271
 
272
+ When I clear my emails
273
+ And I go to "/emails/send_html_email_with_linebreaks"
274
+
275
+ Then the following multiline step should succeed:
276
+ """
277
+ Then an email should have been sent with:
278
+ '''
279
+ From: from@example.com
280
+ Reply-To: reply-to@example.com
281
+ To: to@example.com
282
+ Subject: SUBJECT
283
+
284
+ Hello!
285
+ Bye!
286
+ '''
287
+ """
218
288
 
219
289
  Scenario: /^I follow the (first|second|third)? ?link in the e?mail$/ (HTML e-mail body)
220
290
  When I go to "/emails/send_html_email_with_links"