[Bps-public-commit] rt-extension-mailgate branch, master, updated. 75ac7a9d505dccb82aa61ddc2af642aebb9ee18e

Chia-liang Kao clkao at bestpractical.com
Thu Sep 2 08:40:00 EDT 2010


The branch, master has been updated
       via  75ac7a9d505dccb82aa61ddc2af642aebb9ee18e (commit)
       via  6f9c5316d6ea307a787b7499a9519d28b1274241 (commit)
      from  7d6402b1a0d620f937ba578670ccde5bb905e4a6 (commit)

Summary of changes:
 Makefile.PL                  |    4 ++
 lib/RT/Extension/MailGate.pm |   35 ++++++++++++++++++
 t/basic.t                    |   83 +++++++++++++++++++++++++++++++++---------
 t/fdm.conf                   |   10 -----
 4 files changed, 104 insertions(+), 28 deletions(-)
 delete mode 100644 t/fdm.conf

- Log -----------------------------------------------------------------
commit 6f9c5316d6ea307a787b7499a9519d28b1274241
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Sep 2 19:34:18 2010 +0800

    bits of real tests

diff --git a/t/fdm.conf b/t/fdm.conf
deleted file mode 100644
index 369fcf7..0000000
--- a/t/fdm.conf
+++ /dev/null
@@ -1,10 +0,0 @@
-set lock-file "t/fdm.lock"
-account "test-acconut" imaps server "localhost"
-        port 1993 user "test-account at example.com" pass "test" folder "INBOX"
-
-match "^To:.*contact at example.com" in headers
-        action "rtmail"
-
-action "drop" drop
-action "rtmail" exec "cat"
-

commit 75ac7a9d505dccb82aa61ddc2af642aebb9ee18e
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Sep 2 20:17:51 2010 +0800

    more real tests with test::tcp

diff --git a/Makefile.PL b/Makefile.PL
index 5dff57e..e074d16 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -1,4 +1,8 @@
 use inc::Module::Install;
 RTx('RT-Extension-MailGate');
+
+test_requires 'Net::IMAP::Server';
+test_requires 'Test::TCP';
+
 WriteAll();
 
diff --git a/lib/RT/Extension/MailGate.pm b/lib/RT/Extension/MailGate.pm
index 0cf3eb7..15b129f 100644
--- a/lib/RT/Extension/MailGate.pm
+++ b/lib/RT/Extension/MailGate.pm
@@ -3,5 +3,40 @@ use strict;
 
 package RT::Extension::MailGate;
 our $VERSION = '0.1';
+use Net::IMAP::Simple;
+use Email::Simple;
+
+
+sub fetch_imap {
+    my ($self, $cb, $opt) = @_;
+
+    # Create the object
+    my $imap = Net::IMAP::Simple->new($opt->{server}, use_ssl => $opt->{use_ssl}) ||
+        die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
+
+    # Log on
+    if (!$imap->login($opt->{user},$opt->{pass})) {
+        print STDERR "Login failed: " . $imap->errstr . "\n";
+        return 0;
+    }
+
+    my $nm = $imap->select('INBOX');
+
+    for (my $i = 1; $i <= $nm; $i++) {
+        next if $imap->seen($i);
+
+        my $processed = $cb->(
+            Email::Simple->new(join '', @{ $imap->top($i) } ) );
+
+        if ($processed) {
+            $imap->see($i);
+            $imap->delete($i) if $opt->{delete};
+        }
+    }
+    $imap->expunge_mailbox('INBOX') if $opt->{expunge};
+
+    $imap->quit;
+
+}
 
 1;
diff --git a/t/basic.t b/t/basic.t
index 331c8f7..bf8378b 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 use Test::More;
-
+use Test::TCP;
 use Net::IMAP::Server;
 
 package Demo::IMAP::Auth;
@@ -57,11 +57,13 @@ BEGIN {
 }
 
 use RT;
-use RT::Test tests => 3;
+use RT::Test tests => 10;
 use RT::Test::Email;
 RT->Config->Set( LogToScreen => 'debug' );
 RT->Config->Set('Plugins',qw(RT::Extension::MailGate));
 
+my $imap_pid;
+
 use_ok('RT::Extension::MailGate');
 
 my $everyone_group = RT::Group->new( $RT::SystemUser );
@@ -69,24 +71,69 @@ $everyone_group->LoadSystemInternalGroup( 'Everyone' );
 ok ($everyone_group->Id, "Found group 'everyone'");
 ok( RT::Test->set_rights(
     { Principal => $everyone_group->PrincipalObj,
-      Right => [qw(CreateTicket ReplyToTicket CommentOnTicket)],
+      Right => [qw(CreateTicket ReplyToTicket)],
   },
 ), "Granted everybody the right to create tickets");
 
-warn start_imap();
 
-sleep 1 while 1;
-sub start_imap {
-    if (fork()) {
-        return 'localhost:1143';
+test_tcp(
+    client => sub {
+        my $port = shift;
+        my $opt =
+            { server => 'localhost:'.$port, use_ssl => 1,
+              user => 'test', pass => 'test', delete => 1, expluge => 1 };
+
+        my $queue = RT::Test->load_or_create_queue(
+            Name              => 'Contact',
+            CorrespondAddress => 'contact at example.com',
+            CommentAddress    => 'contact-comments at example.com',
+        );
+        ok $queue && $queue->id, 'loaded or created queue';
+        my ($baseurl, $m) = RT::Test->started_ok;
+
+        RT::Extension::MailGate->fetch_imap(
+            sub {
+                my $es = shift;
+                like($es->header('Subject'), qr/a test message/);
+                is($es->header('To'), 'contact at example.com');
+                return 0;
+            }, $opt);
+
+        mail_ok {
+
+            RT::Extension::MailGate->fetch_imap(
+                sub {
+                    my $es = shift;
+                    if ($es->header('To') eq $queue->CorrespondAddress) {
+                        my ($status, $id) = RT::Test->send_via_mailgate_and_http($es->as_string);
+                        diag "$status $id";
+                        return $id;
+                    }
+                },
+                $opt
+            );
+        } {
+            From => qr/RT/,
+            To => 'jesse at example.com',
+            Subject => qr/a test message/,
+        };
+
+        my $cnt = 0;
+
+        RT::Extension::MailGate->fetch_imap(
+            sub { ++$cnt }, $opt);
+        is($cnt, 0);
+    },
+    server => sub {
+        my $port = shift;
+        Net::IMAP::Server->new(
+            auth_class  => "Demo::IMAP::Auth",
+            model_class => "Demo::IMAP::Model",
+            server_key => 'certs/server-key.pem',
+            server_cert => 'certs/server-cert.pem',
+            port        => empty_port(20000),
+            ssl_port    => $port
+        )->run();
     }
-    Net::IMAP::Server->new(
-        auth_class  => "Demo::IMAP::Auth",
-        model_class => "Demo::IMAP::Model",
-        log_level   => 4,
-        server_key => 'certs/server-key.pem',
-        server_cert => 'certs/server-cert.pem',
-        port        => 1143,
-        ssl_port    => 1993
-    )->run();
-}
+);
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list