Commit | Line | Data |
19c14a20 |
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; |
52262613 |
10 | use Scalar::Util qw/blessed/; |
19c14a20 |
11 | |
12 | use Catalyst::Plugin::Authentication::User::Hash; |
13 | |
14 | my $m; |
15 | BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) } |
16 | |
52262613 |
17 | # from 01-verify.t in Authen-TypeKey-0.04 |
18 | my %user = ( |
209fb56b |
19 | ts => '1091163746', |
52262613 |
20 | email => 'bentwo@stupidfool.org', |
21 | name => 'Melody', |
22 | nick => 'foobar baz', |
23 | ); |
24 | |
19c14a20 |
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; |
209fb56b |
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 | ); |
19c14a20 |
43 | |
44 | my $store = Test::MockObject->new; |
209fb56b |
45 | $store->mock( get_user => |
46 | sub { shift; Catalyst::Plugin::Authentication::User::Hash->new(@_) } ); |
19c14a20 |
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 | |
59 | can_ok( $m, "setup" ); |
60 | |
61 | $c->setup; |
62 | |
63 | isa_ok( $config->{typekey_object}, |
64 | "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' ); |
65 | |
66 | $config->{typekey_object} = $tk; |
67 | |
68 | can_ok( $m, "authenticate_typekey" ); |
69 | |
70 | lives_ok { |
71 | $c->authenticate_typekey; |
72 | } |
73 | "can try to auth with no args, no params"; |
74 | |
75 | ok( !$c->called("set_authenticated"), "nothing was authenticated" ); |
19c14a20 |
76 | |
77 | $_->clear for $c, $tk; |
78 | |
209fb56b |
79 | %{ $req->params } = my %vars = |
80 | ( %user, sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=', |
81 | ); |
19c14a20 |
82 | |
83 | lives_ok { |
84 | $c->authenticate_typekey; |
85 | } |
86 | "can try to auth, no args, all params"; |
87 | |
88 | $tk->called_ok("verify"); |
89 | $c->called_ok( "set_authenticated", "authenticated" ); |
90 | |
91 | $_->clear for $c, $tk; |
92 | |
93 | %{ $req->params } = (); |
94 | $config->{auth_store} = $store; |
95 | |
96 | lives_ok { |
97 | $c->authenticate_typekey(%vars); |
98 | } |
99 | "can try to auth with args"; |
100 | |
101 | $tk->called_ok("verify"); |
102 | $c->called_ok( "set_authenticated", "authenticated" ); |
209fb56b |
103 | $store->called_ok( "get_user", "user retrieved from store" ); |
19c14a20 |
104 | |
105 | $_->clear for $c, $tk, $store; |
106 | |
107 | $tk->set_false("verify"); |
108 | |
19c14a20 |
109 | lives_ok { |
110 | $c->authenticate_typekey(%vars); |
111 | } |
112 | "can try to auth with args"; |
113 | |
114 | $tk->called_ok("verify"); |
209fb56b |
115 | ok( !$c->called("set_authenticated"), "authenticated" ); |
116 | ok( !$store->called("get_user"), "no user retrieved from store" ); |
117 | |
118 | $c->logout; |
19c14a20 |
119 | |
209fb56b |
120 | $tk->set_true("verify"); |
121 | $c->clear; |
122 | |
123 | ok( |
124 | $c->authenticate_typekey( |
125 | my $user = Catalyst::Plugin::Authentication::User::Hash->new( |
126 | typekey_credentials => { %vars } |
127 | ) |
128 | ), |
129 | "can authenticate with user object" |
130 | ); |
19c14a20 |
131 | |
209fb56b |
132 | $c->called_ok("set_authenticated"); |