Checking in changes prior to tagging of version 1.004. Changelog diff is:
[catagits/Catalyst-Authentication-Credential-HTTP.git] / t / basic.t
CommitLineData
2022b950 1#!/usr/bin/perl
2022b950 2use strict;
3use warnings;
c5a1fa88 4use Test::More tests => 34;
2022b950 5use Test::MockObject::Extends;
6use Test::MockObject;
d9914dd2 7use Test::Exception;
2022b950 8use HTTP::Headers;
9
513d8ab6 10my $m; BEGIN { use_ok($m = "Catalyst::Authentication::Credential::HTTP") }
11can_ok( $m, "authenticate" );
2022b950 12can_ok( $m, "authorization_required_response" );
490754a8 13
2022b950 14my $req = Test::MockObject->new;
15my $req_headers = HTTP::Headers->new;
2022b950 16$req->set_always( headers => $req_headers );
2022b950 17my $res = Test::MockObject->new;
2022b950 18my $status;
19$res->mock(status => sub { $status = $_[1] });
f366138e 20my $content_type;
21$res->mock(content_type => sub { $content_type = $_[1] });
22my $body;
513d8ab6 23my $headers;
f366138e 24$res->mock(body => sub { $body = $_[1] });
2022b950 25my $res_headers = HTTP::Headers->new;
26$res->set_always( headers => $res_headers );
513d8ab6 27my $user = Test::MockObject->new;
490754a8 28$user->mock(get => sub { return shift->{$_[0]} });
29my $find_user_opts;
30my $realm = Test::MockObject->new;
513d8ab6 31$realm->mock( find_user => sub { $find_user_opts = $_[1]; return $user; });
32$realm->mock( name => sub { 'foo' } );
33my $c = Test::MockObject->new;
007935b8 34my $cache = Test::MockObject->new;
35$cache->mock(set => sub { shift->{$_[0]} = $_[1] });
36$cache->mock(get => sub { return shift->{$_[0]} });
c5a1fa88 37my $uri_for_called = 0;
38$c->mock(uri_for => sub { my ($c, $uri) = @_; $uri_for_called++; return 'uri_for:' . $uri} );
007935b8 39$c->mock(cache => sub { $cache });
4e8cbd42 40$c->mock(debug => sub { 0 });
2022b950 41my @login_info;
42$c->mock( login => sub { shift; @login_info = @_; 1 } );
513d8ab6 43my $authenticated = 0;
44$c->mock( set_authenticated => sub { $authenticated++; } );
2022b950 45$c->set_always( config => {} );
46$c->set_always( req => $req );
47$c->set_always( res => $res );
513d8ab6 48$c->set_always( request => $req );
49$c->set_always( response => $res );
490754a8 50
51sub 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
59my $self = new_self( type => 'any', password_type => 'clear', password_field => 'password' );
60throws_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/ );
67throws_ok {
68 $self->authenticate( $c, $realm );
69} qr/^ $Catalyst::DETACH $/x, 'Calling authenticate for http auth without header detaches';
70
71# Correct credentials
2022b950 72$req_headers->authorization_basic( qw/foo bar/ );
513d8ab6 73ok($self->authenticate($c, $realm), "auth successful with header");
74is($authenticated, 1, 'authenticated once');
513d8ab6 75is_deeply( $find_user_opts, { username => 'foo'}, "login delegated");
490754a8 76
77# Test all the headers look good.
2022b950 78$req_headers->clear;
bf399285 79$res_headers->clear;
2022b950 80$c->clear;
d9914dd2 81throws_ok {
513d8ab6 82 $self->authenticate( $c, $realm );
d9914dd2 83} qr/^ $Catalyst::DETACH $/x, "detached on no authorization required with bad auth";
2022b950 84is( $status, 401, "401 status code" );
f366138e 85is( $content_type, 'text/plain' );
86is( $body, 'Authorization required.' );
007935b8 87like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Digest/, "WWW-Authenticate header set: digest");
513d8ab6 88like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="foo"/, "WWW-Authenticate header set: digest realm");
007935b8 89like( ($res_headers->header('WWW-Authenticate'))[1], qr/^Basic/, "WWW-Authenticate header set: basic");
513d8ab6 90like( ($res_headers->header('WWW-Authenticate'))[1], qr/realm="foo"/, "WWW-Authenticate header set: basic realm");
6afc3665 91
bf399285 92$res_headers->clear;
490754a8 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;
bf399285 108$res_headers->clear;
513d8ab6 109throws_ok {
110 $self->authenticate( $c, $realm, { realm => 'myrealm' }); # Override realm object's name method by doing this.
490754a8 111} qr/^ $Catalyst::DETACH $/x, "detached on no authorization supplied, overridden realm value";
513d8ab6 112is( $status, 401, "401 status code" );
113is( $content_type, 'text/plain' );
114is( $body, 'Authorization required.' );
bf399285 115like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: digest realm overridden");
116like( ($res_headers->header('WWW-Authenticate'))[1], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm overridden");
117
05512a69 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
f1f73b53 132# Check undef authorization_required_message suppresses crapping in
133# the body.
05512a69 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');
f1f73b53 145}
146
147# Check domain config works
148$req_headers->clear;
149$res_headers->clear;
150$c->clear;
151{
c5a1fa88 152 my $self = new_self( type => 'any', password_type => 'clear');
f1f73b53 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}
c5a1fa88 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}