mms2r 1.1.12 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/History.txt +32 -2
  2. data/Manifest.txt +41 -79
  3. data/README.txt +50 -51
  4. data/Rakefile +3 -2
  5. data/conf/aliases.yml +8 -0
  6. data/conf/message.alltel.com.yml +14 -0
  7. data/conf/messaging.nextel.com.yml +10 -0
  8. data/conf/mms.att.net.yml +9 -0
  9. data/conf/mms.dobson.net.yml +4 -0
  10. data/conf/mms.luxgsm.lu.yml +8 -0
  11. data/conf/mms.myhelio.com.yml +12 -0
  12. data/conf/mms.netcom.no.yml +4 -0
  13. data/conf/mms.o2online.de.yml +8 -0
  14. data/conf/mms.three.co.uk.yml +15 -0
  15. data/conf/mms2r_media.yml +6 -0
  16. data/conf/orangemms.net.yml +13 -0
  17. data/conf/pm.sprint.com.yml +10 -0
  18. data/conf/pxt.vodafone.net.nz.yml +5 -0
  19. data/conf/tmomail.net.yml +16 -0
  20. data/conf/vzwpix.com.yml +8 -0
  21. data/dev_tools/anonymizer.rb +20 -0
  22. data/lib/mms2r.rb +19 -45
  23. data/lib/mms2r/media.rb +288 -300
  24. data/lib/mms2r/media/sprint.rb +189 -0
  25. data/test/fixtures/att-text-01.mail +27 -0
  26. data/test/fixtures/helio-image-01.mail +8 -8
  27. data/test/fixtures/helio-message-01.mail +327 -0
  28. data/test/fixtures/luxgsm-image-01.mail +388 -0
  29. data/test/fixtures/netcom-image-01.mail +556 -0
  30. data/test/fixtures/o2-de-image-01.mail +314 -0
  31. data/test/fixtures/orange-uk-image-01.mail +71 -0
  32. data/test/fixtures/pxt-image-01.mail +118 -0
  33. data/test/fixtures/sprint-broken-image-01.mail +1 -1
  34. data/test/fixtures/sprint-image-01.mail +211 -195
  35. data/test/fixtures/sprint-purged-image-01.mail +1 -1
  36. data/test/fixtures/sprint-two-images-01.mail +231 -198
  37. data/test/fixtures/sprint-video-01.mail +210 -195
  38. data/test/fixtures/three-uk-image-01.mail +1202 -0
  39. data/test/test_helper.rb +39 -0
  40. data/test/{test_mms2r_alltel_media.rb → test_message_alltel_com.rb} +13 -17
  41. data/test/{test_mms2r_nextel_media.rb → test_messaging_nextel_com.rb} +12 -19
  42. data/test/{test_mms2r_sprint_pcs_media.rb → test_messaging_sprintpcs_com.rb} +9 -10
  43. data/test/test_mms2r_media.rb +474 -316
  44. data/test/test_mms_att_net.rb +92 -0
  45. data/test/test_mms_dobson_net.rb +41 -0
  46. data/test/test_mms_luxgsm_lu.rb +50 -0
  47. data/test/test_mms_myhelio_com.rb +60 -0
  48. data/test/test_mms_netcom_no.rb +52 -0
  49. data/test/test_mms_o2online_de.rb +53 -0
  50. data/test/test_mms_three_co_uk.rb +42 -0
  51. data/test/test_orangemms_net.rb +100 -0
  52. data/test/test_pm_sprint_com.rb +201 -0
  53. data/test/test_pxt_vodafone_net_nz.rb +37 -0
  54. data/test/{test_mms2r_t_mobile_media.rb → test_tmomail_net.rb} +8 -19
  55. data/test/{test_mms2r_verizon_media.rb → test_vzwpix_com.rb} +34 -27
  56. data/vendor/plugins/mms2r/lib/autotest/mms2r.rb +16 -11
  57. metadata +128 -157
  58. data/conf/mms2r_alltel_media_ignore.yml +0 -13
  59. data/conf/mms2r_att_media_subject.yml +0 -3
  60. data/conf/mms2r_cingular_me_media_subject.yml +0 -3
  61. data/conf/mms2r_cingular_me_media_transform.yml +0 -7
  62. data/conf/mms2r_dobson_media_ignore.yml +0 -4
  63. data/conf/mms2r_helio_media_ignore.yml +0 -6
  64. data/conf/mms2r_media_ignore.yml +0 -5
  65. data/conf/mms2r_media_subject.yml +0 -3
  66. data/conf/mms2r_my_cingular_media_subject.yml +0 -3
  67. data/conf/mms2r_nextel_media_ignore.yml +0 -11
  68. data/conf/mms2r_orange_france_media_ignore.yml +0 -6
  69. data/conf/mms2r_orange_poland_media_subject.yml +0 -3
  70. data/conf/mms2r_sprint_media_ignore.yml +0 -7
  71. data/conf/mms2r_sprint_media_subject.yml +0 -3
  72. data/conf/mms2r_t_mobile_media_ignore.yml +0 -16
  73. data/conf/mms2r_verizon_media_ignore.yml +0 -4
  74. data/conf/mms2r_verizon_media_transform.yml +0 -5
  75. data/lib/mms2r/alltel_media.rb +0 -10
  76. data/lib/mms2r/att_media.rb +0 -13
  77. data/lib/mms2r/cingular_me_media.rb +0 -23
  78. data/lib/mms2r/dobson_media.rb +0 -13
  79. data/lib/mms2r/helio_media.rb +0 -20
  80. data/lib/mms2r/m_mode_media.rb +0 -13
  81. data/lib/mms2r/my_cingular_media.rb +0 -15
  82. data/lib/mms2r/nextel_media.rb +0 -17
  83. data/lib/mms2r/orange_france_media.rb +0 -10
  84. data/lib/mms2r/orange_poland_media.rb +0 -10
  85. data/lib/mms2r/sprint_media.rb +0 -182
  86. data/lib/mms2r/sprint_pcs_media.rb +0 -16
  87. data/lib/mms2r/t_mobile_media.rb +0 -21
  88. data/lib/mms2r/verizon_media.rb +0 -19
  89. data/lib/mms2r/vtext_media.rb +0 -16
  90. data/lib/vendor/text/format.rb +0 -1466
  91. data/lib/vendor/tmail.rb +0 -3
  92. data/lib/vendor/tmail/address.rb +0 -242
  93. data/lib/vendor/tmail/attachments.rb +0 -39
  94. data/lib/vendor/tmail/base64.rb +0 -71
  95. data/lib/vendor/tmail/config.rb +0 -69
  96. data/lib/vendor/tmail/encode.rb +0 -467
  97. data/lib/vendor/tmail/facade.rb +0 -552
  98. data/lib/vendor/tmail/header.rb +0 -914
  99. data/lib/vendor/tmail/info.rb +0 -35
  100. data/lib/vendor/tmail/loader.rb +0 -1
  101. data/lib/vendor/tmail/mail.rb +0 -447
  102. data/lib/vendor/tmail/mailbox.rb +0 -433
  103. data/lib/vendor/tmail/mbox.rb +0 -1
  104. data/lib/vendor/tmail/net.rb +0 -280
  105. data/lib/vendor/tmail/obsolete.rb +0 -135
  106. data/lib/vendor/tmail/parser.rb +0 -1522
  107. data/lib/vendor/tmail/port.rb +0 -377
  108. data/lib/vendor/tmail/quoting.rb +0 -131
  109. data/lib/vendor/tmail/scanner.rb +0 -41
  110. data/lib/vendor/tmail/scanner_r.rb +0 -263
  111. data/lib/vendor/tmail/stringio.rb +0 -277
  112. data/lib/vendor/tmail/tmail.rb +0 -1
  113. data/lib/vendor/tmail/utils.rb +0 -238
  114. data/test/fixtures/broken_from_spam.mail +0 -80
  115. data/test/fixtures/hello_world_empty_text.mail +0 -7
  116. data/test/fixtures/hello_world_mail_multipart.mail +0 -24
  117. data/test/fixtures/hello_world_mail_plain_no_content_type.mail +0 -7
  118. data/test/fixtures/hello_world_mail_plain_with_content_type.mail +0 -8
  119. data/test/fixtures/simple-with-two-images-two-texts.mail +0 -49
  120. data/test/fixtures/simple_image.mail +0 -19
  121. data/test/fixtures/simple_multipart_alternative.mail +0 -42
  122. data/test/test_mms2r_att_media.rb +0 -54
  123. data/test/test_mms2r_cingular_me_media.rb +0 -51
  124. data/test/test_mms2r_dobson_media.rb +0 -46
  125. data/test/test_mms2r_helio_media.rb +0 -49
  126. data/test/test_mms2r_m_mode_media.rb +0 -36
  127. data/test/test_mms2r_my_cingular_media.rb +0 -31
  128. data/test/test_mms2r_orange_france_media.rb +0 -53
  129. data/test/test_mms2r_orange_poland_media.rb +0 -42
  130. data/test/test_mms2r_sprint_media.rb +0 -269
  131. data/test/test_mms2r_vtext_media.rb +0 -28
