[Bps-public-commit] rt-extension-dynamicwebpath branch master updated. 1b6d332ea1063c458c37568205b006dc7cac1d8e

BPS Git Server git at git.bestpractical.com
Tue Dec 6 21:18:12 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt-extension-dynamicwebpath".

The branch, master has been updated
       via  1b6d332ea1063c458c37568205b006dc7cac1d8e (commit)
      from  bff493625741b676b09e3f5bd7a9d350c7311f48 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 1b6d332ea1063c458c37568205b006dc7cac1d8e
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Dec 7 01:05:55 2022 +0800

    Add basic tests

diff --git a/.gitignore b/.gitignore
index 21640e7..be8108b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ pod2htm*.tmp
 /MYMETA.*
 /t/tmp
 /xt/tmp
+/lib/RT/Extension/DynamicWebPath/Test.pm
diff --git a/META.yml b/META.yml
index c97f9fe..d83634e 100644
--- a/META.yml
+++ b/META.yml
@@ -18,6 +18,8 @@ no_index:
   directory:
     - html
     - inc
+    - t
+    - xt
 requires:
   perl: 5.10.1
 resources:
diff --git a/Makefile.PL b/Makefile.PL
index 303b625..00dac28 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -8,5 +8,18 @@ repository 'https://github.com/bestpractical/rt-extension-dynamicwebpath';
 requires_rt '4.4.0';
 rt_too_new '5.2.0';
 
+my ($lp) = ($INC{'RT.pm'} =~ /^(.*)[\\\/]/);
+my $lib_path = join( ' ', "$RT::LocalPath/lib", $lp );
+
+substitute(
+    {
+        RT_LIB_PATH  => $lib_path,
+    },
+    {
+        sufix => '.in'
+    },
+    'lib/RT/Extension/DynamicWebPath/Test.pm',
+);
+
 sign;
 WriteAll;
