Skip to content

Commit

Permalink
Item14178: Tidy tests, and make work with Foswiki 2.0/2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Sep 7, 2016
1 parent 7036c4c commit 2b8010c
Showing 1 changed file with 86 additions and 57 deletions.
143 changes: 86 additions & 57 deletions test/unit/UpdateAttachmentsPlugin/UpdateAttachmentsPluginTests.pm
Expand Up @@ -8,7 +8,6 @@ use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );
use Error ':try';

use Unit::Request;
use Unit::Response;
use Foswiki;
use Foswiki::UI::Save;
Expand All @@ -23,23 +22,27 @@ sub set_up {

$this->SUPER::set_up();

$this->{attach_web} = "$this->{test_web}Attach";
$this->{attach_web} = "$this->{test_web}Attach";
my $webObject = Foswiki::Meta->new( $this->{session}, $this->{attach_web} );
$webObject->populateNewWeb();

$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Enabled} = 1;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Debug} = 0;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Module} = 'Foswiki::Plugins::UpdateAttachmentsPlugin';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachAsUser} = '';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Debug} = 0;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Module} =
'Foswiki::Plugins::UpdateAttachmentsPlugin';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachAsUser} = '';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{RemoveMissing} = 1;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{CheckUPDATEATACHPermission} = 1;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachFilter} = '^(\\.htaccess|\\.htpasswd|\\.htgroup|_.*)$';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{CheckUPDATEATACHPermission}
= 1;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachFilter} =
'^(\\.htaccess|\\.htpasswd|\\.htgroup|_.*)$';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{HideAttachments} = 1;
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachmentComment} = 'Fibble Foobar';
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{AttachmentComment} =
'Fibble Foobar';

$this->{tmpdatafile8} = $Foswiki::cfg{TempfileDir} . '/eight.bytes';
$this->{tmpdatafile16} = $Foswiki::cfg{TempfileDir} . '/sixteen.bytes';
$this->{tmpdatafile32} = $Foswiki::cfg{TempfileDir} . '/thirtytwo.bytes';
$this->{tmpdatafile16} = $Foswiki::cfg{TempfileDir} . '/sixteen.bytes';
$this->{tmpdatafile32} = $Foswiki::cfg{TempfileDir} . '/thirtytwo.bytes';
$this->{tmpdatafilex} = $Foswiki::cfg{TempfileDir} . '/bad # file @ name';

return;
Expand Down Expand Up @@ -70,7 +73,7 @@ sub _writeTopic {

# Save an attachment into the web/topic
sub _writeFile {
my ($web, $topic, $attach, $content) = @_;
my ( $web, $topic, $attach, $content ) = @_;

$content = "datadata/n" unless ($content);

Expand All @@ -95,10 +98,14 @@ sub _attachFile {
print $fh "$content \n";
close($fh);

Foswiki::Func::saveAttachment( $web, $topic, $attach,
{ file => "$path/$attach",
comment => 'Picture of Health',
hide => 1 } );
Foswiki::Func::saveAttachment(
$web, $topic, $attach,
{
file => "$path/$attach",
comment => 'Picture of Health',
hide => 1
}
);
}

sub _trim {
Expand All @@ -107,28 +114,42 @@ sub _trim {
return $s;
}


# Not a test, a helper.
sub runREST {
my ( $this, $topic ) = @_;

my $web = $this->{attach_web};
my $web = $this->{attach_web};
$topic ||= 'WebHome';

my $query;
if ( $Foswiki::Plugins::VERSION > 2.4 ) {
require Unit::Request::Rest;
$query = Unit::Request::Rest->new(
{
'topic' => "$web.$topic",
't' => time(),
}
);
}
else {
require Unit::Request;
$query = Unit::Request->new(
{
'topic' => "$web.$topic",
't' => time(),
}
);
}

# Compose the query
my $query = Unit::Request->new(
{
'topic' => "$web.$topic",
't' => time(),
}
);
$query->path_info("/UpdateAttachmentsPlugin/update");

my $session = Foswiki->new( $Foswiki::cfg{AdminUserLogin}, $query );
my $text = "Ignore this text";

# invoke the save handler
my ( $resp, $result, $stdout, $stderr ) = $this->captureWithKey( rest => $this->getUIFn('rest'), $session );
my ( $resp, $result, $stdout, $stderr ) =
$this->captureWithKey( rest => $this->getUIFn('rest'), $session );

#print STDERR "$stderr\n";
#print STDERR "RESPONSE: $resp";
Expand All @@ -149,11 +170,11 @@ sub test_SimpleAttachment {
Topic Text
HERE

_writeFile( $web, 'AnotherTopic', 'SomeFile');
_writeFile( $web, 'AnotherTopic', 'SomeFile' );

# first run - attach one file.

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');

my $match = <<"HERE";
attachments updated : 0
Expand All @@ -163,16 +184,18 @@ attachments ignored : 0
HERE

chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );

$resp = $this->runREST( 'WebHome' );
$resp = $this->runREST('WebHome');
$match = <<"HERE";
attachments updated : 0
attachments added : 0
attachments removed : 0
attachments ignored : 0
HERE
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );
}

sub test_Utf8Attachment {
Expand All @@ -184,11 +207,11 @@ sub test_Utf8Attachment {
Topic Text
HERE

_writeFile( $web, 'AśčÁŠŤśěž', 'AśčÁŠŤśěžFile');
_writeFile( $web, 'AśčÁŠŤśěž', 'AśčÁŠŤśěžFile' );

# first run - attach one file.

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');

my $match = <<"HERE";
attachments updated : 0
Expand All @@ -198,20 +221,20 @@ attachments ignored : 0
HERE

chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );

$resp = $this->runREST( 'WebHome' );
$resp = $this->runREST('WebHome');
$match = <<"HERE";
attachments updated : 0
attachments added : 0
attachments removed : 0
attachments ignored : 0
HERE
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );
}



#
# Verify removing all attachments
#
Expand All @@ -224,11 +247,11 @@ sub test_removeAllAttach {
Topic Text
HERE

_attachFile( $web, 'AnotherTopic', 'SomeFile');
_attachFile( $web, 'AnotherTopic', 'SomeFile' );

unlink "$Foswiki::cfg{PubDir}/$web/AnotherTopic/SomeFile";
unlink "$Foswiki::cfg{PubDir}/$web/AnotherTopic/SomeFile";

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');

my $match = <<"HERE";
attachments updated : 0
Expand All @@ -238,10 +261,11 @@ attachments ignored : 0
HERE

chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );

# Run tes a 2nd time - nothing to remove.
my $resp2 = $this->runREST( 'WebHome' );
my $resp2 = $this->runREST('WebHome');

$match = <<"HERE";
attachments updated : 0
Expand All @@ -251,7 +275,8 @@ attachments ignored : 0
HERE

chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp2);#, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp2 )
; #, "Unexpected output from initial attach" );

}

