Tag old version. Update trunk to new version with additional tests
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / t / mock.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More tests => 27;
5 use Test::Exception;
6 use Test::MockObject;
7
8 BEGIN {
9     use_ok('Catalyst::Authentication::Credential::HTTP::Proxy');
10 }
11
12 my $mock_c = Test::MockObject->new;
13 $mock_c->mock('debug' => sub { 0 });
14 my ($authenticated_user, $authenticated);
15 $mock_c->mock('set_authenticated' => sub { $authenticated_user = $_[1]; $authenticated++; });
16
17 my ($auth_info, $user);
18 my $mock_realm = Test::MockObject->new;
19 $mock_realm->mock('find_user' => sub { $auth_info = $_[1]; return $user });
20
21 throws_ok {
22     Catalyst::Authentication::Credential::HTTP::Proxy->new({}, $mock_c, $mock_realm);
23 } qr/Catalyst::Authentication::Credential::HTTP::Proxy/, 'No config throws';
24
25 lives_ok { 
26     Catalyst::Authentication::Credential::HTTP::Proxy->new(
27         {url => 'http://some.proxy:8080'},
28         $mock_c, $mock_realm,
29     );
30 } 'Normal (with url) ok';
31
32 throws_ok {
33     Catalyst::Authentication::Credential::HTTP::Proxy->new(
34         {url => 'http://some.proxy:8080',
35         type => 'foobar'},
36         $mock_c, $mock_realm,
37     );
38 } qr/Catalyst::Authentication::Credential::HTTP::Proxy/, 'Asking for unknown type throws';
39
40 my $log = Test::MockObject->new;
41 $log->mock('info' => sub {});
42 $log->mock('debug' => sub {});
43 my $req = Test::MockObject->new;
44 my $req_headers = HTTP::Headers->new;
45 my $res = Test::MockObject->new;
46 $req->set_always( headers => $req_headers );
47 my $status;
48 $res->mock(status => sub { $status = $_[1] });
49 my $content_type;
50 $res->mock(content_type => sub { $content_type = $_[1] });
51 my $body;
52 my $headers;
53 $res->mock(body => sub { $body = $_[1] });
54 my $res_headers = HTTP::Headers->new;
55 $res->set_always( headers => $res_headers );
56 $mock_c->set_always( debug => 0 );
57 $mock_c->set_always( config => {} );
58 $mock_c->set_always( req => $req );
59 $mock_c->set_always( res => $res );
60 $mock_c->set_always( request => $req );
61 $mock_c->set_always( response => $res );
62 $mock_c->set_always( log => $log );
63
64 $mock_realm->set_always(name => 'myrealm');
65
66 my $cred = Catalyst::Authentication::Credential::HTTP::Proxy->new(
67     {url => 'http://some.proxy:8080',
68     type => 'basic'},
69     $mock_c, $mock_realm,
70 );
71
72 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with no auth headers');
73 throws_ok {
74     $cred->authenticate($mock_c, $mock_realm, {});
75 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach';
76
77 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
78 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm");
79
80 $res_headers->clear;
81
82 $req_headers->authorization_basic( qw/Mufasa password/ );
83 my ($auth_ua, $auth_res, $auth_url);
84 {
85     no warnings qw/redefine once/;
86     *Catalyst::Authentication::Credential::HTTP::Proxy::User::get = sub { $auth_ua = shift; $auth_url = shift; $auth_res };
87 }
88 $auth_res = HTTP::Response->new;
89 $auth_res->code(500);
90 $auth_res->message('FAIL');
91
92 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response !success');
93 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
94 is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
95 throws_ok {
96     $cred->authenticate($mock_c, $mock_realm, {});
97 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';
98
99 like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
100 like( ($res_headers->header('WWW-Authenticate'))[0], qr/realm="myrealm"/, "WWW-Authenticate header set: basic realm");
101
102 $res_headers->clear;
103 $auth_res->code(200);
104 ($auth_url, $auth_ua) = (undef, undef);
105
106 ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response success but no user from realm');
107 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
108 is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
109 is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
110 ok(!$authenticated, 'Not set_authenticated');
111 throws_ok {
112     $cred->authenticate($mock_c, $mock_realm, {});
113 } qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';
114
115 ($auth_url, $auth_ua) = (undef, undef);
116 $res_headers->clear;
117 $user = Test::MockObject->new;
118
119 ok($cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns true with auth response success and user from realm');
120 is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
121 is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
122 ok($authenticated, 'Called set_authenticated');
123 is("$authenticated_user", "$user", 'Called set_authenticated with user object');
124 lives_ok {
125     $cred->authenticate($mock_c, $mock_realm, {});
126 } '$cred->authenticate does not detach';
127 ok(!$res_headers->header('WWW-Authenticate'), 'No authenticate header on successful auth');