add README
[catagits/Catalyst-Authentication-Credential-HTTP-Proxy.git] / t / basic.t
index 12dd00a..dd1b277 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -7,21 +7,43 @@ use Test::More 'no_plan';
 use Test::MockObject::Extends;
 use Test::MockObject;
 use Test::Exception;
+use Scalar::Util qw/blessed/;
 
 use Catalyst::Plugin::Authentication::User::Hash;
 
 my $m;
 BEGIN { use_ok( $m = "Catalyst::Plugin::Authentication::Credential::TypeKey" ) }
 
+# from 01-verify.t in Authen-TypeKey-0.04
+my %user = (
+    ts    => '1091163746',
+    email => 'bentwo@stupidfool.org',
+    name  => 'Melody',
+    nick  => 'foobar baz',
+);
+
 my $req = Test::MockObject->new;
 $req->set_always( params => {} );
 $req->mock( param => sub { $_[0]->params->{ $_[1] } } );
 
 my $tk = Test::MockObject->new;
-$tk->set_true("verify");
+$tk->mock(
+    "verify",
+    sub {
+        my ( $self, $p ) = @_;
+
+        if ( blessed($p) ) {
+            return \%user if ( $p->param("sig") );
+        }
+        else {
+            return \%user if ( $p->{sig} );
+        }
+    }
+);
 
 my $store = Test::MockObject->new;
-$store->mock( get_user => sub { shift; Catalyst::Plugin::Authentication::User::Hash->new( @_ ) } );
+$store->mock( get_user =>
+      sub { shift; Catalyst::Plugin::Authentication::User::Hash->new($_[2]) } );
 
 my $c = Test::MockObject::Extends->new($m);
 $c->set_always( config => {} );
@@ -51,18 +73,12 @@ lives_ok {
   "can try to auth with no args, no params";
 
 ok( !$c->called("set_authenticated"), "nothing was authenticated" );
-ok( !$tk->called("verify"),           "didn't even verify with no params" );
 
 $_->clear for $c, $tk;
 
-# from 01-verify.t in Authen-TypeKey-0.04
-%{ $req->params } = my %vars = (
-    ts    => '1091163746',
-    email => 'bentwo@stupidfool.org',
-    name  => 'Melody',
-    nick  => 'foobar baz',
-    sig   => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
-);
+%{ $req->params } = my %vars =
+  ( %user, sig => 'GWwAIXbkb2xNrQO2e/r2LDl14ek=:U5+tDsPM0+EXeKzFWsosizG7+VU=',
+  );
 
 lives_ok {
     $c->authenticate_typekey;
@@ -84,21 +100,33 @@ lives_ok {
 
 $tk->called_ok("verify");
 $c->called_ok( "set_authenticated", "authenticated" );
-$store->called_ok( "get_user", "user retrieved from store" );
-
+$store->called_ok( "get_user",      "user retrieved from store" );
 
 $_->clear for $c, $tk, $store;
 
 $tk->set_false("verify");
 
-
 lives_ok {
     $c->authenticate_typekey(%vars);
   }
   "can try to auth with args";
 
 $tk->called_ok("verify");
-ok( !$c->called( "set_authenticated" ), "authenticated" );
-ok( !$store->called( "get_user" ), "no user retrieved from store");
+ok( !$c->called("set_authenticated"), "authenticated" );
+ok( !$store->called("get_user"),      "no user retrieved from store" );
+
+$c->logout;
 
+$tk->set_true("verify");
+$c->clear;
+
+ok(
+    $c->authenticate_typekey(
+        my $user = Catalyst::Plugin::Authentication::User::Hash->new(
+            typekey_credentials => { %vars }
+        )
+    ),
+    "can authenticate with user object"
+);
 
+$c->called_ok("set_authenticated");