forked from dreamwidth/dreamwidth
/
captcha.t
103 lines (83 loc) · 3.54 KB
/
captcha.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# t/captcha.t
#
# Test DW::Captcha
#
# Authors:
# Afuna <coder.dw@afunamatata.com>
# Jen Griffin <kareila@livejournal.com>
#
# Copyright (c) 2013 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself. For a copy of the license, please reference
# 'perldoc perlartistic' or 'perldoc perlgpl'.
#
use strict;
use warnings;
use Test::More;
BEGIN { $LJ::_T_CONFIG = 1; require "$ENV{LJHOME}/cgi-bin/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,
nocaptchapage => 0,
);
note( "disabled captcha for a specific page" );
{
my $captcha = DW::Captcha->new( "nocaptchapage" );
ok( ! $captcha->enabled, "Captcha is not enabled for nocaptchapage" );
}
note( "check captcha is enabled" );
{
my $captcha = DW::Captcha->new( "testpage" );
ok( $captcha->enabled, "Captcha is enabled for testpage" );
}
note( "check various implementations are loaded okay" );
{
my $default = $LJ::CAPTCHA_TYPES{$LJ::DEFAULT_CAPTCHA_TYPE};
my $captcha = DW::Captcha->new( 'testpage' );
is( $captcha->name, $default, "Use default captcha implementation" );
SKIP: {
skip "reCAPTCHA disabled.", 2 unless $recaptcha_enabled;
$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" );
}
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" );
ok( $captcha->site_enabled, "not a valid captcha implementation, so used default to make sure we still get captcha" );
}
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 ! $recaptcha_enabled if $module eq "recaptcha";
return $recaptcha_enabled if $module eq "textcaptcha";
} );
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 => $LJ::DEFAULT_CAPTCHA_TYPE );
is( $captcha->name, $default_name, "want $default_name, everything is fine" );
ok( $captcha->site_enabled, "$default_name was enabled" );
$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" );
}