summaryrefslogtreecommitdiffstats
path: root/t/03_other_mailbox.t
diff options
context:
space:
mode:
Diffstat (limited to 't/03_other_mailbox.t')
-rw-r--r--t/03_other_mailbox.t82
1 files changed, 82 insertions, 0 deletions
diff --git a/t/03_other_mailbox.t b/t/03_other_mailbox.t
new file mode 100644
index 0000000..a1ef59a
--- /dev/null
+++ b/t/03_other_mailbox.t
@@ -0,0 +1,82 @@
+use strict;
+use warnings;
+use Test::Differences;
+use Test::More;
+use Test::MockObject;
+
+use Log::Any::Adapter ('Stderr', log_level => "warn");
+
+use App::ImapNotify;
+use App::ImapNotify::ImapClient;
+
+=head1 DESCRIPTION
+
+Simulate a mail arriving in a different mailbox than the currently selected one.
+
+=cut
+
+my $notifier = Test::MockObject->new();
+$notifier->set_true("notify");
+
+my @input_lines = (
+ "* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Fake server ready.\r\n",
+ "CMD-0 OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY LITERAL+ NOTIFY SPECIAL-USE COMPRESS=DEFLATE] Logged in\r\n",
+ "CMD-1 OK [READ-WRITE] Select completed (0.001 + 0.000 secs).\r\n",
+ "CMD-2 OK NOTIFY completed (0.001 + 0.000 secs).\r\n",
+ # STATUS update that mail arrived in other mailbox
+ "* STATUS INBOX.test (MESSAGES 15 UIDNEXT 7601 UNSEEN 15)\r\n",
+ "* OK [CLOSED] Previous mailbox closed.\r\n",
+ "* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft Junk)\r\n",
+ "* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft Junk \\*)] Flags permitted.\r\n",
+ "* 15 EXISTS\r\n",
+ "* 1 RECENT\r\n",
+ "* OK [UNSEEN 1] First unseen.\r\n",
+ "* OK [UIDVALIDITY 1248005923] UIDs valid\r\n",
+ "* OK [UIDNEXT 7601] Predicted next UID\r\n",
+ "* OK [HIGHESTMODSEQ 24120] Highest\r\n",
+ "CMD-3 OK [READ-WRITE] Select completed (0.062 + 0.000 + 0.061 secs).\r\n",
+ # client should request UID 7600 here (the new mail)
+ "* 51 FETCH (UID 7600 BODY[HEADER.FIELDS (FROM TO SUBJECT)] {".(36+24+23+2)."}\r\n",
+ "From: <test\@localhost.localdomain>\r\n",
+ "To: <bluewind\@xinu.at>\r\n",
+ "Subject: Some subject\r\n",
+ "\r\n",
+ ")\r\n",
+ "CMD-4 OK FETCH completed (0.001 + 0.000 secs).\r\n"
+);
+
+my $socket = Test::MockObject->new();
+$socket->mock('readline', sub {my $x = shift @input_lines; ref($x) eq "CODE" ? $x->() : $x;});
+$socket->set_true(qw(writeline));
+
+my $config = {
+ log_id => 'test-id1',
+ host => 'localhost.localdomain',
+ port => 993,
+ username => 'tester1',
+ password => 'secretPW42',
+ mailboxes => [qw(INBOX INBOX.test)],
+ keepalive_timeout => 300,
+};
+
+my $imap_client = App::ImapNotify::ImapClient->new_no_defaults($config, {sock => $socket});
+
+my $app = App::ImapNotify->new_no_defaults($config, {imap_client => $imap_client, notifier => $notifier});
+
+$app->loop();
+
+is(scalar(@input_lines), 0, "all input lines are read");
+
+eq_or_diff($notifier->_calls(), [
+ ['notify', [$notifier, 'From: <test@localhost.localdomain>', 'Subject: Some subject']]
+ ], "notifier is called correctly");
+
+eq_or_diff([grep { $_->[0] eq "writeline" } $socket->_calls()->@*], [
+ ['writeline', [$socket, "CMD-0 login tester1 secretPW42\r\n"]],
+ ['writeline', [$socket, "CMD-1 select INBOX\r\n"]],
+ ['writeline', [$socket, "CMD-2 notify set (selected (MessageExpunge MessageNew (uid body.peek[header.fields (from to subject)]))) (mailboxes (INBOX INBOX.test) (MessageNew MessageExpunge MailboxName))\r\n"]],
+ ['writeline', [$socket, "CMD-3 select INBOX.test\r\n"]],
+ ['writeline', [$socket, "CMD-4 uid fetch 7600 (body.peek[header.fields (from to subject)])\r\n"]],
+ ], "socket writeline is called correctly");
+
+done_testing;