@@ -1,3 +1,7 @@
1
+ require 'set'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
1
5
  module MMS2R
2
6
  module TestHelper
3
7
 
@@ -12,3 +16,38 @@ module MMS2R
12
16
  end
13
17
  end
14
18
  end
19
+
20
+ class Hash
21
+ def except(*keys)
22
+ rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
23
+ reject { |key,| rejected.include?(key) }
24
+ end
25
+
26
+ def except!(*keys)
27
+ replace(except(*keys))
28
+ end
29
+ end
30
+
31
+ # monkey patch Net::HTTP so un caged requests don't go over the wire
32
+ module Net #:nodoc:
33
+ class HTTP #:nodoc:
34
+ alias :old_net_http_request :request
35
+ alias :old_net_http_connect :connect
36
+
37
+ def request(req, body = nil, &block)
38
+ prot = use_ssl ? "https" : "http"
39
+ uri_cls = use_ssl ? URI::HTTPS : URI::HTTP
40
+ query = req.path.split('?',2)
41
+ opts = {:host => self.address,
42
+ :port => self.port, :path => query[0]}
43
+ opts[:query] = query[1] if query[1]
44
+ uri = uri_cls.build(opts)
45
+ raise ArgumentError.new("#{req.method} method to #{uri} not being handled in testing")
46
+ end
47
+
48
+ def connect
49
+ raise ArgumentError.new("connect not being handled in testing")
50
+ end
51
+
52
+ end
53
+ end
@@ -1,36 +1,32 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- require File.dirname(__FILE__) + "/test_helper"
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
3
  require 'test/unit'
4
4
  require 'rubygems'
5
- require 'mms2r'
6
- require 'mms2r/media'
7
- require 'tmail/mail'
8
- require 'logger'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
9
8
 
10
-
11
- class MMS2R::AlltelMediaTest < Test::Unit::TestCase
9
+ class TestMessageAlltelCom < Test::Unit::TestCase
12
10
  include MMS2R::TestHelper
13
11
 
14
12
  def test_alltel_image
15
13
  mail = TMail::Mail.parse(load_mail('alltel-image-01.mail').join)
16
- mms = MMS2R::Media.create(mail)
17
- assert_equal mms.class, MMS2R::AlltelMedia
18
-
19
- mms.process
14
+ mms = MMS2R::Media.new(mail)
20
15
 
21
- assert_equal 1, mms.media.size, "Size is #{mms.media.size}"
22
- assert_not_nil mms.media['image/jpeg'][0]
16
+ assert_equal 1, mms.media.size
17
+ assert_not_nil mms.media['image/jpeg']
18
+ assert_equal 2, mms.media['image/jpeg'].size
23
19
  assert_match(/eastern sky.jpg$/, mms.media['image/jpeg'][0])
24
-
20
+ assert_match(/eastern sky.jpg$/, mms.media['image/jpeg'][1])
25
21
  assert_file_size mms.media['image/jpeg'][0], 337
22
+ assert_file_size mms.media['image/jpeg'][1], 337
26
23
 
27
24
  mms.purge
28
25
  end
29
26
 
30
27
  def test_default_media_should_return_user_generated_content
31
28
  mail = TMail::Mail.parse(load_mail('alltel-image-01.mail').join)
32
- mms = MMS2R::Media.create(mail)
33
- mms.process
29
+ mms = MMS2R::Media.new(mail)
34
30
  file = mms.default_media
35
31
  assert_equal 'eastern sky.jpg', file.original_filename
36
32
  mms.purge
@@ -1,19 +1,17 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- require File.dirname(__FILE__) + "/test_helper"
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
3
  require 'test/unit'
4
4
  require 'rubygems'
5
- require 'mms2r'
6
- require 'mms2r/media'
7
- require 'tmail/mail'
8
- require 'logger'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
9
8
 
10
- class MMS2R::NextelMediaTest < Test::Unit::TestCase
9
+ class TestMessagingNextelCom < Test::Unit::TestCase
11
10
  include MMS2R::TestHelper
12
11
 
13
12
  def test_simple_text_is_nil
14
13
  mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
15
- mms = MMS2R::Media.create(mail)
16
- mms.process
14
+ mms = MMS2R::Media.new(mail)
17
15
 
18
16
  assert_nil mms.default_text
19
17
 
@@ -22,8 +20,7 @@ class MMS2R::NextelMediaTest < Test::Unit::TestCase
22
20
 
23
21
  def test_simple_default_media
24
22
  mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
25
- mms = MMS2R::Media.create(mail)
26
- mms.process
23
+ mms = MMS2R::Media.new(mail)
27
24
 
28
25
  file = mms.default_media
29
26
  assert_file_size file, 337
@@ -36,8 +33,7 @@ class MMS2R::NextelMediaTest < Test::Unit::TestCase
36
33
 
37
34
  def test_simple_image1
38
35
  mail = TMail::Mail.parse(load_mail('nextel-image-01.mail').join)
39
- mms = MMS2R::Media.create(mail)
40
- mms.process
36
+ mms = MMS2R::Media.new(mail)
41
37
 
42
38
  assert_equal 1, mms.media.size
43
39
  assert_nil mms.media['text/plain']
@@ -52,8 +48,7 @@ class MMS2R::NextelMediaTest < Test::Unit::TestCase
52
48
 
53
49
  def test_simple_image2
