f1fae46b764edadabafb9913031659f48c06cd54
[catagits/Catalyst-Authentication-Credential-OpenID.git] / t / live-app.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use FindBin;
6 use IO::Socket;
7 use Test::More;
8 use Test::WWW::Mechanize;
9
10 eval <<_DEPS_;
11    use Catalyst::Runtime 5.7;
12    use Catalyst::Devel 1.0;
13    use Cache::FastMmap;
14    use Catalyst::Authentication::User::Hash;
15    use Catalyst::Plugin::Session::State::Cookie;
16    use Catalyst::Plugin::Session::Store::FastMmap;
17    use Class::Accessor::Fast;
18    use Crypt::DH;
19    use ExtUtils::MakeMaker;
20    use HTML::Parser 3;
21    use LWP::UserAgent;
22    use Net::OpenID::Consumer;
23    use Net::OpenID::Server;
24    use Test::WWW::Mechanize;
25 _DEPS_
26
27 plan skip_all => 'Test application dependencies not satisfied' if $@;
28
29 plan tests => 21;
30
31 # One port for consumer app, one for provider.
32 my $consumer_port = 10000 + int rand(1 + 10000);
33 my $provider_port = $consumer_port;
34 $provider_port = 10000 + int rand(1 + 10000) until $consumer_port != $provider_port;
35
36 my $provider_pipe = "perl -I$FindBin::Bin/../lib -I$FindBin::Bin/Provider/lib $FindBin::Bin/Provider/script/testapp_server.pl -p $consumer_port |";
37
38 my $consumer_pipe = "perl -I$FindBin::Bin/../lib -I$FindBin::Bin/Consumer/lib $FindBin::Bin/Consumer/script/testapp_server.pl -p $provider_port |";
39
40 my $provider_pid = open my $provider, $provider_pipe
41     or die "Unable to spawn standalone HTTP server for Provider: $!";
42
43 diag("Started Provider with pid $provider_pid");
44
45 my $consumer_pid = open my $consumer, $consumer_pipe
46     or die "Unable to spawn standalone HTTP server for Consumer: $!";
47
48 diag("Started Consumer with pid $consumer_pid");
49
50 # How long to wait for test server to start and timeout for UA.
51 my $seconds = 15;
52
53 diag("Waiting (up to $seconds seconds) for application servers to start...");
54
55 eval {
56     local $SIG{ALRM} = sub { die "Servers took too long to start\n" }; # NB: \n required
57     alarm($seconds);
58     sleep 1 while check_port( 'localhost', $provider_port ) != 1;
59     sleep 1 while check_port( 'localhost', $consumer_port ) != 1;
60     alarm(0)
61 };
62
63 if ( $@ )
64 {
65     shut_down();
66     die "Could not run test: $@";
67 }
68
69 my $openid_consumer = $ENV{CATALYST_SERVER} = "http://localhost:$consumer_port";
70 my $openid_server = "http://localhost:$provider_port";
71
72 # Tests start --------------------------------------------
73 diag("Started...") if $ENV{TEST_VERBOSE};
74
75 my $mech = Test::WWW::Mechanize->new(timeout => $seconds);
76
77 $mech->get_ok($openid_consumer, "GET $openid_consumer");
78
79 $mech->content_contains("You are not signed in.", "Content looks right");
80
81 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
82
83 {
84     my $claimed_uri = "$openid_server/provider/paco";
85
86     $mech->submit_form_ok({ form_name => "openid",
87                             fields => { openid_identifier => $claimed_uri,
88                             },
89                           },
90                           "Trying OpenID login, 'openid' realm");
91
92     $mech->content_contains("You're not signed in so you can't be verified",
93                             "Can't use OpenID, not signed in at provider");
94 }
95
96 # Bad claimed URI.
97 {
98     my $claimed_uri = "gopher://localhost:443/what?";
99     $mech->back();
100     $mech->submit_form( form_name => "openid",
101                          fields => { openid_identifier => $claimed_uri,
102                                    },
103                        );
104
105     diag("Trying OpenID with ridiculous URI")
106         if $ENV{TEST_VERBOSE};
107
108     # no_identity_server: The provided URL doesn't declare its OpenID identity server.
109
110     is( $mech->status, 500,
111         "Can't use OpenID: bogus_url" );
112 }
113
114 # Bad claimed URI.
115 {
116     my $claimed_uri = "localhost/some/path";
117     $mech->back();
118     $mech->submit_form( form_name => "openid",
119                          fields => { openid_identifier => $claimed_uri,
120                                    },
121                        );
122
123     diag("Trying OpenID with phony URI")
124         if $ENV{TEST_VERBOSE};
125
126     # no_identity_server: The provided URL doesn't declare its OpenID identity server.
127     is( $mech->status, 500,
128         "Can't use OpenID: no_identity_server");
129 }
130
131
132
133 #
134 $mech->get_ok("$openid_server/login", "GET $openid_consumer/login");
135
136 # diag($mech->content);
137
138 $mech->submit_form_ok({ form_name => "login",
139                         fields => { username => "paco",
140                                     password => "l4s4v3n7ur45",
141                                 },
142                        },
143                       "Trying cleartext login, 'memebers' realm");
144
145 $mech->content_contains("signed in", "Signed in successfully");
146
147 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
148
149 $mech->content_contains("Sign in with OpenID", "Content looks right");
150
151 my $claimed_uri = "$openid_server/provider/paco";
152
153 $mech->submit_form_ok({ form_name => "openid",
154                         fields => { openid_identifier => $claimed_uri,
155                                 },
156                     },
157                       "Trying OpenID login, 'openid' realm");
158
159 $mech->content_contains("You did it with OpenID!",
160                         "Successfully signed in with OpenID");
161
162 $mech->get_ok($openid_consumer, "GET $openid_consumer");
163
164 $mech->content_contains("provider/paco", "OpenID info is in the user");
165
166 # can't be verified
167
168 $mech->get_ok("$openid_consumer/logout", "GET $openid_consumer/logout");
169
170 $mech->get_ok("$openid_consumer/signin_openid", "GET $openid_consumer/signin_openid");
171
172 $mech->content_contains("Sign in with OpenID", "Content looks right");
173
174 $mech->submit_form_ok({ form_name => "openid",
175                         fields => { openid_identifier => $claimed_uri,
176                                 },
177                     },
178                       "Trying OpenID login, 'openid' realm");
179
180 $mech->content_contains("can't be verified",
181                         "Proper failure for unauthenticated memember.");
182
183 shut_down();
184
185 exit 0;
186
187 # Tests end ----------------------------------------------
188
189 sub shut_down {
190     kill INT => $provider_pid, $consumer_pid;
191     close $provider;
192     close $consumer;
193 }
194
195 sub check_port {
196     my ( $host, $port ) = @_;
197
198     my $remote = IO::Socket::INET->new(
199         Proto    => "tcp",
200         PeerAddr => $host,
201         PeerPort => $port
202     );
203     if ($remote) {
204         close $remote;
205         return 1;
206     }
207     else {
208         return 0;
209     }
210 }
211
212 __END__
213