Dwimmer

 view release on metacpan or  search on metacpan

t/010_subscribe.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
plan skip_all => 'Convert the test to use Plack::Test';
exit;


use t::lib::Dwimmer::Test qw(start $admin_mail @users read_file);

use Cwd qw(abs_path);
use Data::Dumper qw(Dumper);

my $password = 'dwimmer';

my $run = start($password);

eval "use Test::More";
eval "use Test::Deep";
require Test::WWW::Mechanize;
plan( skip_all => 'Unsupported OS' ) if not $run;

my $url = "http://localhost:$ENV{DWIMMER_PORT}";

plan( tests => 30 );

require Test::Differences;

use Dwimmer::Client;

my $admin = Dwimmer::Client->new( host => $url );
is_deeply(
	$admin->login( username => 'admin', password => $password ),
	{   success   => 1,
		username  => 'admin',
		userid    => 1,
		logged_in => 1,
	},
	'login success'
);

# create a mailing list
my $list_title        = 'Test list';
my $list_name         = 'test_list';
my $from_address      = 'admin1@dwimmer.org';
my $validate_template = <<'END_VALIDATE';
Opening: I am ready to send you updates.

-----------------------------------------------------------
CONFIRM BY VISITING THE LINK BELOW:

<% url %>

Click the link above to give me permission to send you
information.  It's fast and easy!  If you cannot click the
full URL above, please copy and paste it into your web
browser.

-----------------------------------------------------------
If you do not want to confirm, simply ignore this message.

Thank You Again!

END_VALIDATE

my $confirm_template = <<'END_CONFIRM';
END_CONFIRM

die if $validate_template =~ /\r/;
is_deeply_full(
	$admin->create_list(
		title                    => $list_title,
		name                     => $list_name,
		from_address             => $from_address,
		validate_template        => $validate_template,
		confirm_template         => $confirm_template,
		response_page            => '/response_page',
		validation_page          => '/validate_page',
		validation_response_page => '/final_page',
	),
	{   listid  => 1,
		success => 1,
	},
	'create_list'
);

# TODO: check identical names

t/010_subscribe.t  view on Meta::CPAN

my ( $t2_code, $t2_link ) = _check_validate_mail('t2');
is_deeply(
	$admin->list_members( listid => 1 ),
	{   'members' => [
			{   'approved' => 1,
				'email'    => 't1@dwimmer.org',
				'id'       => 1,
			},
			{   'approved' => 0,
				'email'    => 't2@dwimmer.org',
				'id'       => 2,
			}
		]
	},
	'list of members'
);

$web_user->get_ok($t2_link);
$web_user->content_like(qr/Thanks for subscribing. We will be in touch/);
_check_confirm_mail('t2');

is_deeply(
	$admin->list_members( listid => 1 ),
	{   'members' => [
			{   'approved' => 1,
				'email'    => 't1@dwimmer.org',
				'id'       => 1,
			},
			{   'approved' => 1,
				'email'    => 't2@dwimmer.org',
				'id'       => 2,
			}
		]
	},
	'list of members'
);



exit;

sub _check_validate_mail {
	my $email = shift;

	local $Test::Builder::Level = $Test::Builder::Level + 1;
	my $validate_mail = read_file( $ENV{DWIMMER_MAIL} );
	eval $validate_mail;

	#diag(explain($VAR1));
	# my $validate = $validate_template;
	my $link       = '';
	my $found_code = '';
	if ( $VAR1->{Data}
		=~ s{(http://localhost:$ENV{DWIMMER_PORT}/_dwimmer/validate_email\?listid=1&email=$email\@dwimmer\.org&code=(\w+))}{<% url %>}
		)
	{
		( $link, $found_code ) = ( $1, $2 );
	}

	my $data = delete $VAR1->{Data};
	Test::Differences::eq_or_diff($data, $validate_template, 'validate e-mail data');
	is_deeply_full(
		$VAR1,
		bless(
			{
				'From'    => $from_address,
				'Subject' => "$list_title registration - email validation",
				'To'      => $email . '@dwimmer.org',
			},
			'MIME::Lite'
		),
		"expected validate e-mail structure for $email"
	);
	$VAR1 = undef;

	diag("code='$found_code' link=$link");

	return ( $found_code, $link );
}

sub _check_confirm_mail {
	my $email = shift;

	local $Test::Builder::Level = $Test::Builder::Level + 1;
	my $confirm_mail = read_file( $ENV{DWIMMER_MAIL} );
	eval $confirm_mail;
	my $data = delete $VAR1->{Data};
	Test::Differences::eq_or_diff($data, $confirm_template, 'confirm e-mail data');
	is_deeply_full(
		$VAR1,
		bless(
			{
				'From'    => $from_address,
				'Subject' => "$list_title - Thank you for subscribing",
				'To'      => $email . '@dwimmer.org',
			},
			'MIME::Lite'
		),
		'expected confirm e-mail structure'
	);

	# TODO test what is the response if incorrect validation happens or if it pressed multiple times
}


sub is_deeply_full {
	my ( $result, $expected, $title ) = @_;
	my $ok = is_deeply( $result, $expected, $title );
	diag( explain($result) ) if not $ok;
	return $ok;
}

# TODO validation web page, error messages



( run in 0.646 second using v1.01-cache-2.11-cpan-6aa56a78535 )