-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_pm_sprint_com.rb
319 lines (270 loc) · 10.6 KB
/
test_pm_sprint_com.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
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
require "test_helper"
class TestPmSprintCom < Test::Unit::TestCase
include MMS2R::TestHelper
def mock_sprint_image(message_id)
response = mock('response')
body = mock('body')
connection = mock('connection')
response.expects(:content_type).twice.returns('image/jpeg')
response.expects(:body).returns(body)
response.expects(:code).never
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).once.returns connection
connection.expects(:get2).with(
# 1.9.2
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?inviteToken=PE5rJ5PdYzzwk7V7zoXU&outquality=90&ext=.jpg&iconifyVideo=true&wm=1'
# 1.8.7
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?wm=1&ext=.jpg&outquality=90&iconifyVideo=true&inviteToken=PE5rJ5PdYzzwk7V7zoXU'
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).once.returns(response)
end
def mock_sprint_purged_image(message_id)
response = mock('response')
body = mock('body')
connection = mock('connection')
response.expects(:content_type).once.returns('text/html')
response.expects(:body).returns(body)
response.expects(:code).once.returns('500')
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).once.returns connection
connection.expects(:get2).with(
# 1.9.2
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?inviteToken=PE5rJ5PdYzzwk7V7zoXU&outquality=90&ext=.jpg&iconifyVideo=true&wm=1'
# 1.8.7
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?wm=1&ext=.jpg&outquality=90&iconifyVideo=true&inviteToken=PE5rJ5PdYzzwk7V7zoXU'
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).once.returns(response)
end
def mock_sprint_ajax
response = mock('response')
body = mock('body')
# this is a response from a real sprint message
file = File.open("./test/fixtures/sprint-ajax-response-success.json", "rb")
json = file.read
body.expects(:to_str).returns json
connection = mock('connection')
connection.stubs(:use_ssl=).returns(true)
response.expects(:body).twice.returns(body)
response.expects(:code).once.returns('200')
response.expects(:content_type).twice.returns('text/html')
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).twice.returns connection
connection.expects(:get2).with(
# 1.9.2
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?inviteToken=PE5rJ5PdYzzwk7V7zoXU&outquality=90&ext=.jpg&iconifyVideo=true&wm=1'
# 1.8.7
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?wm=1&ext=.jpg&outquality=90&iconifyVideo=true&inviteToken=PE5rJ5PdYzzwk7V7zoXU'
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).twice.returns(response)
end
def mock_sprint_ajax_purged
response = mock('response')
body = mock('body')
# this is a response from a real sprint message
file = File.open("./test/fixtures/sprint-ajax-response-failure.html", "rb")
error_html = file.read
body.expects(:to_str).returns error_html
connection = mock('connection')
connection.stubs(:use_ssl=).returns(true)
response.expects(:body).twice.returns(body)
response.expects(:code).once.returns('500')
response.expects(:content_type).once.returns('text/html')
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).twice.returns connection
connection.expects(:get2).with(
# 1.9.2
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?inviteToken=PE5rJ5PdYzzwk7V7zoXU&outquality=90&ext=.jpg&iconifyVideo=true&wm=1'
# 1.8.7
# '//mmps/RECIPIENT/001_2066c7013e7ca833_1/2?wm=1&ext=.jpg&outquality=90&iconifyVideo=true&inviteToken=PE5rJ5PdYzzwk7V7zoXU'
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).twice.returns(response)
end
def test_mms_should_have_text
mail = mail('sprint-text-01.mail')
mms = MMS2R::Media.new(mail)
assert_equal "2068765309", mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
assert_equal 1, mms.media['text/plain'].size
file = mms.media['text/plain'].first
assert_equal true, File.exist?(file)
assert_match(/\.txt$/, File.basename(file))
assert_equal "Tea Pot", IO.read(file)
mms.purge
end
def test_mms_should_have_a_phone_number
mail = mail('sprint-image-01.mail')
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
mms.purge
end
def test_should_have_simple_video
mail = mail('sprint-video-01.mail')
response = mock('response')
body = mock('body')
connection = mock('connection')
response.expects(:content_type).twice.returns('video/quicktime')
response.expects(:body).returns(body)
response.expects(:code).never
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).once.returns connection
connection.expects(:get2).with(
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).once.returns(response)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
assert_nil mms.media['text/plain']
assert_nil mms.media['text/html']
assert_not_nil mms.media['video/quicktime']
assert_equal 1, mms.media['video/quicktime'].size
assert_equal "000_259e41e851be9b1d_1-0.mov", File.basename(mms.media['video/quicktime'][0])
mms.purge
end
def test_should_have_simple_image
mail = mail('sprint-image-01.mail')
mock_sprint_image(mail.message_id)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
assert_nil mms.media['text/plain']
assert_nil mms.media['text/html']
assert_not_nil mms.media['image/jpeg'][0]
assert_match(/001_2066c7013e7ca833_1-0.jpg$/, mms.media['image/jpeg'][0])
mms.purge
end
def test_collect_image_using_block
mail = mail('sprint-image-01.mail')
mock_sprint_image(mail.message_id)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
file_array = nil
mms.process do |k, v|
file_array = v if (k == 'image/jpeg')
assert_equal 1, file_array.size
file = file_array.first
assert_not_nil file = file_array.first
assert_equal "001_2066c7013e7ca833_1-0.jpg", File.basename(file)
end
mms.purge
end
def test_process_internals_should_only_be_executed_once
mail = mail('sprint-image-01.mail')
mock_sprint_image(mail.message_id)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
# second time through shouldn't go into the was_processed block
mms.mail.expects(:parts).never
mms.process{|k, v| }
mms.purge
end
def test_should_have_two_images
mail = mail('sprint-two-images-01.mail')
response = mock('response')
body = mock('body')
connection = mock('connection')
response.expects(:content_type).times(4).returns('image/jpeg')
response.expects(:body).twice.returns(body)
response.expects(:code).never
Net::HTTP.expects(:new).with('pictures.sprintpcs.com', 80).twice.returns connection
connection.expects(:get2).with(
kind_of(String),
{ "User-Agent" => MMS2R::Media::USER_AGENT }
).twice.returns(response)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 1, mms.media.size
assert_nil mms.media['text/plain']
assert_nil mms.media['text/html']
assert_equal 2, mms.media['image/jpeg'].size
assert_not_nil mms.media['image/jpeg'][0]
assert_not_nil mms.media['image/jpeg'][1]
assert_match(/001_104058d23d79fb6a_1-0.jpg$/, mms.media['image/jpeg'][0])
assert_match(/001_104058d23d79fb6a_1-1.jpg$/, mms.media['image/jpeg'][1])
mms.purge
end
def test_image_should_be_missing
# this test is questionable
mail = mail('sprint-broken-image-01.mail')
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 0, mms.media.size
mms.purge
end
def test_message_is_missing_in_mail
# this test is questionable
mail = mail('sprint-image-missing-message.mail')
mock_sprint_ajax
mms = MMS2R::Media.new(mail)
assert_equal 2, mms.media['text/plain'].size
# test that the message was extracted from the ajax response
message = IO.read(mms.media['text/plain'].first)
assert_equal "First text content.", message
# test that the was removed ()
assert message.last.bytes.to_a != [194, 160]
mms.purge
end
def test_message_is_missing_in_mail_purged_from_content_server
# this test is questionable
mail = mail('sprint-image-missing-message.mail')
mock_sprint_ajax_purged
mms = MMS2R::Media.new(mail)
assert_equal '5135455555', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 0, mms.media.size
mms.purge
end
def test_image_should_be_purged_from_content_server
mail = mail('sprint-image-01.mail')
mock_sprint_purged_image(mail.message_id)
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal 0, mms.media.size
mms.purge
end
def test_body_should_return_empty_when_there_is_no_user_text
mail = mail('sprint-image-01.mail')
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal "", mms.body
end
def test_sprint_write_file
mail = mock(:message_id => 'a')
mail.expects(:from).at_least_once.returns(['joe@pm.sprint.com'])
mail.expects(:return_path).at_least_once.returns('joe@pm.sprint.com')
mms = MMS2R::Media.new(mail, :process => :lazy)
type = 'text/plain'
content = 'foo'
file = Tempfile.new('sprint')
file.close
type, file = mms.send(:sprint_write_file, type, content, file.path)
assert_equal 'text/plain', type
assert_equal content, IO.read(file)
end
def test_subject
mail = mail('sprint-image-01.mail')
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal "", mms.subject
end
def test_new_subject
mail = mail('sprint-new-image-01.mail')
mock_sprint_ajax
mms = MMS2R::Media.new(mail)
assert_equal '2068675309', mms.number
assert_equal "pm.sprint.com", mms.carrier
assert_equal "", mms.subject
end
end