54
50
  mail = TMail::Mail.parse(load_mail('nextel-image-02.mail').join)
55
- mms = MMS2R::Media.create(mail)
56
- mms.process
51
+ mms = MMS2R::Media.new(mail)
57
52
 
58
53
  assert_equal 1, mms.media.size
59
54
  assert_nil mms.media['text/plain']
@@ -68,8 +63,7 @@ class MMS2R::NextelMediaTest < Test::Unit::TestCase
68
63
 
69
64
  def test_simple_image3
70
65
  mail = TMail::Mail.parse(load_mail('nextel-image-03.mail').join)
71
- mms = MMS2R::Media.create(mail)
72
- mms.process
66
+ mms = MMS2R::Media.new(mail)
73
67
 
74
68
  assert_equal 1, mms.media.size
75
69
  assert_nil mms.media['text/plain']
@@ -84,8 +78,7 @@ class MMS2R::NextelMediaTest < Test::Unit::TestCase
84
78
 
85
79
  def test_simple_image4
86
80
  mail = TMail::Mail.parse(load_mail('nextel-image-04.mail').join)
87
- mms = MMS2R::Media.create(mail)
88
- mms.process
81
+ mms = MMS2R::Media.new(mail)
89
82
 
90
83
  assert_equal 1, mms.media.size
91
84
  assert_nil mms.media['text/plain']
@@ -1,20 +1,19 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- require File.dirname(__FILE__) + "/test_helper"
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
3
  require 'test/unit'
4
4
  require 'rubygems'
5
- require 'mms2r'
6
- require 'mms2r/media'
7
- require 'tmail/mail'
8
- require 'logger'
5
+ require 'mocha'
6
+ gem 'tmail', '>= 1.2.1'
7
+ require 'tmail'
9
8
 
10
- class MMS2R::SprintPcsMediaTest < Test::Unit::TestCase
9
+ class TestMessagingSprintpcsCom < Test::Unit::TestCase
11
10
  include MMS2R::TestHelper
12
11
 
13
12
  def test_simple_text
14
13
  mail = TMail::Mail.parse(load_mail('sprint-pcs-text-01.mail').join)
15
- mms = MMS2R::Media.create(mail)
16
- assert_equal MMS2R::SprintPcsMedia, mms.class, "expected a #{MMS2R::SprintPcsMedia} and received a #{mms.class}"
17
- mms.process
14
+ mms = MMS2R::Media.new(mail)
15
+
16
+ assert_equal 1, mms.media.size
18
17
  assert_not_nil mms.media['text/plain']
19
18
  file = mms.media['text/plain'][0]
20
19
  assert_not_nil file
@@ -1,20 +1,16 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- require File.dirname(__FILE__) + "/test_helper"
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "mms2r")
2
+ require File.join(File.dirname(__FILE__), "test_helper")
3
+ require 'tempfile'
3
4
  require 'test/unit'
4
5
  require 'rubygems'
5
- require 'yaml'
6
- require 'fileutils'
7
- require 'mms2r'
8
- require 'tmail/mail'
9
- require 'logger'
6
+ require 'mocha'
7
+ gem 'tmail', '>= 1.2.1'
8
+ require 'tmail'
10
9
 
11
- class MMS2R::MediaTest < Test::Unit::TestCase
10
+ class TestMms2rMedia < Test::Unit::TestCase
12
11
  include MMS2R::TestHelper
13
12
 
14
- class MMS2R::FakeCarrier < MMS2R::Media; end
15
-
16
- JENNYSNUMER = '2068675309'
17
- GENERIC_CARRIER = 'mms.example.com'
13
+ class MMS2R::Media::NullCarrier < MMS2R::Media; end
18
14
 
19
15
  def use_temp_dirs
20
16
  MMS2R::Media.tmp_dir = @tmpdir
@@ -22,10 +18,6 @@ class MMS2R::MediaTest < Test::Unit::TestCase
22
18
  end
23
19
 
24
20
  def setup
25
- @log = Logger.new(STDOUT)
26
- @log.level = Logger::DEBUG
27
- @log.datetime_format = "%H:%M:%S"
28
-
29
21
  @tmpdir = File.join(Dir.tmpdir, "#{Time.now.to_i}-t")
30
22
  FileUtils.mkdir_p(@tmpdir)
31
23
  @confdir = File.join(Dir.tmpdir, "#{Time.now.to_i}-c")
@@ -42,370 +34,536 @@ class MMS2R::MediaTest < Test::Unit::TestCase
42
34
  MMS2R::Media.conf_dir = @oldconfdir
43
35
  end
44
36
 
45
- def test_version
46
- assert MMS2R::Media::VERSION > '0.0.1'
37
+ def stub_mail(*keys)
38
+ attrs = {
39
+ :message_id => '123',
40
+ :from => ['joe@example.com'],
41
+ :multipart? => false,
42
+ :parts => [],
43
+ :content_type => 'text/plain',
44
+ :sub_header => 'message.txt',
45
+ :body => 'a'
46
+ }.except(keys)
47
+ stub('mail', attrs)
47
48
  end
48
49
 
49
- def test_collect_text_multipart_alternative
50
- mail = TMail::Mail.parse(load_mail('simple_multipart_alternative.mail').join)
51
- mms = MMS2R::Media.create(mail)
52
- mms.process
53
- assert_not_nil mms.media['text/plain']
54
- assert_equal 3, mms.media.size
55
- assert_equal 1, mms.media['text/plain'].size
56
- assert_equal 1, mms.media['text/html'].size
57
- assert_equal 1, mms.media['image/gif'].size
50
+ def temp_text_file(text)
51
+ tf = Tempfile.new("test" + Time.now.to_f.to_s)
52
+ tf.puts(text)
53
+ tf.close
54
+ tf.path
55
+ end
58
56
 
59
- file = mms.media['text/plain'][0]
60
- assert_not_nil file
61
- assert File::exist?(file), "file #{file} does not exist"
62
- text = IO.readlines("#{file}").join
63
- assert_equal "This is an MMS message. Hello World.", text
64
- mms.purge
57
+ def test_tmp_dir
58
+ use_temp_dirs()
59
+ MMS2R::Media.tmp_dir = @tmpdir
60
+ assert_equal @tmpdir, MMS2R::Media.tmp_dir
65
61
  end
66
62
 
67
- def test_collect_simple_image
68
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
69
- mms = MMS2R::Media.create(mail)
70
- mms.process
71
- assert_not_nil mms.media['image/gif']
72
- assert_equal 1, mms.media.size
73
- file = mms.media['image/gif'].first
74
- assert_not_nil file
75
- assert File::exist?(file), "file #{file} does not exist"
76
- assert File.basename(file) =~ /spacer\.gif/, "file #{file} does not exist"
77
- mms.purge
63
+ def test_conf_dir
64
+ use_temp_dirs()
65
+ MMS2R::Media.conf_dir = @confdir
66
+ assert_equal @confdir, MMS2R::Media.conf_dir
78
67
  end
79
68
 
