#!/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_SubmitAttach < Test::Unit::TestCase
  include TestQuickMLModule

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

    str = <<"EOT"
Date: Sun, 01 Aug 2004 12:34:56 +0900
From: Test User <user@e.com>
To: test@example.com
Subject: multipart test
Cc: guest@example.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary"
Content-Transfer-Encoding: 7bit

--boundary
Content-Type: text/plain; charset="ascii"
Content-Transfer-Encoding: 7bit

test1

--boundary
Content-Type: text/plain; charset="ascii"
Content-Transfer-Encoding: 7bit

test2

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

    qml.qwik_post(mail, true)
    assert_equal("multipart test", @site["1"].get_title)
    assert_equal("* multipart test\n{{mail(user@e.com,0)\ntest1\ntest2\n}}\n",
		 @site["1"].load)
  end

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

    str = <<"EOT"
Date: Sun, 01 Aug 2004 12:34:56 +0900
From: user@e.com
To: eto@example.com
Message-Id: <nosuchmessageid@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------_4119D7487CCF04869008_MULTIPART_MIXED_"
Content-Transfer-Encoding: 7bit
Subject: text file attach test
Reply-To: user@e.com

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

test
--------_4119D7487CCF04869008_MULTIPART_MIXED_
Content-Type: application/octet-stream; name="t.txt"
Content-Disposition: attachment;
 filename="t.txt"
Content-Transfer-Encoding: base64

dGVzdA0K

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

    qml.qwik_post(mail, true)
    page = @site.get_by_title("text file attach test")
    assert_equal("text file attach test", page.get_title)
    assert_equal("* text file attach test\n{{mail(user@e.com,0)\ntest\n\n{{file(t.txt)}}\n\n}}\n", page.load)
    assert_equal(true, @site.files(page.key).exist?("t.txt"))
    assert_equal("./test/1.files/t.txt", @site.files(page.key).path("t.txt").to_s)
    str = @site.files(page.key).path("t.txt").open {|f| f.read }
    assert_equal("test\r\n", str)

    # the mail is sent again.
    qml.qwik_post(mail, true)
    page = @site.get_by_title("text file attach test")
    assert_equal("* text file attach test\n{{mail(user@e.com,0)\ntest\n\n{{file(t.txt)}}\n\n}}\n{{mail(user@e.com,0)\ntest\n\n{{file(1-t.txt)}}\n\n}}\n", page.load)
    assert_equal(true, @site.files(page.key).exist?("1-t.txt"))

    # again.
    qml.qwik_post(mail, true)
    page = @site.get_by_title("text file attach test")
    assert_equal("* text file attach test\n{{mail(user@e.com,0)\ntest\n\n{{file(t.txt)}}\n\n}}\n{{mail(user@e.com,0)\ntest\n\n{{file(1-t.txt)}}\n\n}}\n{{mail(user@e.com,0)\ntest\n\n{{file(2-t.txt)}}\n\n}}\n", page.load)
    assert_equal(true, @site.files(page.key).exist?("2-t.txt"))
  end

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

    str = <<"EOT"
Date: Sun, 01 Aug 2004 12:34:56 +0900
From: Test User <user@e.com>
To: test@example.com
Subject: Attach Test
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="ascii"
Content-Transfer-Encoding: 7bit

This is a test.

--------_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)
    assert_equal("Attach Test", @site["1"].get_title)
    assert_equal("* Attach Test\n{{mail(user@e.com,0)\nThis is a test.\n\n{{file(1x1.png)}}\n\n}}\n",
		   @site["1"].load)
    assert_equal(true, @site.files("1").exist?("1x1.png"))
  end
end
