Skip to content

Commit

Permalink
Item9561: Commit a unit test. In fact, there is no possibility to tes…
Browse files Browse the repository at this point in the history
…t this

through unit test, as the anchor is grabbed using JS, but I kept this test for
demonstration purposes, at it is the only one I know of doing the full
Foswiki::UI chain, hence getting the AccessControlExceptions via a 302

git-svn-id: http://svn.foswiki.org/branches/Release01x01@12684 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulHarvey authored and PaulHarvey committed Oct 2, 2011
1 parent 444434a commit eed5f3b
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion UnitTestContrib/test/unit/AccessControlTests.pm
Expand Up @@ -5,6 +5,9 @@ package AccessControlTests;
use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );

# For Anchor test
use Foswiki::UI;

sub new {
my $class = shift;
my $self = $class->SUPER::new( 'AccessControl', @_ );
Expand Down Expand Up @@ -260,7 +263,15 @@ If ALLOWTOPIC is set
1. people in the list are PERMITTED
2. everyone else is DENIED
THIS
$topicObject->putKeyed('PREFERENCE', { name=>"ALLOWTOPICVIEW", title=>"ALLOWTOPICVIEW", type=>"Set", value=>"%USERSWEB%.MrOrange MrYellow" });
$topicObject->putKeyed(
'PREFERENCE',
{
name => "ALLOWTOPICVIEW",
title => "ALLOWTOPICVIEW",
type => "Set",
value => "%USERSWEB%.MrOrange MrYellow"
}
);
$topicObject->save();

# renew Foswiki, so WebPreferences gets re-read
Expand Down Expand Up @@ -567,4 +578,69 @@ THIS
$this->DENIED( "VIEW", $MrOrange );
}

# As anchors are never sent by the browser, this is done through JS
# and only testable by Selenium
# Kept this test here as it is the only one I (Babar) know of doing the full
# Foswiki::UI chain, therefore catching the AccessControlExceptions
sub test_login_redirect_preserves_anchor {
my $this = shift;
my $test_topic = 'TestAnchor';

# Create a topic with an anchor, viewable only by MrYellow
my $topicObject = Foswiki::Meta->new(
$this->{session}, $this->{test_web}, $test_topic,
<<THIS
If there is an anchor, and some access restrictions,
anchor is preserved after login.
#anchor
* Set ALLOWTOPICVIEW = MrYellow
THIS
, undef
);
$topicObject->save();

# Request the page with the full UI
my $query = new Unit::Request(
{
webName => [ $this->{test_web} ],
topicName => ["$test_topic"],
}
);
$query->path_info("/$this->{test_web}/$test_topic");
$query->method('GET');
$query->action('view');
my $viewUrl =
$this->{session}
->getScriptUrl( '0', 'view', $this->{test_web}, $test_topic );
$query->uri("$viewUrl");
my ($text) = $this->capture(
sub {
$Foswiki::Plugins::SESSION->{response} =
&Foswiki::UI::handleRequest($query);
}
);

# Get the login URL to compare
my $loginUrl =
$this->{session}
->getScriptUrl( '0', 'login', $this->{test_web}, $test_topic );

# Extract what we've been redirected to
my ($redirect_to) = $text =~ /^Location: (.*?)\r?$/m;
$this->assert_not_null( $redirect_to,
"Request should have return a 302 to $loginUrl\n"
. "But it returned:\n$text" );

# Check the redirect contains the login url + view to this topic
$this->assert_matches(
qr#^$loginUrl.*/view/$this->{test_web}/$test_topic$#,
$redirect_to,
"Login did not redirect to a page with the proper anchor:\n"
. "Location: $redirect_to\n"
. "Expected: ^$loginUrl.*\%23anchor\$"
);

# Get the redirected page after login
}

1;

0 comments on commit eed5f3b

Please sign in to comment.