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; |
10 | |
11 | use Catalyst::Plugin::Authentication::User::Hash; |
12 | |
13 | my $m; |
14 | BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) } |
15 | |
16 | my $req = Test::MockObject->new; |
17 | $req->set_always( params => {} ); |
18 | $req->mock( param => sub { $_[0]->params->{ $_[1] } } ); |
19 | |
20 | my $tk = Test::MockObject->new; |
21 | $tk->set_true("verify"); |
22 | |
23 | my $store = Test::MockObject->new; |
24 | $store->mock( get_user => sub { shift; Catalyst::Plugin::Authentication::User::Hash->new( @_ ) } ); |
25 | |
26 | my $c = Test::MockObject::Extends->new($m); |
27 | $c->set_always( config => {} ); |
28 | my $config = $c->config->{authentication}{typekey} ||= {}; |
29 | |
30 | $c->set_always( req => $req ); |
31 | $c->set_always( request => $req ); |
32 | $c->set_false("debug"); |
33 | |
34 | my $authenticated; |
35 | $c->mock( set_authenticated => sub { $authenticated = $_[1] } ); |
36 | |
37 | can_ok( $m, "setup" ); |
38 | |
39 | $c->setup; |
40 | |
41 | isa_ok( $config->{typekey_object}, |
42 | "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' ); |
43 | |
44 | $config->{typekey_object} = $tk; |
45 | |
46 | can_ok( $m, "authenticate_typekey" ); |
47 | |
48 | lives_ok { |
49 | $c->authenticate_typekey; |
50 | } |
51 | "can try to auth with no args, no params"; |
52 | |
53 | ok( !$c->called("set_authenticated"), "nothing was authenticated" ); |
54 | ok( !$tk->called("verify"), "didn't even verify with no params" ); |
55 | |
56 | $_->clear for $c, $tk; |
57 | |
58 | # from 01-verify.t in Authen-TypeKey-0.04 |
59 | %{ $req->params } = my %vars = ( |
60 | ts => '1091163746', |
61 | email => 'bentwo@stupidfool.org', |
62 | name => 'Melody', |
63 | nick => 'foobar baz', |
64 | sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=', |
65 | ); |
66 | |
67 | lives_ok { |
68 | $c->authenticate_typekey; |
69 | } |
70 | "can try to auth, no args, all params"; |
71 | |
72 | $tk->called_ok("verify"); |
73 | $c->called_ok( "set_authenticated", "authenticated" ); |
74 | |
75 | $_->clear for $c, $tk; |
76 | |
77 | %{ $req->params } = (); |
78 | $config->{auth_store} = $store; |
79 | |
80 | lives_ok { |
81 | $c->authenticate_typekey(%vars); |
82 | } |
83 | "can try to auth with args"; |
84 | |
85 | $tk->called_ok("verify"); |
86 | $c->called_ok( "set_authenticated", "authenticated" ); |
87 | $store->called_ok( "get_user", "user retrieved from store" ); |
88 | |
89 | |
90 | $_->clear for $c, $tk, $store; |
91 | |
92 | $tk->set_false("verify"); |
93 | |
94 | |
95 | lives_ok { |
96 | $c->authenticate_typekey(%vars); |
97 | } |
98 | "can try to auth with args"; |
99 | |
100 | $tk->called_ok("verify"); |
101 | ok( !$c->called( "set_authenticated" ), "authenticated" ); |
102 | ok( !$store->called( "get_user" ), "no user retrieved from store"); |
103 | |
104 | |