summaryrefslogtreecommitdiff
path: root/test/tc_maildir.rb
blob: 7df47b821a53719fa86e6f51ab51027523b216ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'test/unit'
require 'fileutils'
require 'tmpdir'
require 'mocha/setup'

require 'feed2imap/maildir'

class TestMaildir < Test::Unit::TestCase

  def setup
    @tmpdirs = []
  end

  def teardown
    @tmpdirs.each do |dir|
      FileUtils.rm_rf(dir)
    end
  end

  def test_cleanup
    folder = create_maildir
    msgs = message_count(folder)

    four_days_ago = Time.now - (4 * 24 * 60 * 60)
    old_message = Dir.glob(File.join(folder, '**/*:2,S')).first
    FileUtils.touch old_message, mtime: four_days_ago

    maildir_account.cleanup(folder)

    assert_equal msgs - 1, message_count(folder)
  end

  def test_putmail
    folder = create_maildir
    msgs = message_count(folder)

    mail = RMail::Message.new
    mail.header['Subject'] = 'a message I just created'
    mail.body = 'to test maildir'
    maildir_account.putmail(folder, mail)

    assert_equal msgs + 1, message_count(folder)
  end

  def test_updatemail
    folder = create_maildir
    path = maildir_account.send(
      :find_mails,
      folder,
      'regular-message-id@debian.org'
    ).first
    assert_not_nil path
    mail = RMail::Message.new
    mail.header['Subject'] = 'a different subject'
    mail.header['Message-ID'] = 'regular-message-id@debian.org'
    mail.body = 'This is the body of the message'
    maildir_account.updatemail(folder, mail, 'regular-message-id@debian.org')

    updated_path = maildir_account.send(
      :find_mails,
      folder,
      'regular-message-id@debian.org'
    ).first
    updated_mail = RMail::Parser.read(File.open(File.join(folder, updated_path)))

    assert_equal 'a different subject', updated_mail.header['Subject']
  end

  def test_find_mails
    folder = create_maildir
    assert_equal 0, maildir_account.send(:find_mails, folder, 'SomeRandomMessageID').size
  end

  private

  def create_maildir
    parent = Dir.mktmpdir
    @tmpdirs << parent
    FileUtils.cp_r('test/maildir', parent)
    return File.join(parent, 'maildir')
  end

  def message_count(folder)
    Dir.glob(File.join(folder, '**', '*')).reject { |f| File.directory?(f) }.size
  end

  def maildir_account
    @maildir_account ||=
      begin
        MaildirAccount.new.tap do |account|
          account.stubs(:puts)
        end
      end
  end

end