Removed 'password' from minimal store in example because it's not relevant to the...
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / t / mock.t
CommitLineData
b2cd5ce7 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 27;
5use Test::Exception;
6use Test::MockObject;
7
8BEGIN {
9 use_ok('Catalyst::Authentication::Credential::HTTP::Proxy');
10}
11
12my $mock_c = Test::MockObject->new;
13$mock_c->mock('debug' => sub { 0 });
14my ($authenticated_user, $authenticated);
15$mock_c->mock('set_authenticated' => sub { $authenticated_user = $_[1]; $authenticated++; });
16
17my ($auth_info, $user);
18my $mock_realm = Test::MockObject->new;
19$mock_realm->mock('find_user' => sub { $auth_info = $_[1]; return $user });
20
21throws_ok {
22 Catalyst::Authentication::Credential::HTTP::Proxy->new({}, $mock_c, $mock_realm);
23} qr/Catalyst::Authentication::Credential::HTTP::Proxy/, 'No config throws';
24
25lives_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
32throws_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
40my $log = Test::MockObject->new;
41$log->mock('info' => sub {});
42$log->mock('debug' => sub {});
43my $req = Test::MockObject->new;
44my $req_headers = HTTP::Headers->new;
45my $res = Test::MockObject->new;
46$req->set_always( headers => $req_headers );
47my $status;
48$res->mock(status => sub { $status = $_[1] });
49my $content_type;
50$res->mock(content_type => sub { $content_type = $_[1] });
51my $body;
52my $headers;
53$res->mock(body => sub { $body = $_[1] });
54my $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
66my $cred = Catalyst::Authentication::Credential::HTTP::Proxy->new(
67 {url => 'http://some.proxy:8080',
68 type => 'basic'},
69 $mock_c, $mock_realm,
70);
71
72ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with no auth headers');
73throws_ok {
74 $cred->authenticate($mock_c, $mock_realm, {});
75} qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach';
76
77like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
78like( ($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/ );
83my ($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
92ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response !success');
93is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
94is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
95throws_ok {
96 $cred->authenticate($mock_c, $mock_realm, {});
97} qr/^$Catalyst::DETACH$/, '$cred->authenticate calls detach with auth response !success';
98
99like( ($res_headers->header('WWW-Authenticate'))[0], qr/^Basic/, "WWW-Authenticate header set: basic");
100like( ($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
106ok(!$cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns false with auth response success but no user from realm');
107is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
108is($auth_url, 'http://some.proxy:8080', 'get http://some.proxy:8080');
109is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
110ok(!$authenticated, 'Not set_authenticated');
111throws_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
119ok($cred->authenticate_basic($mock_c, $mock_realm, {}), '_authenticate_basic returns true with auth response success and user from realm');
120is_deeply([$auth_ua->get_basic_credentials], [qw/Mufasa password/], 'Basic auth in useragent is Mufasa/password');
121is_deeply($auth_info, { username => 'Mufasa'}, '$realm->find_user({ username => "Mufasa" })');
122ok($authenticated, 'Called set_authenticated');
123is("$authenticated_user", "$user", 'Called set_authenticated with user object');
124lives_ok {
125 $cred->authenticate($mock_c, $mock_realm, {});
126} '$cred->authenticate does not detach';
127ok(!$res_headers->header('WWW-Authenticate'), 'No authenticate header on successful auth');