80
- def test_collect_simple_image_using_block
81
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
82
- mms = MMS2R::Media.create(mail)
83
- file_array = nil
84
- mms.process do |k, v|
85
- file_array = v if (k == 'image/gif')
86
- assert_not_nil(file = file_array.first)
87
- assert(File::exist?(file), "file #{file} does not exist")
88
- assert(File.basename(file) =~ /spacer\.gif/, "file #{file} does not exist")
89
- end
90
- # mms.purge has to be called manually
91
- assert File.exist?(file_array.first)
69
+ def test_safe_message_id
70
+ mid1_b="1234abcd"
71
+ mid1_a="1234abcd"
72
+ mid2_b="<01234567.0123456789012.JavaMail.fooba@foo-bars999>"
73
+ mid2_a="012345670123456789012JavaMailfoobafoo-bars999"
74
+ assert_equal mid1_a, MMS2R::Media.safe_message_id(mid1_b)
75
+ assert_equal mid2_a, MMS2R::Media.safe_message_id(mid2_b)
92
76
  end
93
77
 
94
- def test_collect_text_plain
95
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
96
- mms = MMS2R::Media.create(mail)
97
- mms.process
98
- assert_not_nil mms.media['text/plain']
99
- assert_equal 1, mms.media.size
100
- file = mms.media['text/plain'][0]
101
- assert_not_nil file
102
- assert File::exist?(file), "file #{file} does not exist"
103
- text = IO.readlines("#{file}").join
104
- assert_equal "hello world", text
105
- mms.purge
78
+ def test_default_ext
79
+ assert_equal nil, MMS2R::Media.default_ext(nil)
80
+ assert_equal 'text', MMS2R::Media.default_ext('text')
81
+ assert_equal 'txt', MMS2R::Media.default_ext('text/plain')
82
+ assert_equal 'test', MMS2R::Media.default_ext('example/test')
106
83
  end
107
84
 
108
- def test_collect_text_multi
109
- mail = TMail::Mail.parse(load_mail('hello_world_mail_multipart.mail').join)
110
- mms = MMS2R::Media.create(mail)
111
- mms.process
112
- assert_not_nil mms.media['text/plain']
113
- assert_not_nil mms.media['application/smil']
114
- assert_equal 2, mms.media.size
115
- file = mms.media['text/plain'][0]
116
- assert_not_nil file
117
- assert File::exist?(file), "file #{file} does not exist"
118
- text = IO.readlines("#{file}").join
119
- assert_equal "hello world", text
120
- mms.purge
85
+ def test_part_type
86
+ part = stub(:content_type => nil)
87
+ assert_equal 'text/plain', MMS2R::Media.part_type?(part)
88
+
89
+ part = stub(:content_type => 'image/jpeg')
90
+ assert_equal 'image/jpeg', MMS2R::Media.part_type?(part)
121
91
  end
122
92
 
123
- def test_purge
124
- mail = TMail::Mail.new
125
- mail.message_id = TMail.new_message_id
126
- mail.from = ["#{JENNYSNUMER}@#{GENERIC_CARRIER}"]
127
- mail.body = "hello world"
128
- mms = MMS2R::Media.create(mail)
129
- mms.process
130
- file = mms.media['text/plain'][0]
131
- assert File::exist?(file), "file #{file} does not exist"
132
- mms.purge
133
- assert !File::exist?(file), "file #{file} should not exist!"
93
+ def test_main_type
94
+ part = stub(:content_type => 'image/jpeg')
95
+ assert_equal 'image', MMS2R::Media.main_type?(part)
134
96
  end
135
97
 
136
- def test_custom_media_carrier
137
- cls = MMS2R::FakeCarrier
138
- host = 'mms.fakecarrier.com'
139
- MMS2R::CARRIER_CLASSES[host] = cls
140
- mail = TMail::Mail.new
141
- mail.from = ["#{JENNYSNUMER}@#{host}"]
142
- mms = MMS2R::Media.create(mail)
143
- assert_equal cls, mms.class, "expected a #{cls} and received a #{mms.class}"
144
- end
145
-
146
- def test_create
147
- MMS2R::CARRIER_CLASSES.each do |car, cls|
148
- mail = TMail::Mail.new
149
- mail.from = ["#{JENNYSNUMER}@#{car}"]
150
- mms = MMS2R::Media.create(mail)
151
- assert_equal cls, mms.class, "expected a #{cls} and received a #{mms.class}"
152
- mms = MMS2R::Media.create(mail)
153
- assert_equal cls, mms.class, "expected a #{cls} and received a #{mms.class}"
154
- assert_equal car, mms.carrier, "expected a #{car} and received a #{mms.carrier}"
155
- end
98
+ def test_sub_type
99
+ part = stub(:content_type => 'image/jpeg')
100
+ assert_equal 'jpeg', MMS2R::Media.sub_type?(part)
156
101
  end
157
102
 
158
- def test_logging
159
- MMS2R::CARRIER_CLASSES.each do |car, cls|
160
- mail = TMail::Mail.new
161
- mail.from = ["#{JENNYSNUMER}@#{car}"]
162
- mms = MMS2R::Media.create(mail,@log)
163
- assert_equal cls, mms.class, "expected a #{cls} and received a #{mms.class}"
164
- end
103
+ def test_base_initialize_config_tries_to_open_default_config_yaml
104
+ f = File.join(MMS2R::Media.conf_dir, 'mms2r_media.yml')
105
+ YAML.expects(:load_file).once.with(f).returns({})
106
+ config = MMS2R::Media.initialize_config(nil)
165
107
  end
166
108
 
167
- def test_tmp_dir
168
- use_temp_dirs()
169
- MMS2R::Media.tmp_dir = @tmpdir
170
- assert MMS2R::Media.tmp_dir.eql?(@tmpdir)
109
+ def test_base_initialize_config
110
+ config = MMS2R::Media.initialize_config(nil)
111
+
112
+ # test defaults shipped in mms2r_media.yml
113
+ assert_not_nil config
114
+ assert_not_nil config['ignore']
115
+ assert_not_nil config['ignore']['text/plain']
116
+ assert_not_nil config['ignore']['text/plain'].detect{|v| v == '/^\(no subject\)$/i'}
117
+ assert_not_nil config['ignore']['multipart/mixed']
118
+ assert_not_nil config['ignore']['multipart/mixed'].detect{|v| v == '/^Attachment: /i'}
119
+ assert_nil config['ignore']['text/plain'].detect{|v| v == '/A TEST/'}
171
120
  end
172
121
 
173
- def test_conf_dir
174
- use_temp_dirs()
175
- MMS2R::Media.conf_dir = @confdir
176
- assert MMS2R::Media.conf_dir.eql?(@confdir)
122
+ def test_initialize_config_contatenation
123
+ c = {'ignore' => {'text/plain' => ['/A TEST/']} }
124
+ config = MMS2R::Media.initialize_config(c)
125
+ assert_not_nil config['ignore']['text/plain'].detect{|v| v == '/^\(no subject\)$/i'}
126
+ assert_not_nil config['ignore']['text/plain'].detect{|v| v == '/A TEST/'}
177
127
  end
178
128
 
179
- def test_transform_text
180
- use_temp_dirs()
181
- t={"hello world" => "foo bar"}
182
- h={'text/plain' => t}
183
- f = File.join(MMS2R::Media.conf_dir, 'mms2r_media_transform.yml')
184
- File.open(f, 'w') do |out|
185
- YAML.dump(h, out)
186
- end
187
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
129
+ def test_create_with_default_processor
130
+ mail = mock()
131
+ mail.expects(:from).at_least_once.returns(['joe@unknown.example.com'])
188
132
  mms = MMS2R::Media.create(mail)
