Extend WebGUI::Test::Maker::Permission to handle class methods, like Asset->canAdd and Operations.

Add canEdit and canAdd tests to Asset.t
This commit is contained in:
Colin Kuskie 2007-12-23 23:30:26 +00:00
parent 9ebae3087e
commit 924f09a09f
2 changed files with 213 additions and 112 deletions

View file

@ -13,6 +13,7 @@ use strict;
use lib "$FindBin::Bin/../lib";
use WebGUI::Test;
use WebGUI::Test::Maker::Permission;
use WebGUI::Session;
use WebGUI::Asset;
use WebGUI::Asset::Wobject::Navigation;
@ -25,99 +26,49 @@ use Test::MockObject;
my $session = WebGUI::Test->session;
my @fixIdTests = (
{
id => '0',
pass => 1,
comment => 'digit zero',
},
{
id => '1',
pass => 1,
comment => 'digit one',
},
{
id => '123',
pass => 1,
comment => '3 digit integer',
},
{
id => '12345678901'x2,
pass => 1,
comment => '22 digit integer',
},
{
id => '12345678901'x4,
pass => 0,
comment => '44 digit integer',
},
{
id => '',
pass => 0,
comment => 'null string is rejected',
},
{
id => 'a',
pass => 0,
comment => 'single lower case character rejected',
},
{
# '1234567890123456789012'
id => 'abc123ZYX098deadbeef()',
pass => 0,
comment => 'illegal characters in length 22 string rejected',
},
{
id => $session->id->generate,
pass => 1,
comment => 'valid id accepted',
},
);
my @fixIdTests = getFixIdTests($session);
my @fixTitleTests = getFixTitleTests($session);
my @fixTitleTests = (
{
title => undef,
fixed => 0,
comment => "undef returns the Asset's title",
},
{
title => '',
fixed => 0,
comment => "null string returns the Asset's title",
},
{
title => 'untitled',
fixed => 0,
comment => "'untitled' returns the Asset's title",
},
{
title => 'UnTiTlEd',
fixed => 0,
comment => "'untitled' in any case returns the Asset's title",
},
{
title => 'Username: ^@;',
fixed => 'Username: ^@;',
comment => "Macros are negated",
},
{
title => '<b>A bold title</b>',
fixed => 'A bold title',
comment => "Markup is stripped out",
},
{
title => 'Javascript: <script>Evil code goes in here</script>',
fixed => 'Javascript: ',
comment => "javascript removed",
},
{
title => 'This is a good Title',
fixed => 'This is a good Title',
comment => "Good titles are passed",
},
);
my $rootAsset = WebGUI::Asset->getRoot($session);
plan tests => 56 + scalar(@fixIdTests) + scalar(@fixTitleTests);
my $canAddMaker = WebGUI::Test::Maker::Permission->new();
$canAddMaker->prepare({
'className' => 'WebGUI::Asset',
'session' => $session,
'method' => 'canAdd',
'pass' => [3],
'fail' => [1],
});
my $properties;
$properties = {
# '1234567890123456789012'
id => 'canEditAsset0000000010',
title => 'canEdit Asset Test',
url => 'canEditAsset1',
className => 'WebGUI::Asset',
};
my $versionTag2 = WebGUI::VersionTag->getWorking($session);
my $canEditAsset = $rootAsset->addChild($properties, $properties->{id});
$versionTag2->commit;
my $canEditMaker = WebGUI::Test::Maker::Permission->new();
$canEditMaker->prepare({
'object' => $canEditAsset,
'method' => 'canEdit',
'pass' => [3],
'fail' => [1],
});
plan tests => 56
+ scalar(@fixIdTests)
+ scalar(@fixTitleTests)
+ $canAddMaker->plan
+ $canEditMaker->plan
;
# Test the default constructor
my $defaultAsset = WebGUI::Asset->getDefault($session);
@ -180,7 +131,6 @@ is ($deadAsset, undef,'newByDynamicClass constructor with invalid assetId return
}
# Root Asset
my $rootAsset = WebGUI::Asset->getRoot($session);
isa_ok($rootAsset, 'WebGUI::Asset');
is($rootAsset->getId, 'PBasset000000000000001', 'Root Asset ID check');
@ -250,7 +200,7 @@ $session->{_request} = $origRequest;
my $versionTag = WebGUI::VersionTag->getWorking($session);
$versionTag->set({name=>"Asset tests"});
my $properties = {
$properties = {
# '1234567890123456789012'
id => 'fixUrlAsset00000000012',
title => 'fixUrl Asset Test',
@ -404,9 +354,131 @@ TODO: {
ok(0, "Test the default name for the icon, if not given in the definition sub");
}
################################################################
#
# canAdd
#
################################################################
$canAddMaker->run;
################################################################
#
# canEdit
#
################################################################
$canEditMaker->run;
END: {
$session->config->set('extrasURL', $origExtras);
$session->config->set('uploadsURL', $origUploads);
$session->setting->set('urlExtension', $origUrlExtension);
$versionTag->rollback;
foreach my $vTag ($versionTag, $versionTag2) {
$vTag->rollback;
}
}
##Return an array of hashrefs. Each hashref describes a test
##for the fixId method.
sub getFixIdTests {
my $session = shift;
return (
{
id => '0',
pass => 1,
comment => 'digit zero',
},
{
id => '1',
pass => 1,
comment => 'digit one',
},
{
id => '123',
pass => 1,
comment => '3 digit integer',
},
{
id => '12345678901'x2,
pass => 1,
comment => '22 digit integer',
},
{
id => '12345678901'x4,
pass => 0,
comment => '44 digit integer',
},
{
id => '',
pass => 0,
comment => 'null string is rejected',
},
{
id => 'a',
pass => 0,
comment => 'single lower case character rejected',
},
{
# '1234567890123456789012'
id => 'abc123ZYX098deadbeef()',
pass => 0,
comment => 'illegal characters in length 22 string rejected',
},
{
id => $session->id->generate,
pass => 1,
comment => 'valid id accepted',
},
);
}
##Return an array of hashrefs. Each hashref describes a test
##for the fixTitle method. If "fixed" != 0, it should
##contain what the fixTitle method will return.
sub getFixTitleTests {
my $session = shift;
return ({
title => undef,
fixed => 0,
comment => "undef returns the Asset's title",
},
{
title => '',
fixed => 0,
comment => "null string returns the Asset's title",
},
{
title => 'untitled',
fixed => 0,
comment => "'untitled' returns the Asset's title",
},
{
title => 'UnTiTlEd',
fixed => 0,
comment => "'untitled' in any case returns the Asset's title",
},
{
title => 'Username: ^@;',
fixed => 'Username: &#94;@;',
comment => "Macros are negated",
},
{
title => '<b>A bold title</b>',
fixed => 'A bold title',
comment => "Markup is stripped out",
},
{
title => 'Javascript: <script>Evil code goes in here</script>',
fixed => 'Javascript: ',
comment => "javascript removed",
},
{
title => 'This is a good Title',
fixed => 'This is a good Title',
comment => "Good titles are passed",
},
);
}

View file

@ -2,6 +2,7 @@ package WebGUI::Test::Maker::Permission;
use base 'WebGUI::Test::Maker';
use Scalar::Util qw( blessed );
use Carp qw( croak );
use Test::More;
@ -141,8 +142,10 @@ sub prepare {
for my $test (@tests) {
$test_num++;
croak("Couldn't prepare: Test $test_num has no object")
unless $test->{object};
croak("Couldn't prepare: Test $test_num has no object or className")
unless $test->{object} || exists($test->{className});
croak("Couldn't prepare: Test $test_num has needs a session object")
if exists($test->{className}) && !$test->{session};
croak("Couldn't prepare: Test $test_num has no method")
unless $test->{method};
croak("Couldn't prepare: Test $test_num has no pass/fail")
@ -153,8 +156,8 @@ sub prepare {
if $test->{fail} && ref $test->{fail} ne "ARRAY";
# Make sure pass and fail arrayrefs are userIds
for my $array ( $test->{pass, fail} ) {
for ( my $i = 0; $i < @$array; $i++ ) {
for my $array ( $test->{'pass'}, $test->{'fail'} ) {
for ( my $i = 0; $i < @{ $array }; $i++ ) {
# If is a User object, replace with userId
if ( blessed $array->[$i] && $array->[$i]->isa("WebGUI::User") ) {
$array->[$i] = $array->[$i]->userId;
@ -180,31 +183,57 @@ sub run {
my $self = shift;
while (my $test = shift @{ $self->{_tests} }) {
my $o = $test->{object};
my $m = $test->{method};
my $session;
my @methodArguments = ();
my ($o, $m, $comment);
if (exists $test->{className}) {
$o = $test->{className};
$m = $test->{method};
$session = $test->{session};
push @methodArguments, $session;
$comment = $test->{className};
}
else {
$o = $test->{object};
$m = $test->{method};
$session = $o->session;
$comment = blessed $o;
}
##This needs to be refactored into a sub/method, instead of copy/paste
##duplicated in fail, below.
if ($test->{pass}) {
for my $userId (@{$test->{pass}}) {
# Test the userId parameter
ok( $o->$m($userId), "$userId passes $m check for " . blessed $o );
my @args = @methodArguments;
# Test the default session user
my $oldUser = $o->session->user;
$o->session->user( WebGUI::User->new($o->session, $userId) );
ok( $o->$m(), "$userId passes $m check using default user for " . blessed $o );
$o->session->user($oldUser);
my $oldUser = $session->user;
$session->user( { userId => $userId } );
ok( $o->$m(@args), "userId $userId passes $m check using default user for " . $comment );
$session->user( { user => $oldUser });
# Test the specified userId
push @args, $userId;
# Test the userId parameter
ok( $o->$m(@args), "userId $userId passes $m check for " . $comment );
}
}
if ($test->{fail}) {
for my $userId (@{$test->{fail}}) {
# Test the userId parameter
ok( !($o->$m($userId)), "$userId fails $m check for " . blessed $o );
my @args = @methodArguments;
# Test the default session user
my $oldUser = $o->session->user;
$o->session->user( WebGUI::User->new($o->session, $userId) );
ok( !($o->$m()), "$userId fails $m check using default user for " . blessed $o );
$o->session->user($oldUser);
my $oldUser = $session->user;
$session->user( { userId => $userId } );
ok( !($o->$m(@args)), "userId $userId fails $m check using default user for " . $comment );
$session->user( { user => $oldUser });
# Test the userId parameter
push @args, $userId;
ok( !($o->$m(@args)), "userId $userId fails $m check for " . $comment );
}
}
}