From: <pau...@us...> - 2006-12-06 09:43:28
|
Revision: 920 http://svn.sourceforge.net/everydevel/?rev=920&view=rev Author: paul_the_nomad Date: 2006-12-06 01:43:27 -0800 (Wed, 06 Dec 2006) Log Message: ----------- Tests for EveryAuth.pm. Also code cleanup. Small amount of documentation for Auth.pm Modified Paths: -------------- trunk/ebase/lib/Everything/Auth/EveryAuth.pm trunk/ebase/lib/Everything/Auth.pm Added Paths: ----------- trunk/ebase/lib/Everything/Auth/Test/ trunk/ebase/lib/Everything/Auth/Test/EveryAuth.pm trunk/ebase/t/Everything/Auth/ trunk/ebase/t/Everything/Auth/EveryAuth.t Modified: trunk/ebase/lib/Everything/Auth/EveryAuth.pm =================================================================== --- trunk/ebase/lib/Everything/Auth/EveryAuth.pm 2006-12-04 10:19:05 UTC (rev 919) +++ trunk/ebase/lib/Everything/Auth/EveryAuth.pm 2006-12-06 09:43:27 UTC (rev 920) @@ -49,7 +49,7 @@ my $passwd = $query->param("passwd"); my $cookie; - my $U = getNode( $user, 'user' ); + my $U = $DB->getNode( $user, 'user' ); $user = $$U{title} if $U; my $USER_HASH; @@ -95,7 +95,7 @@ my $cookie = $query->cookie( -name => 'userpass', -value => "" ); #We need to force the guest user on logouts here, otherwise the cookie won't get cleared. - my $user = getNode( $this->{options}->{guest_user} ); + my $user = $DB->getNode( $this->{options}->{guest_user} ); $$user{cookie} = $cookie if ($cookie); return $user; @@ -117,7 +117,6 @@ sub authUser { my $this = shift; - my ( $user_id, $cookie, $user, $passwd ); my $USER_HASH; if ( my $oldcookie = $query->cookie("userpass") ) @@ -126,13 +125,7 @@ confirmUser( split( /\|/, Everything::Util::unescape($oldcookie) ) ); } - - # Get the user node - $USER_HASH ||= getNode($user_id); - - # Store this user's cookie! - $$USER_HASH{cookie} = $cookie if ( $cookie and $USER_HASH ); - + return unless $USER_HASH; return $USER_HASH; } @@ -156,7 +149,7 @@ sub confirmUser { my ( $nick, $crpasswd ) = @_; - my $user = $DB->getNode( $nick, getType('user') ); + my $user = $DB->getNode( $nick, $DB->getType('user') ); my $genCrypt; return undef unless ($user); Added: trunk/ebase/lib/Everything/Auth/Test/EveryAuth.pm =================================================================== --- trunk/ebase/lib/Everything/Auth/Test/EveryAuth.pm (rev 0) +++ trunk/ebase/lib/Everything/Auth/Test/EveryAuth.pm 2006-12-06 09:43:27 UTC (rev 920) @@ -0,0 +1,185 @@ +package Everything::Auth::Test::EveryAuth; + +use base 'Everything::Test::Abstract'; +use Test::More; +use Test::MockObject; +use SUPER; +use CGI; +use strict; + +sub startup : Test(startup=> 2) { + my $self = shift; + my $module = $self->module_class(); + my $mock = Test::MockObject->new; + $mock->fake_module('Everything'); + $mock->fake_module('Everything::HTML'); + my $cgi = CGI->new; + no strict 'refs'; + *{ $module . '::query' } = \$cgi; + *{ $module . '::DB' } = \$mock; + use strict 'refs'; + use_ok($module) or exit; + $self->{class} = $module; + my $instance = $self->{class}->new; + isa_ok( $instance, $self->{class} ); + $self->{instance} = $instance; + $self->{mock} = $mock; + $self->{cgi} = $cgi; + +} + +sub fixture : Test(setup) { + my $self = shift; + my $class = $self->{class}; + my $cgi = CGI->new; + no strict 'refs'; + *{ $class . '::query' } = \$cgi; + use strict 'refs'; + $self->{cgi} = $cgi; +} + +## +## returns a node of type "user" setting $user->{cookie}. + +sub test_login_user : Test(5) { + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + my $cgi = $self->{cgi}; + my $mock = $self->{mock}; + $mock->clear; + can_ok( $class, 'loginUser' ); + + my @args = (); + no strict 'refs'; + local *{ $class . '::confirmUser' }; + *{ $class . '::confirmUser' } = sub { + @args = @_; + return { user => { a => 'user' } }; + }; + use strict 'refs'; + + $mock->set_always( 'getNode', $mock ); + $mock->{title} = "a user title"; + + $cgi->param( 'user', 'username' ); + $cgi->param( 'passwd', 'pw' ); + + my $result = $instance->loginUser; + my ( $method, $args ) = $mock->next_call; + is( $method, 'getNode', '...should get a user node.' ); + is( $args->[1], 'username', '... with args passed by the cgi object.' ); + + ## and calls the confirm user sub + is_deeply( + [@args], + [ 'a user title', crypt( 'pw', 'a user title' ) ], + '...calls confirmUser with correct args' + ); + + my $cookie = $cgi->cookie( + -name => "userpass", + -value => + $cgi->escape( 'a user title' . '|' . crypt( 'pw', 'a user title' ) ), + -expires => $cgi->param("expires") + ); + is( $result->{cookie}, $cookie, + '...and returns a hash containing the cookie.' ); +} + +## returns the node of type 'user' specified by $self-> +sub test_logout_user : Test(4) { + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + my $cgi = $self->{cgi}; + my $mock = $self->{mock}; + $mock->clear; + can_ok( $class, 'logoutUser' ); + my $result = $instance->logoutUser; + my ( $method, $args ) = $mock->next_call; + is( $method, 'getNode', '...gets the guest user node' ); + is( + $args->[1], + $instance->{options}->{guest_user}, + '...using the guest user' + ); + my $cookie = $cgi->cookie( + -name => "userpass", + -value => '' + ); + is( $result->{cookie}, $cookie, '...unsetting the cookie value.' ); +} + +## gets cookie and compares it with db using confirmUser. +## returns undef on failure. A node of type user on success. +sub test_auth_user : Test(4) { + + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + my $cgi = $self->{cgi}; + my $mock = $self->{mock}; + $mock->set_always( 'getNode', $mock ); + can_ok( $class, 'authUser' ) || return "Can't authUser"; + + ## mock the $query global + my $fake_cgi = Test::MockObject->new; + no strict 'refs'; + local *{ $class . '::query' }; + *{ $class . '::query' } = \$fake_cgi; + use strict 'refs'; + + my $oldcookie = 'a cookie'; + $fake_cgi->set_always( 'cookie', $oldcookie ); + + ## setup confirmUser behaviour + my @a = (); + my @rv = ( { cookie => 'a cookie' }, undef ); + no strict 'refs'; + local *{ $class . '::confirmUser' }; + *{ $class . '::confirmUser' } = sub { + @a = @_; + return shift @rv; + }; + use strict 'refs'; + + my $result = $instance->authUser; + is( "@a", $oldcookie, '...should grab the old cookie.' ); + is( $result->{cookie}, $oldcookie, '...returns the user with the cookie.' ); + + $result = $instance->authUser; + is( $result, undef, '...and returns undef on failure.' ); +} + +## takes two args. The username and a hash of the password +## if no such username or passwords don't match returns undef +## other returns the user node. +sub test_confirm_user : Test(3) { + + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + my $cgi = $self->{cgi}; + my $mock = $self->{mock}; + + can_ok( $class, 'confirmUser' ) || return "Can't confirmUser"; + + my $pw = 'password'; + my $name = 'name'; + my $expected_rv = + { title => $name, passwd => $pw, lasttime => 'timestamp' }; + my $crypted = crypt( $pw, $name ); + $mock->set_series( 'getNode', undef, $expected_rv ); + $mock->set_true('getType'); + $mock->set_always( 'sqlSelect', 'timestamp' ); + + my $confirmUser = \&{ $class . '::confirmUser' }; + my $result = $confirmUser->( $name, $crypted ); + is( $result, undef, '..returns undef if getNode doesn\'t get a node.' ); + + $result = $confirmUser->( $name, $crypted ); + is_deeply( $result, $expected_rv, '...returns node if passwords match.' ); +} + +1; Property changes on: trunk/ebase/lib/Everything/Auth/Test/EveryAuth.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Modified: trunk/ebase/lib/Everything/Auth.pm =================================================================== --- trunk/ebase/lib/Everything/Auth.pm 2006-12-04 10:19:05 UTC (rev 919) +++ trunk/ebase/lib/Everything/Auth.pm 2006-12-06 09:43:27 UTC (rev 920) @@ -31,6 +31,19 @@ Everything::HTML doesn't really need to know that another plugin is there. We should be able to swap them out without changing anything. +It takes one argument, a hash ref. The hash ref takes the following key => value pairs. + +=over + +=item Auth => name of the authorisation module to use. +Defaults to EveryAuth. + +=item guest_user => a node object that is the Guest User to use. + +The authorisation modules may accept other options. Check their document. + +=back + =cut sub new Added: trunk/ebase/t/Everything/Auth/EveryAuth.t =================================================================== --- trunk/ebase/t/Everything/Auth/EveryAuth.t (rev 0) +++ trunk/ebase/t/Everything/Auth/EveryAuth.t 2006-12-06 09:43:27 UTC (rev 920) @@ -0,0 +1,5 @@ +#!/usr/bin/perl + +use Everything::Auth::Test::EveryAuth; + +Everything::Auth::Test::EveryAuth->runtests; \ No newline at end of file Property changes on: trunk/ebase/t/Everything/Auth/EveryAuth.t ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |