Checking in changes prior to tagging of version 1.004. Changelog diff is:
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / basic.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More tests => 34;
5 use Test::MockObject::Extends;
6 use Test::MockObject;
7 use Test::Exception;
8 use HTTP::Headers;
9
10 my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::HTTP") }
11 can_ok( $m, "authenticate" );
12 can_ok( $m, "authorization_required_response" );
13
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;
18 my $status;
19 $res->mock(status => sub { $status = $_[1] });
20 my $content_type;
21 $res->mock(content_type => sub { $content_type = $_[1] });
22 my $body;
23 my $headers;
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]} });
29 my $find_user_opts;
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 });
41 my @login_info;
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 );
50
51 sub new_self {
52     my $config = { @_ };
53     my $raw_self = $m->new($config, $c, $realm);
54     return Test::MockObject::Extends->new( $raw_self );
55 }
56
57 # Normal auth, simple as possible.
58 # No credentials
59 my $self = new_self( type => 'any', password_type => 'clear', password_field => 'password' );
60 throws_ok {
61     $self->authenticate( $c, $realm );
62 } qr/^ $Catalyst::DETACH $/x, 'Calling authenticate for http auth without header detaches';
63 $user->{password} = 'bar';
64
65 # Wrong credentials
66 $req_headers->authorization_basic( qw/foo quux/ );
67 throws_ok {
68     $self->authenticate( $c, $realm );
69 } qr/^ $Catalyst::DETACH $/x, 'Calling authenticate for http auth without header detaches';
70
71 # Correct credentials
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");
76
77 # Test all the headers look good.
78 $req_headers->clear;
79 $res_headers->clear;
80 $c->clear;
81 throws_ok {
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");
91
92 $res_headers->clear;
93 # Check password_field works
94 {
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");
100     $c->clear;
101     $req_headers->authorization_basic( qw/foo bar/ );
102     throws_ok {
103         $self->authenticate( $c, $realm );
104     } qr/^ $Catalyst::DETACH $/x, "detached on bad password (different password field)";
105 }
106
107 $req_headers->clear;
108 $res_headers->clear;
109 throws_ok {
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");
117
118 # Check authorization_required_message works
119 $req_headers->clear;
120 $res_headers->clear;
121 $c->clear;
122 {
123     my $self = new_self( type => 'any', password_type => 'clear',
124         authorization_required_message => 'foobar'
125     );
126     throws_ok {
127         $self->authenticate( $c, $realm );
128     } qr/^ $Catalyst::DETACH $/x, "detached";
129     is( $body, 'foobar', 'Body is supplied auth message');
130 }
131
132 # Check undef authorization_required_message suppresses crapping in
133 # the body.
134 $req_headers->clear;
135 $res_headers->clear;
136 $c->clear;
137 {
138     my $self = new_self( type => 'any', password_type => 'clear',
139         authorization_required_message => undef
140     );
141     throws_ok {
142         $self->authenticate( $c, $realm );
143     } qr/^ $Catalyst::DETACH $/x, "detached";
144     is( $body, undef, 'Body is not set - user overrode auth message');
145 }
146
147 # Check domain config works
148 $req_headers->clear;
149 $res_headers->clear;
150 $c->clear;
151 {
152     my $self = new_self( type => 'any', password_type => 'clear');
153     throws_ok {
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");
158 }
159
160 # Check domain config works with use_uri_for option
161 $req_headers->clear;
162 $res_headers->clear;
163 $c->clear;
164 {
165     my $self = new_self( type => 'any', password_type => 'clear', use_uri_for => 1);
166     throws_ok {
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");
173 }