summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-07-17 15:26:40 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-07-17 15:26:40 +0200
commit50a37d8812c95c60203b0102db91f0c080d4d93b (patch)
tree86d1c9b2e5461e872fdfbcb207a4e07a9f37283e /t
parent6e43e5e33b7d4fceaadb42e8264a7319002848cb (diff)
downloadApp-ImapNotify-50a37d8812c95c60203b0102db91f0c080d4d93b.tar.gz
App-ImapNotify-50a37d8812c95c60203b0102db91f0c080d4d93b.tar.xz
Initial port of single file script
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 't')
-rw-r--r--t/00_compile.t4
-rw-r--r--t/01_basic.t58
-rw-r--r--t/02_noop.t73
3 files changed, 135 insertions, 0 deletions
diff --git a/t/00_compile.t b/t/00_compile.t
index f5854a8..06f7f97 100644
--- a/t/00_compile.t
+++ b/t/00_compile.t
@@ -1,8 +1,12 @@
use strict;
+use warnings;
use Test::More 0.98;
use_ok $_ for qw(
App::ImapNotify
+ App::ImapNotify::ImapClient
+ App::ImapNotify::Notifier
+ App::ImapNotify::Socket::SSL
);
done_testing;
diff --git a/t/01_basic.t b/t/01_basic.t
new file mode 100644
index 0000000..6f9d076
--- /dev/null
+++ b/t/01_basic.t
@@ -0,0 +1,58 @@
+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;
+
+
+my $notifier = Test::MockObject->new();
+$notifier->set_true("notify");
+
+my $socket = Test::MockObject->new();
+$socket->set_series('readline',
+ "* 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",
+ # this stuff should be ignored
+ "* 51 EXISTS\r\n",
+ "* 1 RECENT\r\n",
+ # this is a notification
+ "* 51 FETCH (UID 41023 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",
+);
+
+$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($socket->readline(), undef, "readline queue is read fully");
+
+eq_or_diff($notifier->_calls(), [
+ ['notify', [$notifier, 'From: <test@localhost.localdomain>', 'Subject: Some subject']]
+ ], "notifier is called correctly");
+
+done_testing;
diff --git a/t/02_noop.t b/t/02_noop.t
new file mode 100644
index 0000000..c00a3c2
--- /dev/null
+++ b/t/02_noop.t
@@ -0,0 +1,73 @@
+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
+
+This test simulates a server that doesn't send any notifications. The client
+then runs into the keepalive timeout at which point the server just happens to
+receive a new message which it returns before the NOOP (keepalive) response.
+
+=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",
+ # this sub is aborted by the timeout
+ sub {sleep 10; fail("Keepalive failed to kill readline() operation");},
+ # notification that got in before the NOOP response
+ "* 51 FETCH (UID 41023 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-3 OK NOOP 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 => 0.01,
+};
+
+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 noop\r\n"]],
+ ], "notifier is called correctly");
+
+done_testing;