189
- mms.process
190
- assert_not_nil mms.media['text/plain']
191
- file = mms.media['text/plain'][0]
192
- assert_not_nil file
193
- assert File::exist?(file), "file #{file} does not exist"
194
- text = IO.readlines("#{file}").join
195
- assert_equal "foo bar", text
196
- mms.purge
133
+ assert_equal MMS2R::Media, mms
197
134
  end
198
135
 
199
- def test_transform_text_for_application_smil
200
- use_temp_dirs()
201
- t={"Water" => "Juice"}
202
- h={'application/smil' => t}
203
- f = File.join(MMS2R::Media.conf_dir, 'mms2r_media_transform.yml')
204
- File.open(f, 'w') do |out|
205
- YAML.dump(h, out)
206
- end
207
- mail = TMail::Mail.parse(load_mail('hello_world_mail_multipart.mail').join)
136
+ def test_create_with_special_processor
137
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
138
+ mail = mock()
139
+ mail.expects(:from).at_least_once.returns(['joe@null.example.com'])
208
140
  mms = MMS2R::Media.create(mail)
209
- mms.process
210
- assert_not_nil mms.media['application/smil']
211
- file = mms.media['application/smil'][0]
212
- assert_not_nil file
213
- assert File::exist?(file), "file #{file} does not exist"
214
- text = IO.readlines("#{file}").join
215
- assert_equal "Juice", text
216
- mms.purge
141
+ assert_equal MMS2R::Media::NullCarrier, mms
217
142
  end
218
143
 
219
- def test_mms_with_two_images_should_default_media_to_largest_file
220
- mail = TMail::Mail.parse(load_mail('simple-with-two-images-two-texts.mail').join)
221
- mms = MMS2R::Media.create(mail)
222
- mms.process
223
- file = mms.default_media
224
- assert_equal 'big.jpg', file.original_filename
225
- mms.purge
144
+ def test_create_should_fail_gracefully_with_broken_from
145
+ mail = mock()
146
+ mail.expects(:from).at_least_once.returns(nil)
147
+ assert_nothing_raised { MMS2R::Media.create(mail) }
226
148
  end
227
149
 
228
- def test_mms_with_two_texts_should_text_to_largest_file
229
- mail = TMail::Mail.parse(load_mail('simple-with-two-images-two-texts.mail').join)
230
- mms = MMS2R::Media.create(mail)
231
- mms.process
232
- file = mms.default_text
233
- assert_equal 'big.txt', file.original_filename
234
- mms.purge
150
+ def test_aliased_new_returns_custom_processor_instance
151
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
152
+ mail = stub_mail(:from)
153
+ mail.expects(:from).at_least_once.returns(['joe@null.example.com'])
154
+
155
+ mms = MMS2R::Media.new(mail)
156
+ assert_not_nil mms
157
+ assert_equal MMS2R::Media::NullCarrier, mms.class
158
+ assert_equal true, mms.respond_to?(:process)
235
159
  end
236
160
 
237
- def test_mms_should_have_a_phone_number
238
- mail = TMail::Mail.parse(load_mail('hello_world_empty_text.mail').join)
239
- mms = MMS2R::Media.create(mail)
240
- mms.process
241
- assert_equal '2068675309', mms.number
242
- mms.purge
161
+ def test_aliased_new_returns_default_processor_instance
162
+ mms = MMS2R::Media.new(stub_mail())
163
+ assert_not_nil mms
164
+ assert_equal true, mms.respond_to?(:process)
165
+ assert_equal MMS2R::Media, mms.class
243
166
  end
244
167
 
245
- def test_ignore_text
246
- use_temp_dirs()
247
- a=[/^hello world$/]
248
- h={'text/plain' => a}
249
- f = File.join(MMS2R::Media.conf_dir, 'mms2r_media_ignore.yml')
250
- File.open(f, 'w') do |out|
251
- YAML.dump(h, out)
252
- end
253
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
254
- mms = MMS2R::Media.create(mail)
255
- mms.process
256
- assert mms.media['text/plain'].nil?
257
- assert_equal 0, mms.media.size
258
- mms.purge
168
+ def test_lazy_process_option
169
+ mms = MMS2R::Media.new(stub_mail(), :process => :lazy)
170
+ mms.expects(:process).never
259
171
  end
260
172
 
261
- def test_ignore_media
262
- use_temp_dirs()
263
- a=["spacer.gif"]
264
- h={'image/gif' => a}
265
- f = File.join(MMS2R::Media.conf_dir, 'mms2r_media_ignore.yml')
266
- File.open(f, 'w') do |out|
267
- YAML.dump(h, out)
268
- end
269
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
270
- mms = MMS2R::Media.create(mail)
271
- mms.process
272
- assert mms.media['image/gif'].nil?
273
- assert_equal 0, mms.media.size
274
- mms.purge
173
+ def test_logger_option
174
+ logger = mock()
175
+ logger.expects(:info).at_least_once
176
+ mms = MMS2R::Media.new(stub_mail(), :logger => logger)
275
177
  end
276
178
 
277
- def test_when_ignore_media_does_nothing
278
- use_temp_dirs()
279
- a=["foo.gif"]
280
- h={'image/gif' => a}
281
- f = File.join(MMS2R::Media.conf_dir, 'mms2r_media_ignore.yml')
282
- File.open(f, 'w') do |out|
283
- YAML.dump(h, out)
179
+ def test_default_processor_initialize_tries_to_open_config_for_carrier
180
+ f = File.join(MMS2R::Media.conf_dir, 'example.com.yml')
181
+ YAML.expects(:load_file).once.with(f)
182
+ mms = MMS2R::Media.new(stub_mail())
183
+ end
184
+
185
+ def test_mms_phone_number
186
+ mail = stub_mail()
187
+ mail.stubs(:from).returns(['2068675309@example.com'])
188
+ mms = MMS2R::Media.new(mail)
189
+ assert_equal '2068675309', mms.number
190
+ end
191
+
192
+ def test_mms_phone_number_with_errors
193
+ mail = stub_mail(:from)
194
+ mail.stubs(:from).returns(nil)
195
+ mms = MMS2R::Media.new(mail)
196
+ assert_nothing_raised do
197
+ assert_equal '', mms.number
284
198
  end
285
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
286
- mms = MMS2R::Media.create(mail)
287
- mms.process
288
- assert_equal 1, mms.media['image/gif'].size
289
- assert_equal 1, mms.media.size
290
- mms.purge
291
199
  end
292
200
 
