refactor Mail/Send test to make test smtp server useful
This commit is contained in:
parent
4d8ff20a08
commit
e99c78cb58
2 changed files with 101 additions and 78 deletions
|
|
@ -17,26 +17,14 @@ use lib "$FindBin::Bin/../lib";
|
||||||
use JSON qw( from_json to_json );
|
use JSON qw( from_json to_json );
|
||||||
use Test::More;
|
use Test::More;
|
||||||
use Test::Deep;
|
use Test::Deep;
|
||||||
use File::Spec;
|
|
||||||
use Data::Dumper;
|
use Data::Dumper;
|
||||||
use MIME::Parser;
|
use MIME::Parser;
|
||||||
use IO::Select;
|
|
||||||
use Encode qw/decode/;
|
use Encode qw/decode/;
|
||||||
|
|
||||||
use WebGUI::Test;
|
use WebGUI::Test;
|
||||||
|
|
||||||
use WebGUI::Mail::Send;
|
use WebGUI::Mail::Send;
|
||||||
|
|
||||||
# Load Net::SMTP::Server
|
|
||||||
my $hasServer; # This is true if we have a Net::SMTP::Server module
|
|
||||||
BEGIN {
|
|
||||||
eval {
|
|
||||||
require Net::SMTP::Server;
|
|
||||||
require Net::SMTP::Server::Client;
|
|
||||||
};
|
|
||||||
$hasServer = 1 unless $@;
|
|
||||||
}
|
|
||||||
|
|
||||||
$| = 1;
|
$| = 1;
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
@ -47,25 +35,9 @@ my $mail; # The WebGUI::Mail::Send object
|
||||||
my $mime; # for getMimeEntity
|
my $mime; # for getMimeEntity
|
||||||
|
|
||||||
# See if we have an SMTP server to use
|
# See if we have an SMTP server to use
|
||||||
my $smtpdPid;
|
my $hasServer = 0;
|
||||||
my $smtpdStream;
|
eval { WebGUI::Test->prepareMailServer; $hasServer = 1 };
|
||||||
my $smtpdSelect;
|
if ( $@ ) { diag( "Can't prepare mail server: $@" ) }
|
||||||
|
|
||||||
my $SMTP_HOST = 'localhost';
|
|
||||||
my $SMTP_PORT = '54921';
|
|
||||||
if ($hasServer) {
|
|
||||||
my $smtpd = File::Spec->catfile( WebGUI::Test->root, 't', 'smtpd.pl' );
|
|
||||||
$smtpdPid = open $smtpdStream, '-|', $^X, $smtpd, $SMTP_HOST, $SMTP_PORT
|
|
||||||
or die "Could not open pipe to SMTPD: $!";
|
|
||||||
|
|
||||||
$smtpdSelect = IO::Select->new;
|
|
||||||
$smtpdSelect->add($smtpdStream);
|
|
||||||
|
|
||||||
$session->setting->set( 'smtpServer', $SMTP_HOST . ':' . $SMTP_PORT );
|
|
||||||
|
|
||||||
WebGUI::Test->originalConfig('emailToLog');
|
|
||||||
$session->config->set( 'emailToLog', 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Tests
|
# Tests
|
||||||
|
|
@ -175,7 +147,8 @@ SKIP: {
|
||||||
} );
|
} );
|
||||||
$mail->addText( 'His judgement cometh and that right soon.' );
|
$mail->addText( 'His judgement cometh and that right soon.' );
|
||||||
|
|
||||||
my $received = sendToServer( $mail );
|
$mail->send;
|
||||||
|
my $received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
if (!$received) {
|
if (!$received) {
|
||||||
skip "Cannot test emailOverride: No response received from smtpd", $numtests;
|
skip "Cannot test emailOverride: No response received from smtpd", $numtests;
|
||||||
|
|
@ -209,7 +182,8 @@ SKIP: {
|
||||||
} );
|
} );
|
||||||
$mail->addText( "I understand you're a man who knows how to get things." );
|
$mail->addText( "I understand you're a man who knows how to get things." );
|
||||||
|
|
||||||
my $received = sendToServer( $mail );
|
$mail->send;
|
||||||
|
my $received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
if (!$received) {
|
if (!$received) {
|
||||||
skip "Cannot test messageIds: No response received from smtpd", $numtests;
|
skip "Cannot test messageIds: No response received from smtpd", $numtests;
|
||||||
|
|
@ -232,7 +206,8 @@ SKIP: {
|
||||||
} );
|
} );
|
||||||
$mail->addText( "What say you there, fuzzy-britches? Feel like talking?" );
|
$mail->addText( "What say you there, fuzzy-britches? Feel like talking?" );
|
||||||
|
|
||||||
$received = sendToServer( $mail );
|
$mail->send;
|
||||||
|
$received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
$parsed_message = $parser->parse_data($received->{contents});
|
$parsed_message = $parser->parse_data($received->{contents});
|
||||||
$head = $parsed_message->head;
|
$head = $parsed_message->head;
|
||||||
|
|
@ -248,7 +223,8 @@ SKIP: {
|
||||||
} );
|
} );
|
||||||
$mail->addText( "Dear Warden, You were right. Salvation lies within." );
|
$mail->addText( "Dear Warden, You were right. Salvation lies within." );
|
||||||
|
|
||||||
$received = sendToServer( $mail );
|
$mail->send;
|
||||||
|
$received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
$parsed_message = $parser->parse_data($received->{contents});
|
$parsed_message = $parser->parse_data($received->{contents});
|
||||||
$head = $parsed_message->head;
|
$head = $parsed_message->head;
|
||||||
|
|
@ -264,7 +240,8 @@ SKIP: {
|
||||||
} );
|
} );
|
||||||
$mail->addText( "Neither are they. You have to be human first. They don't qualify." );
|
$mail->addText( "Neither are they. You have to be human first. They don't qualify." );
|
||||||
|
|
||||||
$received = sendToServer( $mail );
|
$mail->send;
|
||||||
|
$received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
$parsed_message = $parser->parse_data($received->{contents});
|
$parsed_message = $parser->parse_data($received->{contents});
|
||||||
$head = $parsed_message->head;
|
$head = $parsed_message->head;
|
||||||
|
|
@ -318,7 +295,8 @@ SKIP: {
|
||||||
);
|
);
|
||||||
$mail->addText( 'sent via email' );
|
$mail->addText( 'sent via email' );
|
||||||
|
|
||||||
my $received = sendToServer( $mail ) ;
|
$mail->send;
|
||||||
|
my $received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
# Test the mail
|
# Test the mail
|
||||||
is($received->{to}->[0], '<ellis_boyd_redding@shawshank.gov>', 'send, toUser with email address');
|
is($received->{to}->[0], '<ellis_boyd_redding@shawshank.gov>', 'send, toUser with email address');
|
||||||
|
|
@ -334,7 +312,8 @@ SKIP: {
|
||||||
);
|
);
|
||||||
$mail->addText( 'sent via SMS' );
|
$mail->addText( 'sent via SMS' );
|
||||||
|
|
||||||
my $received = sendToServer( $mail ) ;
|
$mail->send;
|
||||||
|
my $received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
# Test the mail
|
# Test the mail
|
||||||
is($received->{to}->[0], '<55555@textme.com>', 'send, toUser with SMS address');
|
is($received->{to}->[0], '<55555@textme.com>', 'send, toUser with SMS address');
|
||||||
|
|
@ -350,7 +329,8 @@ SKIP: {
|
||||||
);
|
);
|
||||||
$mail->addText( 'sent via SMS' );
|
$mail->addText( 'sent via SMS' );
|
||||||
|
|
||||||
my $received = sendToServer( $mail ) ;
|
$mail->send;
|
||||||
|
my $received = WebGUI::Test->getMail;
|
||||||
|
|
||||||
# Test the mail
|
# Test the mail
|
||||||
cmp_bag(
|
cmp_bag(
|
||||||
|
|
@ -406,45 +386,6 @@ cmp_bag(
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Cleanup
|
# Cleanup
|
||||||
END {
|
END {
|
||||||
if ($smtpdPid) {
|
|
||||||
kill INT => $smtpdPid;
|
|
||||||
}
|
|
||||||
if ($smtpdStream) {
|
|
||||||
close $smtpdStream;
|
|
||||||
# we killed it, so there will be an error. Prevent that from setting the exit value.
|
|
||||||
$? = 0;
|
|
||||||
}
|
|
||||||
$session->db->write('delete from mailQueue');
|
$session->db->write('delete from mailQueue');
|
||||||
}
|
}
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
|
||||||
# sendToServer ( mail )
|
|
||||||
# Spawns a server (using t/smtpd.pl), sends the mail, and grabs it from the
|
|
||||||
# child
|
|
||||||
# The child process builds a Net::SMTP::Server and listens for the parent to
|
|
||||||
# send the mail. The entire result is returned as a hash reference with the
|
|
||||||
# following keys:
|
|
||||||
#
|
|
||||||
# to - who the mail was to
|
|
||||||
# from - who the mail was from
|
|
||||||
# contents - The complete contents of the message, suitable to be parsed
|
|
||||||
# by a MIME::Entity parser
|
|
||||||
sub sendToServer {
|
|
||||||
my $mail = shift;
|
|
||||||
my $status = $mail->send;
|
|
||||||
my $json;
|
|
||||||
if ($status && $smtpdSelect->can_read(5)) {
|
|
||||||
$json = <$smtpdStream>;
|
|
||||||
}
|
|
||||||
elsif ($status) {
|
|
||||||
$json = ' { "error" : "unable to read from smptd.pl" } ';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$json = ' { "error": "mail not sent" } ';
|
|
||||||
}
|
|
||||||
if (!$json) {
|
|
||||||
$json = ' { "error": "error in getting mail" } ';
|
|
||||||
}
|
|
||||||
return from_json( $json );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,14 @@ our ( $SESSION, $WEBGUI_ROOT, $CONFIG_FILE, $WEBGUI_LIB, $WEBGUI_TEST_COLLATERAL
|
||||||
use Config qw[];
|
use Config qw[];
|
||||||
use IO::Handle qw[];
|
use IO::Handle qw[];
|
||||||
use File::Spec qw[];
|
use File::Spec qw[];
|
||||||
|
use IO::Select qw[];
|
||||||
use Cwd qw[];
|
use Cwd qw[];
|
||||||
use Test::MockObject::Extends;
|
use Test::MockObject::Extends;
|
||||||
use WebGUI::PseudoRequest;
|
use WebGUI::PseudoRequest;
|
||||||
use Scalar::Util qw( blessed );
|
use Scalar::Util qw( blessed );
|
||||||
use List::MoreUtils qw/ any /;
|
use List::MoreUtils qw/ any /;
|
||||||
|
use Carp qw[ carp croak ];
|
||||||
|
use JSON qw( from_json to_json );
|
||||||
|
|
||||||
##Hack to get ALL test output onto STDOUT.
|
##Hack to get ALL test output onto STDOUT.
|
||||||
use Test::Builder;
|
use Test::Builder;
|
||||||
|
|
@ -58,6 +61,10 @@ my @sessionsToDelete;
|
||||||
my @storagesToDelete;
|
my @storagesToDelete;
|
||||||
my @tagsToRollback;
|
my @tagsToRollback;
|
||||||
|
|
||||||
|
my $smtpdPid;
|
||||||
|
my $smtpdStream;
|
||||||
|
my $smtpdSelect;
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
|
|
||||||
STDERR->autoflush(1);
|
STDERR->autoflush(1);
|
||||||
|
|
@ -192,6 +199,16 @@ END {
|
||||||
}
|
}
|
||||||
$SESSION->var->end;
|
$SESSION->var->end;
|
||||||
$SESSION->close if defined $SESSION;
|
$SESSION->close if defined $SESSION;
|
||||||
|
|
||||||
|
# Close SMTPD
|
||||||
|
if ($smtpdPid) {
|
||||||
|
kill INT => $smtpdPid;
|
||||||
|
}
|
||||||
|
if ($smtpdStream) {
|
||||||
|
close $smtpdStream;
|
||||||
|
# we killed it, so there will be an error. Prevent that from setting the exit value.
|
||||||
|
$? = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
@ -401,6 +418,41 @@ sub webguiBirthday {
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 prepareMailServer ( )
|
||||||
|
|
||||||
|
Prepare a Net::SMTP::Server to use for testing mail.
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub prepareMailServer {
|
||||||
|
eval {
|
||||||
|
require Net::SMTP::Server;
|
||||||
|
require Net::SMTP::Server::Client;
|
||||||
|
};
|
||||||
|
croak "Cannot load Net::SMTP::Server: $@" if $@;
|
||||||
|
|
||||||
|
my $SMTP_HOST = 'localhost';
|
||||||
|
my $SMTP_PORT = '54921';
|
||||||
|
my $smtpd = File::Spec->catfile( WebGUI::Test->root, 't', 'smtpd.pl' );
|
||||||
|
$smtpdPid = open $smtpdStream, '-|', $^X, $smtpd, $SMTP_HOST, $SMTP_PORT
|
||||||
|
or die "Could not open pipe to SMTPD: $!";
|
||||||
|
|
||||||
|
$smtpdSelect = IO::Select->new;
|
||||||
|
$smtpdSelect->add($smtpdStream);
|
||||||
|
|
||||||
|
$SESSION->setting->set( 'smtpServer', $SMTP_HOST . ':' . $SMTP_PORT );
|
||||||
|
|
||||||
|
WebGUI::Test->originalConfig('emailToLog');
|
||||||
|
$SESSION->config->set( 'emailToLog', 0 );
|
||||||
|
|
||||||
|
# Let it start up yo
|
||||||
|
sleep 2;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 originalConfig ( $param )
|
=head2 originalConfig ( $param )
|
||||||
|
|
||||||
Stores the original data from the config file, to be restored
|
Stores the original data from the config file, to be restored
|
||||||
|
|
@ -433,6 +485,36 @@ sub groupsToDelete {
|
||||||
push @groupsToDelete, @_;
|
push @groupsToDelete, @_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
=head2 getMail ( )
|
||||||
|
|
||||||
|
Read a sent mail from the prepared mail server (L<prepareMailServer>)
|
||||||
|
|
||||||
|
=cut
|
||||||
|
|
||||||
|
sub getMail {
|
||||||
|
my $json;
|
||||||
|
|
||||||
|
if ( !$smtpdSelect ) {
|
||||||
|
return from_json ' { "error": "mail server not prepared" }';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($smtpdSelect->can_read(5)) {
|
||||||
|
$json = <$smtpdStream>;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$json = ' { "error": "mail not sent" } ';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$json) {
|
||||||
|
$json = ' { "error": "error in getting mail" } ';
|
||||||
|
}
|
||||||
|
|
||||||
|
return from_json( $json );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 storagesToDelete ( $storage, [$storageId ] )
|
=head2 storagesToDelete ( $storage, [$storageId ] )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue