# HG changeset patch
# User Toshi MARUYAMA
# Date 1353286834 -32400
# Node ID 49118568253be6ccc7024360c9067b8ed9435b61
# Parent  50df88f93ef478974baa1a97c334ae1f5a56c83e
gmail_with_attachment.eml

diff --git a/test/fixtures/mail_handler/gmail_with_attachment.eml b/test/fixtures/mail_handler/gmail_with_attachment.eml
new file mode 100644
--- /dev/null
+++ b/test/fixtures/mail_handler/gmail_with_attachment.eml
@@ -0,0 +1,20 @@
+Date: Mon, 19 Nov 2012 10:17:45 +0900
+Message-ID: <CANBr5-U6cXMfLek5QiB2ZrBPR3vTThn9_Upvdkf3Dkod664+Xw@mail.gmail.com>
+Subject: test
+From: John Smith <JSmith@somenet.foo>
+To: redmine@somenet.foo
+Content-Type: multipart/mixed; boundary=bcaec54ee4ea84f77904cecee22e
+
+--bcaec54ee4ea84f77904cecee22e
+Content-Type: text/plain; charset=ISO-8859-1
+
+test
+
+--bcaec54ee4ea84f77904cecee22e
+Content-Type: text/plain; charset=US-ASCII; name="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
+Content-Disposition: attachment; filename="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
+Content-Transfer-Encoding: base64
+X-Attachment-Id: f_h9owndpv0
+
+dGVzdAo=
+--bcaec54ee4ea84f77904cecee22e--
# HG changeset patch
# User Toshi MARUYAMA
# Date 1353320714 -32400
# Node ID 0c8a00ff3c710aac036cb8050a10abbab8115ce3
# Parent  49118568253be6ccc7024360c9067b8ed9435b61
[mq]: tb-mail-attach-01.diff

diff --git a/test/fixtures/mail_handler/thunderbird_with_attachment.eml b/test/fixtures/mail_handler/thunderbird_with_attachment.eml
new file mode 100644
--- /dev/null
+++ b/test/fixtures/mail_handler/thunderbird_with_attachment.eml
@@ -0,0 +1,26 @@
+Message-ID: <50AA00C6.4070108@gmail.com>
+Date: Mon, 19 Nov 2012 18:49:58 +0900
+From: John Smith <JSmith@somenet.foo>
+User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101027 Fedora/3.0.10-1.fc12 Lightning/1.0b1 Thunderbird/3.0.10
+MIME-Version: 1.0
+To: redmine@somenet.foo
+Subject: test
+Content-Type: multipart/mixed;
+ boundary="------------030104060902010800050907"
+
+This is a multi-part message in MIME format.
+--------------030104060902010800050907
+Content-Type: text/plain; charset=ISO-2022-JP
+Content-Transfer-Encoding: 7bit
+
+test
+
+--------------030104060902010800050907
+Content-Type: text/plain;
+ name="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
+Content-Transfer-Encoding: base64
+Content-Disposition: attachment;
+ filename*=ISO-2022-JP''%1B%24%42%25%46%25%39%25%48%1B%28%42%2E%74%78%74
+
+dGVzdAo=
+--------------030104060902010800050907--
# HG changeset patch
# User Toshi MARUYAMA
# Date 1353320717 -32400
# Node ID 7376c97af2181f95e8a8cf39fd25a7ee98f59dfa
# Parent  0c8a00ff3c710aac036cb8050a10abbab8115ce3
[mq]: dummy_mail_handler_test-01.diff

diff --git a/test/unit/mail_handler_test_1.rb b/test/unit/mail_handler_test_1.rb
new file mode 100644
--- /dev/null
+++ b/test/unit/mail_handler_test_1.rb
@@ -0,0 +1,70 @@
+# encoding: utf-8
+
+require File.expand_path('../../test_helper', __FILE__)
+
+class MailHandlerTest < ActiveSupport::TestCase
+  fixtures :users, :projects, :enabled_modules, :roles,
+           :members, :member_roles, :users,
+           :issues, :issue_statuses,
+           :workflows, :trackers, :projects_trackers,
+           :versions, :enumerations, :issue_categories,
+           :custom_fields, :custom_fields_trackers, :custom_fields_projects,
+           :boards, :messages
+
+  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
+
+  def setup
+    ActionMailer::Base.deliveries.clear
+    Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
+  end
+
+  def teardown
+    Setting.clear_cache
+  end
+
+  def test_tb
+    issue = submit_email(
+              'thunderbird_with_attachment.eml',
+              :issue => {:project => 'ecookbook'}
+            )
+    assert_kind_of Issue, issue
+    assert_equal 1, issue.attachments.size
+
+    attachment = issue.attachments.first
+    assert_equal 'テスト.txt', attachment.filename
+    assert_equal 5, attachment.filesize
+    assert File.exist?(attachment.diskfile)
+    assert_equal 5, File.size(attachment.diskfile)
+    assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
+  end
+
+  def test_gmail
+    issue = submit_email(
+              'gmail_with_attachment.eml',
+              :issue => {:project => 'ecookbook'}
+            )
+    assert_kind_of Issue, issue
+    assert_equal 1, issue.attachments.size
+
+    attachment = issue.attachments.first
+    assert_equal 'テスト.txt', attachment.filename
+    assert_equal 5, attachment.filesize
+    assert File.exist?(attachment.diskfile)
+    assert_equal 5, File.size(attachment.diskfile)
+    assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
+  end
+
+  private
+
+  def submit_email(filename, options={})
+    raw = IO.read(File.join(FIXTURES_PATH, filename))
+    yield raw if block_given?
+    MailHandler.receive(raw, options)
+  end
+
+  def assert_issue_created(issue)
+    assert issue.is_a?(Issue)
+    assert !issue.new_record?
+    issue.reload
+  end
+end
