File: matchers.rb

package info (click to toggle)
ruby-email-spec 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 980 kB
  • sloc: ruby: 2,420; makefile: 3
file content (377 lines) | stat: -rw-r--r-- 12,349 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
module EmailSpec
  module Matchers
    class EmailMatcher
      def address_array
        if @email.perform_deliveries
          Array(yield)
        else
          []
        end
      end
    end

    class ReplyTo
      def initialize(email)
        @expected_reply_to = Mail::ReplyToField.new(email).addrs.first
      end

      def description
        "have reply to as #{@expected_reply_to.address}"
      end

      def matches?(email)
        @email = email
        @actual_reply_to = (email.reply_to || []).first
        !@actual_reply_to.nil? &&
          @actual_reply_to == @expected_reply_to.address
      end

      def failure_message
        "expected #{@email.inspect} to reply to #{@expected_reply_to.address.inspect}, but it replied to #{@actual_reply_to.inspect}"
      end

      def failure_message_when_negated
        "expected #{@email.inspect} not to deliver to #{@expected_reply_to.address.inspect}, but it did"
      end
      alias negative_failure_message failure_message_when_negated
    end

    def reply_to(email)
      ReplyTo.new(email)
    end

    alias :have_reply_to :reply_to

    class DeliverTo < EmailMatcher
      def initialize(expected_email_addresses_or_objects_that_respond_to_email)
        emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
          email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
        end

        @expected_recipients = Mail::ToField.new(emails).addrs.map(&:to_s).sort
      end

      def description
        "be delivered to #{@expected_recipients.inspect}"
      end

      def matches?(email)
        @email = email
        recipients = email.header[:to] || email.header[:bcc]
        @actual_recipients = address_array{ recipients  && recipients.addrs }.map(&:to_s).sort
        @actual_recipients == @expected_recipients
      end

      def failure_message
        "expected #{@email.inspect} to deliver to #{@expected_recipients.inspect}, but it delivered to #{@actual_recipients.inspect}"
      end

      def failure_message_when_negated
        "expected #{@email.inspect} not to deliver to #{@expected_recipients.inspect}, but it did"
      end
      alias negative_failure_message failure_message_when_negated
    end

    def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email)
      DeliverTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
    end

    alias :be_delivered_to :deliver_to

    class DeliverFrom < EmailMatcher

      def initialize(email)
        @expected_sender = Mail::FromField.new(email).addrs.first
      end

      def description
        "be delivered from #{@expected_sender}"
      end

      def matches?(email)
        @email = email
        @actual_sender = address_array{ email.header[:from].addrs }.first

        !@actual_sender.nil? &&
          @actual_sender.to_s == @expected_sender.to_s
      end

      def failure_message
        %(expected #{@email.inspect} to deliver from "#{@expected_sender.to_s}", but it delivered from "#{@actual_sender.to_s}")
      end

      def failure_message_when_negated
        %(expected #{@email.inspect} not to deliver from "#{@expected_sender.to_s}", but it did)
      end
      alias negative_failure_message failure_message_when_negated
    end

    def deliver_from(email)
      DeliverFrom.new(email)
    end

    alias :be_delivered_from :deliver_from

    class BccTo < EmailMatcher

      def initialize(expected_email_addresses_or_objects_that_respond_to_email)
        emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
          email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
        end

        @expected_email_addresses = emails.sort
      end

      def description
        "be bcc'd to #{@expected_email_addresses.inspect}"
      end

      def matches?(email)
        @email = email
        @actual_recipients = address_array{ email.bcc }.sort
        @actual_recipients == @expected_email_addresses
      end

      def failure_message
        "expected #{@email.inspect} to bcc to #{@expected_email_addresses.inspect}, but it was bcc'd to #{@actual_recipients.inspect}"
      end

      def failure_message_when_negated
        "expected #{@email.inspect} not to bcc to #{@expected_email_addresses.inspect}, but it did"
      end
      alias negative_failure_message failure_message_when_negated
    end

    def bcc_to(*expected_email_addresses_or_objects_that_respond_to_email)
      BccTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
    end

    class CcTo < EmailMatcher

      def initialize(expected_email_addresses_or_objects_that_respond_to_email)
        emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
          email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
        end

        @expected_email_addresses = emails.sort
      end

      def description
        "be cc'd to #{@expected_email_addresses.inspect}"
      end

      def matches?(email)
        @email = email
        @actual_recipients = address_array { email.cc }.sort
        @actual_recipients == @expected_email_addresses
      end

      def failure_message
        "expected #{@email.inspect} to cc to #{@expected_email_addresses.inspect}, but it was cc'd to #{@actual_recipients.inspect}"
      end

      def failure_message_when_negated
        "expected #{@email.inspect} not to cc to #{@expected_email_addresses.inspect}, but it did"
      end
      alias negative_failure_message failure_message_when_negated
    end

    def cc_to(*expected_email_addresses_or_objects_that_respond_to_email)
      CcTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
    end

    class HaveSubject

      def initialize(subject)
        @expected_subject = subject
      end

      def description
        if @expected_subject.is_a?(String)
          "have subject of #{@expected_subject.inspect}"
        else
          "have subject matching #{@expected_subject.inspect}"
        end
      end

      def matches?(email)
        @given_subject = email.subject
        if @expected_subject.is_a?(String)
          @given_subject == @expected_subject
        else
          !!(@given_subject =~ @expected_subject)
        end
      end

      def failure_message
        if @expected_subject.is_a?(String)
          "expected the subject to be #{@expected_subject.inspect} but was #{@given_subject.inspect}"
        else
          "expected the subject to match #{@expected_subject.inspect}, but did not.  Actual subject was: #{@given_subject.inspect}"
        end
      end

      def failure_message_when_negated
        if @expected_subject.is_a?(String)
          "expected the subject not to be #{@expected_subject.inspect} but was"
        else
          "expected the subject not to match #{@expected_subject.inspect} but #{@given_subject.inspect} does match it."
        end
      end
      alias negative_failure_message failure_message_when_negated
    end

    def have_subject(subject)
      HaveSubject.new(subject)
    end

    class IncludeEmailWithSubject

      def initialize(subject)
        @expected_subject = subject
      end

      def description
        if @expected_subject.is_a?(String)
          "include email with subject of #{@expected_subject.inspect}"
        else
          "include email with subject matching #{@expected_subject.inspect}"
        end
      end

      def matches?(emails)
        @given_emails = emails
        if @expected_subject.is_a?(String)
          @given_emails.map(&:subject).include?(@expected_subject)
        else
          !!(@given_emails.any?{ |mail| mail.subject =~ @expected_subject })
        end
      end

      def failure_message
        if @expected_subject.is_a?(String)
          "expected at least one email to have the subject #{@expected_subject.inspect} but none did. Subjects were #{@given_emails.map(&:subject).inspect}"
        else
          "expected at least one email to have a subject matching #{@expected_subject.inspect}, but none did. Subjects were #{@given_emails.map(&:subject).inspect}"
        end
      end

      def failure_message_when_negated
        if @expected_subject.is_a?(String)
          "expected no email with the subject #{@expected_subject.inspect} but found at least one. Subjects were #{@given_emails.map(&:subject).inspect}"
        else
          "expected no email to have a subject matching #{@expected_subject.inspect} but found at least one. Subjects were #{@given_emails.map(&:subject).inspect}"
        end
      end
      alias negative_failure_message failure_message_when_negated
    end

    def include_email_with_subject(*emails)
      IncludeEmailWithSubject.new(emails.flatten.first)
    end

    class HaveBodyText

      def initialize(text)
        @expected_text = text
      end

      def description
        if @expected_text.is_a?(String)
          "have body including #{@expected_text.inspect}"
        else
          "have body matching #{@expected_text.inspect}"
        end
      end

      def matches?(email)
        if @expected_text.is_a?(String)
          @given_text = email.default_part_body.to_s.gsub(/\s+/, " ")
          @expected_text = @expected_text.gsub(/\s+/, " ")
          @given_text.include?(@expected_text)
        else
          @given_text = email.default_part_body.to_s
          !!(@given_text =~ @expected_text)
        end
      end

      def failure_message
        if @expected_text.is_a?(String)
          "expected the body to contain #{@expected_text.inspect} but was #{@given_text.inspect}"
        else
          "expected the body to match #{@expected_text.inspect}, but did not.  Actual body was: #{@given_text.inspect}"
        end
      end

      def failure_message_when_negated
        if @expected_text.is_a?(String)
          "expected the body not to contain #{@expected_text.inspect} but was #{@given_text.inspect}"
        else
          "expected the body not to match #{@expected_text.inspect} but #{@given_text.inspect} does match it."
        end
      end
      alias negative_failure_message failure_message_when_negated
    end

    def have_body_text(text)
      HaveBodyText.new(text)
    end

    class HaveHeader

      def initialize(name, value)
        @expected_name, @expected_value = name, value
      end

      def description
        if @expected_value.is_a?(String)
          "have header #{@expected_name}: #{@expected_value}"
        else
          "have header #{@expected_name} with value matching #{@expected_value.inspect}"
        end
      end

      def matches?(email)
        @given_header = email.header

        if @expected_value.is_a?(String)
          @given_header[@expected_name].to_s == @expected_value
        else
          @given_header[@expected_name].to_s =~ @expected_value
        end      end

      def failure_message
        if @expected_value.is_a?(String)
          "expected the headers to include '#{@expected_name}: #{@expected_value}' but they were #{mail_headers_hash(@given_header).inspect}"
        else
          "expected the headers to include '#{@expected_name}' with a value matching #{@expected_value.inspect} but they were #{mail_headers_hash(@given_header).inspect}"
        end
      end

      def failure_message_when_negated
        if @expected_value.is_a?(String)
          "expected the headers not to include '#{@expected_name}: #{@expected_value}' but they were #{mail_headers_hash(@given_header).inspect}"
        else
          "expected the headers not to include '#{@expected_name}' with a value matching #{@expected_value.inspect} but they were #{mail_headers_hash(@given_header).inspect}"
        end
      end
      alias negative_failure_message failure_message_when_negated

      def mail_headers_hash(email_headers)
        email_headers.fields.inject({}) { |hash, field| hash[field.field.class::FIELD_NAME] = field.to_s; hash }
      end
    end

    def have_header(name, value)
      HaveHeader.new(name, value)
    end

    def self.included base
      if base.respond_to? :register_matcher
        instance_methods.each do |name|
          base.register_matcher name, name
        end
      end
    end
  end
end