4 use Test::More tests => 34;
5 use Test::MockObject::Extends;
10 my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::HTTP") }
11 can_ok( $m, "authenticate" );
12 can_ok( $m, "authorization_required_response" );
14 my $req = Test::MockObject->new;
15 my $req_headers = HTTP::Headers->new;
16 $req->set_always( headers => $req_headers );
17 my $res = Test::MockObject->new;
19 $res->mock(status => sub { $status = $_[1] });
21 $res->mock(content_type => sub { $content_type = $_[1] });
24 $res->mock(body => sub { $body = $_[1] });
25 my $res_headers = HTTP::Headers->new;
26 $res->set_always( headers => $res_headers );
27 my $user = Test::MockObject->new;
28 $user->mock(get => sub { return shift->{$_[0]} });
30 my $realm = Test::MockObject->new;
31 $realm->mock( find_user => sub { $find_user_opts = $_[1]; return $user; });
32 $realm->mock( name => sub { 'foo' } );
33 my $c = Test::MockObject->new;
34 my $cache = Test::MockObject->new;
35 $cache->mock(set => sub { shift->{$_[0]} = $_[1] });
36 $cache->mock(get => sub { return shift->{$_[0]} });
37 my $uri_for_called = 0;
38 $c->mock(uri_for => sub { my ($c, $uri) = @_; $uri_for_called++; return 'uri_for:' . $uri} );
39 $c->mock(cache => sub { $cache });
40 $c->mock(debug => sub { 0 });
42 $c->mock( login => sub { shift; @login_info = @_; 1 } );
43 my $authenticated = 0;
44 $c->mock( set_authenticated => sub { $authenticated++; } );
45 $c->set_always( config => {} );
46 $c->set_always( req => $req );
47 $c->set_always( res => $res );
48 $c->set_always( request => $req );
49 $c->set_always( response => $res );
53 my $raw_self = $m->new($config, $c, $realm);
54 return Test::MockObject::Extends->new( $raw_self );
57 # Normal auth, simple as possible.
59 my $self = new_self( type => 'any', password_type => 'clear', password_field => 'password' );
61 $self->authenticate( $c, $realm );
62 } qr/^ $Catalyst::DETACH $/x, 'Calling authenticate for http auth without header detaches';
63 $user->{password} = 'bar';
66 $req_headers->authorization_basic( qw/foo quux/ );
68 $self->authenticate( $c, $realm );
69 } qr/^ $Catalyst::DETACH $/x, 'Calling authenticate for http auth without header detaches';
72 $req_headers->authorization_basic( qw/foo bar/ );
73 ok($self->authenticate($c, $realm), "auth successful with header");
74 is($authenticated, 1, 'authenticated once');
75 is_deeply( $find_user_opts, { username => 'foo'}, "login delegated");
77 # Test all the headers look good.
82 $self->authenticate( $c, $realm );
83 } qr/^ $Catalyst::DETACH $/x, "detached on no authorization required with bad auth";
84 is( $status, 401, "401 status code" );
85 is( $content_type, 'text/plain' );
86 is( $body, 'Authorization required.' );
87 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Digest/, "WWW-Authenticate header set: digest");
88 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="foo"/, "WWW-Authenticate header set: digest realm");
89 like( ($res_headers->header('WWW-Authenticate'))[1], qr/^Basic/, "WWW-Authenticate header set: basic");
90 like( ($res_headers->header('WWW-Authenticate'))[1], qr/realm="foo"/, "WWW-Authenticate header set: basic realm");
93 # Check password_field works
95 my $self = new_self( type => 'any', password_type => 'clear', password_field => 'the_other_password' );
96 local $user->{password} = 'bar';
97 local $user->{the_other_password} = 'the_other_password';
98 $req_headers->authorization_basic( qw/foo the_other_password/ );
99 ok($self->authenticate($c, $realm), "auth successful with header and alternate password field");
101 $req_headers->authorization_basic( qw/foo bar/ );
103 $self->authenticate( $c, $realm );
104 } qr/^ $Catalyst::DETACH $/x, "detached on bad password (different password field)";
110 $self->authenticate( $c, $realm, { realm => 'myrealm' }); # Override realm object's name method by doing this.
111 } qr/^ $Catalyst::DETACH $/x, "detached on no authorization supplied, overridden realm value";
112 is( $status, 401, "401 status code" );
113 is( $content_type, 'text/plain' );
114 is( $body, 'Authorization required.' );
115 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: digest realm overridden");
116 like( ($res_headers->header('WWW-Authenticate'))[1], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm overridden");
118 # Check authorization_required_message works
123 my $self = new_self( type => 'any', password_type => 'clear',
124 authorization_required_message => 'foobar'
127 $self->authenticate( $c, $realm );
128 } qr/^ $Catalyst::DETACH $/x, "detached";
129 is( $body, 'foobar', 'Body is supplied auth message');
132 # Check undef authorization_required_message suppresses crapping in
138 my $self = new_self( type => 'any', password_type => 'clear',
139 authorization_required_message => undef
142 $self->authenticate( $c, $realm );
143 } qr/^ $Catalyst::DETACH $/x, "detached";
144 is( $body, undef, 'Body is not set - user overrode auth message');
147 # Check domain config works
152 my $self = new_self( type => 'any', password_type => 'clear');
154 $self->authenticate( $c, $realm, {domain => [qw/dom1 dom2/]} );
155 } qr/^ $Catalyst::DETACH $/x, "detached";
156 like( ($res_headers->header('WWW-Authenticate'))[0], qr/domain="dom1 dom2"/, "WWW-Authenticate header set: digest domains set");
157 like( ($res_headers->header('WWW-Authenticate'))[1], qr/domain="dom1 dom2"/, "WWW-Authenticate header set: basic domains set");
160 # Check domain config works with use_uri_for option
165 my $self = new_self( type => 'any', password_type => 'clear', use_uri_for => 1);
167 $self->authenticate( $c, $realm, {domain => [qw/dom1 dom2/]} );
168 } qr/^ $Catalyst::DETACH $/x, "detached";
169 like( ($res_headers->header('WWW-Authenticate'))[0], qr/domain="uri_for:dom1 uri_for:dom2"/,
170 "WWW-Authenticate header set: digest domains set with use_uri_for");
171 like( ($res_headers->header('WWW-Authenticate'))[1], qr/domain="uri_for:dom1 uri_for:dom2"/,
172 "WWW-Authenticate header set: basic domains set with use_uri_for");