receiving-mail-and-test.diff

Toshi MARUYAMA, 2012-11-19 11:39

Download (4.93 KB)

View differences:

test/fixtures/mail_handler/gmail_with_attachment.eml
1
Date: Mon, 19 Nov 2012 10:17:45 +0900
2
Message-ID: <CANBr5-U6cXMfLek5QiB2ZrBPR3vTThn9_Upvdkf3Dkod664+Xw@mail.gmail.com>
3
Subject: test
4
From: John Smith <JSmith@somenet.foo>
5
To: redmine@somenet.foo
6
Content-Type: multipart/mixed; boundary=bcaec54ee4ea84f77904cecee22e
7

  
8
--bcaec54ee4ea84f77904cecee22e
9
Content-Type: text/plain; charset=ISO-8859-1
10

  
11
test
12

  
13
--bcaec54ee4ea84f77904cecee22e
14
Content-Type: text/plain; charset=US-ASCII; name="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
15
Content-Disposition: attachment; filename="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
16
Content-Transfer-Encoding: base64
17
X-Attachment-Id: f_h9owndpv0
18

  
19
dGVzdAo=
20
--bcaec54ee4ea84f77904cecee22e--
test/fixtures/mail_handler/thunderbird_with_attachment.eml
1
Message-ID: <50AA00C6.4070108@gmail.com>
2
Date: Mon, 19 Nov 2012 18:49:58 +0900
3
From: John Smith <JSmith@somenet.foo>
4
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
5
MIME-Version: 1.0
6
To: redmine@somenet.foo
7
Subject: test
8
Content-Type: multipart/mixed;
9
 boundary="------------030104060902010800050907"
10

  
11
This is a multi-part message in MIME format.
12
--------------030104060902010800050907
13
Content-Type: text/plain; charset=ISO-2022-JP
14
Content-Transfer-Encoding: 7bit
15

  
16
test
17

  
18
--------------030104060902010800050907
19
Content-Type: text/plain;
20
 name="=?ISO-2022-JP?B?GyRCJUYlOSVIGyhCLnR4dA==?="
21
Content-Transfer-Encoding: base64
22
Content-Disposition: attachment;
23
 filename*=ISO-2022-JP''%1B%24%42%25%46%25%39%25%48%1B%28%42%2E%74%78%74
24

  
25
dGVzdAo=
26
--------------030104060902010800050907--
test/unit/mail_handler_test_1.rb
1
# encoding: utf-8
2

  
3
require File.expand_path('../../test_helper', __FILE__)
4

  
5
class MailHandlerTest < ActiveSupport::TestCase
6
  fixtures :users, :projects, :enabled_modules, :roles,
7
           :members, :member_roles, :users,
8
           :issues, :issue_statuses,
9
           :workflows, :trackers, :projects_trackers,
10
           :versions, :enumerations, :issue_categories,
11
           :custom_fields, :custom_fields_trackers, :custom_fields_projects,
12
           :boards, :messages
13

  
14
  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
15

  
16
  def setup
17
    ActionMailer::Base.deliveries.clear
18
    Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
19
  end
20

  
21
  def teardown
22
    Setting.clear_cache
23
  end
24

  
25
  def test_tb
26
    issue = submit_email(
27
              'thunderbird_with_attachment.eml',
28
              :issue => {:project => 'ecookbook'}
29
            )
30
    assert_kind_of Issue, issue
31
    assert_equal 1, issue.attachments.size
32

  
33
    attachment = issue.attachments.first
34
    assert_equal 'テスト.txt', attachment.filename
35
    assert_equal 5, attachment.filesize
36
    assert File.exist?(attachment.diskfile)
37
    assert_equal 5, File.size(attachment.diskfile)
38
    assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
39
  end
40

  
41
  def test_gmail
42
    issue = submit_email(
43
              'gmail_with_attachment.eml',
44
              :issue => {:project => 'ecookbook'}
45
            )
46
    assert_kind_of Issue, issue
47
    assert_equal 1, issue.attachments.size
48

  
49
    attachment = issue.attachments.first
50
    assert_equal 'テスト.txt', attachment.filename
51
    assert_equal 5, attachment.filesize
52
    assert File.exist?(attachment.diskfile)
53
    assert_equal 5, File.size(attachment.diskfile)
54
    assert_equal 'd8e8fca2dc0f896fd7cb4cb0031ba249', attachment.digest
55
  end
56

  
57
  private
58

  
59
  def submit_email(filename, options={})
60
    raw = IO.read(File.join(FIXTURES_PATH, filename))
61
    yield raw if block_given?
62
    MailHandler.receive(raw, options)
63
  end
64

  
65
  def assert_issue_created(issue)
66
    assert issue.is_a?(Issue)
67
    assert !issue.new_record?
68
    issue.reload
69
  end
70
end