Doc update for latest TypeKey goodness
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / t / basic.t
CommitLineData
19c14a20 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More 'no_plan';
7use Test::MockObject::Extends;
8use Test::MockObject;
9use Test::Exception;
52262613 10use Scalar::Util qw/blessed/;
19c14a20 11
12use Catalyst::Plugin::Authentication::User::Hash;
13
14my $m;
15BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) }
16
52262613 17# from 01-verify.t in Authen-TypeKey-0.04
18my %user = (
19 ts => '1091163746',
20 email => 'bentwo@stupidfool.org',
21 name => 'Melody',
22 nick => 'foobar baz',
23);
24
19c14a20 25my $req = Test::MockObject->new;
26$req->set_always( params => {} );
27$req->mock( param => sub { $_[0]->params->{ $_[1] } } );
28
29my $tk = Test::MockObject->new;
52262613 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});
19c14a20 39
40my $store = Test::MockObject->new;
41$store->mock( get_user => sub { shift; Catalyst::Plugin::Authentication::User::Hash->new( @_ ) } );
42
43my $c = Test::MockObject::Extends->new($m);
44$c->set_always( config => {} );
45my $config = $c->config->{authentication}{typekey} ||= {};
46
47$c->set_always( req => $req );
48$c->set_always( request => $req );
49$c->set_false("debug");
50
51my $authenticated;
52$c->mock( set_authenticated => sub { $authenticated = $_[1] } );
53
54can_ok( $m, "setup" );
55
56$c->setup;
57
58isa_ok( $config->{typekey_object},
59 "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' );
60
61$config->{typekey_object} = $tk;
62
63can_ok( $m, "authenticate_typekey" );
64
65lives_ok {
66 $c->authenticate_typekey;
67 }
68 "can try to auth with no args, no params";
69
70ok( !$c->called("set_authenticated"), "nothing was authenticated" );
19c14a20 71
72$_->clear for $c, $tk;
73
19c14a20 74%{ $req->params } = my %vars = (
52262613 75 %user,
76 sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
19c14a20 77);
78
79lives_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
92lives_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
107lives_ok {
108 $c->authenticate_typekey(%vars);
109 }
110 "can try to auth with args";
111
112$tk->called_ok("verify");
113ok( !$c->called( "set_authenticated" ), "authenticated" );
114ok( !$store->called( "get_user" ), "no user retrieved from store");
115
116