forked from mikel/mail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_helper.rb
258 lines (202 loc) · 4.22 KB
/
spec_helper.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
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
# encoding: utf-8
require File.expand_path('../environment', __FILE__)
unless defined?(MAIL_ROOT)
STDERR.puts("Running Specs under Ruby Version #{RUBY_VERSION}")
MAIL_ROOT = File.join(File.dirname(__FILE__), '../')
end
unless defined?(SPEC_ROOT)
SPEC_ROOT = File.join(File.dirname(__FILE__))
end
require File.join(File.dirname(__FILE__), 'matchers', 'break_down_to')
require 'mail'
Spec::Runner.configure do |config|
config.include(CustomMatchers)
end
def fixture(*name)
File.join(SPEC_ROOT, 'fixtures', name)
end
alias doing lambda
# Produces an array or printable ascii by default.
#
# We can assume if a, m and z and 1, 5, 0 work, then the rest
# of the letters and numbers work.
def ascii(from = 33, to = 126)
chars = []
from.upto(to) { |c| chars << ('' << c) }
boring = ('b'..'l').to_a + ('n'..'o').to_a +
('p'..'y').to_a + ('B'..'L').to_a + ('N'..'O').to_a +
('P'..'Y').to_a + ('1'..'4').to_a + ('6'..'8').to_a
chars - boring
end
# Original mockup from ActionMailer
class MockSMTP
def self.deliveries
@@deliveries
end
def initialize
@@deliveries = []
end
def sendmail(mail, from, to)
@@deliveries << [mail, from, to]
'OK'
end
def start(*args)
if block_given?
return yield(self)
else
return self
end
end
def finish
return true
end
def self.clear_deliveries
@@deliveries = []
end
# in the standard lib: net/smtp.rb line 577
# a TypeError is thrown unless this arg is a
# kind of OpenSSL::SSL::SSLContext
def enable_tls(context = nil)
if context && context.kind_of?(OpenSSL::SSL::SSLContext)
true
elsif context
raise TypeError,
"wrong argument (#{context.class})! "+
"(Expected kind of OpenSSL::SSL::SSLContext)"
end
end
def enable_starttls_auto
true
end
end
class Net::SMTP
def self.new(*args)
MockSMTP.new
end
end
class MockPopMail
def initialize(rfc2822, number)
@rfc2822 = rfc2822
@number = number
@deleted = false
end
def pop
@rfc2822
end
def number
@number
end
def to_s
"#{number}: #{pop}"
end
def delete
@deleted = true
end
def deleted?
@deleted
end
end
class MockPOP3
@@start = false
def initialize
@@popmails = []
20.times do |i|
# "test00", "test01", "test02", ..., "test19"
@@popmails << MockPopMail.new("test#{i.to_s.rjust(2, '0')}", i)
end
end
def self.popmails
@@popmails.clone
end
def each_mail(*args)
@@popmails.each do |popmail|
yield popmail
end
end
def mails(*args)
@@popmails.clone
end
def start(*args)
@@start = true
block_given? ? yield(self) : self
end
def enable_ssl(*args)
true
end
def started?
@@start == true
end
def self.started?
@@start == true
end
def reset
end
def finish
@@start = false
end
def delete_all
@@popmails = []
end
end
require 'net/pop'
class Net::POP3
def self.new(*args)
MockPOP3.new
end
end
class MockIMAPFetchData
attr_reader :attr, :number
def initialize(rfc822, number)
@attr = { 'RFC822' => rfc822 }
@number = number
end
end
class MockIMAP
@@connection = false
@@mailbox = nil
@@marked_for_deletion = []
def self.examples
@@examples
end
def initialize
@@examples = []
(0..19).each do |i|
@@examples << MockIMAPFetchData.new("test#{i.to_s.rjust(2, '0')}", i)
end
end
def login(user, password)
@@connection = true
end
def disconnect
@@connection = false
end
def select(mailbox)
@@mailbox = mailbox
end
def uid_search(keys, charset=nil)
[*(0..@@examples.size - 1)]
end
def uid_fetch(set, attr)
[@@examples[set]]
end
def uid_store(set, attr, flags)
if attr == "+FLAGS" && flags.include?(Net::IMAP::DELETED)
@@marked_for_deletion << set
end
end
def expunge
@@marked_for_deletion.reverse.each do |i| # start with highest index first
@@examples.delete_at(i)
end
@@marked_for_deletion = []
end
def self.mailbox; @@mailbox end # test only
def self.disconnected?; @@connection == false end
def disconnected?; @@connection == false end
end
require 'net/imap'
class Net::IMAP
def self.new(*args)
MockIMAP.new
end
end