293
- def test_safe_message_id
294
- mid1_b="1234abcd"
295
- mid1_a="1234abcd"
296
- mid2_b="<01234567.0123456789012.JavaMail.fooba@foo-bars999>"
297
- mid2_a="012345670123456789012JavaMailfoobafoo-bars999"
298
- assert MMS2R::Media.safe_message_id(mid1_b).eql?(mid1_a)
299
- assert MMS2R::Media.safe_message_id(mid2_b).eql?(mid2_a)
201
+ def test_transform_text
202
+ mail = stub_mail()
203
+ mail.stubs(:from).returns(nil)
204
+ mms = MMS2R::Media.new(mail)
205
+
206
+ type = 'test/type'
207
+ text = 'hello'
208
+
209
+ # no match in the config
210
+ result = [type, text]
211
+ assert_equal result, mms.transform_text(type, text)
212
+
213
+ # has a bad regexp
214
+ mms.expects(:config).once.returns({'transform' => {type => [['(hello)', 'world']]}})
215
+ assert_equal result, mms.transform_text(type, text)
216
+
217
+ # matches in config
218
+ mms.expects(:config).once.returns({'transform' => {type => [['/(hello)/', 'world']]}})
219
+ assert_equal [type, 'world'], mms.transform_text(type, text)
220
+
221
+ mms.expects(:config).once.returns({'transform' => {type => [['/^Ignore this part, (.+)/', '\1']]}})
222
+ assert_equal [type, text], mms.transform_text(type, "Ignore this part, " + text)
223
+
224
+ # chaining transforms
225
+ mms.expects(:config).once.returns({'transform' => {type => [['/(hello)/', 'world'],
226
+ ['/(world)/', 'mars']]}})
227
+ assert_equal [type, 'mars'], mms.transform_text(type, text)
228
+
229
+ # has a Iconv problem
230
+ Iconv.expects(:new).raises
231
+ mms.expects(:config).once.returns({'transform' => {type => [['(hello)', 'world']]}})
232
+ assert_equal result, mms.transform_text(type, text)
300
233
  end
301
234
 
302
- def default_ext
303
- assert MMS2R::Media.default_ext('text').nil?
304
- assert MMS2R::Media.default_ext('text/plain').eql?('plain')
305
- assert MMS2R::Media.default_ext('image/jpeg').eql?('jpeg')
306
- assert MMS2R::Media.default_ext('video/mp4').eql?('mp4')
235
+ def test_subject
236
+ s = 'hello world'
237
+ mail = stub_mail()
238
+ mail.stubs(:subject).returns(s)
239
+ mms = MMS2R::Media.new(mail)
240
+ assert_equal s, mms.subject
241
+
242
+ # second time through shouldn't process the subject again
243
+ mail.expects(:subject).never
244
+ assert_equal s, mms.subject
307
245
  end
308
246
 
309
- def test_part_type
310
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
311
- assert MMS2R::Media.part_type?(mail).eql?('text/plain')
312
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_with_content_type.mail').join)
313
- assert MMS2R::Media.part_type?(mail).eql?('text/plain')
314
- mail = TMail::Mail.parse(load_mail('hello_world_mail_multipart.mail').join)
315
- assert MMS2R::Media.part_type?(mail.parts[0]).eql?('text/plain')
316
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
317
- assert MMS2R::Media.part_type?(mail.parts[0]).eql?('image/gif')
247
+ def test_subject_with_bad_mail_subject
248
+ mail = stub_mail()
249
+ mail.stubs(:subject).returns(nil)
250
+ mms = MMS2R::Media.new(mail)
251
+ assert_equal '', mms.subject
318
252
  end
319
253
 
320
- def test_main_type
321
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
322
- assert MMS2R::Media.main_type?(mail).eql?('text')
323
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_with_content_type.mail').join)
324
- assert MMS2R::Media.main_type?(mail).eql?('text')
325
- mail = TMail::Mail.parse(load_mail('hello_world_mail_multipart.mail').join)
326
- assert MMS2R::Media.main_type?(mail.parts[0]).eql?('text')
327
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
328
- assert MMS2R::Media.main_type?(mail.parts[0]).eql?('image')
254
+ def test_subject_with_subject_ignored
255
+ s = 'hello world'
256
+ mail = stub_mail()
257
+ mail.stubs(:subject).returns(s)
258
+ mms = MMS2R::Media.new(mail)
259
+ mms.stubs(:config).returns({'ignore' => {'text/plain' => [s]}})
260
+ assert_equal '', mms.subject
329
261
  end
330
262
 
331
- def test_sub_type
332
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
333
- assert MMS2R::Media.sub_type?(mail).eql?('plain')
334
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_with_content_type.mail').join)
335
- assert MMS2R::Media.sub_type?(mail).eql?('plain')
336
- mail = TMail::Mail.parse(load_mail('hello_world_mail_multipart.mail').join)
337
- assert MMS2R::Media.sub_type?(mail.parts[0]).eql?('plain')
338
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
339
- assert MMS2R::Media.sub_type?(mail.parts[0]).eql?('gif')
263
+ def test_subject_with_subject_transformed
264
+ s = 'Default Subject: hello world'
265
+ mail = stub_mail()
266
+ mail.stubs(:subject).returns(s)
267
+ mms = MMS2R::Media.new(mail)
268
+ mms.stubs(:config).returns({'transform' => {'text/plain' => [['/Default Subject: (.+)/', '\1']]}})
269
+ assert_equal 'hello world', mms.subject
340
270
  end
341
271
 
342
- def test_subject
343
- subjects = [nil, '', '(no subject)']
344
-
345
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
346
- subjects.each do |s|
347
- mail.subject = s
348
- mms = MMS2R::Media.create(mail)
349
- mms.process
350
- assert_equal nil, mms.subject, "Default subject not scrubbed."
351
- mms.purge
352
- end
272
+ def test_attachment_should_return_nil_if_files_for_type_are_not_found
273
+ mms = MMS2R::Media.new(stub_mail())
274
+ mms.stubs(:media).returns({})
275
+ assert_nil mms.send(:attachment, ['text'])
276
+ end
353
277
 
354
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
355
- mms = MMS2R::Media.create(mail)
356
- mms.process
357
- assert_equal 'text only', mms.subject
358
- mms.purge
278
+ def test_attachment_should_return_nil_if_empty_files_are_found
279
+ mms = MMS2R::Media.new(stub_mail())
280
+ mms.stubs(:media).returns({'text/plain' => [Tempfile.new('test')]})
281
+ assert_nil mms.send(:attachment, ['text'])
359
282
  end
360
-
283
+
284
+ def test_attachment_should_return_duck_typed_file
285
+ mms = MMS2R::Media.new(stub_mail())
286
+ temp_big = temp_text_file("hello world")
287
+ size = File.size(temp_text_file("hello world"))
288
+ temp_small = temp_text_file("hello")
289
+ mms.stubs(:media).returns({'text/plain' => [temp_small, temp_big]})
290
+ duck_file = mms.send(:attachment, ['text'])
291
+ assert_not_nil duck_file
292
+ assert_equal true, File::exist?(duck_file)
293
+ assert_equal true, File::exist?(temp_big)
294
+ assert_equal temp_big, duck_file.local_path
295
+ assert_equal File.basename(temp_big), duck_file.original_filename
296
+ assert_equal size, duck_file.size
297
+ assert_equal 'text/plain', duck_file.content_type
298
+ end
299
+
300
+ def test_empty_body
301
+ mms = MMS2R::Media.new(stub_mail())
302
+ mms.stubs(:default_text).returns(nil)
303
+ assert_equal "", mms.body
304
+ end
305
+
361
306
  def test_body
