#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/test-module-quickml"
require "qwik/quickml"

class TC_SubmitJapanese < Test::Unit::TestCase
  include TestQuickMLModule

  def test_mail
    str = <<"EOT"
From: "Test User" <user@e.com>
To: "Test Mailing List" <test@example.com>
Subject: Re: [test:1] テスト 
Date: Mon, 3 Feb 2001 12:34:56 +0900

これはテストです。
EOT
    mail = ml_mail("user@e.com", "test@example.com", str)

    assert_equal("これはテストです。\n".sjistojis, mail.body)
    assert_equal(str.sjistojis, mail.bare)
    assert_equal("Re: [test:1] テスト ".sjistojis, mail["Subject"])
    assert_equal("[test:1] Re: =?iso-2022-jp?B?GyRCJUYlOSVIGyhCIA==?= ",
		   QuickML::Mail.encode_field("[test:1] Re: テスト "))
  end

  def test_submit_1
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config

    assert_instance_of(QuickML::QuickML, qml)
    assert_equal("test", qml.name)
    assert_equal(nil, @site["1"])

    # 1st mail
    str = <<"EOT"
From: "Test User" <user@e.com>
To: "Test Mailing List" <test@example.com>
Subject: Re: [test:1] テスト 
Date: Mon, 3 Feb 2001 12:34:56 +0900

これはテストです。
EOT
    mail = ml_mail("user@e.com", "test@example.com", str)

    assert_equal(" Re: テスト ",
		 QuickML::Mail.clean_subject("[test:1] Re: テスト "))
    assert_equal("[test:1] Re: =?iso-2022-jp?B?GyRCJUYlOSVIGyhCIA==?= ",
		 QuickML::Mail.rewrite_subject(mail["Subject"], qml.name, 1))

    assert_equal("テスト", mail.get_clean_subject)
    assert_equal("これはテストです。\n".sjistojis, mail.get_body)
    assert_equal("Mon, 3 Feb 2001 12:34:56 +0900", mail["Date"])

    qml.qwik_post(mail, true)
    assert_equal("テスト", @site["1"].get_title)
    assert_equal("* テスト\n{{mail(user@e.com,0)\nこれはテストです。\n}}\n",
		 @site["1"].load)

    # 2nd mail
    str = <<"EOT"
From: "Guest User" <guest@example.com>
To: "Test Mailing List" <test@example.com>
Subject: Re: [test:2] テスト 
Date: Mon, 4 Feb 2001 12:34:56 +0900

これもテストです。
EOT
    mail = ml_mail("user@e.com", "test@example.com", str)

    qml.qwik_post(mail, true)
    assert_equal("テスト", @site["1"].get_title)
    assert_equal("* テスト\n{{mail(user@e.com,0)\nこれはテストです。\n}}\n{{mail(user@e.com,0)\nこれもテストです。\n}}\n", @site["1"].load) # the new mail is added.
  end

  def test_submit_3
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config
    str = <<"EOT"
Date: Thu, 15 Apr 2004 00:00:02 +0900
From: Kouichirou Eto <eto@example.com>
To: test@example.com
Subject: 進捗
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-2022-JP"
Content-Transfer-Encoding: 7bit

これもテスト。
EOT
    mail = ml_mail("eto@example.com", "test@example.com", str)

    qml.qwik_post(mail, true)
    assert_equal("進捗", @site["1"].get_title)
    assert_equal("* 進捗\n{{mail(eto@example.com,0)\nこれもテスト。\n}}\n",
		 @site["1"].load)
  end

  def test_submit_4
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config
    str = <<"EOT"
Date: Thu, 15 Apr 2004 21:00:11 +0900
From: Kouichirou Eto <eto@example.com>
To: test@example.com
Subject: コ

あ
EOT
    mail = ml_mail("eto@example.com", "test@example.com", str)

    qml.qwik_post(mail, true)
    assert_equal("コ", @site["1"].get_title)
    assert_equal("* コ\n{{mail(eto@example.com,0)\nあ\n}}\n",
		 @site["1"].load)
  end

  def test_submit_with_attach_image
    qml = QuickML::QuickML.new(@ml_config, "test@example.com")
    qml.setup_test_config

    mail = QuickML::Mail.new
    mail.mail_from = "user@e.com"
    mail.add_recipient("test@example.com")
    str = <<"EOT"
Date: Sun, 01 Aug 2004 12:34:56 +0900
From: Test User <user@e.com>
To: test@example.com
Subject: =?iso-2022-jp?B?GyRCRTpJVSVGJTklSBsoQg==?=
Cc: guest@example.com
Message-Id: <nosuchmessageid@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------_410DDC04C7AD046D3600_MULTIPART_MIXED_"
Content-Transfer-Encoding: 7bit

--------_410DDC04C7AD046D3600_MULTIPART_MIXED_
Content-Type: text/plain; charset="ISO-2022-JP"
Content-Transfer-Encoding: 7bit

テストです。

--------_410DDC04C7AD046D3600_MULTIPART_MIXED_
Content-Type: image/png; name="1x1.png"
Content-Disposition: attachment;
 filename="1x1.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4//8/AAX+Av4zEpUU
AAAAAElFTkSuQmCC

--------_410DDC04C7AD046D3600_MULTIPART_MIXED_--
EOT
    mail = ml_mail("user@e.com", "test@example.com", str)

    qml.qwik_post(mail, true)
    page = @site["1"]
    assert_equal("添付テスト", page.get_title)
    assert_equal("* 添付テスト\n{{mail(user@e.com,0)\nテストです。\n\n{{file(1x1.png)}}\n\n}}\n",
		   page.load)
    assert_equal(true, @site.files("1").exist?("1x1.png"))
  end
end
