7 use Test::WWW::Mechanize;
10 use Catalyst::Runtime;
13 use Catalyst::Authentication::User::Hash;
14 use Catalyst::Plugin::Session::State::Cookie;
15 use Catalyst::Plugin::Session::Store::FastMmap;
16 use Class::Accessor::Fast;
18 use ExtUtils::MakeMaker;
21 use Net::OpenID::Consumer;
22 use Net::OpenID::Server;
23 use Test::WWW::Mechanize;
30 plan skip_all => 'Test application dependencies not satisfied';
32 elsif ( not $ENV{TEST_HTTP} )
34 plan skip_all => 'set TEST_HTTP to enable this test';
41 # One port for consumer app, one for provider.
42 my $consumer_port = 10000 + int rand(1 + 10000);
43 my $provider_port = $consumer_port;
44 $provider_port = 10000 + int rand(1 + 10000) until $consumer_port != $provider_port;
46 my $provider_pipe = "perl -I$FindBin::Bin/../lib -I$FindBin::Bin/Provider/lib $FindBin::Bin/Provider/script/testapp_server.pl -p $consumer_port |";
48 my $consumer_pipe = "perl -I$FindBin::Bin/../lib -I$FindBin::Bin/Consumer/lib $FindBin::Bin/Consumer/script/testapp_server.pl -p $provider_port |";
50 my $provider_pid = open my $provider, $provider_pipe
51 or die "Unable to spawn standalone HTTP server for Provider: $!";
53 diag("Started Provider with pid $provider_pid");
55 my $consumer_pid = open my $consumer, $consumer_pipe
56 or die "Unable to spawn standalone HTTP server for Consumer: $!";
58 diag("Started Consumer with pid $consumer_pid");
60 # How long to wait for test server to start and timeout for UA.
63 diag("Waiting (up to $seconds seconds) for application servers to start...");
66 local $SIG{ALRM} = sub { die "Servers took too long to start\n" }; # NB: \n required
68 sleep 1 while check_port( 'localhost', $provider_port ) != 1;
69 sleep 1 while check_port( 'localhost', $consumer_port ) != 1;
76 die "Could not run test: $@";
79 my $openid_consumer = $ENV{CATALYST_SERVER} = "http://localhost:$consumer_port";
80 my $openid_server = "http://localhost:$provider_port";
82 # Tests start --------------------------------------------
83 diag("Started...") if $ENV{TEST_VERBOSE};
85 my $mech = Test::WWW::Mechanize->new(timeout => $seconds);
87 $mech->get_ok($openid_consumer, "GET $openid_consumer");
89 $mech->content_contains("You are not signed in.", "Content looks right");
91 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
94 my $claimed_uri = "$openid_server/provider/paco";
96 $mech->submit_form_ok({ form_name => "openid",
97 fields => { openid_identifier => $claimed_uri,
100 "Trying OpenID login, 'openid' realm");
102 $mech->content_contains("You're not signed in so you can't be verified",
103 "Can't use OpenID, not signed in at provider");
108 my $claimed_uri = "gopher://localhost:443/what?";
110 $mech->submit_form( form_name => "openid",
111 fields => { openid_identifier => $claimed_uri,
115 diag("Trying OpenID with ridiculous URI")
116 if $ENV{TEST_VERBOSE};
118 # no_identity_server: The provided URL doesn't declare its OpenID identity server.
120 is( $mech->status, 500,
121 "Can't use OpenID: bogus_url" );
126 my $claimed_uri = "localhost/some/path";
128 $mech->submit_form( form_name => "openid",
129 fields => { openid_identifier => $claimed_uri,
133 diag("Trying OpenID with phony URI")
134 if $ENV{TEST_VERBOSE};
136 # no_identity_server: The provided URL doesn't declare its OpenID identity server.
137 is( $mech->status, 500,
138 "Can't use OpenID: no_identity_server");
144 $mech->get_ok("$openid_server/login", "GET $openid_consumer/login");
146 # diag($mech->content);
148 $mech->submit_form_ok({ form_name => "login",
149 fields => { username => "paco",
150 password => "l4s4v3n7ur45",
153 "Trying cleartext login, 'memebers' realm");
155 $mech->content_contains("signed in", "Signed in successfully");
157 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
159 $mech->content_contains("Sign in with OpenID", "Content looks right");
161 my $claimed_uri = "$openid_server/provider/paco";
163 $mech->submit_form_ok({ form_name => "openid",
164 fields => { openid_identifier => $claimed_uri,
167 "Trying OpenID login, 'openid' realm");
169 $mech->content_contains("You did it with OpenID!",
170 "Successfully signed in with OpenID");
172 $mech->get_ok($openid_consumer, "GET $openid_consumer");
174 $mech->content_contains("provider/paco", "OpenID info is in the user");
178 $mech->get_ok("$openid_consumer/logout", "GET $openid_consumer/logout");
180 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
182 $mech->content_contains("Sign in with OpenID", "Content looks right");
184 $mech->submit_form_ok({ form_name => "openid",
185 fields => { openid_identifier => $claimed_uri,
188 "Trying OpenID login, 'openid' realm");
190 $mech->content_contains("can't be verified",
191 "Proper failure for unauthenticated memember.");
197 # Tests end ----------------------------------------------
200 kill INT => $provider_pid, $consumer_pid;
206 my ( $host, $port ) = @_;
208 my $remote = IO::Socket::INET->new(