362
- mail = TMail::Mail.parse(load_mail('hello_world_mail_plain_no_content_type.mail').join)
363
- mms = MMS2R::Media.create(mail)
364
- mms.process
365
- assert_equal 'hello world', mms.body
307
+ mms = MMS2R::Media.new(stub_mail())
308
+ temp_big = temp_text_file("hello world")
309
+ mms.stubs(:default_text).returns(File.new(temp_big))
310
+ assert_equal "hello world", mms.body
311
+ end
312
+
313
+ def test_default_text
314
+ mms = MMS2R::Media.new(stub_mail())
315
+ temp_big = temp_text_file("hello world")
316
+ temp_small = temp_text_file("hello")
317
+ mms.stubs(:media).returns({'text/plain' => [temp_small, temp_big]})
318
+
319
+ assert_equal temp_big, mms.default_text.local_path
320
+
321
+ # second time through shouldn't setup the @default_text by calling attachment
322
+ mms.expects(:attachment).never
323
+ assert_equal temp_big, mms.default_text.local_path
324
+ end
325
+
326
+ def test_default_media
327
+ mms = MMS2R::Media.new(stub_mail())
328
+ #it doesn't matter that these are text files, we just need say they are images
329
+ temp_big = temp_text_file("hello world")
330
+ temp_small = temp_text_file("hello")
331
+ mms.stubs(:media).returns({'image/jpeg' => [temp_small, temp_big]})
332
+
333
+ assert_equal temp_big, mms.default_media.local_path
334
+
335
+ # second time through shouldn't setup the @default_media by calling attachment
336
+ mms.expects(:attachment).never
337
+ assert_equal temp_big, mms.default_media.local_path
338
+ end
339
+
340
+ def test_default_media_treats_image_and_video_equally
341
+ mms = MMS2R::Media.new(stub_mail())
342
+ #it doesn't matter that these are text files, we just need say they are images
343
+ temp_big_image = temp_text_file("hello world")
344
+ temp_small_image = temp_text_file("hello")
345
+ temp_big_video = temp_text_file("hello world again")
346
+ temp_small_video = temp_text_file("hello again")
347
+ mms.stubs(:media).returns({'image/jpeg' => [temp_small_image, temp_big_image],
348
+ 'video/mpeg' => [temp_small_video, temp_big_video],
349
+ })
350
+
351
+ assert_equal temp_big_video, mms.default_media.local_path
352
+
353
+ # second time through shouldn't setup the @default_media by calling attachment
354
+ mms.expects(:attachment).never
355
+ assert_equal temp_big_video, mms.default_media.local_path
356
+ end
357
+
358
+ def test_purge
359
+ mms = MMS2R::Media.new(stub_mail())
366
360
  mms.purge
361
+ assert_equal false, File.exist?(mms.media_dir)
362
+ end
363
+
364
+ def test_ignore_media_by_filename_equality
365
+ name = 'foo.txt'
366
+ type = 'text/plain'
367
+ mms = MMS2R::Media.new(stub_mail())
368
+ mms.stubs(:config).returns({'ignore' => {type => [name]}})
369
+
370
+ # type is not in the ingore
371
+ part = stub(:sub_header => name)
372
+ assert_equal false, mms.ignore_media?('text/test', part)
373
+ # type and filename are in the ingore
374
+ part = stub(:sub_header => name)
375
+ assert_equal true, mms.ignore_media?(type, part)
376
+ # type but not file name are in the ignore
377
+ part = stub(:sub_header => 'bar.txt')
378
+ assert_equal false, mms.ignore_media?(type, part)
379
+ end
380
+
381
+ def test_ignore_media_by_filename_regexp
382
+ name = 'foo.txt'
383
+ regexp = '/foo\.txt/i'
384
+ type = 'text/plain'
385
+ mms = MMS2R::Media.new(stub_mail())
386
+ mms.stubs(:config).returns({'ignore' => {type => [regexp, 'nil.txt']}})
387
+
388
+ # type is not in the ingore
389
+ part = stub(:sub_header => name)
390
+ assert_equal false, mms.ignore_media?('text/test', part)
391
+ # type and regexp for the filename are in the ingore
392
+ part = stub(:sub_header => name)
393
+ assert_equal true, mms.ignore_media?(type, part)
394
+ # type but not regexp for filename are in the ignore
395
+ part = stub(:sub_header => 'bar.txt')
396
+ assert_equal false, mms.ignore_media?(type, part)
397
+ end
398
+
399
+ def test_ignore_media_by_regexp_on_file_content
400
+ name = 'foo.txt'
401
+ content = "aaaaaaahello worldbbbbbbbbb"
402
+ regexp = '/.*Hello World.*/i'
403
+ type = 'text/plain'
404
+ mms = MMS2R::Media.new(stub_mail())
405
+ mms.stubs(:config).returns({'ignore' => {type => ['nil.txt', regexp]}})
406
+
407
+ part = stub(:sub_header => name, :body => content)
408
+
409
+ # type is not in the ingore
410
+ assert_equal false, mms.ignore_media?('text/test', part)
411
+ # type and regexp for the content are in the ingore
412
+ assert_equal true, mms.ignore_media?(type, part)
413
+ # type but not regexp for content are in the ignore
414
+ part = stub(:sub_header => name, :body => 'no teapots')
415
+ assert_equal false, mms.ignore_media?(type, part)
416
+ end
417
+
418
+ def test_ignore_media_when_file_content_is_empty
419
+ mms = MMS2R::Media.new(stub_mail())
420
+
421
+ # there is no conf but the part's body is empty
422
+ part = stub(:sub_header => 'foo.txt', :body => "")
423
+ assert_equal true, mms.ignore_media?('text/test', part)
424
+
425
+ # there is no conf but the part's body is white space
426
+ part = stub(:sub_header => 'foo.txt', :body => "\t\n\t\n ")
427
+ assert_equal true, mms.ignore_media?('text/test', part)
428
+ end
429
+
430
+ def test_add_file
431
+ MMS2R.register('null.example.com', MMS2R::Media::NullCarrier)
432
+ mail = stub_mail()
433
+ mail.stubs(:from).returns(['joe@null.example.com'])
434
+
435
+ mms = MMS2R::Media.new(mail)
436
+ mms.stubs(:media).returns({})
437
+
438
+ assert_nil mms.media['text/html']
439
+ mms.add_file('text/html', '/tmp/foo.html')
440
+ assert_equal ['/tmp/foo.html'], mms.media['text/html']
441
+ mms.add_file('text/html', '/tmp/bar.html')
442
+ assert_equal ['/tmp/foo.html', '/tmp/bar.html'], mms.media['text/html']
367
443
  end
368
444
 
369
- def test_attachment_should_return_duck_typed_file
445
+ def test_temp_file
446
+ name = 'foo.txt'
447
+ mms = MMS2R::Media.new(stub_mail())
448
+ part = stub(:sub_header => name)
449
+ assert_equal name, File.basename(mms.temp_file(part))
450
+ end
370
451
 
