require "vnd.dovecot.testsuite";
require "vacation";
require "variables";

test_set "message" text:
From: stephan@example.org
Subject: frop
References: <1234@local.machine.example> <3456@example.net>
 <435444@ttms.com> <4223@froop.example.net> <m345444444@message-id.exp>
Message-ID: <432df324@example.org>
To: nico@frop.example.org

Frop
.
;

test "UTF-8 Subject" {
	/* Trigger vacation response with rediculous Russian subject */
	vacation :subject "Auto: Я могу есть стекло, оно мне не вредит."
		"I am not in today";

	/* Execute Sieve result (sending message to dummy SMTP) */
	if not test_result_execute {
		test_fail "execution of result failed";
	}

	/* Retrieve message from dummy SMTP and set it as the active message under
	 * test.
	 */
	test_message :smtp 0;

	set "expected" "Auto: Я могу есть стекло, оно мне не вредит.";
	if not header :is "subject" "${expected}" {
		if header :matches "subject" "*" { set "subject" "${1}"; }

		test_fail text:
subject header is not encoded/decoded properly:
expected: ${expected}
decoded: ${subject}
.
;
	}
}

test_result_reset;

test_set "message" text:
From: stephan@example.org
Subject: frop
References: <1234@local.machine.example> <3456@example.net>
 <435444@ttms.com> <4223@froop.example.net> <m345444444@message-id.exp>
Message-ID: <432df324@example.org>
To: nico@frop.example.org

Frop
.
;


test "MIME Encoded Subject" {
	/* Trigger vacation response with rediculous Russian subject */
	vacation :subject "=?utf-8?b?w4TDlsOc?= sadasd"
		"I am not in today";

	/* Execute Sieve result (sending message to dummy SMTP) */
	if not test_result_execute {
		test_fail "execution of result failed";
	}

	/* Retrieve message from dummy SMTP and set it as the active message under
	 * test.
	 */
	test_message :smtp 0;

	set "expected" "ÄÖÜ sadasd";
	if not header :is "subject" "${expected}" {
		if header :matches "subject" "*" { set "subject" "${1}"; }

		test_fail text:
subject header is not encoded/decoded properly:
expected: ${expected}
decoded: ${subject}
.
;
	}
}

test_result_reset;

test_set "message" text:
From: stephan@example.org
Subject: frop
Message-ID: <432df324@example.org>
To: <g.m.karotte@example.com>

Frop
.
;


test "MIME Encoded From" {
	vacation :subject "Frop"
		:from "=?utf-8?q?G=C3=BCnther?= M. Karotte <g.m.karotte@example.com>"
		"I am not in today";

	/* Execute Sieve result (sending message to dummy SMTP) */
	if not test_result_execute {
		test_fail "execution of result failed";
	}

	/* Retrieve message from dummy SMTP and set it as the active message under
	 * test.
	 */
	test_message :smtp 0;

	set "expected" "Günther M. Karotte <g.m.karotte@example.com>";
	if not header :is "from" "${expected}" {
		if header :matches "from" "*" { set "decoded" "${1}"; }

		test_fail text:
from header is not encoded/decoded properly:
expected: ${expected}
decoded: ${decoded}
.
;
	}
}

test_result_reset;

test_set "message" text:
From: stephan@example.org
Subject: frop
Message-ID: <432df324@example.org>
To: <g.m.karotte@example.com>

Frop
.
;


test "MIME Encoded From - UTF-8 in phrase" {
	vacation :subject "Frop"
		:from "Günther M. Karotte <g.m.karotte@example.com>"
		"I am not in today";

	/* Execute Sieve result (sending message to dummy SMTP) */
	if not test_result_execute {
		test_fail "execution of result failed";
	}

	/* Retrieve message from dummy SMTP and set it as the active message under
	 * test.
	 */
	test_message :smtp 0;

	set "expected" "Günther M. Karotte <g.m.karotte@example.com>";
	if not header :is "from" "${expected}" {
		if header :matches "from" "*" { set "decoded" "${1}"; }

		test_fail text:
from header is not encoded/decoded properly:
expected: ${expected}
decoded: ${decoded}
.
;
	}
}
