Skip to content

Commit

Permalink
Item13897: Partially working AccessControlTests
Browse files Browse the repository at this point in the history
Test fails on group access denial – seemingly because of metacache
problems. Needs investigating.

Also fails due to lack of accesscontrolexception support in Foswiki::App.

All other tests are passing.

- Foswiki::UserMapping was turned into a role.

- Moved all unit test support code from Foswiki::App into Unit::TestApp.

- Added method invalidate() on TopicUserMapping. Used to forcingly refresh
object status – used by unit test user registration code to get the latest
user list.

- Test engine now supports initialization using its intialAttributes
attribute in addition to the existing init methods.

- Test engine supports simulation of cgi/psgi in finalizeReturn() method to
support capture*() methods. Though in some cases capturing shall be
reviewed and replaced with calls to $app->handleRequest.

- New attribute engineParams on Unit::TestApp.
  • Loading branch information
vrurg committed May 18, 2016
1 parent 353f23d commit be55502
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 226 deletions.
33 changes: 23 additions & 10 deletions TopicUserMappingContrib/lib/Foswiki/Users/TopicUserMapping.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use Foswiki::Func ();

use Moo;
use namespace::clean;
extends qw(Foswiki::UserMapping);
extends qw(Foswiki::Object);
with qw(Foswiki::AppObject Foswiki::UserMapping);

has passwords => (
is => 'ro',
Expand All @@ -51,8 +52,8 @@ has _eachGroupMember =>
( is => 'rw', lazy => 1, clearer => 1, default => sub { {} }, );
has singleGroupMembers =>
( is => 'rw', lazy => 1, clearer => 1, default => sub { {} }, );
has groupsList => ( is => 'rw', );
has _MAP_OF_EMAILS => ( is => 'rw', );
has groupsList => ( is => 'rw', clearer => 1, );
has _MAP_OF_EMAILS => ( is => 'rw', clearer => 1, );
has CACHED => ( is => 'rw', default => 0, );

#use Monitor;
Expand Down Expand Up @@ -140,8 +141,11 @@ define $this->mapping_id = 'TopicUserMapping_';
=cut

sub handlesUser {
my ( $this, $cUID, $login, $wikiname ) = @_;
around handlesUser => sub {
my $orig = shift;
my $this = shift;
my ( $cUID, $login, $wikiname ) = @_;

my $mapping_id = $this->mapping_id;
if ( defined $cUID && !length($mapping_id) ) {

Expand All @@ -164,7 +168,7 @@ sub handlesUser {
}

return 0;
}
};

=begin TML
Expand Down Expand Up @@ -1608,14 +1612,15 @@ returns the string unchanged if no issue found.
=cut

sub validateRegistrationField {
around validateRegistrationField => sub {
my $orig = shift;
my $this = shift;

#my ($this, $field, $value) = @_;
my $this = shift;

# For now just let Foswiki::UserMapping do the validation - nothing special needed.
return $this->SUPER::validateRegistrationField(@_);
}
return $orig->( $this, @_ );
};

# TODO: and probably flawed in light of multiple cUIDs mapping to one wikiname
sub _cacheUser {
Expand Down Expand Up @@ -1796,6 +1801,14 @@ sub _expandUserList {
return \@l;
}

sub invalidate {
my $this = shift;

$this->CACHED(0);
$this->clear_groupsList;
$this->_clear_MAP_OF_EMAILS;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Expand Down
51 changes: 44 additions & 7 deletions UnitTestContrib/lib/Foswiki/Engine/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ use Moo;
use namespace::clean;
extends qw(Foswiki::Engine);

has simulate => (
is => 'rw',
default => 'psgi',
coerce => sub {
my $method = lc shift;
Foswiki::Engine::Fatal->throw( text =>
"Unknown test engine simulate environment requested: $method" )
unless $method =~ /^(psgi|cgi)$/;
return $method;
},
);
has initialAttributes => ( is => 'rw', default => sub { {} }, );

around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
Expand All @@ -43,13 +56,19 @@ around BUILDARGS => sub {
return $orig->( $class, %defaults, @_ );
};

# Form a data hash using keys either from initialAttributes (higher prio) or
# from env.
sub initFromEnv {
my $this = shift;
my %initHash = map {
my $this = shift;
my $initAttrs = $this->initialAttributes;
my $env = $this->env;
my %initHash = map {
my $eKey = uc( 'FOSWIKI_TEST_' . $_ );
defined( $this->env->{$eKey} )
? ( $_ => $this->env->{$eKey} )
: ()
defined( $initAttrs->{$_} ) ? ( $_ => $initAttrs->{$_} )
: (
defined( $env->{$eKey} ) ? ( $_ => $env->{$eKey} )
: ()
)
} @_;
return \%initHash;
}
Expand Down Expand Up @@ -79,11 +98,29 @@ around _prepareQueryParameters => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, $this->env->{FOSWIKI_TEST_QUERY_STRING} )
if defined $this->env->{FOSWIKI_TEST_QUERY_STRING};
my $queryString = $this->initialAttributes->{query_string}
// $this->env->{FOSWIKI_TEST_QUERY_STRING};

return $orig->( $this, $queryString ) if defined $queryString;
return [];
};

around finalizeReturn => sub {
my $orig = shift;
my $this = shift;
my ($return) = @_;

my $rc = $return;
if ( $this->simulate eq 'cgi' ) {
$rc = 0;

push @{ $return->[1] }, 'Status' => $return->[0];
print $this->stringifyHeaders($return);
print @{ $return->[2] };
}
return $rc;
};

1;

__END__
Expand Down
30 changes: 29 additions & 1 deletion UnitTestContrib/lib/Unit/TestApp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ extends qw(Foswiki::App);
# qw(
# access attach cache cfg env forms
# logger engine heap i18n plugins prefs
# renderer request _requestParams response
# renderer request requestParams response
# search store templates macros context
# ui remoteUser user users zones _dispatcherAttrs
# )
# ;
#}

# requestParams hash is used to initialize a new request object.
has requestParams => (
is => 'rwp',
lazy => 1,
default => sub { {} },
);

# engineParams hash is used to initialize a new engine object.
has engineParams => (
is => 'rw',
lazy => 1,
default => sub { {} },
);

sub BUILD {
my $this = shift;

Expand Down Expand Up @@ -57,6 +71,20 @@ sub cloneEnv {
return $this->_cloneData( $this->env, 'env' );
}

around _prepareRequest => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, %{ $this->requestParams } );
};

around _prepareEngine => sub {
my $orig = shift;
my $this = shift;

return $orig->( $this, %{ $this->engineParams } );
};

1;

__DATA__
Expand Down
Loading

0 comments on commit be55502

Please sign in to comment.