371
- mail = TMail::Mail.parse(load_mail('simple_image.mail').join)
372
- mms = MMS2R::Media.create(mail)
373
- mms.process
374
-
375
- file_size = 43
376
- base_name = 'spacer.gif'
377
- mime_type = 'image/gif'
378
-
379
- test = mms.media[mime_type].first
380
- assert_not_nil test
381
- assert_file_size test, file_size
382
- assert File::exist?(test), "file #{test} does not exist"
383
- assert_equal base_name, File.basename(test), "file #{test} does not exist as #{base_name}"
384
-
385
- # default_media calls attachment and
386
- # attachment should return a file that has some duck sauce for
387
- # act_as_attachment and attachment_fu
388
- file = mms.default_media
389
- assert_not_nil file, "file #{file} does not exist"
390
- assert_equal test, file.local_path
391
- assert_equal base_name, file.original_filename
392
- assert_equal file_size, file.size
393
- assert_equal mime_type, file.content_type
452
+ def test_process_media_for_text
453
+ name = 'foo.txt'
454
+ mms = MMS2R::Media.new(stub_mail())
455
+ mms.stubs(:transform_text_part).returns(['text/plain', nil])
456
+ part = stub(:sub_header => name, :content_type => 'text/plain')
457
+
458
+ assert_equal ['text/plain', nil], mms.process_media(part)
459
+
460
+ mms.stubs(:transform_text_part).returns(['text/plain', 'hello world'])
461
+ result = mms.process_media(part)
462
+ assert_equal 'text/plain', result.first
463
+ assert_equal 'hello world', IO.read(result.last)
464
+ mms.purge # have to call purge since a file is put to disk as side effect
465
+ end
394
466
 
467
+ def test_process_media_for_application_smil
468
+ name = 'foo.txt'
469
+ mms = MMS2R::Media.new(stub_mail())
470
+ mms.stubs(:transform_text_part).returns(['application/smil', nil])
471
+ part = stub(:sub_header => name, :content_type => 'application/smil')
472
+
473
+ assert_equal ['application/smil', nil], mms.process_media(part)
474
+
475
+ mms.stubs(:transform_text_part).returns(['application/smil', 'hello world'])
476
+ result = mms.process_media(part)
477
+ assert_equal 'application/smil', result.first
478
+ assert_equal 'hello world', IO.read(result.last)
479
+ mms.purge # have to call purge since a file is put to disk as side effect
480
+ end
481
+
482
+ def test_process_media_for_all_other_media
483
+ name = 'foo.txt'
484
+ mms = MMS2R::Media.new(stub_mail())
485
+ part = stub(:sub_header => name, :content_type => 'faux/text', :body => nil)
486
+
487
+ assert_equal ['faux/text', nil], mms.process_media(part)
488
+
489
+ part = stub(:sub_header => name, :content_type => 'faux/text', :body => 'hello world')
490
+ result = mms.process_media(part)
491
+ assert_equal 'faux/text', result.first
492
+ assert_equal 'hello world', IO.read(result.last)
493
+ mms.purge # have to call purge since a file is put to disk as side effect
494
+ end
495
+
496
+ def test_process
497
+ mms = MMS2R::Media.new(stub_mail())
498
+ assert_equal 1, mms.media.size
499
+ assert_not_nil mms.media['text/plain']
500
+ assert_equal 1, mms.media['text/plain'].size
501
+ assert_equal 'message.txt', File.basename(mms.media['text/plain'].first)
502
+ assert_equal true, File.exist?(mms.media['text/plain'].first)
503
+ assert_equal 1, File.size(mms.media['text/plain'].first)
395
504
  mms.purge
396
505
  end
397
506
 
398
- def test_yaml_file_name
399
- assert_equal 'mms2r_my_cingular_media_subject.yml', MMS2R::Media.yaml_file_name(MMS2R::MyCingularMedia,:subject)
400
- assert_equal 'mms2r_t_mobile_media_subject.yml', MMS2R::Media.yaml_file_name(MMS2R::TMobileMedia,:subject)
401
- assert_equal 'mms2r_media_ignore.yml', MMS2R::Media.yaml_file_name(MMS2R::MyCingularMedia.superclass,:ignore)
402
- assert_equal 'mms2r_media_transform.yml', MMS2R::Media.yaml_file_name(MMS2R::Media,:transform)
507
+ def test_process_with_multipart_alternative_parts
508
+ mail = stub_mail()
509
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :body => 'a')
510
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :body => 'a')
511
+ multi = stub(:content_type => 'multipart/alternative', :parts => [plain, html])
512
+ mail.stubs(:multipart?).returns(true)
513
+ mail.stubs(:parts).returns([multi])
514
+
515
+ # the multipart/alternative should get flattend to text and html
516
+ mms = MMS2R::Media.new(mail)
517
+ assert_equal 2, mms.media.size
518
+ assert_not_nil mms.media['text/plain']
519
+ assert_not_nil mms.media['text/html']
520
+ assert_equal 1, mms.media['text/plain'].size
521
+ assert_equal 1, mms.media['text/html'].size
522
+ assert_equal 'message.txt', File.basename(mms.media['text/plain'].first)
523
+ assert_equal 'message.html', File.basename(mms.media['text/html'].first)
524
+ assert_equal true, File.exist?(mms.media['text/plain'].first)
525
+ assert_equal true, File.exist?(mms.media['text/html'].first)
526
+ assert_equal 1, File.size(mms.media['text/plain'].first)
527
+ assert_equal 1, File.size(mms.media['text/html'].first)
528
+ mms.purge
403
529
  end
404
-
405
- def test_create_should_fail_gracefully_with_broken_from
406
- assert_nothing_raised do
407
- mail = TMail::Mail.parse(load_mail('broken_from_spam.mail').join)
408
- mms = MMS2R::Media.create(mail)
530
+
531
+ def test_process_when_media_is_ignored
532
+ mail = stub_mail()
533
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :body => '')
534
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :body => '')
535
+ multi = stub(:content_type => 'multipart/alternative', :parts => [plain, html])
536
+ mail.stubs(:multipart?).returns(true)
537
+ mail.stubs(:parts).returns([multi])
538
+ mms = MMS2R::Media.new(mail, :process => :lazy)
539
+ mms.stubs(:config).returns({'ignore' => {'text/plain' => ['message.txt'],
540
+ 'text/html' => ['message.html']}})
541
+ assert_nothing_raised { mms.process }
542
+ # the multipart/alternative should get flattend to text and html and then
543
+ # what's flattened is ignored
544
+ assert_equal 0, mms.media.size
545
+ mms.purge
546
+ end
547
+
548
+ def test_process_when_yielding_to_a_block
549
+ mail = stub_mail()
550
+
551
+ plain = stub(:sub_header => 'message.txt', :content_type => 'text/plain', :body => 'a')
552
+ html = stub(:sub_header => 'message.html', :content_type => 'text/html', :body => 'b')
553
+ mail.stubs(:multipart?).returns(true)
554
+ mail.stubs(:parts).returns([plain, html])
555
+
556
+ # the multipart/alternative should get flattend to text and html
557
+ mms = MMS2R::Media.new(mail)
558
+ assert_equal 2, mms.media.size
559
+ mms.process do |type, files|
560
+ assert_equal 1, files.size
561
+ assert_equal true, type == 'text/plain' || type == 'text/html'
562
+ assert_equal true, File.basename(files.first) == 'message.txt' ||
563
+ File.basename(files.first) == 'message.html'
564
+ assert_equal true, File::exist?(files.first)
409
565
  end
566
+ mms.purge
410
567
  end
568
+
411
569
  end