committing fixes helps sometimes... also more DWIM for typekey
[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("verify", sub {
31         my ( $self, $p ) = @_;
32
33         if ( blessed($p) ) {
34                 return \%user if ( $p->param("sig") );
35         } else {
36                 return \%user if ( $p->{sig} );
37         }
38 });
39
40 my $store = Test::MockObject->new;
41 $store->mock( get_user => sub { shift; Catalyst::Plugin::Authentication::User::Hash->new( @_ ) } );
42
43 my $c = Test::MockObject::Extends->new($m);
44 $c->set_always( config => {} );
45 my $config = $c->config->{authentication}{typekey} ||= {};
46
47 $c->set_always( req     => $req );
48 $c->set_always( request => $req );
49 $c->set_false("debug");
50
51 my $authenticated;
52 $c->mock( set_authenticated => sub { $authenticated = $_[1] } );
53
54 can_ok( $m, "setup" );
55
56 $c->setup;
57
58 isa_ok( $config->{typekey_object},
59     "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' );
60
61 $config->{typekey_object} = $tk;
62
63 can_ok( $m, "authenticate_typekey" );
64
65 lives_ok {
66     $c->authenticate_typekey;
67   }
68   "can try to auth with no args, no params";
69
70 ok( !$c->called("set_authenticated"), "nothing was authenticated" );
71
72 $_->clear for $c, $tk;
73
74 %{ $req->params } = my %vars = (
75     %user,
76         sig   => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
77 );
78
79 lives_ok {
80     $c->authenticate_typekey;
81   }
82   "can try to auth, no args, all params";
83
84 $tk->called_ok("verify");
85 $c->called_ok( "set_authenticated", "authenticated" );
86
87 $_->clear for $c, $tk;
88
89 %{ $req->params } = ();
90 $config->{auth_store} = $store;
91
92 lives_ok {
93     $c->authenticate_typekey(%vars);
94   }
95   "can try to auth with args";
96
97 $tk->called_ok("verify");
98 $c->called_ok( "set_authenticated", "authenticated" );
99 $store->called_ok( "get_user", "user retrieved from store" );
100
101
102 $_->clear for $c, $tk, $store;
103
104 $tk->set_false("verify");
105
106
107 lives_ok {
108     $c->authenticate_typekey(%vars);
109   }
110   "can try to auth with args";
111
112 $tk->called_ok("verify");
113 ok( !$c->called( "set_authenticated" ), "authenticated" );
114 ok( !$store->called( "get_user" ), "no user retrieved from store");
115
116