File: imap_spec.rb

package info (click to toggle)
ruby-mail 2.6.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,256 kB
  • ctags: 1,327
  • sloc: ruby: 44,678; makefile: 3
file content (260 lines) | stat: -rw-r--r-- 8,669 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
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'

describe "IMAP Retriever" do

  before(:each) do
    Mail.defaults do
      retriever_method :imap, { :address    => 'localhost',
                                :port       => 993,
                                :user_name  => nil,
                                :password   => nil,
                                :enable_ssl => true }
    end
  end

  describe "find with and without block" do
    it "should find all emails with a given block" do
      expect(MockIMAP).to be_disconnected

      messages = []
      Mail.all do |message|
        messages << message
      end
      expect(messages.map { |m| m.raw_source }.sort).to eq MockIMAP.examples.map { |m| m.attr['RFC822']}.sort

      expect(MockIMAP).to be_disconnected
    end
    it "should get all emails without a given block" do
      expect(MockIMAP).to be_disconnected

      messages = Mail.all
      expect(messages.map { |m| m.raw_source }.sort).to eq MockIMAP.examples.map { |m| m.attr['RFC822']}.sort

      expect(MockIMAP).to be_disconnected
    end
    it "should get all emails and yield the imap, uid, and email when given a block of arity 3" do
      expect(MockIMAP).to be_disconnected

      messages = []
      uids = []
      Mail.all do |message, imap, uid|
        expect(MockIMAP).to be === imap
        messages << message
        uids << uid
      end
      expect(messages.map { |m| m.raw_source }.sort).to eq MockIMAP.examples.map { |m| m.attr['RFC822']}.sort
      expect(uids.sort).to eq MockIMAP.examples.map { |m| m.number }.sort

      expect(MockIMAP).to be_disconnected
    end
  end

  describe "find and options" do
    it "should handle the :count option" do
      messages = Mail.find(:count => :all, :what => :last, :order => :asc)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }

      message = Mail.find(:count => 1, :what => :last)
      expect(message.raw_source).to eq MockIMAP.examples.last.attr['RFC822']

      messages = Mail.find(:count => 2, :what => :last, :order => :asc)
      expect(messages[0..1].map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }[-2..-1]
    end
    it "should handle the :what option" do
      messages = Mail.find(:count => :all, :what => :last)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }

      messages = Mail.find(:count => 2, :what => :first, :order => :asc)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }[0..1]
    end
    it "should handle the :order option" do
      messages = Mail.find(:order => :desc, :count => 5, :what => :last)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }[-5..-1].reverse

      messages = Mail.find(:order => :asc, :count => 5, :what => :last)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }[-5..-1]
    end
    it "should handle the :mailbox option" do
      Mail.find(:mailbox => 'SOME-RANDOM-MAILBOX')

      expect(MockIMAP.mailbox).to eq 'SOME-RANDOM-MAILBOX'
    end
    it "should handle the :uid option" do
      messages = Mail.find(:uid => 1)

      expect(messages[0].raw_source).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }[1]
    end
    it "should find the last 10 messages by default" do
      messages = Mail.find

      expect(messages.size).to eq 10
    end
    it "should search the mailbox 'INBOX' by default" do
      Mail.find

      expect(MockIMAP.mailbox).to eq 'INBOX'
    end

    it "should handle the delete_after_find_option" do
      Mail.find(:delete_after_find => false)
      expect(MockIMAP.examples.size).to eq 20

      Mail.find(:delete_after_find => true)
      expect(MockIMAP.examples.size).to eq 10

      Mail.find(:delete_after_find => true) { |message| }
      expect(MockIMAP.examples.size).to eq 10
    end

    it "should handle the find_and_delete method" do
      Mail.find_and_delete(:count => 15)
      expect(MockIMAP.examples.size).to eq 5
    end

  end

  describe "last" do
    it "should find the last received messages" do
      messages = Mail.last(:count => 5)

      expect(messages).to be_instance_of(Array)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822']}[-5..-1]
    end
    it "should find the last received message" do
      message = Mail.last

      expect(message.raw_source).to eq MockIMAP.examples.last.attr['RFC822']
    end
  end

  describe "first" do
    it "should find the first received messages" do
      messages = Mail.first(:count => 5)

      expect(messages).to be_instance_of(Array)
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822']}[0..4]
    end
    it "should find the first received message" do
      message = Mail.first

      expect(message.raw_source).to eq MockIMAP.examples.first.attr['RFC822']
    end
  end

  describe "all" do
    it "should find all messages" do
      messages = Mail.all

      expect(messages.size).to eq MockIMAP.examples.size
      expect(messages.map { |m| m.raw_source }).to eq MockIMAP.examples.map { |m| m.attr['RFC822'] }
    end
  end

  describe "delete_all" do
    it "should delete all messages" do
      Mail.all

      expect(Net::IMAP).to receive(:encode_utf7).once
      Mail.delete_all

      expect(MockIMAP.examples.size).to eq 0
    end
  end

  describe "connection" do
    it "should raise an Error if no block is given" do
      expect { Mail.connection { |m| raise ArgumentError.new } }.to raise_error(ArgumentError)
    end
    it "should yield the connection object to the given block" do
      Mail.connection do |connection|
        expect(connection).to be_an_instance_of(MockIMAP)
      end
    end
  end

  describe "handling of options" do
    it "should set default options" do
      retrievable = Mail::IMAP.new({})
      options = retrievable.send(:validate_options, {})

      expect(Mail::Utilities.blank?(options[:count])).not_to be_truthy
      expect(options[:count]).to eq 10

      expect(Mail::Utilities.blank?(options[:order])).not_to be_truthy
      expect(options[:order]).to eq :asc

      expect(Mail::Utilities.blank?(options[:what])).not_to be_truthy
      expect(options[:what]).to eq :first

      expect(Mail::Utilities.blank?(options[:mailbox])).not_to be_truthy
      expect(options[:mailbox]).to eq 'INBOX'
    end
    it "should not replace given configuration" do
      retrievable = Mail::IMAP.new({})
      options = retrievable.send(:validate_options, {
        :mailbox => 'some/mail/box',
        :count => 2,
        :order => :asc,
        :what => :first
      })

      expect(Mail::Utilities.blank?(options[:count])).not_to be_truthy
      expect(options[:count]).to eq 2

      expect(Mail::Utilities.blank?(options[:order])).not_to be_truthy
      expect(options[:order]).to eq :asc

      expect(Mail::Utilities.blank?(options[:what])).not_to be_truthy
      expect(options[:what]).to eq :first

      expect(Mail::Utilities.blank?(options[:mailbox])).not_to be_truthy
      expect(options[:mailbox]).to eq 'some/mail/box'
    end
    it "should ensure utf7 conversion for mailbox names" do
      retrievable = Mail::IMAP.new({})

      expect(Net::IMAP).to receive(:encode_utf7) { 'UTF7_STRING' }
      options = retrievable.send(:validate_options, {
        :mailbox => 'UTF8_STRING'
      })
      expect(options[:mailbox]).to eq 'UTF7_STRING'
    end
  end

  describe "error handling" do
    it "should finish the IMAP connection if an exception is raised" do
      expect(MockIMAP).to be_disconnected

      expect { Mail.all { |m| raise ArgumentError.new } }.to raise_error(ArgumentError)

      expect(MockIMAP).to be_disconnected
    end
  end

  describe "authentication mechanism" do
    before(:each) do
      @imap = MockIMAP.new
      allow(MockIMAP).to receive(:new).and_return(@imap)
    end
    it "should be login by default" do
      expect(@imap).not_to receive(:authenticate)
      expect(@imap).to receive(:login).with('foo', 'secret')
      Mail.defaults do
        retriever_method :imap, {:user_name => 'foo', :password => 'secret'}
      end
      Mail.find
    end
    it "should be changeable" do
      expect(@imap).to receive(:authenticate).with('CRAM-MD5', 'foo', 'secret')
      expect(@imap).not_to receive(:login)
      Mail.defaults do
        retriever_method :imap, {:authentication => 'CRAM-MD5', :user_name => 'foo', :password => 'secret'}
      end
      Mail.find
    end
  end

end