brendanlim-mms2r 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. data/.gitignore +3 -0
  2. data/History.txt +292 -0
  3. data/Manifest.txt +169 -0
  4. data/README.txt +202 -0
  5. data/Rakefile +36 -0
  6. data/conf/1nbox.net.yml +13 -0
  7. data/conf/aliases.yml +17 -0
  8. data/conf/bellsouth.net.yml +2 -0
  9. data/conf/from.yml +2 -0
  10. data/conf/mediamessaging.o2.co.uk.yml +4 -0
  11. data/conf/messaging.nextel.com.yml +10 -0
  12. data/conf/mms.3ireland.ie.yml +17 -0
  13. data/conf/mms.ae.yml +5 -0
  14. data/conf/mms.alltel.com.yml +15 -0
  15. data/conf/mms.att.net.yml +11 -0
  16. data/conf/mms.dobson.net.yml +4 -0
  17. data/conf/mms.luxgsm.lu.yml +13 -0
  18. data/conf/mms.mobileiam.ma.yml +16 -0
  19. data/conf/mms.mtn.co.za.yml +13 -0
  20. data/conf/mms.mycricket.com.yml +4 -0
  21. data/conf/mms.myhelio.com.yml +12 -0
  22. data/conf/mms.netcom.no.yml +4 -0
  23. data/conf/mms.o2online.de.yml +8 -0
  24. data/conf/mms.three.co.uk.yml +15 -0
  25. data/conf/mms.vodacom4me.co.za.yml +13 -0
  26. data/conf/mms2r_media.yml +24 -0
  27. data/conf/mobile.indosat.net.id.yml +14 -0
  28. data/conf/msg.telus.com.yml +6 -0
  29. data/conf/orangemms.net.yml +13 -0
  30. data/conf/pm.sprint.com.yml +12 -0
  31. data/conf/pxt.vodafone.net.nz.yml +5 -0
  32. data/conf/rci.rogers.com.yml +18 -0
  33. data/conf/sms.sasktel.com.yml +12 -0
  34. data/conf/tmomail.net.yml +28 -0
  35. data/conf/txt.bell.ca.yml +4 -0
  36. data/conf/unicel.com.yml +6 -0
  37. data/conf/vmpix.com.yml +11 -0
  38. data/conf/vzwpix.com.yml +8 -0
  39. data/conf/waw.plspictures.com.yml +15 -0
  40. data/dev_tools/anonymizer.rb +20 -0
  41. data/dev_tools/debug_sprint_hpricot_parsing.rb +82 -0
  42. data/dev_tools/github.rb +8 -0
  43. data/lib/mms2r.rb +58 -0
  44. data/lib/mms2r/media.rb +746 -0
  45. data/lib/mms2r/media/sprint.rb +189 -0
  46. data/lib/tmail_ext.rb +17 -0
  47. data/mms2r.gemspec +41 -0
  48. data/test/fixtures/1nbox-2images-01.mail +738 -0
  49. data/test/fixtures/1nbox-2images-02.mail +736 -0
  50. data/test/fixtures/1nbox-2images-03.mail +736 -0
  51. data/test/fixtures/1nbox-2images-04.mail +732 -0
  52. data/test/fixtures/3ireland-mms-01.mail +671 -0
  53. data/test/fixtures/alltel-image-01.mail +319 -0
  54. data/test/fixtures/alltel-mms-01.mail +270 -0
  55. data/test/fixtures/alltel-mms-03.mail +270 -0
  56. data/test/fixtures/apple-double-image-01.mail +53 -0
  57. data/test/fixtures/att-blackberry-02.mail +54 -0
  58. data/test/fixtures/att-blackberry.mail +60 -0
  59. data/test/fixtures/att-image-01.mail +37 -0
  60. data/test/fixtures/att-image-02.mail +37 -0
  61. data/test/fixtures/att-iphone-01.mail +73 -0
  62. data/test/fixtures/att-iphone-02.mail +61 -0
  63. data/test/fixtures/att-iphone-03.mail +61 -0
  64. data/test/fixtures/att-text-01.mail +27 -0
  65. data/test/fixtures/bell-canada-image-01.mail +2273 -0
  66. data/test/fixtures/cingularme-text-01.mail +13 -0
  67. data/test/fixtures/cingularme-text-02.mail +14 -0
  68. data/test/fixtures/dobson-image-01.mail +64 -0
  69. data/test/fixtures/dot.jpg +0 -0
  70. data/test/fixtures/generic.mail +50 -0
  71. data/test/fixtures/helio-image-01.mail +348 -0
  72. data/test/fixtures/helio-message-01.mail +327 -0
  73. data/test/fixtures/iconv-fr-text-01.mail +50 -0
  74. data/test/fixtures/indosat-image-01.mail +751 -0
  75. data/test/fixtures/indosat-image-02.mail +751 -0
  76. data/test/fixtures/info2go-image-01.mail +43 -0
  77. data/test/fixtures/luxgsm-image-01.mail +388 -0
  78. data/test/fixtures/maroctelecom-france-mms-01.mail +511 -0
  79. data/test/fixtures/mediamessaging_o2_co_uk-image-01.mail +61 -0
  80. data/test/fixtures/mmode-image-01.mail +36 -0
  81. data/test/fixtures/mms.ae-image-01.mail +45 -0
  82. data/test/fixtures/mms.mycricket.com-pic-and-text.mail +87 -0
  83. data/test/fixtures/mms.mycricket.com-pic.mail +78 -0
  84. data/test/fixtures/mmsreply.t-mobile.co.uk-text-image-01.mail +431 -0
  85. data/test/fixtures/mobile.mycingular.com-text-01.mail +25 -0
  86. data/test/fixtures/mtn-southafrica-mms.mail +127 -0
  87. data/test/fixtures/mycingular-image-01.mail +66 -0
  88. data/test/fixtures/netcom-image-01.mail +63 -0
  89. data/test/fixtures/nextel-image-01.mail +46 -0
  90. data/test/fixtures/nextel-image-02.mail +43 -0
  91. data/test/fixtures/nextel-image-03.mail +44 -0
  92. data/test/fixtures/nextel-image-04.mail +43 -0
  93. data/test/fixtures/o2-de-image-01.mail +134 -0
  94. data/test/fixtures/orange-uk-image-01.mail +71 -0
  95. data/test/fixtures/orangefrance-text-and-image.mail +68 -0
  96. data/test/fixtures/orangepoland-text-01.mail +7 -0
  97. data/test/fixtures/orangepoland-text-02.mail +7 -0
  98. data/test/fixtures/pics.cingularme.com-image-01.mail +44 -0
  99. data/test/fixtures/pxt-image-01.mail +118 -0
  100. data/test/fixtures/rogers-canada-mms-01.mail +1029 -0
  101. data/test/fixtures/sasktel-image-01.mail +1791 -0
  102. data/test/fixtures/sprint-broken-image-01.mail +195 -0
  103. data/test/fixtures/sprint-image-01.mail +211 -0
  104. data/test/fixtures/sprint-pcs-text-01.mail +8 -0
  105. data/test/fixtures/sprint-purged-image-01.mail +195 -0
  106. data/test/fixtures/sprint-text-01.mail +195 -0
  107. data/test/fixtures/sprint-two-images-01.mail +231 -0
  108. data/test/fixtures/sprint-video-01.mail +210 -0
  109. data/test/fixtures/sprint.mov +0 -0
  110. data/test/fixtures/suncom-blackberry.mail +43 -0
  111. data/test/fixtures/telus-image-01.mail +89 -0
  112. data/test/fixtures/three-uk-image-01.mail +684 -0
  113. data/test/fixtures/tmo.blackberry.net-image-01.mail +48 -0
  114. data/test/fixtures/tmobile-blackberry-02.mail +50 -0
  115. data/test/fixtures/tmobile-blackberry.mail +44 -0
  116. data/test/fixtures/tmobile-image-01.mail +145 -0
  117. data/test/fixtures/tmobile-image-02.mail +145 -0
  118. data/test/fixtures/unicel-image-01.mail +60 -0
  119. data/test/fixtures/verizon-blackberry.mail +53 -0
  120. data/test/fixtures/verizon-image-01.mail +44 -0
  121. data/test/fixtures/verizon-image-02.mail +50 -0
  122. data/test/fixtures/verizon-image-03.mail +53 -0
  123. data/test/fixtures/verizon-text-01.mail +10 -0
  124. data/test/fixtures/verizon-video-01.mail +336 -0
  125. data/test/fixtures/virgin-mobile-image-01.mail +73 -0
  126. data/test/fixtures/virgin.ca-text-01.mail +21 -0
  127. data/test/fixtures/vodacom4me-co-za-01.mail +104 -0
  128. data/test/fixtures/vodacom4me-co-za-02.mail +108 -0
  129. data/test/fixtures/vodacom4me-southafrica-mms-01.mail +152 -0
  130. data/test/fixtures/vodacom4me-southafrica-mms-04.mail +655 -0
  131. data/test/fixtures/vtext-text-01.mail +10 -0
  132. data/test/fixtures/waw.plspictures.com-image-01.mail +1611 -0
  133. data/test/test_1nbox_net.rb +78 -0
  134. data/test/test_bell_canada.rb +44 -0
  135. data/test/test_bellsouth_net.rb +22 -0
  136. data/test/test_github.rb +12 -0
  137. data/test/test_helper.rb +58 -0
  138. data/test/test_mediamessaging_o2_co_uk.rb +52 -0
  139. data/test/test_messaging_nextel_com.rb +93 -0
  140. data/test/test_messaging_sprintpcs_com.rb +26 -0
  141. data/test/test_mms2r_media.rb +760 -0
  142. data/test/test_mms_3ireland_ie.rb +25 -0
  143. data/test/test_mms_ae.rb +46 -0
  144. data/test/test_mms_alltel_com.rb +53 -0
  145. data/test/test_mms_att_net.rb +142 -0
  146. data/test/test_mms_dobson_net.rb +41 -0
  147. data/test/test_mms_luxgsm_lu.rb +58 -0
  148. data/test/test_mms_mobileiam_ma.rb +26 -0
  149. data/test/test_mms_mtn_co_za.rb +25 -0
  150. data/test/test_mms_mycricket_com.rb +60 -0
  151. data/test/test_mms_myhelio_com.rb +62 -0
  152. data/test/test_mms_netcom_no.rb +52 -0
  153. data/test/test_mms_o2online_de.rb +53 -0
  154. data/test/test_mms_three_co_uk.rb +42 -0
  155. data/test/test_mms_vodacom4me_co_za.rb +107 -0
  156. data/test/test_mobile_indosat_net_id.rb +52 -0
  157. data/test/test_msg_telus_com.rb +28 -0
  158. data/test/test_orangemms_net.rb +100 -0
  159. data/test/test_pm_sprint_com.rb +202 -0
  160. data/test/test_pxt_vodafone_net_nz.rb +37 -0
  161. data/test/test_rci_rogers_com.rb +28 -0
  162. data/test/test_sms_sasktel_com.rb +28 -0
  163. data/test/test_tmomail_net.rb +108 -0
  164. data/test/test_unicel_com.rb +49 -0
  165. data/test/test_vmpix_com.rb +56 -0
  166. data/test/test_vzwpix_com.rb +131 -0
  167. data/test/test_waw_plspictures_com.rb +27 -0
  168. data/vendor/plugins/mms2r/lib/autotest/discover.rb +3 -0
  169. data/vendor/plugins/mms2r/lib/autotest/mms2r.rb +38 -0
  170. metadata +288 -0
