Tag old version. Update trunk to new version with additional tests
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / t / basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7 use Test::MockObject::Extends;
8 use Test::MockObject;
9 use Test::Exception;
10 use Scalar::Util qw/blessed/;
11
12 use Catalyst::Plugin::Authentication::User::Hash;
13
14 my $m;
15 BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) }
16
17 # from 01-verify.t in Authen-TypeKey-0.04
18 my %user = (
19     ts    => '1091163746',
20     email => 'bentwo@stupidfool.org',
21     name  => 'Melody',
22     nick  => 'foobar baz',
23 );
24
25 my $req = Test::MockObject->new;
26 $req->set_always( params => {} );
27 $req->mock( param => sub { $_[0]->params->{ $_[1] } } );
28
29 my $tk = Test::MockObject->new;
30 $tk->mock(
31     "verify",
32     sub {
33         my ( $self, $p ) = @_;
34
35         if ( blessed($p) ) {
36             return \%user if ( $p->param("sig") );
37         }
38         else {
39             return \%user if ( $p->{sig} );
40         }
41     }
42 );
43
44 my $store = Test::MockObject->new;
45 $store->mock( get_user =>
46       sub { shift; Catalyst::Plugin::Authentication::User::Hash->new($_[2]) } );
47
48 my $c = Test::MockObject::Extends->new($m);
49 $c->set_always( config => {} );
50 my $config = $c->config->{authentication}{typekey} ||= {};
51
52 $c->set_always( req     => $req );
53 $c->set_always( request => $req );
54 $c->set_false("debug");
55
56 my $authenticated;
57 $c->mock( set_authenticated => sub { $authenticated = $_[1] } );
58 $c->mock( logout => sub { undef $authenticated } );
59
60 can_ok( $m, "setup" );
61
62 $c->setup;
63
64 isa_ok( $config->{typekey_object},
65     "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' );
66
67 $config->{typekey_object} = $tk;
68
69 can_ok( $m, "authenticate_typekey" );
70
71 lives_ok {
72     $c->authenticate_typekey;
73   }
74   "can try to auth with no args, no params";
75
76 ok( !$c->called("set_authenticated"), "nothing was authenticated" );
77
78 $_->clear for $c, $tk;
79
80 %{ $req->params } = my %vars =
81   ( %user, sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
82   );
83
84 lives_ok {
85     $c->authenticate_typekey;
86   }
87   "can try to auth, no args, all params";
88
89 $tk->called_ok("verify");
90 $c->called_ok( "set_authenticated", "authenticated" );
91
92 $_->clear for $c, $tk;
93
94 %{ $req->params } = ();
95 $config->{auth_store} = $store;
96
97 lives_ok {
98     $c->authenticate_typekey(%vars);
99   }
100   "can try to auth with args";
101
102 $tk->called_ok("verify");
103 $c->called_ok( "set_authenticated", "authenticated" );
104 $store->called_ok( "get_user",      "user retrieved from store" );
105
106 $_->clear for $c, $tk, $store;
107
108 $tk->set_false("verify");
109
110 lives_ok {
111     $c->authenticate_typekey(%vars);
112   }
113   "can try to auth with args";
114
115 $tk->called_ok("verify");
116 ok( !$c->called("set_authenticated"), "authenticated" );
117 ok( !$store->called("get_user"),      "no user retrieved from store" );
118
119 $c->logout;
120
121 $tk->set_true("verify");
122 $c->clear;
123
124 ok(
125     $c->authenticate_typekey(
126         my $user = Catalyst::Plugin::Authentication::User::Hash->new(
127             typekey_credentials => { %vars }
128         )
129     ),
130     "can authenticate with user object"
131 );
132
133 $c->called_ok("set_authenticated");