Expand All @@ -268,9 +293,9 @@ sub test_badAttachment {
Topic Text
HERE

_writeFile( $web, 'AnotherTopic', 'bad # file @ name');
_writeFile( $web, 'AnotherTopic', 'bad # file @ name' );

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');

my $match = <<"HERE";
attachments updated : 0
Expand All @@ -279,7 +304,8 @@ attachments removed : 0
attachments ignored : 1
HERE
chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "Unexpected output from initial attach" );
$this->assert_matches( qr#.*$match.*#, $resp,
"Unexpected output from initial attach" );

}

Expand All @@ -296,18 +322,19 @@ sub test_internalAttachment {
Topic Text
HERE

_writeFile( $web, 'AnotherTopic', '.htaccess');
_writeFile( $web, 'AnotherTopic', '.htpasswd');
_writeFile( $web, 'AnotherTopic', '.htaccess' );
_writeFile( $web, 'AnotherTopic', '.htpasswd' );

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');
my $match = <<"HERE";
attachments updated : 0
attachments added : 0
attachments removed : 0
attachments ignored : 0
HERE
chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "internal files should not be attached" );
$this->assert_matches( qr#.*$match.*#, $resp,
"internal files should not be attached" );

}

Expand All @@ -323,18 +350,19 @@ sub test_dotPrefixAttachments {
Topic Text
HERE

_writeFile( $web, 'AnotherTopic', '.bashrc');
_writeFile( $web, 'AnotherTopic', '.keep');
_writeFile( $web, 'AnotherTopic', '.bashrc' );
_writeFile( $web, 'AnotherTopic', '.keep' );

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');
my $match = <<"HERE";
attachments updated : 0
attachments added : 2
attachments removed : 0
attachments ignored : 0
HERE
chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "internal files should not be attached" );
$this->assert_matches( qr#.*$match.*#, $resp,
"internal files should not be attached" );

}
#
Expand All @@ -350,17 +378,18 @@ sub test_hiddenAttachment {
Topic Text
HERE

_writeFile( $web, 'AnotherTopic', '_hideMe.txt');
_writeFile( $web, 'AnotherTopic', '_hideMe.txt' );

my $resp = $this->runREST( 'WebHome' );
my $resp = $this->runREST('WebHome');
my $match = <<"HERE";
attachments updated : 0
attachments added : 0
attachments removed : 0
attachments ignored : 0
HERE
chomp $match;
$this->assert_matches( qr#.*$match.*#, $resp, "hidden files should not be attached" );
$this->assert_matches( qr#.*$match.*#, $resp,
"hidden files should not be attached" );

}

Expand All @@ -372,7 +401,7 @@ HERE
#
# Test Attach as user & verify metadata
#
# Test removing all attachments
# Test removing all attachments
#
# Test valid prefixed attachments, .bashrc for example
#
Expand Down

0 comments on commit 2b8010c

Please sign in to comment.