diff --git a/inc/Module/Install/Substitute.pm b/inc/Module/Install/Substitute.pm
new file mode 100644
index 0000000..56af7fe
--- /dev/null
+++ b/inc/Module/Install/Substitute.pm
@@ -0,0 +1,131 @@
+#line 1
+package Module::Install::Substitute;
+
+use strict;
+use warnings;
+use 5.008; # I don't care much about earlier versions
+
+use Module::Install::Base;
+our @ISA = qw(Module::Install::Base);
+
+our $VERSION = '0.03';
+
+require File::Temp;
+require File::Spec;
+require Cwd;
+
+#line 89
+
+sub substitute
+{
+	my $self = shift;
+	$self->{__subst} = shift;
+	$self->{__option} = {};
+	if( UNIVERSAL::isa( $_[0], 'HASH' ) ) {
+		my $opts = shift;
+		while( my ($k,$v) = each( %$opts ) ) {
+			$self->{__option}->{ lc( $k ) } = $v || '';
+		}
+	}
+	$self->_parse_options;
+
+	my @file = @_;
+	foreach my $f (@file) {
+		$self->_rewrite_file( $f );
+	}
+
+	return;
+}
+
+sub _parse_options
+{
+	my $self = shift;
+	my $cwd = Cwd::getcwd();
+	foreach my $t ( qw(from to) ) {
+        $self->{__option}->{$t} = $cwd unless $self->{__option}->{$t};
+		my $d = $self->{__option}->{$t};
+		die "Couldn't read directory '$d'" unless -d $d && -r _;
+	}
+}
+
+sub _rewrite_file
+{
+	my ($self, $file) = @_;
+	my $source = File::Spec->catfile( $self->{__option}{from}, $file );
+	$source .= $self->{__option}{sufix} if $self->{__option}{sufix};
+	unless( -f $source && -r _ ) {
+		print STDERR "Couldn't find file '$source'\n";
+		return;
+	}
+	my $dest = File::Spec->catfile( $self->{__option}{to}, $file );
+	return $self->__rewrite_file( $source, $dest );
+}
+
+sub __rewrite_file
+{
+	my ($self, $source, $dest) = @_;
+
+	my $mode = (stat($source))[2];
+
+	open my $sfh, "<$source" or die "Couldn't open '$source' for read";
+	print "Open input '$source' file for substitution\n";
+
+	my ($tmpfh, $tmpfname) = File::Temp::tempfile('mi-subst-XXXX', UNLINK => 1);
+	$self->__process_streams( $sfh, $tmpfh, ($source eq $dest)? 1: 0 );
+	close $sfh;
+
+	seek $tmpfh, 0, 0 or die "Couldn't seek in tmp file";
+
+	open my $dfh, ">$dest" or die "Couldn't open '$dest' for write";
+	print "Open output '$dest' file for substitution\n";
+
+	while( <$tmpfh> ) {
+		print $dfh $_;
+	}
+	close $dfh;
+	chmod $mode, $dest or "Couldn't change mode on '$dest'";
+}
+
+sub __process_streams
+{
+	my ($self, $in, $out, $replace) = @_;
+	
+	my @queue = ();
+	my $subst = $self->{'__subst'};
+	my $re_subst = join('|', map {"\Q$_"} keys %{ $subst } );
+
+	while( my $str = <$in> ) {
+		if( $str =~ /^###\s*(before|replace|after)\:\s?(.*)$/s ) {
+			my ($action, $nstr) = ($1,$2);
+			$nstr =~ s/\@($re_subst)\@/$subst->{$1}/ge;
+
+			die "Replace action is bad idea for situations when dest is equal to source"
+                if $replace && $action eq 'replace';
+			if( $action eq 'before' ) {
+				die "no line before 'before' action" unless @queue;
+				# overwrite prev line;
+				pop @queue;
+				push @queue, $nstr;
+				push @queue, $str;
+			} elsif( $action eq 'replace' ) {
+				push @queue, $nstr;
+			} elsif( $action eq 'after' ) {
+				push @queue, $str;
+				push @queue, $nstr;
+				# skip one line;
+				<$in>;
+			}
+		} else {
+			push @queue, $str;
+		}
+		while( @queue > 3 ) {
+			print $out shift(@queue);
+		}
+	}
+	while( scalar @queue ) {
+		print $out shift(@queue);
+	}
+}
+
+1;
+
diff --git a/lib/RT/Extension/DynamicWebPath/Test.pm.in b/lib/RT/Extension/DynamicWebPath/Test.pm.in
new file mode 100644
index 0000000..18517e0
--- /dev/null
+++ b/lib/RT/Extension/DynamicWebPath/Test.pm.in
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+
+### after: use lib qw(@RT_LIB_PATH@);
+use lib qw(/opt/rt5/local/lib /opt/rt5/lib);
+
+package RT::Extension::DynamicWebPath::Test;
+use base 'RT::Test';
+
+sub import {
+    my $class = shift;
+    my %args  = @_;
+
+    $args{'requires'} ||= [];
+    if ( $args{'testing'} ) {
+        unshift @{ $args{'requires'} }, 'RT::Extension::DynamicWebPath';
+    } else {
+        $args{'testing'} = 'RT::Extension::DynamicWebPath';
+    }
+
+    $class->SUPER::import( %args );
+    $class->export_to_level(1);
+
+    require RT::Extension::DynamicWebPath;
+}
+
+1;
diff --git a/t/data/configs/apache2.4+fcgid.conf b/t/data/configs/apache2.4+fcgid.conf
new file mode 100644
index 0000000..f710d52
--- /dev/null
+++ b/t/data/configs/apache2.4+fcgid.conf
@@ -0,0 +1,51 @@
+ServerRoot %%SERVER_ROOT%%
+PidFile %%PID_FILE%%
+ServerAdmin root at localhost
+
+%%LOAD_MODULES%%
+
+ServerName localhost
+Listen %%LISTEN%%
+
+ErrorLog "%%LOG_FILE%%"
+LogLevel debug
+
+<Directory />
+    Options FollowSymLinks
+    AllowOverride None
+    Require all denied
+</Directory>
+
+AddDefaultCharset UTF-8
+
+FcgidMaxRequestLen 1073741824
+FcgidConnectTimeout 20
+FcgidProcessTableFile %%tmp_dir%%/shm
+FcgidIPCDir %%tmp_dir%%
+FcgidInitialEnv RT_TESTING 1
+FcgidInitialEnv RT_SITE_CONFIG %%RT_SITE_CONFIG%%
+
+ScriptAlias /rt %%RT_SBIN_PATH%%/rt-server.fcgi/
+ScriptAlias / %%RT_SBIN_PATH%%/rt-server.fcgi/
+
+DocumentRoot "%%DOCUMENT_ROOT%%"
+<Location />
+
+    <RequireAll>
+        Require all granted
+%%BASIC_AUTH%%
+    </RequireAll>
+
+    Options +ExecCGI
+    AddHandler fcgid-script .fcgi
+</Location>
+
+<LocationMatch "^/(rt$|rt|REST/)">
+
+    <RequireAll>
+        Require all granted
+    </RequireAll>
+
+    Options +ExecCGI
+    AddHandler fcgid-script .fcgi
+</LocationMatch>
diff --git a/xt/basic.t b/xt/basic.t
new file mode 100644
index 0000000..4e099a8
--- /dev/null
+++ b/xt/basic.t
@@ -0,0 +1,80 @@
+use strict;
+use warnings;
+
+use RT::Extension::DynamicWebPath::Test tests => undef;
+
+plan skip_all => 'This test only works for RT_TEST_WEB_HANDLER=apache+fcgid'
+  unless ( $ENV{RT_TEST_WEB_HANDLER} // '' ) eq 'apache+fcgid';
+
+RT->Config->Set( WebRemoteUserAuth => 1 );
+RT->Config->Set( WebFallbackToRTLogin => 0 );
+
+RT->Config->Set(
+    'DynamicWebPath' => {
+        '' => {
+            WebRemoteUserAuth    => 1,
+            WebFallbackToRTLogin => 0,
+        },
+        '/rt' => {
+            WebRemoteUserAuth    => 0,
+            WebFallbackToRTLogin => 1,
+        },
+    },
+);
+
+my $autoreply = RT::Template->new( RT->SystemUser );
+$autoreply->Load('Autoreply in HTML');
+my ( $ret, $msg ) = $autoreply->SetContent( $autoreply->Content . <<'EOF' );
+{RT->Config->Get("WebURL")}Ticket/Display.html?id={$Ticket->id}
+EOF
+ok( $ret, $msg );
+
+{
+    my ( $url, $m ) = RT::Extension::DynamicWebPath::Test->started_ok( basic_auth => 'anon' );
+
+    # REMOTE_USER of root
+    $m->auth("root");
+
+    # Automatically logged in as root without Login page
+    $m->get_ok($url);
+    ok $m->logged_in_as("root"), "Logged in as root";
+
+    # Drop credentials
+    $m->auth('');
+
+    $m->get($url);
+    is $m->status, 403, "403 Forbidden from RT";
+
+    $m->get( $url . '/?user=root;pass=password' );
+    is $m->status, 403, "403 Forbidden from RT";
+
+    $m->get_ok( $url . '/rt' );
+    $m->content_like( qr/Login/, "Login form" );
+    $m->get_ok( $url . '/rt/?user=root;pass=password' );
+    ok $m->logged_in_as("root"), "Logged in as root";
+
+    my $text = <<EOF;
+From: root\@localhost
+To: rt\@@{[RT->Config->Get('rtname')]}
+Subject: This is a test of new ticket creation
+
+Test email
+EOF
+
+    my ( $status, $id ) = RT::Extension::DynamicWebPath::Test->send_via_mailgate_and_http($text);
+    is( $status >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "Created ticket" );
+
+    my $tick = RT::Extension::DynamicWebPath::Test->last_ticket;
+    isa_ok( $tick, 'RT::Ticket' );
+    is( $tick->Id, $id, "correct ticket id" );
+    is( $tick->Subject, 'This is a test of new ticket creation', "Created the ticket" );
+
+    my @mails = RT::Extension::DynamicWebPath::Test->fetch_caught_mails;
+    is( scalar @mails, 1, 'Got one email' );
+    like( $mails[0], qr!$url/Ticket/Display\.html!, 'WebURL in emails is /' );
+
+    $m->no_warnings_ok;
+}
+
+done_testing;
-----------------------------------------------------------------------

Summary of changes:
 .gitignore                                 |   1 +
 META.yml                                   |   2 +
 Makefile.PL                                |  13 +++
 inc/Module/Install/Substitute.pm           | 131 +++++++++++++++++++++++++++++
 lib/RT/Extension/DynamicWebPath/Test.pm.in |  27 ++++++
 t/data/configs/apache2.4+fcgid.conf        |  51 +++++++++++
 xt/basic.t                                 |  80 ++++++++++++++++++
 7 files changed, 305 insertions(+)
 create mode 100644 inc/Module/Install/Substitute.pm
 create mode 100644 lib/RT/Extension/DynamicWebPath/Test.pm.in
 create mode 100644 t/data/configs/apache2.4+fcgid.conf
 create mode 100644 xt/basic.t


hooks/post-receive
-- 
rt-extension-dynamicwebpath


More information about the Bps-public-commit mailing list