TypeKey: $store->get_user( \%params, \%result ) made into $store->get( $username...
[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 = (
209fb56b 19 ts => '1091163746',
52262613 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;
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
44my $store = Test::MockObject->new;
209fb56b 45$store->mock( get_user =>
4332afab 46 sub { shift; Catalyst::Plugin::Authentication::User::Hash->new($_[2]) } );
19c14a20 47
48my $c = Test::MockObject::Extends->new($m);
49$c->set_always( config => {} );
50my $config = $c->config->{authentication}{typekey} ||= {};
51
52$c->set_always( req => $req );
53$c->set_always( request => $req );
54$c->set_false("debug");
55
56my $authenticated;
57$c->mock( set_authenticated => sub { $authenticated = $_[1] } );
58
59can_ok( $m, "setup" );
60
61$c->setup;
62
63isa_ok( $config->{typekey_object},
64 "Authen::TypeKey", '$c->config->{authentication}{typekey}{obj}' );
65
66$config->{typekey_object} = $tk;
67
68can_ok( $m, "authenticate_typekey" );
69
70lives_ok {
71 $c->authenticate_typekey;
72 }
73 "can try to auth with no args, no params";
74
75ok( !$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
83lives_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
96lives_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 109lives_ok {
110 $c->authenticate_typekey(%vars);
111 }
112 "can try to auth with args";
113
114$tk->called_ok("verify");
209fb56b 115ok( !$c->called("set_authenticated"), "authenticated" );
116ok( !$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
123ok(
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");