fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2012-03-21 08:37 am

[dw-free] CAPTCHA test suite is failing on my dreamhack

[commit: http://hg.dwscoalition.org/dw-free/rev/d9c6ebe27dfa]

http://bugs.dwscoalition.org/show_bug.cgi?id=4340

Tweak code to fix tests. Make captcha-related tests abort early with a
message if we don't have captcha set up for this installation.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/DW/Captcha/reCAPTCHA.pm
  • cgi-bin/DW/Captcha/textCAPTCHA.pm
  • t/captcha-textcaptcha.t
  • t/captcha.t
--------------------------------------------------------------------------------
diff -r d876640023ca -r d9c6ebe27dfa cgi-bin/DW/Captcha/reCAPTCHA.pm
--- a/cgi-bin/DW/Captcha/reCAPTCHA.pm	Wed Mar 21 16:05:00 2012 +0800
+++ b/cgi-bin/DW/Captcha/reCAPTCHA.pm	Wed Mar 21 16:38:02 2012 +0800
@@ -25,7 +25,10 @@
 use strict;
 
 package DW::Captcha::reCAPTCHA;
-use base 'DW::Captcha';
+
+# avoid base pragma - causes circular requirements
+require DW::Captcha;
+our @ISA = qw( DW::Captcha );
 
 BEGIN {
     my $rv = eval <<USE;
diff -r d876640023ca -r d9c6ebe27dfa cgi-bin/DW/Captcha/textCAPTCHA.pm
--- a/cgi-bin/DW/Captcha/textCAPTCHA.pm	Wed Mar 21 16:05:00 2012 +0800
+++ b/cgi-bin/DW/Captcha/textCAPTCHA.pm	Wed Mar 21 16:38:02 2012 +0800
@@ -25,7 +25,10 @@
 use strict;
 
 package DW::Captcha::textCAPTCHA;
-use base 'DW::Captcha';
+
+# avoid base pragma - causes circular requirements
+require DW::Captcha;
+our @ISA = qw( DW::Captcha );
 
 use XML::Simple;
 use Digest::MD5 ();
diff -r d876640023ca -r d9c6ebe27dfa t/captcha-textcaptcha.t
--- a/t/captcha-textcaptcha.t	Wed Mar 21 16:05:00 2012 +0800
+++ b/t/captcha-textcaptcha.t	Wed Mar 21 16:38:02 2012 +0800
@@ -7,6 +7,7 @@
 
 use DW::Captcha;
 use XML::Simple;
+use LJ::Test;
 
 my $fakeanswers_single = {
     question => 'The white bank is what colour?',
@@ -123,7 +124,10 @@
     # now validate user response
     ok( DW::Captcha::textCAPTCHA::Logic::check_answer( $captcha->{answers}, "white", $auth, $captcha_auth ), "correct" );
 
-    # whoo captcha succeded! let's try to reuse it
-    LJ::start_request();
-    ok( ! DW::Captcha::textCAPTCHA::Logic::check_answer( $captcha->{answers}, "white", $auth, $captcha_auth ), "tried to reuse captcha results" );
+    # whoo captcha succeeded! let's try to reuse it
+    SKIP: {
+        skip "Memcache configured but not active.", 1 unless LJ::Test::check_memcache;
+        LJ::start_request();
+        ok( ! DW::Captcha::textCAPTCHA::Logic::check_answer( $captcha->{answers}, "white", $auth, $captcha_auth ), "tried to reuse captcha results" );
+    }
 };
diff -r d876640023ca -r d9c6ebe27dfa t/captcha.t
--- a/t/captcha.t	Wed Mar 21 16:05:00 2012 +0800
+++ b/t/captcha.t	Wed Mar 21 16:38:02 2012 +0800
@@ -1,10 +1,22 @@
 # -*-perl-*-
 
 use strict;
-use Test::More tests => 14;
+use Test::More;
 use lib "$ENV{LJHOME}/cgi-bin";
 require 'ljlib.pl';
 
+my $recaptcha_enabled   = DW::Captcha::reCAPTCHA->site_enabled;
+my $textcaptcha_enabled = DW::Captcha::textCAPTCHA->site_enabled;
+
+if ( ! DW::Captcha->site_enabled ) {
+    plan skip_all => "CAPTCHA functionality disabled.";
+} elsif ( ! $recaptcha_enabled && ! $textcaptcha_enabled ) {
+    plan skip_all => "No valid CAPTCHA configuration.";
+} else {
+    plan tests => 13;
+}
+
+
 # override for the sake of the test
 %LJ::CAPTCHA_FOR = (
     testpage => 1,
@@ -26,23 +38,28 @@
 note( "check various implementations are loaded okay" );
 {
     my $default = $LJ::CAPTCHA_TYPES{$LJ::DEFAULT_CAPTCHA_TYPE};
-
-    ok( DW::Captcha->site_enabled, "Captcha is enabled site-wide" );
     my $captcha = DW::Captcha->new( 'testpage' );
     is( $captcha->name, $default, "Use default captcha implementation" );
 
 
-    $captcha = DW::Captcha->new( 'testpage', want => 'I' );
-    is( $captcha->name, "recaptcha", "Using reCAPTCHA" );
+    SKIP: {
+        skip "reCAPTCHA disabled.", 2 unless $recaptcha_enabled;
 
-    # can also be done using DW::Captcha::reCAPTCHA->site_enabled
-    # but technically we shouldn't be worrying about module names
-    ok( $captcha->site_enabled, "reCAPTCHA is enabled and configured on this site" );
+        $captcha = DW::Captcha->new( 'testpage', want => 'I' );
+        is( $captcha->name, "recaptcha", "Using reCAPTCHA" );
 
+        # can also be done using DW::Captcha::reCAPTCHA->site_enabled
+        # but technically we shouldn't be worrying about module names
+        ok( $captcha->site_enabled, "reCAPTCHA is enabled and configured on this site" );
+    }
 
-    $captcha = DW::Captcha->new( 'testpage', want => 'T' );
-    is( $captcha->name, "textcaptcha", "Using textCAPTCHA" );
-    ok( $captcha->site_enabled, "textCAPTCHA is enabled and configured on this site" );
+    SKIP: {
+        skip "textCAPTCHA disabled.", 2 unless $textcaptcha_enabled;
+
+        $captcha = DW::Captcha->new( 'testpage', want => 'T' );
+        is( $captcha->name, "textcaptcha", "Using textCAPTCHA" );
+        ok( $captcha->site_enabled, "textCAPTCHA is enabled and configured on this site" );
+    }
 
     $captcha = DW::Captcha->new( 'testpage', want => 'abc' );
     is( $captcha->name, $default, "not a valid captcha implementation, so used default" );
@@ -50,19 +67,22 @@
 }
 
 note( "user tries to use a disabled captcha type" );
+# it's possible only one type currently works, so activate a good one
 {
     local %LJ::DISABLED = ( captcha  => sub {
         my $module = $_[0];
-        return 0 if $module eq "recaptcha";
-        return 1 if $module eq "textcaptcha";
+        return ! $recaptcha_enabled if $module eq "recaptcha";
+        return $recaptcha_enabled if $module eq "textcaptcha";
     } );
-    local $LJ::DEFAULT_CAPTCHA_TYPE = "I";
+    local $LJ::DEFAULT_CAPTCHA_TYPE = $recaptcha_enabled ? "I" : "T";
+    my $BAD_CAPTCHA_TYPE = $recaptcha_enabled ? "T" : "I";
+    my $default_name = $LJ::CAPTCHA_TYPES{$LJ::DEFAULT_CAPTCHA_TYPE};
 
-    my $captcha = DW::Captcha->new( "testpage", want => "I" ); # image
-    is( $captcha->name, "recaptcha", "want recaptcha, everything is fine" );
-    ok( $captcha->site_enabled, "recaptcha was enabled" );
+    my $captcha = DW::Captcha->new( "testpage", want => $LJ::DEFAULT_CAPTCHA_TYPE );
+    is( $captcha->name, $default_name, "want $default_name, everything is fine" );
+    ok( $captcha->site_enabled, "$default_name was enabled" );
 
-    my $captcha = DW::Captcha->new( "testpage", want => "T" ); # text
-    is( $captcha->name, "recaptcha", "wanted textcaptcha, but it's not enabled so use recaptcha instead" );
-    ok( $captcha->site_enabled, "recaptcha (our fallback) is enabled" );
+    my $captcha = DW::Captcha->new( "testpage", want => $BAD_CAPTCHA_TYPE );
+    is( $captcha->name, $default_name, "wanted other type, but it's not enabled so use default instead" );
+    ok( $captcha->site_enabled, "our fallback is enabled" );
 }
--------------------------------------------------------------------------------