@@ -0,0 +1,78 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class Test1nboxNet < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_image_and_text_and_number
13
+ mail = TMail::Mail.parse(load_mail('1nbox-2images-01.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_equal "+919812345678", mms.number
17
+ assert_equal 2, mms.media.size
18
+
19
+ assert_not_nil mms.media['text/plain']
20
+ assert_equal 1, mms.media['text/plain'].size
21
+ assert_equal "testing123456789012", open(mms.media['text/plain'].first).read
22
+
23
+ assert_not_nil mms.media['image/jpeg']
24
+ assert_equal 1, mms.media['text/plain'].size
25
+ assert_match(/@003\.jpg$/, mms.media['image/jpeg'].first)
26
+
27
+ mms.purge
28
+ end
29
+
30
+ def test_image_with_no_plain_text
31
+ mail = TMail::Mail.parse(load_mail('1nbox-2images-02.mail').join)
32
+ mms = MMS2R::Media.new(mail)
33
+
34
+ assert_nil mms.media['text/plain']
35
+
36
+ assert_equal "+919898765432", mms.number
37
+ assert_equal 1, mms.media.size
38
+
39
+ assert_not_nil mms.media['image/jpeg']
40
+ assert_equal 1, mms.media['image/jpeg'].size
41
+ assert_match(/showpicture\.jpeg$/, mms.media['image/jpeg'].first)
42
+
43
+ mms.purge
44
+ end
45
+
46
+ def test_image_with_extra_chars
47
+ mail = TMail::Mail.parse(load_mail('1nbox-2images-03.mail').join)
48
+ mms = MMS2R::Media.new(mail)
49
+
50
+ assert_equal 1, mms.media.size
51
+ assert_nil mms.media['text/plain']
52
+
53
+ assert_not_nil mms.media['image/jpeg']
54
+ assert_equal 1, mms.media['image/jpeg'].size
55
+ assert_match(/=\?UTF-8\?Q\?Copy_of_test1\.jpg\?=/, mms.media['image/jpeg'].first)
56
+
57
+ mms.purge
58
+ end
59
+
60
+ def test_image_with_plain_email
61
+ mail = TMail::Mail.parse(load_mail('1nbox-2images-04.mail').join)
62
+ mms = MMS2R::Media.new(mail)
63
+
64
+ assert_equal 2, mms.media.size
65
+
66
+ assert_not_nil mms.media['text/plain']
67
+ assert_equal 1, mms.media['text/plain'].size
68
+ assert_equal "Hi,I'M TESTERS/MALE/23/IND/GOV.SRVC.01234567890.", open(mms.media['text/plain'].first).read
69
+
70
+ assert_equal "1234567890", mms.number
71
+
72
+ assert_not_nil mms.media['image/gif']
73
+ assert_equal 1, mms.media['image/gif'].size
74
+ assert_match(/@003\.gif$/, mms.media['image/gif'].first)
75
+
76
+ mms.purge
77
+ end
78
+ end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class TestBellCanada < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_image_and_text_and_number
13
+ mail = TMail::Mail.parse(load_mail('bell-canada-image-01.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_equal "2068675309", mms.number
17
+ assert_equal "A Picture/Video Message!", mms.subject
18
+
19
+ assert_equal 4, mms.media.size
20
+ assert_equal 1, mms.media['image/jpeg'].size
21
+ assert_equal 6, mms.media['image/gif'].size
22
+ assert_equal 1, mms.media['text/html'].size
23
+ assert_equal 1, mms.media['text/plain'].size
24
+
25
+ # make sure transform strips out massive dtd at start of doc
26
+ assert_equal 3331, File.size(mms.media['text/html'].first)
27
+
28
+ mms.purge
29
+ end
30
+
31
+
32
+ def test_default_media_should_return_user_generated_content
33
+ mail = TMail::Mail.parse(load_mail('bell-canada-image-01.mail').join)
34
+ mms = MMS2R::Media.new(mail)
35
+ file = mms.default_media
36
+
37
+ # make sure the users jpg is the one that we default to
38
+ assert_equal 31962, file.size
39
+ assert_equal '.jpg', File.extname(file.path)
40
+
41
+ mms.purge
42
+ end
43
+
44
+ end
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class TestBellsouthNet < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_image_from_blackberry
13
+ mail = TMail::Mail.parse(load_mail('suncom-blackberry.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_nil mms.media['text/plain']
17
+
18
+ assert_not_nil mms.media['image/jpeg'].first
19
+ assert_match(/Windows-1252\?B\?SU1HMDAwNjUuanBn/, mms.media['image/jpeg'].first)
20
+ mms.purge
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ # based on Trouble shooting section in http://gems.github.com/
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+
5
+ class TestGithub < Test::Unit::TestCase
6
+
7
+ def test_build_github_gem
8
+ result = `ruby #{File.join(File.dirname(__FILE__), "..", "dev_tools", "github.rb")}`
9
+ assert_equal 0, $?.exitstatus
10
+ end
11
+
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'set'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'pp'
5
+
6
+ module MMS2R
7
+ module TestHelper
8
+
9
+ def assert_file_size(file, size)
10
+ assert_not_nil(file, "file was nil")
11
+ assert(File::exist?(file), "file #{file} does not exist")
12
+ assert(File::size(file) == size, "file #{file} is #{File::size(file)} bytes, not #{size} bytes")
13
+ end
14
+
15
+ def load_mail(file)
16
+ IO.readlines(mail_fixture(file))
17
+ end
18
+
19
+ def mail_fixture(file)
20
+ "#{File.dirname(__FILE__)}/fixtures/#{file}"
21
+ end
22
+ end
23
+ end
24
+
25
+ class Hash
26
+ def except(*keys)
27
+ rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
28
+ reject { |key,| rejected.include?(key) }
29
+ end
30
+
31
+ def except!(*keys)
32
+ replace(except(*keys))
33
+ end
34
+ end
35
+
36
+ # monkey patch Net::HTTP so un caged requests don't go over the wire
37
+ module Net #:nodoc:
38
+ class HTTP #:nodoc:
39
+ alias :old_net_http_request :request
40
+ alias :old_net_http_connect :connect
41
+
42
+ def request(req, body = nil, &block)
43
+ prot = use_ssl ? "https" : "http"
44
+ uri_cls = use_ssl ? URI::HTTPS : URI::HTTP
45
+ query = req.path.split('?',2)
46
+ opts = {:host => self.address,
47
+ :port => self.port, :path => query[0]}
48
+ opts[:query] = query[1] if query[1]
49
+ uri = uri_cls.build(opts)
50
+ raise ArgumentError.new("#{req.method} method to #{uri} not being handled in testing")
51
+ end
52
+
53
+ def connect
54
+ raise ArgumentError.new("connect not being handled in testing")
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class TestMediamessagingO2CoUk < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_subject
13
+ mail = TMail::Mail.parse(load_mail('mediamessaging_o2_co_uk-image-01.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+ assert_equal "Office pic", mms.subject
16
+ mms.purge
17
+ end
18
+
19
+ def test_image
20
+ mail = TMail::Mail.parse(load_mail('mediamessaging_o2_co_uk-image-01.mail').join)
21
+ mms = MMS2R::Media.new(mail)
22
+
23
+ assert_equal 2, mms.media.size
24
+ assert_not_nil mms.media['text/plain']
25
+ assert_equal 1, mms.media['text/plain'].size
26
+ assert_not_nil mms.media['image/jpeg']
27
+ assert_equal 1, mms.media['image/jpeg'].size
28
+ assert mms.media['image/jpeg'].detect{|f| File.size(f) == 337}
29
+
30
+ mms.purge
31
+ end
32
+
33
+ def test_default_media_should_return_user_generated_content
34
+ mail = TMail::Mail.parse(load_mail('mediamessaging_o2_co_uk-image-01.mail').join)
35
+ mms = MMS2R::Media.new(mail)
36
+ file = mms.default_media
37
+ assert_equal 337, file.size
38
+
39
+ mms.purge
40
+ end
41
+
42
+ def test_default_text_should_return_user_generated_content
43
+ mail = TMail::Mail.parse(load_mail('mediamessaging_o2_co_uk-image-01.mail').join)
44
+ mms = MMS2R::Media.new(mail)
45
+ file = mms.default_text
46
+ assert_equal 10, file.size
47
+ assert_equal "Office pic", file.read
48
+
49
+ mms.purge
50
+ end
51
+
52
+ end
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class TestMessagingNextelCom < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_simple_text_is_nil
13
+ mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_nil mms.default_text
17
+
18
+ mms.purge
19
+ end
20
+
21
+ def test_simple_default_media
22
+ mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
23
+ mms = MMS2R::Media.new(mail)
24
+
25
+ file = mms.default_media
26
+ assert_file_size file, 337
27
+ assert_equal 'Jan15_0001.jpg', file.original_filename
28
+ assert_equal 337, file.size
29
+ assert_match(/Jan15_0001.jpg$/, file.local_path)
30
+
31
+ mms.purge
32
+ end
33
+
34
+ def test_simple_image1
35
+ mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
36
+ mms = MMS2R::Media.new(mail)
37
+
38
+ assert_equal 1, mms.media.size
39
+ assert_nil mms.media['text/plain']
40
+ assert_nil mms.media['text/html']
41
+ assert_not_nil mms.media['image/jpeg'][0]
42
+ assert_match(/Jan15_0001.jpg$/, mms.media['image/jpeg'][0])
43
+
44
+ assert_file_size mms.media['image/jpeg'][0], 337
45
+
46
+ mms.purge
47
+ end
48
+
49
+ def test_simple_image2
50
+ mail = TMail::Mail.parse(load_mail('nextel-image-02.mail').join)
51
+ mms = MMS2R::Media.new(mail)
52
+
53
+ assert_equal 1, mms.media.size
54
+ assert_nil mms.media['text/plain']
55
+ assert_nil mms.media['text/html']
56
+ assert_not_nil mms.media['image/jpeg'][0]
57
+ assert_match(/Mar12_0001.jpg$/, mms.media['image/jpeg'][0])
58
+
59
+ assert_file_size mms.media['image/jpeg'][0], 337
60
+
61
+ mms.purge
62
+ end
63
+
64
+ def test_simple_image3
65
+ mail = TMail::Mail.parse(load_mail('nextel-image-03.mail').join)
66
+ mms = MMS2R::Media.new(mail)
67
+
68
+ assert_equal 1, mms.media.size
69
+ assert_nil mms.media['text/plain']
70
+ assert_nil mms.media['text/html']
71
+ assert_not_nil mms.media['image/jpeg'][0]
72
+ assert_match(/Apr01_0001.jpg$/, mms.media['image/jpeg'][0])
73
+
74
+ assert_file_size mms.media['image/jpeg'][0], 337
75
+
76
+ mms.purge
77
+ end
78
+
79
+ def test_simple_image4
80
+ mail = TMail::Mail.parse(load_mail('nextel-image-04.mail').join)
81
+ mms = MMS2R::Media.new(mail)
82
+
83
+ assert_equal 1, mms.media.size
84
+ assert_nil mms.media['text/plain']
85
+ assert_nil mms.media['text/html']
86
+ assert_not_nil mms.media['image/jpeg'][0]
87
+ assert_match(/Mar20_0001.jpg$/, mms.media['image/jpeg'][0])
88
+
89
+ assert_file_size mms.media['image/jpeg'][0], 337
90
+
91
+ mms.purge
92
+ end
93
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
8
+
9
+ class TestMessagingSprintpcsCom < Test::Unit::TestCase
10
+ include MMS2R::TestHelper
11
+
12
+ def test_simple_text
13
+ mail = TMail::Mail.parse(load_mail('sprint-pcs-text-01.mail').join)
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_equal 1, mms.media.size
17
+ assert_not_nil mms.media['text/plain']
18
+ file = mms.media['text/plain'][0]
19
+ assert_not_nil file
20
+ assert File::exist?(file), "file #{file} does not exist"
21
+ text = IO.readlines("#{file}").join
22
+ assert_match(/hello world/, text)
23
+ mms.purge
24
+ end
25
+
26
+ end
@@ -0,0 +1,760 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'tempfile'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'mocha'
7
+ gem 'tmail', '>= 1.2.1'
8
+ require 'tmail'
9
+
10
+ class TestMms2rMedia < Test::Unit::TestCase
11
+ include MMS2R::TestHelper
12
+
13
+ class MMS2R::Media::NullCarrier < MMS2R::Media; end
14
+
15
+ def use_temp_dirs
16
+ MMS2R::Media.tmp_dir = @tmpdir
17
+ MMS2R::Media.conf_dir = @confdir
18
+ end
19
+
20
+ def setup
21
+ @tmpdir = File.join(Dir.tmpdir, "#{Time.now.to_i}-t")
22
+ FileUtils.mkdir_p(@tmpdir)
23
+ @confdir = File.join(Dir.tmpdir, "#{Time.now.to_i}-c")
24
+ FileUtils.mkdir_p(@confdir)
25
+
26
+ @oldtmpdir = MMS2R::Media.tmp_dir
27
+ @oldconfdir = MMS2R::Media.conf_dir
28
+ end
29
+
30
+ def teardown
31
+ FileUtils.rm_rf(@tmpdir)
32
+ FileUtils.rm_rf(@confdir)
33
+ MMS2R::Media.tmp_dir = @oldtmpdir
34
+ MMS2R::Media.conf_dir = @oldconfdir
35
+ end
36
+
37
+ def stub_mail(*keys)
38
+ attrs = {
39
+ :message_id => '123',
40
+ :from => ['joe@example.com'],
41
+ :multipart? => false,
42
+ :parts => [],
43
+ :main_type => 'text',
44
+ :content_type => 'text/plain',
45
+ :part_type? => 'text/plain',
46
+ :sub_header => 'message.txt',
47
+
48
+ :body => 'a',
49
+ :header => {}
50
+ }.except(keys)
51
+ stub('mail', attrs)
52
+ end
53
+
54
+ def temp_text_file(text)
55
+ tf = Tempfile.new("test" + Time.now.to_f.to_s)
56
+ tf.puts(text)
57
+ tf.close
58
+ tf.path
59
+ end
60
+
61
+ def test_tmp_dir
62
+ use_temp_dirs()
63
+ MMS2R::Media.tmp_dir = @tmpdir
64
+ assert_equal @tmpdir, MMS2R::Media.tmp_dir
65
+ end
66
+
67
+ def test_conf_dir
68
+ use_temp_dirs()
69
+ MMS2R::Media.conf_dir = @confdir
70
+ assert_equal @confdir, MMS2R::Media.conf_dir
71
+ end
72
+
73
+ def test_safe_message_id
74
+ mid1_b="1234abcd"
75
+ mid1_a="1234abcd"
76
+ mid2_b="<01234567.0123456789012.JavaMail.fooba@foo-bars999>"
77
+ mid2_a="012345670123456789012JavaMailfoobafoo-bars999"
78
+ assert_equal mid1_a, MMS2R::Media.safe_message_id(mid1_b)
79
+ assert_equal mid2_a, MMS2R::Media.safe_message_id(mid2_b)
80
+ end
81
+
82
+ def test_default_ext
83
+ assert_equal nil, MMS2R::Media.default_ext(nil)
84
+ assert_equal 'text', MMS2R::Media.default_ext('text')
85
+ assert_equal 'txt', MMS2R::Media.default_ext('text/plain')
86
+ assert_equal 'test', MMS2R::Media.default_ext('example/test')
87
+ end
88
+
89
+ def test_base_initialize_config_tries_to_open_default_config_yaml
90
+ f = File.join(MMS2R::Media.conf_dir, 'mms2r_media.yml')
91
+ YAML.expects(:load_file).once.with(f).returns({})
92
+ config = MMS2R::Media.initialize_config(nil)
93
+ end
94
+
95
+ def test_base_initialize_config
96
+ config = MMS2R::Media.initialize_config(nil)
97
+
98
+ # test defaults shipped in mms2r_media.yml
99
+ assert_not_nil config
100
+ assert_equal true, config['ignore'].is_a?(Hash)
101
+ assert_equal true, config['transform'].is_a?(Hash)
102
+ assert_equal true, config['number'].is_a?(Array)
103
+ end
104
+
105
+ def test_initialize_config_contatenation
106
+ c = {'ignore' => {'text/plain' => ['/A TEST/']},
107
+ 'transform' => {'text/plain' => ['/FOO/', '']},
108
+ 'number' => ['from', '/^([^\s]+)\s.*/', '\1']
109
+ }
110
+ config = MMS2R::Media.initialize_config(c)
111
+ assert_not_nil config['ignore']['text/plain'].detect{|v| v == '/A TEST/'}
112
+ assert_not_nil config['transform']['text/plain'].detect{|v| v == '/FOO/'}
113
+ assert_not_nil config['number'].first == 'from'
114
+ end
115
+
116
+ def test_create_with_default_processor
117
+ mail = mock()
118
+ mail.expects(:header).at_least_once.returns({})
119
+ mail.expects(:from).at_least_once.returns(['joe@unknown.example.com'])
120
+ mms = MMS2R::Media.create(mail)
121
+ assert_equal [MMS2R::Media, 'unknown.example.com'] , mms
122
+ end
123
+
124
+ def test_create_with_special_processor
125
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
126
+ mail = mock()
127
+ mail.expects(:header).at_least_once.returns({})
128
+ mail.expects(:from).at_least_once.returns(['joe@null.example.com'])
129
+ mms = MMS2R::Media.create(mail)
130
+ assert_equal [MMS2R::Media::NullCarrier, 'null.example.com'], mms
131
+ end
132
+
133
+ def test_create_with_special_processor_and_return_path
134
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
135
+ mail = mock()
136
+ mail.expects(:header).at_least_once.returns({'return-path' => '<joe@null.example.com>'})
137
+ mail.expects(:from).at_least_once.returns([])
138
+ mms = MMS2R::Media.create(mail)
139
+ assert_equal [MMS2R::Media::NullCarrier, 'null.example.com'], mms
140
+ end
141
+
142
+ def test_create_should_fail_gracefully_with_broken_from
143
+ mail = mock()
144
+ mail.expects(:header).at_least_once.returns({})
145
+ mail.expects(:from).at_least_once.returns(nil)
146
+ assert_nothing_raised { MMS2R::Media.create(mail) }
147
+ end
148
+
149
+ def test_aliased_new_returns_custom_processor_instance
150
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
151
+ mail = stub_mail(:from)
152
+ mail.expects(:from).at_least_once.returns(['joe@null.example.com'])
153
+
154
+ mms = MMS2R::Media.new(mail)
155
+ assert_not_nil mms
156
+ assert_equal MMS2R::Media::NullCarrier, mms.class
157
+ assert_equal true, mms.respond_to?(:process)
158
+ end
159
+
160
+ def test_aliased_new_returns_default_processor_instance
161
+ mms = MMS2R::Media.new(stub_mail())
162
+ assert_not_nil mms
163
+ assert_equal true, mms.respond_to?(:process)
164
+ assert_equal MMS2R::Media, mms.class
165
+ end
166
+
167
+ def test_lazy_process_option
168
+ mms = MMS2R::Media.new(stub_mail(), :process => :lazy)
169
+ mms.expects(:process).never
170
+ end
171
+
172
+ def test_logger_option
173
+ logger = mock()
174
+ logger.expects(:info).at_least_once
175
+ mms = MMS2R::Media.new(stub_mail(), :logger => logger)
176
+ end
177
+
178
+ def test_default_processor_initialize_tries_to_open_config_for_carrier
179
+ f = File.join(MMS2R::Media.conf_dir, 'example.com.yml')
180
+ YAML.expects(:load_file).once.with(f)
181
+ mms = MMS2R::Media.new(stub_mail())
182
+ end
183
+
184
+ def test_mms_phone_number
185
+ mail = stub_mail()
186
+ mail.stubs(:from).returns(['2068675309@example.com'])
187
+ mms = MMS2R::Media.new(mail)
188
+ assert_equal '2068675309', mms.number
189
+ end
190
+
191
+ def test_mms_phone_number_from_config
192
+ mail = stub_mail()
193
+ mail.stubs(:header).returns({'from' => TMail::AddressHeader.new('from', '"+2068675309" <BCVOZH@mms.vodacom4me.co.za>')})
194
+ mms = MMS2R::Media.new(mail)
195
+ mms.expects(:config).once.returns({'number' => ['from', '/^([^\s]+)\s.*/', '\1']})
196
+ assert_equal '+2068675309', mms.number
197
+ end
198
+
199
+ def test_mms_phone_number_with_errors
200
+ mail = stub_mail(:from)
201
+ mail.stubs(:from).returns(nil)
202
+ mms = MMS2R::Media.new(mail)
203
+ assert_nothing_raised do
204
+ assert_equal '', mms.number
205
+ end
206
+ end
207
+
208
+ def test_transform_text
209
+ mail = stub_mail()
210
+ mail.stubs(:from).returns(nil)
211
+ mms = MMS2R::Media.new(mail)
212
+
213
+ type = 'test/type'
214
+ text = 'hello'
215
+
216
+ # no match in the config
217
+ result = [type, text]
218
+ assert_equal result, mms.transform_text(type, text)
219
+
220
+ # testing the default config
221
+ assert_equal ['text/plain', ''], mms.transform_text('text/plain', "Sent via BlackBerry from T-Mobile")
222
+ assert_equal ['text/plain', ''], mms.transform_text('text/plain', "Sent from my Verizon Wireless BlackBerry")
223
+ assert_equal ['text/plain', ''], mms.transform_text('text/plain', 'Sent from my iPhone')
224
+ assert_equal ['text/plain', ''], mms.transform_text('text/plain', 'Sent from your iPhone.')
225
+ assert_equal ['text/plain', ''], mms.transform_text('text/plain', " \n\nimage/jpeg")
226
+
227
+ # has a bad regexp
228
+ mms.expects(:config).once.returns({'transform' => {type => [['(hello)', 'world']]}})
229
+ assert_equal result, mms.transform_text(type, text)
230
+
231
+ # matches in config
232
+ mms.expects(:config).once.returns({'transform' => {type => [['/(hello)/', 'world']]}})
233
+ assert_equal [type, 'world'], mms.transform_text(type, text)
234
+
235
+ mms.expects(:config).once.returns({'transform' => {type => [['/^Ignore this part, (.+)/', '\1']]}})
236
+ assert_equal [type, text], mms.transform_text(type, "Ignore this part, " + text)
237
+
238
+ # chaining transforms
239
+ mms.expects(:config).once.returns({'transform' => {type => [['/(hello)/', 'world'],
240
+ ['/(world)/', 'mars']]}})
241
+ assert_equal [type, 'mars'], mms.transform_text(type, text)
242
+
243
+ # has a Iconv problem
244
+ Iconv.expects(:new).raises
245
+ mms.expects(:config).once.returns({'transform' => {type => [['(hello)', 'world']]}})
246
+ assert_equal result, mms.transform_text(type, text)
247
+ end
248
+
249
+ def test_transform_text_to_utf8
250
+ mail = TMail::Mail.load(mail_fixture('iconv-fr-text-01.mail'))
251
+ mms = MMS2R::Media.new(mail)
252
+
253
+ assert_equal 2, mms.media.size
254
+ assert_equal 1, mms.media['text/plain'].size
255
+ assert_equal 1, mms.media['text/html'].size
256
+ file = mms.media['text/plain'][0]
257
+ assert_not_nil file
258
+ assert_equal true, File::exist?(file)
259
+ text = IO.readlines("#{file}").join
260
+ #assert_match(/D'ici un mois Géorgie/, text)
261
+ assert_match(/D'ici un mois G\303\203\302\251orgie/, text)
262
+ assert_equal("sample email message Fwd: sub D'ici un mois G\303\203\302\251orgie",
263
+ mms.subject)
264
+ mms.purge
265
+ end
266
+
267
+ def test_subject
268
+ s = 'hello world'
269
+ mail = stub_mail()
270
+ mail.stubs(:subject).returns(s)
271
+ mms = MMS2R::Media.new(mail)
272
+ assert_equal s, mms.subject
273
+
274
+ # second time through shouldn't process the subject again
275
+ mail.expects(:subject).never
276
+ assert_equal s, mms.subject
277
+ end
278
+
279
+ def test_subject_with_bad_mail_subject
280
+ mail = stub_mail()
281
+ mail.stubs(:subject).returns(nil)
282
+ mms = MMS2R::Media.new(mail)
283
+ assert_equal '', mms.subject
284
+ end
285
+
286
+ def test_subject_with_subject_ignored
287
+ s = 'hello world'
288
+ mail = stub_mail()
289
+ mail.stubs(:subject).returns(s)
290
+ mms = MMS2R::Media.new(mail)
291
+ mms.stubs(:config).returns({'ignore' => {'text/plain' => [s]}})
292
+ assert_equal '', mms.subject
293
+ end
294
+
295
+ def test_subject_with_subject_transformed
296
+ s = 'Default Subject: hello world'
297
+ mail = stub_mail()
298
+ mail.stubs(:subject).returns(s)
299
+ mms = MMS2R::Media.new(mail)
300
+ mms.stubs(:config).returns(
301
+ { 'ignore' => {},
302
+ 'transform' => {'text/plain' => [['/Default Subject: (.+)/', '\1']]}})
303
+ assert_equal 'hello world', mms.subject
304
+ end
305
+
306
+ def test_attachment_should_return_nil_if_files_for_type_are_not_found
307
+ mms = MMS2R::Media.new(stub_mail())
308
+ mms.stubs(:media).returns({})
309
+ assert_nil mms.send(:attachment, ['text'])
310
+ end
311
+
312
+ def test_attachment_should_return_nil_if_empty_files_are_found
313
+ mms = MMS2R::Media.new(stub_mail())
314
+ mms.stubs(:media).returns({'text/plain' => [Tempfile.new('test')]})
315
+ assert_nil mms.send(:attachment, ['text'])
316
+ end
317
+
318
+ def test_type_from_filename(filename)
319
+ mms = MMS2R::Media.new(stub_mail())
320
+ assert_equal 'image/jpeg', mms.send(:type_from_filename, "example.jpg")
321
+ end
322
+
323
+ def test_type_from_filename_should_be_nil(filename)
324
+ mms = MMS2R::Media.new(stub_mail())
325
+ assert_nil mms.send(:type_from_filename, "example.example")
326
+ end
327
+
328
+ def test_attachment_should_return_duck_typed_file
329
+ mms = MMS2R::Media.new(stub_mail())
330
+ temp_big = temp_text_file("hello world")
331
+ size = File.size(temp_text_file("hello world"))
332
+ temp_small = temp_text_file("hello")
333
+ mms.stubs(:media).returns({'text/plain' => [temp_small, temp_big]})
334
+ duck_file = mms.send(:attachment, ['text'])
335
+ assert_not_nil duck_file
336
+ assert_equal true, File::exist?(duck_file)
337
+ assert_equal true, File::exist?(temp_big)
338
+ assert_equal temp_big, duck_file.local_path
339
+ assert_equal File.basename(temp_big), duck_file.original_filename
340
+ assert_equal size, duck_file.size
341
+ assert_equal 'text/plain', duck_file.content_type
342
+ end
343
+
344
+ def test_empty_body
345
+ mms = MMS2R::Media.new(stub_mail())
346
+ mms.stubs(:default_text).returns(nil)
347
+ assert_equal "", mms.body
348
+ end
349
+
350
+ def test_body
351
+ mms = MMS2R::Media.new(stub_mail())
352
+ temp_big = temp_text_file("hello world")
353
+ mms.stubs(:default_text).returns(File.new(temp_big))
354
+ assert_equal "hello world", mms.body
355
+ end
356
+
357
+ def test_default_text
358
+ mms = MMS2R::Media.new(stub_mail())
359
+ temp_big = temp_text_file("hello world")
360
+ temp_small = temp_text_file("hello")
361
+ mms.stubs(:media).returns({'text/plain' => [temp_small, temp_big]})
362
+
363
+ assert_equal temp_big, mms.default_text.local_path
364
+
365
+ # second time through shouldn't setup the @default_text by calling attachment
366
+ mms.expects(:attachment).never
367
+ assert_equal temp_big, mms.default_text.local_path
368
+ end
369
+
370
+ def test_default_media
371
+ mms = MMS2R::Media.new(stub_mail())
372
+ #it doesn't matter that these are text files, we just need say they are images
373
+ temp_big = temp_text_file("hello world")
374
+ temp_small = temp_text_file("hello")
375
+ mms.stubs(:media).returns({'image/jpeg' => [temp_small, temp_big]})
376
+
377
+ assert_equal temp_big, mms.default_media.local_path
378
+
379
+ # second time through shouldn't setup the @default_media by calling attachment
380
+ mms.expects(:attachment).never
381
+ assert_equal temp_big, mms.default_media.local_path
382
+ end
383
+
384
+ def test_default_media_treats_image_and_video_equally
385
+ mms = MMS2R::Media.new(stub_mail())
386
+ #it doesn't matter that these are text files, we just need say they are images
387
+ temp_big_image = temp_text_file("hello world")
388
+ temp_small_image = temp_text_file("hello")
389
+ temp_big_video = temp_text_file("hello world again")
390
+ temp_small_video = temp_text_file("hello again")
391
+ mms.stubs(:media).returns({'image/jpeg' => [temp_small_image, temp_big_image],
392
+ 'video/mpeg' => [temp_small_video, temp_big_video],
393
+ })
394
+
395
+ assert_equal temp_big_video, mms.default_media.local_path
396
+
397
+ # second time through shouldn't setup the @default_media by calling attachment
398
+ mms.expects(:attachment).never
399
+ assert_equal temp_big_video, mms.default_media.local_path
400
+ end
401
+
402
+ def test_default_media_treats_gif_and_jpg_equally
403
+ #it doesn't matter that these are text files, we just need say they are images
404
+ temp_big = temp_text_file("hello world")
405
+ temp_small = temp_text_file("hello")
406
+
407
+ mms = MMS2R::Media.new(stub_mail())
408
+ mms.stubs(:media).returns({'image/jpeg' => [temp_big], 'image/gif' => [temp_small]})
409
+ assert_equal temp_big, mms.default_media.local_path
410
+
411
+ mms = MMS2R::Media.new(stub_mail())
412
+ mms.stubs(:media).returns({'image/gif' => [temp_big], 'image/jpg' => [temp_small]})
413
+ assert_equal temp_big, mms.default_media.local_path
414
+ end
415
+
416
+ def test_purge
417
+ mms = MMS2R::Media.new(stub_mail())
418
+ mms.purge
419
+ assert_equal false, File.exist?(mms.media_dir)
420
+ end
421
+
422
+ def test_ignore_media_by_filename_equality
423
+ name = 'foo.txt'
424
+ type = 'text/plain'
425
+ mms = MMS2R::Media.new(stub_mail())
426
+ mms.stubs(:config).returns({'ignore' => {type => [name]}})
427
+
428
+ # type is not in the ingore
429
+ part = stub(:sub_header => name, :body => 'a')
430
+ assert_equal false, mms.ignore_media?('text/test', part)
431
+ # type and filename are in the ingore
432
+ part = stub(:sub_header => name)
433
+ assert_equal true, mms.ignore_media?(type, part)
434
+ # type but not file name are in the ignore
435
+ part = stub(:sub_header => 'bar.txt', :body => 'a')
436
+ assert_equal false, mms.ignore_media?(type, part)
437
+ end
438
+
439
+ def test_filename
440
+ name = 'x' * 300 + '.txt'
441
+ mms = MMS2R::Media.new(stub_mail())
442
+ part = stub(:sub_header => name, :content_type => 'text/plain')
443
+ assert_equal 'x' * 251 + '.txt', mms.filename?(part)
444
+ end
445
+
446
+ def test_long_filename
447
+ name = 'foo.txt'
448
+ mms = MMS2R::Media.new(stub_mail())
449
+ part = stub(:sub_header => name, :content_type => 'text/plain')
450
+ assert_equal 'foo.txt', mms.filename?(part)
451
+ end
452
+
453
+ def test_filename_when_file_extension_missing_part
454
+ name = 'foo'
455
+ mms = MMS2R::Media.new(stub_mail())
456
+ part = stub(:sub_header => name, :content_type => 'text/plain', :part_type? => 'text/plain')
457
+ assert_equal 'foo.txt', mms.filename?(part)
458
+
459
+ name = 'foo.janky'
460
+ mms = MMS2R::Media.new(stub_mail())
461
+ part = stub(:sub_header => name, :content_type => 'text/plain', :part_type? => 'text/plain')
462
+ assert_equal 'foo.janky.txt', mms.filename?(part)
463
+ end
464
+
465
+ def test_ignore_media_by_filename_regexp
466
+ name = 'foo.txt'
467
+ regexp = '/foo\.txt/i'
468
+ type = 'text/plain'
469
+ mms = MMS2R::Media.new(stub_mail())
470
+ mms.stubs(:config).returns({'ignore' => {type => [regexp, 'nil.txt']}})
471
+
472
+ # type is not in the ingore
473
+ part = stub(:sub_header => name, :body => 'a')
474
+ assert_equal false, mms.ignore_media?('text/test', part)
475
+ # type and regexp for the filename are in the ingore
476
+ part = stub(:sub_header => name)
477
+ assert_equal true, mms.ignore_media?(type, part)
478
+ # type but not regexp for filename are in the ignore
479
+ part = stub(:sub_header => 'bar.txt', :body => 'a')
480
+ assert_equal false, mms.ignore_media?(type, part)
481
+ end
482
+
483
+ def test_ignore_media_by_regexp_on_file_content
484
+ name = 'foo.txt'
485
+ content = "aaaaaaahello worldbbbbbbbbb"
486
+ regexp = '/.*Hello World.*/i'
487
+ type = 'text/plain'
488
+ mms = MMS2R::Media.new(stub_mail())
489
+ mms.stubs(:config).returns({'ignore' => {type => ['nil.txt', regexp]}})
490
+
491
+ part = stub(:sub_header => name, :body => content)
492
+
493
+ # type is not in the ingore
494
+ assert_equal false, mms.ignore_media?('text/test', part)
495
+ # type and regexp for the content are in the ingore
496
+ assert_equal true, mms.ignore_media?(type, part)
497
+ # type but not regexp for content are in the ignore
498
+ part = stub(:sub_header => name, :body => 'no teapots')
499
+ assert_equal false, mms.ignore_media?(type, part)
500
+ end
501
+
502
+ def test_ignore_media_when_file_content_is_empty
503
+ mms = MMS2R::Media.new(stub_mail())
504
+
505
+ # there is no conf but the part's body is empty
506
+ part = stub(:sub_header => 'foo.txt', :body => "")
507
+ assert_equal true, mms.ignore_media?('text/test', part)
508
+
509
+ # there is no conf but the part's body is white space
510
+ part = stub(:sub_header => 'foo.txt', :body => "\t\n\t\n ")
511
+ assert_equal true, mms.ignore_media?('text/test', part)
512
+ end
513
+
514
+ def test_add_file
515
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
516
+ mail = stub_mail()
517
+ mail.stubs(:from).returns(['joe@null.example.com'])
518
+
519
+ mms = MMS2R::Media.new(mail)
520
+ mms.stubs(:media).returns({})
521
+
522
+ assert_nil mms.media['text/html']
523
+ mms.add_file('text/html', '/tmp/foo.html')
524
+ assert_equal ['/tmp/foo.html'], mms.media['text/html']
525
+ mms.add_file('text/html', '/tmp/bar.html')
526
+ assert_equal ['/tmp/foo.html', '/tmp/bar.html'], mms.media['text/html']
527
+ end
528
+
529
+ def test_temp_file
530
+ name = 'foo.txt'
531
+ mms = MMS2R::Media.new(stub_mail())
532
+ part = stub(:sub_header => name)
533
+ assert_equal name, File.basename(mms.temp_file(part))
534
+ end
535
+
536
+ def test_process_media_for_text
537
+ name = 'foo.txt'
538
+ mms = MMS2R::Media.new(stub_mail())
539
+ mms.stubs(:transform_text_part).returns(['text/plain', nil])
540
+ part = stub(:sub_header => name, :content_type => 'text/plain', :part_type? => 'text/plain', :main_type => 'text')
541
+
542
+ assert_equal ['text/plain', nil], mms.process_media(part)
543
+
544
+ mms.stubs(:transform_text_part).returns(['text/plain', 'hello world'])
545
+ result = mms.process_media(part)
546
+ assert_equal 'text/plain', result.first
547
+ assert_equal 'hello world', IO.read(result.last)
548
+ mms.purge # have to call purge since a file is put to disk as side effect
549
+ end
550
+
551
+ def test_process_media_with_empty_text
552
+ name = 'foo.txt'
553
+ mms = MMS2R::Media.new(stub_mail())
554
+ mms.stubs(:transform_text_part).returns(['text/plain', nil])
555
+ part = stub(:sub_header => name, :content_type => 'text/plain', :part_type? => 'text/plain', :main_type => 'text')
556
+
557
+ assert_equal ['text/plain', nil], mms.process_media(part)
558
+
559
+ mms.stubs(:transform_text_part).returns(['text/plain', ''])
560
+ assert_equal ['text/plain', nil], mms.process_media(part)
561
+ mms.purge # have to call purge since a file is put to disk as side effect
562
+ end
563
+
564
+ def test_process_media_for_application_smil
565
+ name = 'foo.txt'
566
+ mms = MMS2R::Media.new(stub_mail())
567
+ mms.stubs(:transform_text_part).returns(['application/smil', nil])
568
+ part = stub(:sub_header => name, :content_type => 'application/smil', :part_type? => 'application/smil', :main_type => 'application')
569
+
570
+ assert_equal ['application/smil', nil], mms.process_media(part)
571
+
572
+ mms.stubs(:transform_text_part).returns(['application/smil', 'hello world'])
573
+ result = mms.process_media(part)
574
+ assert_equal 'application/smil', result.first
575
+ assert_equal 'hello world', IO.read(result.last)
576
+ mms.purge # have to call purge since a file is put to disk as side effect
577
+ end
578
+
579
+ def test_process_media_for_application_octet_stream_when_image
580
+ name = 'fake.jpg'
581
+ mms = MMS2R::Media.new(stub_mail())
582
+ part = stub(:sub_header => name, :content_type => 'application/octet-stream', :part_type? => 'application/octet-stream', :body => "data", :main_type => 'application')
583
+ result = mms.process_media(part)
584
+ assert_equal 'image/jpeg', result.first
585
+ assert_match(/fake\.jpg$/, result.last)
586
+ mms.purge # have to call purge since a file is put to disk as side effect
587
+ end
588
+
589
+ def test_process_media_for_all_other_media
590
+ name = 'foo.txt'
591
+ mms = MMS2R::Media.new(stub_mail())
592
+ part = stub(:sub_header => name, :content_type => 'faux/text', :part_type? => 'faux/text', :body => nil)
593
+ part.expects(:main_type).with('text').returns('faux')
594
+
595
+ assert_equal ['faux/text', nil], mms.process_media(part)
596
+
597
+ part = stub(:sub_header => name, :content_type => 'faux/text', :part_type? => 'faux/text', :body => 'hello world')
598
+ part.expects(:main_type).with('text').returns('faux')
599
+ result = mms.process_media(part)
600
+ assert_equal 'faux/text', result.first
601
+ assert_equal 'hello world', IO.read(result.last)
602
+ mms.purge # have to call purge since a file is put to disk as side effect
603
+ end
604
+
605
+ def test_process
606
+ mms = MMS2R::Media.new(stub_mail())
607
+ assert_equal 1, mms.media.size
608
+ assert_not_nil mms.media['text/plain']
609
+ assert_equal 1, mms.media['text/plain'].size
610
+ assert_equal 'message.txt', File.basename(mms.media['text/plain'].first)
611
+ assert_equal true, File.exist?(mms.media['text/plain'].first)
612
+ assert_equal 1, File.size(mms.media['text/plain'].first)
613
+ mms.purge
614
+ end
615
+
616
+ def test_process_with_multipart_double_parts
617
+ mail = TMail::Mail.parse(load_mail('apple-double-image-01.mail').join)
618
+ mms = MMS2R::Media.new(mail)
619
+
620
+ assert_equal 2, mms.media.size
621
+
622
+ assert_not_nil mms.media['application/applefile']
623
+ assert_equal 1, mms.media['application/applefile'].size
624
+
625
+ assert_not_nil mms.media['image/jpeg']
626
+ assert_equal 1, mms.media['image/jpeg'].size
627
+ assert_match(/DSCN1715\.jpg$/, mms.media['image/jpeg'].first)
628
+
629
+ assert_file_size mms.media['image/jpeg'].first, 337
630
+
631
+ mms.purge
632
+ end
633
+
634
+ def test_process_with_multipart_alternative_parts
635
+ mail = stub_mail()
636
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :part_type? => 'text/plain', :body => 'a', :main_type => 'text')
637
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :part_type? => 'text/html', :body => 'a', :main_type => 'text')
638
+ multi = stub(:content_type => 'multipart/alternative', :part_type? => 'multipart/alternative', :parts => [plain, html])
639
+ mail.stubs(:multipart?).returns(true)
640
+ mail.stubs(:parts).returns([multi])
641
+
642
+ # the multipart/alternative should get flattend to text and html
643
+ mms = MMS2R::Media.new(mail)
644
+ assert_equal 2, mms.media.size
645
+ assert_equal 2, mms.media.size
646
+ assert_not_nil mms.media['text/plain']
647
+ assert_not_nil mms.media['text/html']
648
+ assert_equal 1, mms.media['text/plain'].size
649
+ assert_equal 1, mms.media['text/html'].size
650
+ assert_equal 'message.txt', File.basename(mms.media['text/plain'].first)
651
+ assert_equal 'message.html', File.basename(mms.media['text/html'].first)
652
+ assert_equal true, File.exist?(mms.media['text/plain'].first)
653
+ assert_equal true, File.exist?(mms.media['text/html'].first)
654
+ assert_equal 1, File.size(mms.media['text/plain'].first)
655
+ assert_equal 1, File.size(mms.media['text/html'].first)
656
+ mms.purge
657
+ end
658
+
659
+ def test_process_when_media_is_ignored
660
+ mail = stub_mail()
661
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :part_type? => 'text/plain', :body => '')
662
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :part_type? => 'text/html', :body => '')
663
+ multi = stub(:content_type => 'multipart/alternative', :part_type? => 'multipart/alternative', :parts => [plain, html])
664
+ mail.stubs(:multipart?).returns(true)
665
+ mail.stubs(:parts).returns([multi])
666
+ mms = MMS2R::Media.new(mail, :process => :lazy)
667
+ mms.stubs(:config).returns({'ignore' => {'text/plain' => ['message.txt'],
668
+ 'text/html' => ['message.html']}})
669
+ assert_nothing_raised { mms.process }
670
+ # the multipart/alternative should get flattend to text and html and then
671
+ # what's flattened is ignored
672
+ assert_equal 0, mms.media.size
673
+ mms.purge
674
+ end
675
+
676
+ def test_process_when_yielding_to_a_block
677
+ mail = stub_mail()
678
+
679
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :part_type? => 'text/plain', :body => 'a', :main_type => 'text')
680
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :part_type? => 'text/html', :body => 'b', :main_type => 'text')
681
+ mail.stubs(:multipart?).returns(true)
682
+ mail.stubs(:parts).returns([plain, html])
683
+
684
+ # the multipart/alternative should get flattend to text and html
685
+ mms = MMS2R::Media.new(mail)
686
+ assert_equal 2, mms.media.size
687
+ mms.process do |type, files|
688
+ assert_equal 1, files.size
689
+ assert_equal true, type == 'text/plain' || type == 'text/html'
690
+ assert_equal true, File.basename(files.first) == 'message.txt' ||
691
+ File.basename(files.first) == 'message.html'
692
+ assert_equal true, File::exist?(files.first)
693
+ end
694
+ mms.purge
695
+ end
696
+
697
+ def test_domain_from_return_path
698
+ mail = mock()
699
+ mail.expects(:header).at_least_once.returns({'return-path' => '<joe@null.example.com>'})
700
+ mail.expects(:from).at_least_once.returns([])
701
+ domain = MMS2R::Media.domain(mail)
702
+ assert_equal 'null.example.com', domain
703
+ end
704
+
705
+ def test_domain_from_from
706
+ mail = mock()
707
+ mail.expects(:header).at_least_once.returns({})
708
+ mail.expects(:from).at_least_once.returns(['joe@null.example.com'])
709
+ domain = MMS2R::Media.domain(mail)
710
+ assert_equal 'null.example.com', domain
711
+ end
712
+
713
+ def test_domain_from_from_yaml
714
+ f = File.join(MMS2R::Media.conf_dir, 'from.yml')
715
+ YAML.expects(:load_file).once.with(f).returns(['example.com'])
716
+ mail = mock()
717
+ mail.expects(:header).at_least_once.returns({'return-path' => '<joe@null.example.com>'})
718
+ mail.expects(:from).at_least_once.returns(['joe@example.com'])
719
+ domain = MMS2R::Media.domain(mail)
720
+ assert_equal 'example.com', domain
721
+ end
722
+
723
+ def test_unknown_device_type
724
+ mail = TMail::Mail.load(mail_fixture('generic.mail'))
725
+ mms = MMS2R::Media.new(mail)
726
+ assert_equal :unknown, mms.device_type?
727
+ assert_equal false, mms.is_mobile?
728
+ end
729
+
730
+ def test_iphone_device_type
731
+ mail = TMail::Mail.load(mail_fixture('att-iphone-01.mail'))
732
+ mms = MMS2R::Media.new(mail)
733
+ assert_equal :iphone, mms.device_type?
734
+ assert_equal true, mms.is_mobile?
735
+ end
736
+
737
+ def test_blackberry_device_type
738
+ berries = ['att-blackberry.mail',
739
+ 'suncom-blackberry.mail',
740
+ 'tmobile-blackberry-02.mail',
741
+ 'tmobile-blackberry.mail',
742
+ 'tmo.blackberry.net-image-01.mail',
743
+ 'verizon-blackberry.mail',
744
+ 'verizon-blackberry.mail']
745
+ berries.each do |berry|
746
+ mail = TMail::Mail.load(mail_fixture(berry))
747
+ mms = MMS2R::Media.new(mail)
748
+ assert_equal :blackberry, mms.device_type?, "fixture #{berry} was not a blackberrry"
749
+ assert_equal true, mms.is_mobile?
750
+ end
751
+ end
752
+
753
+ def test_handset_device_type
754
+ mail = TMail::Mail.load(mail_fixture('att-image-01.mail'))
755
+ mms = MMS2R::Media.new(mail)
756
+ assert_equal :handset, mms.device_type?
757
+ assert_equal true, mms.is_mobile?
758
+ end
759
+
760
+ end